14_SoundEffects.as 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Sound effects example
  2. // This sample demonstrates:
  3. // - Playing sound effects and music
  4. // - Controlling sound and music master volume
  5. #include "Scripts/Utilities/Sample.as"
  6. Array<String> soundNames = {
  7. "Fist",
  8. "Explosion",
  9. "Power-up"
  10. };
  11. Array<String> soundResourceNames = {
  12. "Sounds/PlayerFistHit.wav",
  13. "Sounds/BigExplosion.wav",
  14. "Sounds/Powerup.wav"
  15. };
  16. SoundSource@ musicSource;
  17. void Start()
  18. {
  19. // Execute the common startup for samples
  20. SampleStart();
  21. // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  22. scene_ = Scene();
  23. // Create music sound source
  24. @musicSource = scene_.CreateComponent("SoundSource");
  25. // Set the sound type to music so that master volume control works correctly
  26. musicSource.soundType = SOUND_MUSIC;
  27. // Enable OS cursor
  28. input.mouseVisible = true;
  29. // Create the user interface
  30. CreateUI();
  31. // Set the mouse mode to use in the sample
  32. SampleInitMouseMode(MM_FREE);
  33. }
  34. void CreateUI()
  35. {
  36. XMLFile@ uiStyle = cache.GetResource("XMLFile", "UI/DefaultStyle.xml");
  37. // Set style to the UI root so that elements will inherit it
  38. ui.root.defaultStyle = uiStyle;
  39. // Create buttons for playing back sounds
  40. for (uint i = 0; i < soundNames.length; ++i)
  41. {
  42. Button@ button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]);
  43. // Store the sound effect resource name as a custom variable into the button
  44. button.vars["SoundResource"] = soundResourceNames[i];
  45. SubscribeToEvent(button, "Pressed", "HandlePlaySound");
  46. }
  47. // Create buttons for playing/stopping music
  48. Button@ button = CreateButton(20, 80, 120, 40, "Play Music");
  49. SubscribeToEvent(button, "Released", "HandlePlayMusic");
  50. button = CreateButton(160, 80, 120, 40, "Stop Music");
  51. SubscribeToEvent(button, "Released", "HandleStopMusic");
  52. // Create sliders for controlling sound and music master volume
  53. Slider@ slider = CreateSlider(20, 140, 200, 20, "Sound Volume");
  54. slider.value = audio.masterGain[SOUND_EFFECT];
  55. SubscribeToEvent(slider, "SliderChanged", "HandleSoundVolume");
  56. slider = CreateSlider(20, 200, 200, 20, "Music Volume");
  57. slider.value = audio.masterGain[SOUND_MUSIC];
  58. SubscribeToEvent(slider, "SliderChanged", "HandleMusicVolume");
  59. }
  60. Button@ CreateButton(int x, int y, int xSize, int ySize, const String&in text)
  61. {
  62. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  63. // Create the button and center the text onto it
  64. Button@ button = ui.root.CreateChild("Button");
  65. button.SetStyleAuto();
  66. button.SetPosition(x, y);
  67. button.SetSize(xSize, ySize);
  68. Text@ buttonText = button.CreateChild("Text");
  69. buttonText.SetAlignment(HA_CENTER, VA_CENTER);
  70. buttonText.SetFont(font, 12);
  71. buttonText.text = text;
  72. return button;
  73. }
  74. Slider@ CreateSlider(int x, int y, int xSize, int ySize, const String& text)
  75. {
  76. Font@ font = cache.GetResource("Font", "Fonts/Anonymous Pro.ttf");
  77. // Create text and slider below it
  78. Text@ sliderText = ui.root.CreateChild("Text");
  79. sliderText.SetPosition(x, y);
  80. sliderText.SetFont(font, 12);
  81. sliderText.text = text;
  82. Slider@ slider = ui.root.CreateChild("Slider");
  83. slider.SetStyleAuto();
  84. slider.SetPosition(x, y + 20);
  85. slider.SetSize(xSize, ySize);
  86. // Use 0-1 range for controlling sound/music master volume
  87. slider.range = 1.0f;
  88. return slider;
  89. }
  90. void HandlePlaySound(StringHash eventType, VariantMap& eventData)
  91. {
  92. Button@ button = GetEventSender();
  93. String soundResourceName = button.vars["SoundResource"].GetString();
  94. // Get the sound resource
  95. Sound@ sound = cache.GetResource("Sound", soundResourceName);
  96. if (sound !is null)
  97. {
  98. // Create a SoundSource component for playing the sound. The SoundSource component plays
  99. // non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  100. // SoundSource3D component would be used instead
  101. SoundSource@ soundSource = scene_.CreateComponent("SoundSource");
  102. soundSource.autoRemoveMode = REMOVE_COMPONENT;
  103. soundSource.Play(sound);
  104. // In case we also play music, set the sound volume below maximum so that we don't clip the output
  105. soundSource.gain = 0.7f;
  106. }
  107. }
  108. void HandlePlayMusic(StringHash eventType, VariantMap& eventData)
  109. {
  110. Sound@ music = cache.GetResource("Sound", "Music/Ninja Gods.ogg");
  111. // Set the song to loop
  112. music.looped = true;
  113. musicSource.Play(music);
  114. }
  115. void HandleStopMusic(StringHash eventType, VariantMap& eventData)
  116. {
  117. musicSource.Stop();
  118. }
  119. void HandleSoundVolume(StringHash eventType, VariantMap& eventData)
  120. {
  121. float newVolume = eventData["Value"].GetFloat();
  122. audio.masterGain[SOUND_EFFECT] = newVolume;
  123. }
  124. void HandleMusicVolume(StringHash eventType, VariantMap& eventData)
  125. {
  126. float newVolume = eventData["Value"].GetFloat();
  127. audio.masterGain[SOUND_MUSIC] = newVolume;
  128. }
  129. // Create XML patch instructions for screen joystick layout specific to this sample app
  130. String patchInstructions =
  131. "<patch>" +
  132. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">" +
  133. " <attribute name=\"Is Visible\" value=\"false\" />" +
  134. " </add>" +
  135. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" +
  136. " <attribute name=\"Is Visible\" value=\"false\" />" +
  137. " </add>" +
  138. "</patch>";