14_SoundEffects.lua 5.5 KB

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