CapsAdmin

Untitled

Apr 9th, 2013
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.38 KB | None | 0 0
  1. local print = epoe.Print
  2. local me = me
  3. -- config / gmod specific
  4.  
  5. local SOUNDS = {
  6.     hi = "ambient/levels/prison/radio_random11.wav",
  7.     oh = "ambient/water/rain_drip4.wav",
  8.     boop = "buttons/button16.wav",
  9. }
  10.  
  11. local MODIFIERS = {
  12.     ["--"] = {
  13.         args = {
  14.             function(stop_percent) return tonumber(stop_percent) or 100 end
  15.         },
  16.        
  17.         init = function(self, stop_percent)
  18.             self.duration = self.duration / (stop_percent / 100)
  19.         end,   
  20.     },
  21.     ["="] = {
  22.         args = {
  23.             function(time) return tonumber(time) or 0 end
  24.         },
  25.        
  26.         init = function(self, time)
  27.             self.duration = time
  28.         end,
  29.     },
  30.     ["%"] = {
  31.         args = {
  32.             function(pitch) return math.Clamp(tonumber(pitch) or 100, 0, 255) end,
  33.             function(time) return tonumber(time) or 0 end,
  34.         },
  35.        
  36.         init = function(self, pitch, time)         
  37.             self.duration = self.duration / (pitch / 100)                          
  38.             self.duration = self.duration + time
  39.         end,
  40.        
  41.         think = function(self, pitch, time)        
  42.             if self.csp then
  43.                 self.csp:ChangePitch(pitch, time)
  44.                
  45.             end
  46.         end,
  47.     },
  48.     ["^"] = {
  49.         args = {
  50.             function(volume) return math.Clamp(tonumber(volume) or 1, 0, 1) end,
  51.             function(time) return tonumber(time) or 0 end,
  52.         },
  53.        
  54.         think = function(self, volume, time)
  55.             if self.csp then
  56.                 if self.source_entity == LocalPlayer() then
  57.                     volume = volume / 2
  58.                 end
  59.                                    
  60.                 self.csp:ChangeVolume(volume / 100, time)
  61.             end
  62.         end,
  63.     }
  64. }
  65.  
  66. local function PLAY_SOUND(self)
  67.     local csp = CreateSound(me, self.path)
  68.     csp:PlayEx(1, 0)
  69.     self.source_entity = me
  70.     self.csp = csp
  71. end
  72.  
  73. local function STOP_SOUND(self)
  74.     if self.csp then
  75.         self.csp:Stop()
  76.     end
  77. end
  78.  
  79. local function GET_DURATION(self)
  80.     return SoundDuration(self.path) or 0
  81. end
  82.  
  83. --
  84. local cache = {}
  85.  
  86. local function parse(str)
  87.     if cache[str] then return cache[str] end
  88.    
  89.     local out = {}
  90.    
  91.     -- lowercase it so we don't have to deal with case sensitivity
  92.     str = str:lower()
  93.    
  94.     -- add a last space so it matches the end as well
  95.     str = str .. " "
  96.    
  97.     -- split the input by either space or ;
  98.     for line in str:gmatch("(.-)%s") do
  99.         local key, mods = line:match("([a-z_]+)([%p%d]+)")
  100.                
  101.         -- if it doesn't have a modifier, use the whole line
  102.         if not key then
  103.             key = line
  104.         end
  105.        
  106.         local modifiers = {}
  107.        
  108.         if mods then           
  109.             for mod, args in mods:gmatch("(%p+)([%d%.,]+)") do         
  110.                 if args then
  111.                     -- add a last . so it matches the end as well
  112.                     args = args .. ","
  113.                    
  114.                     local temp = {}
  115.                    
  116.                     -- split the args by .
  117.                     for arg in args:gmatch("(.-),") do
  118.                        
  119.                         -- if it's a number make it a number, otherwise leave it as string
  120.                         arg = tonumber(arg) or arg
  121.                        
  122.                         table.insert(temp, arg)
  123.                     end    
  124.                    
  125.                     modifiers[mod] = temp
  126.                 end        
  127.             end
  128.         end
  129.            
  130.         table.insert(out, {key = key, modifiers = modifiers})
  131.     end
  132.        
  133.     cache[str] = out
  134.    
  135.     return out
  136. end
  137.  
  138. local function play(sounds)
  139.     local id = "LOL_" ..tostring(sounds)
  140.     local active_sounds = {}
  141.    
  142.     hook.Add("RenderScene", id, function()
  143.         for key, data in pairs(active_sounds) do
  144.             if data.stop_time < SysTime() then
  145.                 active_sounds[key] = nil
  146.                 if data.last_sound then
  147.                     hook.Remove("RenderScene", id)
  148.                 end
  149.             else
  150.                 data:think()
  151.             end
  152.         end
  153.     end)
  154.    
  155.     -- copy the table since we're going to modify it
  156.     sounds = table.Copy(sounds)
  157.    
  158.     for i, data in ipairs(sounds) do
  159.         local path = SOUNDS[data.key]
  160.        
  161.         if path then
  162.             data.path = path
  163.             data.duration = GET_DURATION(data)
  164.            
  165.             data.play = function(self)
  166.                 PLAY_SOUND(self)
  167.             end
  168.            
  169.             data.stop = function(self)
  170.                 STOP_SOUND(self)
  171.             end
  172.            
  173.             -- only add a think function if the modifier exists
  174.             if data.modifiers then             
  175.                
  176.                 -- if args is defined use it to default and clamp the arguments
  177.                 for mod, args in pairs(data.modifiers) do
  178.                     mod = MODIFIERS[mod]
  179.                     if mod and mod.args then
  180.                         for i, func in pairs(mod.args) do
  181.                             args[i] = func(args[i])
  182.                         end
  183.                     end
  184.                 end
  185.                
  186.                 data.think = function(self)
  187.                     for mod, args in pairs(self.modifiers) do
  188.                         mod = MODIFIERS[mod]                       
  189.                         if mod and mod.think then
  190.                             mod.think(self, unpack(args))
  191.                         end
  192.                     end
  193.                 end
  194.             end
  195.         end
  196.     end
  197.        
  198.     -- play it
  199.     local duration = 0
  200.     local last
  201.    
  202.     for i, data in ipairs(sounds) do
  203.        
  204.         if data.play then
  205.                        
  206.             -- let it be able to think once first so we can modify duration and such when changing pitch
  207.             if data.think then
  208.                 data:think()
  209.             end
  210.            
  211.             for mod, args in pairs(data.modifiers) do
  212.                 local mod = MODIFIERS[mod]                     
  213.                 if mod and mod.init then
  214.                     mod.init(data, unpack(args))
  215.                 end
  216.             end
  217.                                
  218.             timer.Simple(duration, function()              
  219.                 if last then
  220.                     last:stop()
  221.                 end
  222.                
  223.                 data.stop_time = SysTime() + data.duration
  224.                 if i == #sounds then
  225.                     data.last_sound = true 
  226.                 end
  227.                    
  228.                 data:play()
  229.                                    
  230.                 if data.think then
  231.                     table.insert(active_sounds, data)
  232.                 end
  233.                                
  234.                 last = data
  235.             end)
  236.                        
  237.             duration = duration + data.duration        
  238.         end
  239.        
  240.     end
  241. end
  242.  
  243. local data = parse(
  244. [[
  245. boop%100=0.125
  246. boop%100=0.125
  247. boop%50=0.5
  248. boop%50=0.5
  249. boop%50=0.35
  250. boop%50=0.40
  251.  
  252. boop%100=0.125
  253. boop%100=0.125
  254. boop%50=0.5
  255. boop%50=0.5
  256. boop%50=0.35
  257. boop%50=1
  258. ]])
  259.  
  260. play(data)
  261.  
  262. local data = parse(
  263. [[
  264. hi%100=0.5
  265. hi%100=0.5
  266. hi%100=0.5
  267. hi%100=0.5
  268. hi%100=0.5
  269. hi%100=0.5
  270. hi%100=0.5
  271. hi%100=0.5
  272. ]])
  273.  
  274. play(data)
Advertisement
Add Comment
Please, Sign In to add comment