SoundEffects.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Audio/Audio.h>
  4. #include <Urho3D/Audio/AudioEvents.h>
  5. #include <Urho3D/Audio/Sound.h>
  6. #include <Urho3D/Audio/SoundSource.h>
  7. #include <Urho3D/Engine/Engine.h>
  8. #include <Urho3D/Input/Input.h>
  9. #include <Urho3D/IO/Log.h>
  10. #include <Urho3D/Resource/ResourceCache.h>
  11. #include <Urho3D/Scene/Scene.h>
  12. #include <Urho3D/UI/Button.h>
  13. #include <Urho3D/UI/Font.h>
  14. #include <Urho3D/UI/Slider.h>
  15. #include <Urho3D/UI/Text.h>
  16. #include <Urho3D/UI/UI.h>
  17. #include <Urho3D/UI/UIEvents.h>
  18. #include "SoundEffects.h"
  19. #include <Urho3D/DebugNew.h>
  20. // Custom variable identifier for storing sound effect name within the UI element
  21. static const StringHash VAR_SOUNDRESOURCE("SoundResource");
  22. static const unsigned NUM_SOUNDS = 3;
  23. static const char* soundNames[] = {
  24. "Fist",
  25. "Explosion",
  26. "Power-up"
  27. };
  28. static const char* soundResourceNames[] = {
  29. "Sounds/PlayerFistHit.wav",
  30. "Sounds/BigExplosion.wav",
  31. "Sounds/Powerup.wav"
  32. };
  33. URHO3D_DEFINE_APPLICATION_MAIN(SoundEffects)
  34. SoundEffects::SoundEffects(Context* context) :
  35. Sample(context),
  36. musicSource_(nullptr)
  37. {
  38. }
  39. void SoundEffects::Setup()
  40. {
  41. // Modify engine startup parameters
  42. Sample::Setup();
  43. engineParameters_[EP_SOUND] = true;
  44. }
  45. void SoundEffects::Start()
  46. {
  47. // Execute base class startup
  48. Sample::Start();
  49. // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  50. scene_ = new Scene(context_);
  51. // Create music sound source
  52. musicSource_ = scene_->CreateComponent<SoundSource>();
  53. // Set the sound type to music so that master volume control works correctly
  54. musicSource_->SetSoundType(SOUND_MUSIC);
  55. // Enable OS cursor
  56. GetSubsystem<Input>()->SetMouseVisible(true);
  57. // Create the user interface
  58. CreateUI();
  59. // Set the mouse mode to use in the sample
  60. Sample::InitMouseMode(MM_FREE);
  61. }
  62. void SoundEffects::CreateUI()
  63. {
  64. UIElement* root = GetSubsystem<UI>()->GetRoot();
  65. auto* cache = GetSubsystem<ResourceCache>();
  66. auto* uiStyle = cache->GetResource<XMLFile>("UI/DefaultStyle.xml");
  67. // Set style to the UI root so that elements will inherit it
  68. root->SetDefaultStyle(uiStyle);
  69. // Create buttons for playing back sounds
  70. for (unsigned i = 0; i < NUM_SOUNDS; ++i)
  71. {
  72. Button* button = CreateButton(i * 140 + 20, 20, 120, 40, soundNames[i]);
  73. // Store the sound effect resource name as a custom variable into the button
  74. button->SetVar(VAR_SOUNDRESOURCE, soundResourceNames[i]);
  75. SubscribeToEvent(button, E_PRESSED, URHO3D_HANDLER(SoundEffects, HandlePlaySound));
  76. }
  77. // Create buttons for playing/stopping music
  78. Button* button = CreateButton(20, 80, 120, 40, "Play Music");
  79. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandlePlayMusic));
  80. button = CreateButton(160, 80, 120, 40, "Stop Music");
  81. SubscribeToEvent(button, E_RELEASED, URHO3D_HANDLER(SoundEffects, HandleStopMusic));
  82. auto* audio = GetSubsystem<Audio>();
  83. // Create sliders for controlling sound and music master volume
  84. Slider* slider = CreateSlider(20, 140, 200, 20, "Sound Volume");
  85. slider->SetValue(audio->GetMasterGain(SOUND_EFFECT));
  86. SubscribeToEvent(slider, E_SLIDERCHANGED, URHO3D_HANDLER(SoundEffects, HandleSoundVolume));
  87. slider = CreateSlider(20, 200, 200, 20, "Music Volume");
  88. slider->SetValue(audio->GetMasterGain(SOUND_MUSIC));
  89. SubscribeToEvent(slider, E_SLIDERCHANGED, URHO3D_HANDLER(SoundEffects, HandleMusicVolume));
  90. }
  91. Button* SoundEffects::CreateButton(int x, int y, int xSize, int ySize, const String& text)
  92. {
  93. UIElement* root = GetSubsystem<UI>()->GetRoot();
  94. auto* cache = GetSubsystem<ResourceCache>();
  95. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  96. // Create the button and center the text onto it
  97. auto* button = root->CreateChild<Button>();
  98. button->SetStyleAuto();
  99. button->SetPosition(x, y);
  100. button->SetSize(xSize, ySize);
  101. auto* buttonText = button->CreateChild<Text>();
  102. buttonText->SetAlignment(HA_CENTER, VA_CENTER);
  103. buttonText->SetFont(font, 12);
  104. buttonText->SetText(text);
  105. return button;
  106. }
  107. Slider* SoundEffects::CreateSlider(int x, int y, int xSize, int ySize, const String& text)
  108. {
  109. UIElement* root = GetSubsystem<UI>()->GetRoot();
  110. auto* cache = GetSubsystem<ResourceCache>();
  111. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  112. // Create text and slider below it
  113. auto* sliderText = root->CreateChild<Text>();
  114. sliderText->SetPosition(x, y);
  115. sliderText->SetFont(font, 12);
  116. sliderText->SetText(text);
  117. auto* slider = root->CreateChild<Slider>();
  118. slider->SetStyleAuto();
  119. slider->SetPosition(x, y + 20);
  120. slider->SetSize(xSize, ySize);
  121. // Use 0-1 range for controlling sound/music master volume
  122. slider->SetRange(1.0f);
  123. return slider;
  124. }
  125. void SoundEffects::HandlePlaySound(StringHash eventType, VariantMap& eventData)
  126. {
  127. auto* button = static_cast<Button*>(GetEventSender());
  128. const String& soundResourceName = button->GetVar(VAR_SOUNDRESOURCE).GetString();
  129. // Get the sound resource
  130. auto* cache = GetSubsystem<ResourceCache>();
  131. auto* sound = cache->GetResource<Sound>(soundResourceName);
  132. if (sound)
  133. {
  134. // Create a SoundSource component for playing the sound. The SoundSource component plays
  135. // non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  136. // SoundSource3D component would be used instead
  137. auto* soundSource = scene_->CreateComponent<SoundSource>();
  138. // Component will automatically remove itself when the sound finished playing
  139. soundSource->SetAutoRemoveMode(REMOVE_COMPONENT);
  140. soundSource->Play(sound);
  141. // In case we also play music, set the sound volume below maximum so that we don't clip the output
  142. soundSource->SetGain(0.75f);
  143. }
  144. }
  145. void SoundEffects::HandlePlayMusic(StringHash eventType, VariantMap& eventData)
  146. {
  147. auto* cache = GetSubsystem<ResourceCache>();
  148. auto* music = cache->GetResource<Sound>("Music/Ninja Gods.ogg");
  149. // Set the song to loop
  150. music->SetLooped(true);
  151. musicSource_->Play(music);
  152. }
  153. void SoundEffects::HandleStopMusic(StringHash eventType, VariantMap& eventData)
  154. {
  155. // Remove the music player node from the scene
  156. musicSource_->Stop();
  157. }
  158. void SoundEffects::HandleSoundVolume(StringHash eventType, VariantMap& eventData)
  159. {
  160. using namespace SliderChanged;
  161. float newVolume = eventData[P_VALUE].GetFloat();
  162. GetSubsystem<Audio>()->SetMasterGain(SOUND_EFFECT, newVolume);
  163. }
  164. void SoundEffects::HandleMusicVolume(StringHash eventType, VariantMap& eventData)
  165. {
  166. using namespace SliderChanged;
  167. float newVolume = eventData[P_VALUE].GetFloat();
  168. GetSubsystem<Audio>()->SetMasterGain(SOUND_MUSIC, newVolume);
  169. }