14_SoundEffects.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 soundNames = {
  7. "Fist",
  8. "Explosion",
  9. "Power-up"
  10. }
  11. local soundResourceNames = {
  12. "Sounds/PlayerFistHit.wav",
  13. "Sounds/BigExplosion.wav",
  14. "Sounds/Powerup.wav"
  15. }
  16. local musicSource
  17. function Start()
  18. -- Execute the common startup for samples
  19. SampleStart()
  20. -- Create a scene which will not be actually rendered, but is used to hold SoundSource components while they play sounds
  21. scene_ = Scene()
  22. -- Create music sound source
  23. musicSource = scene_:CreateComponent("SoundSource")
  24. -- Set the sound type to music so that master volume control works correctly
  25. musicSource.soundType = SOUND_MUSIC
  26. -- Enable OS cursor
  27. input.mouseVisible = true
  28. -- Create the user interface
  29. CreateUI()
  30. -- Set the mouse mode to use in the sample
  31. SampleInitMouseMode(MM_FREE)
  32. end
  33. function CreateUI()
  34. local uiStyle = cache:GetResource("XMLFile", "UI/DefaultStyle.xml")
  35. -- Set style to the UI root so that elements will inherit it
  36. ui.root.defaultStyle = uiStyle
  37. -- Create buttons for playing back sounds
  38. for i, v in ipairs(soundNames) do
  39. local button = CreateButton((i - 1) * 140 + 20, 20, 120, 40, v)
  40. -- Store the sound effect resource name as a custom variable into the button
  41. button:SetVar(StringHash("SoundResource"), Variant(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(StringHash("SoundResource")):GetString()
  88. -- Get the sound resource
  89. local sound = cache:GetResource("Sound", soundResourceName)
  90. if sound ~= nil then
  91. -- Create 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 soundSource = scene_:CreateComponent("SoundSource")
  95. soundSource:SetAutoRemoveMode(REMOVE_COMPONENT)
  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. end
  100. end
  101. function HandlePlayMusic(eventType, eventData)
  102. local music = cache:GetResource("Sound", "Music/Ninja Gods.ogg")
  103. -- Set the song to loop
  104. music.looped = true
  105. musicSource:Play(music)
  106. end
  107. function HandleStopMusic(eventType, eventData)
  108. musicSource:Stop()
  109. end
  110. function HandleSoundVolume(eventType, eventData)
  111. local newVolume = eventData["Value"]:GetFloat()
  112. audio:SetMasterGain(SOUND_EFFECT, newVolume)
  113. end
  114. function HandleMusicVolume(eventType, eventData)
  115. local newVolume = eventData["Value"]:GetFloat()
  116. audio:SetMasterGain(SOUND_MUSIC, newVolume)
  117. end
  118. -- Create XML patch instructions for screen joystick layout specific to this sample app
  119. function GetScreenJoystickPatchString()
  120. return
  121. "<patch>" ..
  122. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Button2']]\">" ..
  123. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  124. " </add>" ..
  125. " <add sel=\"/element/element[./attribute[@name='Name' and @value='Hat0']]\">" ..
  126. " <attribute name=\"Is Visible\" value=\"false\" />" ..
  127. " </add>" ..
  128. "</patch>"
  129. end