14_SoundEffects.lua 5.6 KB

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