Guest User

HowTo for adding sounds in LUA

a guest
Mar 7th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.85 KB | None | 0 0
  1. --[[
  2. Sounds in DCS must belong to a sound host.  Each sound host is specific to a certain context, like cockpit vs external or 2D sounds vs 3D sounds.  You can create as many sound hosts as you wish
  3. but I usually group them based on emission source type (mechanical, warning, systems, engine, etc.).
  4.  
  5. -- CREATING A SOUND HOST --
  6. create_sound_host(<SOUND_HOST_NAME>, <CONTEXT>, x, y, z)
  7.     <SOUND_HOST_NAME> is a name chosen by the programmer
  8.     <CONTEXT> is a string of the sound context, known contexts for lua: "2D", "3D", "HEADPHONES" (I do not know how to add external sounds, these contexts only work for the cockpit)
  9.     x, y, z are the coordinates of the sound host emission point for the "3D" context
  10.  
  11. -- SOUND HOST FUNCTIONS --
  12. create_sound(<SDEF_PATH>) -- create a sound source that belongs to the sound host
  13.     <SDEF_PATH> is the path to the sdef file for the sound, starting from the sdef folder in the mounted sounds directory
  14.     for example, in entry.lua, mount_vfs_sound_path    (current_mod_path.."/Sounds") -- the sdef folder should be in the Sounds folder,
  15.     so creating a sound from the sdef file "<MOD_DIR>/Sounds/sdef/Aircrafts/A-4/Cockpit/FuelPump.sdef" is    create_sound("Aircrafts/A-4/Cockpit/FuelPump")
  16. update(gain, x, y, z) -- update the global gain and/or x,y,z coordinates for the sound host, effective for all owned sound sources.
  17.  
  18. -- SOUND SOURCE FUNCTIONS --
  19. stop()
  20. play_once()
  21. play_continue() -- will loop the sound until stop() command
  22. is_playing() --note: this does NOT tell you if the sound file has finished playing or not.  It only tells you if a play_once() or play_continue() command has been given for the sound and has not yet been stopped with a stop() command
  23. update(pitch, gain, lowpass_freq) -- tell the sound to update its pitch, gain, or lowpass frequency values (use nil for any parameter you don't wish to change)
  24.  
  25.  
  26. --]]
  27.  
  28. -- Full Example:
  29.  
  30. local dev       = GetSelf()
  31. local dt = 0.006
  32. make_default_activity(dt)
  33.  
  34. local iCommandPlaneFonar = 71
  35.  
  36. dev:listen_command(iCommandPlaneFonar)
  37.  
  38. -- Note: Not shown is logic for determining which direction the canopy is moving
  39.  
  40. local canopyOpenSoundCommand = -1
  41. local canopyCloseSoundCommand = -1
  42.  
  43.  
  44. function createCockpitSound(sound_host, sdef_name) 
  45.     return sound_host:create_sound("Aircrafts/A-4/Cockpit/" .. sdef_name)
  46. end
  47.  
  48. -- Function to create an object that stores the parameters and sound sources necessary to easily do looping sounds with start and ending sounds
  49. -- Using this looping sound source structure is possible even without starting or ending sound sources (just send nil in place of a sdef path)
  50. -- Note: Starting sound length is important, so that the looping sound knows when to start playing after the start sound should be ended.  
  51. --       This is necessary, since there are no functions or callbacks to get the real state of a sound source or when it has finished playing
  52. function createCockpitSoundLoop(sound_host, start_sound_length, sdef_name_start, sdef_name_loop, sdef_name_end)
  53.     start_length_ = start_sound_length or 0
  54.    
  55.     if sdef_name_start then
  56.         sound_start_ = createCockpitSound(sound_host, sdef_name_start)
  57.     else
  58.         sound_start_ = nil
  59.     end
  60.    
  61.     sound_loop_ = createCockpitSound(sound_host, sdef_name_loop)
  62.    
  63.     if sdef_name_end then
  64.         sound_end_ = createCockpitSound(sound_host, sdef_name_end)
  65.     else
  66.         sound_end_ = nil
  67.     end
  68.    
  69.     return {
  70.         startLength = start_length_,
  71.         timePlaying = 0,
  72.         isPlaying = false,
  73.         sound_start = sound_start_,
  74.         sound_loop = sound_loop_,
  75.         sound_end = sound_end_,
  76.     }
  77. end
  78.  
  79. function post_initialize()
  80.    
  81.     if create_sound_host ~= nil then
  82.         cpt_mech_sh                 = create_sound_host("COCKPIT_MECH_SOUND_HOST","2D",0,0,0)
  83.        
  84.         sound_CanopyOpenLoop        = createCockpitSoundLoop(cpt_mech_sh, 0.7041, "CanopyOpenStart", "CanopyOpenLoop", "CanopyOpenEnd")
  85.         sound_CanopyCloseLoop       = createCockpitSoundLoop(cpt_mech_sh, 1.1459, "CanopyCloseStart", "CanopyCloseLoop", "CanopyCloseEnd")
  86.         sound_CanopyClose           = createCockpitSound(cpt_mech_sh, "CanopyClose")
  87.         sound_CanopyOpen            = createCockpitSound(cpt_mech_sh, "CanopyOpen")
  88.     end
  89.  
  90. end
  91.  
  92. function stopSound(sound)  
  93.     if sound and sound:is_playing() then
  94.         sound:stop()       
  95.     end
  96. end
  97.  
  98. function playSoundOnce(sound)
  99.     if sound then
  100.         if sound:is_playing() then
  101.             sound:stop()
  102.         end    
  103.         sound:play_once()
  104.     end
  105. end
  106.  
  107. function stopLoopSound(loopSound, playEndSound)
  108.     stopSound(loopSound.sound_start)
  109.     stopSound(loopSound.sound_loop)
  110.     loopSound.isPlaying = false
  111.     loopSound.timePlaying = 0
  112.     if playEndSound then
  113.         playSoundOnce(loopSound.sound_end)
  114.     end
  115. end
  116.  
  117. -- Function to handle looping sounds (start, loop, and end sources) more easily
  118. -- command is a value that tells the function how to handle the looping source (-1 = no command, 0 = stop, 1 = start)
  119. function updateLoopingSound(command, loopSound)
  120.     if command > -1
  121.         if loopSound then
  122.             if command == 0 and loopSound.isPlaying then
  123.                 --Stop command given
  124.                 stopLoopSound(loopSound, true)
  125.             elseif command > 0 then
  126.                 --Play command given
  127.                 if loopSound.isPlaying then
  128.                     -- first stop if playing
  129.                     stopLoopSound(loopSound)
  130.                 end
  131.                
  132.                 if loopSound.sound_start then
  133.                     loopSound.sound_start:play_once()
  134.                 end
  135.                 loopSound.isPlaying = true
  136.             end
  137.         end    
  138.     else
  139.         --no sound command given, manage start/looping sound
  140.         if loopSound.isPlaying then
  141.             loopSound.timePlaying = loopSound.timePlaying + dt
  142.            
  143.             if loopSound.sound_start and loopSound.sound_start:is_playing() then
  144.                 if loopSound.timePlaying > (loopSound.startLength - dt) then                   
  145.                     loopSound.sound_loop:play_continue()
  146.                 end
  147.                 if loopSound.timePlaying > loopSound.startLength then
  148.                     loopSound.sound_start:stop()
  149.                     loopSound.sound_loop:play_continue()
  150.                 end
  151.             else
  152.                 loopSound.sound_loop:play_continue()
  153.             end
  154.         end
  155.     end
  156. end
  157.  
  158.  
  159. function update()
  160.    
  161.     updateLoopingSound(canopyOpenSoundCommand, sound_CanopyOpenLoop)
  162.     updateLoopingSound(canopyCloseSoundCommand, sound_CanopyCloseLoop)
  163.    
  164.     -- reset sound commands
  165.     canopyOpenSoundCommand = -1
  166.     canopyCloseSoundCommand = -1
  167.    
  168.  
  169.    
  170. end
  171.  
  172.  
  173. function SetCommand(command,value)
  174.  
  175.    
  176.     if command == iCommandPlaneFonar then
  177.         -- Note: Not shown is logic for determining which direction the canopy is moving or when to switch from playing/stopping... this depends on the mechanics of the system being modelled
  178.        
  179.         -- Example of a looping sound play
  180.         canopyOpenSoundCommand = 1 -- open canopy looping sound will start playing on next iteration
  181.         canopyCloseSoundCommand = 0 -- close canopy looping sound will stop playing on next iteration  
  182.         -- or
  183.         canopyCloseSoundCommand = 1 -- close canopy looping sound will start playing on next iteration
  184.         canopyOpenSoundCommand = 0 -- open canopy looping sound will stop playing on next iteration
  185.        
  186.        
  187.         -- Examples of a single sound play
  188.         playSoundOnce(sound_CanopyOpen)
  189.         -- or
  190.         playSoundOnce(sound_CanopyClose)
  191.     end
  192. end
Add Comment
Please, Sign In to add comment