14_SoundEffects.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. // Copyright (c) 2016 THUNDERBEAST GAMES LLC
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. using System.Collections.Generic;
  25. using System.Linq;
  26. using AtomicEngine;
  27. namespace FeatureExamples
  28. {
  29. public class SoundEffectsSample : Sample
  30. {
  31. Scene scene;
  32. readonly Dictionary<string, string> sounds = new Dictionary<string, string>
  33. {
  34. {"Fist", "Sounds/PlayerFistHit.wav"},
  35. {"Explosion", "Sounds/BigExplosion.wav"},
  36. {"Power-up", "Sounds/Powerup.wav"},
  37. };
  38. public SoundEffectsSample() : base() { }
  39. public override void Start()
  40. {
  41. base.Start();
  42. CreateUI();
  43. }
  44. void CreateUI()
  45. {
  46. var cache = GetSubsystem<ResourceCache>();
  47. var layout = new UILayout() { Axis = UI_AXIS.UI_AXIS_Y };
  48. layout.Rect = UIView.Rect;
  49. UIView.AddChild(layout);
  50. // Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  51. scene = new Scene();
  52. // Create buttons for playing back sounds
  53. foreach (var item in sounds)
  54. {
  55. var button = new UIButton();
  56. layout.AddChild(button);
  57. button.Text = item.Key;
  58. button.SubscribeToEvent<WidgetEvent>(button, e => {
  59. if (e.Type == UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK)
  60. {
  61. // Get the sound resource
  62. Sound sound = cache.Get<Sound>(item.Value);
  63. if (sound != null)
  64. {
  65. // Create a scene node with a SoundSource component for playing the sound. The SoundSource component plays
  66. // non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  67. // SoundSource3D component would be used instead
  68. Node soundNode = scene.CreateChild("Sound");
  69. SoundSource soundSource = soundNode.CreateComponent<SoundSource>();
  70. soundSource.Play(sound);
  71. // In case we also play music, set the sound volume below maximum so that we don't clip the output
  72. soundSource.Gain = 0.75f;
  73. // Set the sound component to automatically remove its scene node from the scene when the sound is done playing
  74. }
  75. }
  76. });
  77. }
  78. // Create buttons for playing/stopping music
  79. var playMusicButton = new UIButton();
  80. layout.AddChild(playMusicButton);
  81. playMusicButton.Text = "Play Music";
  82. playMusicButton.SubscribeToEvent<WidgetEvent> (playMusicButton, e => {
  83. if (e.Type != UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK)
  84. return;
  85. if (scene.GetChild ("Music", false) != null)
  86. return;
  87. var music = cache.Get<Sound>("Music/StoryTime.ogg");
  88. music.Looped = true;
  89. Node musicNode = scene.CreateChild ("Music");
  90. SoundSource musicSource = musicNode.CreateComponent<SoundSource> ();
  91. // Set the sound type to music so that master volume control works correctly
  92. musicSource.SetSoundType ("Music");
  93. musicSource.Play (music);
  94. });
  95. var audio = GetSubsystem<Audio>();
  96. // FIXME: Removing the music node is not stopping music
  97. var stopMusicButton = new UIButton();
  98. layout.AddChild(stopMusicButton);
  99. stopMusicButton.Text = "Stop Music";
  100. stopMusicButton.SubscribeToEvent<WidgetEvent>(stopMusicButton, e =>
  101. {
  102. if (e.Type != UI_EVENT_TYPE.UI_EVENT_TYPE_CLICK)
  103. return;
  104. scene.RemoveChild(scene.GetChild("Music", false));
  105. });
  106. // Effect Volume Slider
  107. var slider = new UISlider();
  108. layout.AddChild(slider);
  109. slider.SetLimits(0, 1);
  110. slider.Text = "Sound Volume";
  111. slider.SubscribeToEvent<WidgetEvent>(slider, e =>
  112. {
  113. if (e.Type != UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED)
  114. return;
  115. Log.Info($"Setting Effects to {slider.Value}");
  116. audio.SetMasterGain("Effect", slider.Value);
  117. });
  118. // Music Volume Slider
  119. var slider2 = new UISlider();
  120. layout.AddChild(slider2);
  121. slider2.SetLimits(0, 1);
  122. slider2.Text = "Music Volume";
  123. slider2.SubscribeToEvent<WidgetEvent>(slider2, e =>
  124. {
  125. if (e.Type != UI_EVENT_TYPE.UI_EVENT_TYPE_CHANGED)
  126. return;
  127. Log.Info($"Setting Music to {slider2.Value}");
  128. audio.SetMasterGain("Music", slider2.Value);
  129. });
  130. }
  131. }
  132. }