14_SoundEffects.lua 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. -- Sound effects example
  2. -- This sample demonstrates:
  3. -- - Playing sound effects and music
  4. -- - Controlling sound and music master volume
  5. require "LuaScripts/Utilities/Sample"
  6. local scene_ = nil
  7. local soundNames = {
  8. "Fist",
  9. "Explosion",
  10. "Power-up"
  11. }
  12. local soundResourceNames = {
  13. "Sounds/PlayerFistHit.wav",
  14. "Sounds/BigExplosion.wav",
  15. "Sounds/Powerup.wav"
  16. }
  17. function Start()
  18. -- Execute the common startup for samples
  19. SampleStart()
  20. -- Enable OS cursor
  21. input.mouseVisible = true
  22. -- Create the user interface
  23. CreateUI()
  24. end
  25. function CreateUI()
  26. -- Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  27. scene_ = Scene()
  28. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  29. -- Set style to the UI root so that elements will inherit it
  30. ui.root.defaultStyle = uiStyle
  31. -- Create buttons for playing back sounds
  32. for i, v in ipairs(soundNames) do
  33. local button = CreateButton((i - 1) * 140 + 20, 20, 120, 40, v)
  34. -- Store the sound effect resource name as a custom variable into the button
  35. button:SetVar(ShortStringHash("SoundResource"), Variant(soundResourceNames[i]))
  36. SubscribeToEvent(button, "Pressed", "HandlePlaySound")
  37. end
  38. -- Create buttons for playing/stopping music
  39. local button = CreateButton(20, 80, 120, 40, "Play Music")
  40. SubscribeToEvent(button, "Released", "HandlePlayMusic")
  41. button = CreateButton(160, 80, 120, 40, "Stop Music")
  42. SubscribeToEvent(button, "Released", "HandleStopMusic")
  43. -- Create sliders for controlling sound and music master volume
  44. local slider = CreateSlider(20, 140, 200, 20, "Sound Volume")
  45. slider.value = audio:GetMasterGain(SOUND_EFFECT)
  46. SubscribeToEvent(slider, "SliderChanged", "HandleSoundVolume")
  47. slider = CreateSlider(20, 200, 200, 20, "Music Volume")
  48. slider.value = audio:GetMasterGain(SOUND_MUSIC)
  49. SubscribeToEvent(slider, "SliderChanged", "HandleMusicVolume")
  50. end
  51. function CreateButton(x, y, xSize, ySize, text)
  52. local font = cache:GetResource("Font", "Fonts/Anonymous Pro.ttf")
  53. -- Create the button and center the text onto it
  54. local button = ui.root:CreateChild("Button")
  55. button:SetStyleAuto()
  56. button:SetPosition(x, y)
  57. button:SetSize(xSize, ySize)
  58. local buttonText = button:CreateChild("Text")
  59. buttonText:SetAlignment(HA_CENTER, VA_CENTER)
  60. buttonText:SetFont(font, 12)
  61. buttonText:SetText(text)
  62. return button
  63. end
  64. function CreateSlider(x, y, xSize, ySize, text)
  65. local font = cache:GetResource("Font", "Fonts/Anonymous Pro.ttf")
  66. -- Create text and slider below it
  67. local sliderText = ui.root:CreateChild("Text")
  68. sliderText:SetPosition(x, y)
  69. sliderText:SetFont(font, 12)
  70. sliderText:SetText(text)
  71. local slider = ui.root:CreateChild("Slider")
  72. slider:SetStyleAuto()
  73. slider:SetPosition(x, y + 20)
  74. slider:SetSize(xSize, ySize)
  75. -- Use 0-1 range for controlling sound/music master volume
  76. slider.range = 1.0
  77. return slider
  78. end
  79. function HandlePlaySound(sender, eventType, eventData)
  80. local button = tolua.cast(GetEventSender(), "Button")
  81. local soundResourceName = button:GetVar(ShortStringHash("SoundResource")):GetString()
  82. -- Get the sound resource
  83. local sound = cache:GetResource("Sound", soundResourceName)
  84. if sound ~= nil then
  85. -- Create a scene node with a SoundSource component for playing the sound. The SoundSource component plays
  86. -- non-positional audio, so its 3D position in the scene does not matter. For positional sounds the
  87. -- SoundSource3D component would be used instead
  88. local soundNode = scene_:CreateChild("Sound")
  89. local soundSource = soundNode:CreateComponent("SoundSource")
  90. soundSource:Play(sound)
  91. -- In case we also play music, set the sound volume below maximum so that we don't clip the output
  92. soundSource.gain = 0.7
  93. -- Set the sound component to automatically remove its scene node from the scene when the sound is done playing
  94. soundSource.autoRemove = true
  95. end
  96. end
  97. function HandlePlayMusic(eventType, eventData)
  98. -- Check if the music player node/component already exist
  99. if scene_:GetChild("Music") ~= nil then
  100. return
  101. end
  102. local music = cache:GetResource("Sound", "Music/Ninja Gods.ogg")
  103. -- Set the song to loop
  104. music.looped = true
  105. -- Create a scene node and a sound source for the music
  106. local musicNode = scene_:CreateChild("Music")
  107. local musicSource = musicNode:CreateComponent("SoundSource")
  108. -- Set the sound type to music so that master volume control works correctly
  109. musicSource.soundType = SOUND_MUSIC
  110. musicSource:Play(music)
  111. end
  112. function HandleStopMusic(eventType, eventData)
  113. -- Remove the music player node from the scene
  114. scene_:RemoveChild(scene_:GetChild("Music"))
  115. end
  116. function HandleSoundVolume(eventType, eventData)
  117. local newVolume = eventData:GetFloat("Value")
  118. audio:SetMasterGain(SOUND_EFFECT, newVolume)
  119. end
  120. function HandleMusicVolume(eventType, eventData)
  121. local newVolume = eventData:GetFloat("Value")
  122. audio:SetMasterGain(SOUND_MUSIC, newVolume)
  123. end