Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

sr_sound2d with working stereo

By: Daemonion on Oct 18th, 2012  |  syntax: Lua  |  size: 7.60 KB  |  views: 91  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. --[[------------------------------------------------------------------------------------------------------------------
  2. Original by: Tunduk Vladimir aka Sidorovich
  3. Updated: 20121018
  4. Edited by: Daemonion for LURK 1.2
  5. Credits: Daemonion, Malindrinus
  6. Thanks: NatVac, Malindrinus, Artos, AMK forums, GSC Forums
  7. Changes: Pseudo-stereo audio now works properly!
  8. --------------------------------------------------------------------------------------------------------------------]]
  9.  
  10.  
  11. class "action_sound2d"
  12. ----------------------
  13. function action_sound2d:__init (obj, storage)
  14.         self.object = obj
  15.         self.st = storage
  16.         self.snd_obj_l = nil
  17.         self.snd_obj_r = nil
  18.         self.time = 0
  19.         self.playing = false
  20.         self.current = 0
  21. end
  22. ----------------------
  23. function action_sound2d:reset_scheme()
  24.  
  25.     self:recreate_sounds ()
  26.    
  27.         self.st.signals = {}
  28.         self.time = time_global ()
  29.         self.playing = false
  30. end
  31. ----------------------
  32. function action_sound2d:recreate_sounds ()
  33.     self.snd_obj_l = nil
  34.     self.snd_obj_r = nil
  35.     local name_left, name_right = self:get_sound_name ()
  36.    
  37.     if name_left ~= nil then
  38.        printf ("RECREATE_SOUNDS. LEFT CHANNEL = %s", name_left)
  39.     end    
  40.  
  41.     if name_right ~= nil then
  42.        printf ("RECREATE_SOUNDS. RIGHT CHANNEL = %s", name_right)
  43.     end    
  44.  
  45.     self.snd_obj_l = xr_sound.get_safe_sound_object (name_left)
  46.     if name_right ~= nil then
  47.        self.snd_obj_r = xr_sound.get_safe_sound_object (name_right)
  48.     else  
  49.        self.snd_obj_r = nil
  50.     end  
  51. end
  52. ----------------------
  53. function action_sound2d:get_next_sound_index ()
  54.     local idx = self.current
  55.     local tsize = table.getn (sound_theme.ph_snd_themes[self.st.snd_theme])
  56.     if tsize == 1 then
  57.        idx = 1
  58.     else    
  59.        while idx == self.current do
  60.              idx = math.random (1, tsize)
  61.        end
  62.     end  
  63.     self.current = idx
  64.     return idx
  65. end
  66. ----------------------
  67. function action_sound2d:get_sound_name ()
  68.     local name
  69.     if self.st.snd_name ~= nil then
  70.        name = self.st.snd_name
  71.     else  
  72.        name = sound_theme.ph_snd_themes[self.st.snd_theme][self:get_next_sound_index ()]
  73.                 if type(name) == "table" then
  74.                         name = name[1]
  75.                 end
  76.     end
  77.        
  78.     if self.st.stereo == false then
  79.        return name, nil
  80.     end
  81.     return string.format ("%s_l", name), string.format ("%s_r", name)
  82. end
  83. ----------------------
  84. function action_sound2d:update (delta)
  85.     if self.snd_obj_l == nil then
  86.        return
  87.     end
  88.    
  89.     local actor = db.actor
  90.    
  91.     if self.playing == false then
  92.        self:play_sounds ()
  93.        self.playing = true
  94.        return
  95.     end
  96.    
  97.     --[[if self.st.stereo == true then
  98.        local pos_l, pos_r = self:get_source_positions ()
  99.        if self.snd_obj_l ~= nil and self.snd_obj_l:playing () == true then
  100.           self.snd_obj_l:set_position (pos_l)
  101.        end  
  102.        if self.snd_obj_r ~= nil and self.snd_obj_r:playing () == true then
  103.           self.snd_obj_r:set_position (pos_r)
  104.        end  
  105.     end   ]]
  106.    
  107.        
  108.     if self.snd_obj_l:playing () == false then
  109.        self.st.signals["sound_end"] = true
  110.        if self.st.snd_theme ~= nil then
  111.           self:recreate_sounds ()  
  112.           self.time = time_global ()
  113.               self.playing = false
  114.        end      
  115.     end  
  116.    
  117.     xr_logic.try_switch_to_another_section (self.object, self.st, actor)
  118. end
  119. ----------------------
  120. function action_sound2d:play_sounds ()
  121.  
  122.     --ðàññ÷åò çàäåðæêè
  123.     local d
  124.     if self.st.delay == self.st.delay_max then
  125.        d = self.st.delay
  126.     else
  127.        d = math.random (self.st.delay, self.st.delay_max)
  128.     end      
  129.  
  130.     local actor = db.actor
  131.     -- åñëè çâóê ìîíî, òî èãðàåì òîëüêî ñ ëåâîãî êàíàëà è òîëüêî â æáàíå àêòåðà
  132.     if self.st.stereo == false then
  133.        if self.snd_obj_l ~= nil then
  134.           self.snd_obj_l:play_at_pos (actor, vector ():set (0, 0, 0), d / 1000.0, sound_object.s2d)
  135.        end
  136.     else
  137.        --åñëè îäèí èç êàíàëîâ îòñóòñâóåò, òî ïèíàòü æåñòîêî çâóêîâèêà
  138.        if self.snd_obj_l == nil or self.snd_obj_r == nil then
  139.           abort ("Can't play stereo sound because sound file for left or right channel not loaded")  
  140.        end
  141.        --ðàññ÷èòàåì íàïðàâëåíèÿ íà èñòî÷íèêè çâóêà äëÿ ëåâîãî è ïðàâîãî ëîïóõà
  142.        --local pos_l, pos_r = self:get_source_positions ()
  143.        --self.snd_obj_l:play_at_pos (actor, pos_l, d / 1000.0, sound_object.s3d)
  144.        --self.snd_obj_r:play_at_pos (actor, pos_r, d / 1000.0, sound_object.s3d)
  145.            ---------------------------------------------------------
  146.            --   This change here allows for stereo audio - Daemonion
  147.            ---------------------------------------------------------
  148.            self.snd_obj_l:play_at_pos (actor, vector():set(-0.5,0.0,-0.3), d / 1000.0, sound_object.s2d)
  149.            self.snd_obj_r:play_at_pos (actor, vector():set( 0.5,0.0,0.3), d / 1000.0, sound_object.s2d)
  150.  
  151.     end      
  152. end
  153. ----------------------
  154. function action_sound2d:get_source_positions ()
  155.     local actor = db.actor
  156.     local dir = actor:direction ()
  157.     dir.y = 0.0
  158.     local dir_left = vector_rotate_y (dir, 45)
  159.     local dir_right = vector_rotate_y (dir, -45)
  160.     dir_left:normalize ()
  161.     dir_right:normalize ()
  162.        --ðàññ÷èòàåì âûñîòó
  163.     local pos = actor:position ()
  164.     pos.y = pos.y + 1.5   --íó íèçêîðîñëûé ó íàñ àêòåð...
  165.     local pos_l = pos;
  166.     return pos_l:add (dir_left:mul (1.5)), pos:add (dir_right:mul (1.5))
  167. end
  168. ----------------------
  169. function action_sound2d:deactivate ()
  170.     if self.snd_obj_l ~= nil then
  171.        if self.snd_obj_l:playing () == true then
  172.           self.snd_obj_l:stop ()
  173.        end
  174.        self.snd_obj_l = nil
  175.     end        
  176.    
  177.     if self.snd_obj_r ~= nil then
  178.        if self.snd_obj_r:playing () == true then
  179.           self.snd_obj_r:stop ()
  180.        end
  181.        self.snd_obj_r = nil
  182.     end        
  183. end
  184.  
  185.  
  186.  
  187. ----------------------
  188. function add_to_binder( npc, ini, scheme, section, storage )
  189.  
  190. --      local new_action =
  191.  
  192.         -- Çàðåãèñòðèðîâàòü âñå actions, â êîòîðûõ äîëæåí áûòü âûçâàí ìåòîä reset_scheme ïðè èçìåíåíèè íàñòðîåê ñõåìû:
  193.         xr_logic.subscribe_action_for_events (npc, storage, action_sound2d (npc, storage))
  194. end
  195. ----------------------
  196. function set_scheme (obj, ini, scheme, section, gulag_name)
  197.  
  198.         local st = xr_logic.assign_storage_and_bind (obj, ini, scheme, section)
  199.         st.logic = xr_logic.cfg_get_switch_conditions (ini, section, obj)
  200.         st.snd_name = utils.cfg_get_string (ini, section, "snd", obj, false, "", nil)
  201.         st.snd_theme = utils.cfg_get_string (ini, section, "theme", obj, false, "", nil)
  202.        
  203.         if st.snd_theme ~= nil and st.snd_name then
  204.            abort ("SR_SOUND2D : can't definition sound file name and sound theme at the one time");
  205.         end    
  206.        
  207.         st.delay = utils.cfg_get_number (ini, section, "delay", obj, false, 0)
  208.         st.delay_max = utils.cfg_get_number (ini, section, "delay_max", obj, false, 0)
  209.         st.stereo = utils.cfg_get_bool (ini, section, "stereo", obj, false, false)
  210.        
  211.         if st.delay_max < st.delay then
  212.             st.delay_max = st.delay
  213.         end
  214.        
  215.         if st.snd_name ~= nil then
  216.        if st.snd_name == "" then
  217.               abort ("SR_SOUND2D : invalid sound name")
  218.            end  
  219.         else  
  220.            if st.snd_theme == nil or st.snd_theme == "" then
  221.               abort ("SR_SOUND2D : invalid sound theme")  
  222.            end
  223.         end  
  224.        
  225. end
  226. ----------------------
  227. ----------------------
  228. ----------------------
  229. ----------------------