Advertisement
CapsAdmin

Untitled

Apr 9th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.88 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/energy/spark3.wav",
  8.     boop = "buttons/button16.wav",
  9.     kick = "weapons/crossbow/hitbod1.wav",
  10.     beep = "npc/attack_helicopter/aheli_crash_alert2.wav",
  11.     snare = "physics/flesh/flesh_strider_impact_bullet3.wav",
  12.     wood = "physics/wood/wood_box_impact_bullet4.wav"
  13. }
  14.  
  15. local MODIFIERS = {
  16.     ["=="] = {
  17.         args = {
  18.             function(time) return tonumber(time) or 1 end
  19.         },
  20.        
  21.         init = function(self, time)
  22.             self.duration = time
  23.         end,   
  24.     },
  25.     ["--"] = {
  26.         args = {
  27.             function(stop_percent) return tonumber(stop_percent) or 100 end
  28.         },
  29.        
  30.         init = function(self, stop_percent)
  31.             self.duration = self.duration * (stop_percent / 100)
  32.         end,   
  33.     },
  34.     ["="] = {
  35.         args = {
  36.             function(time) return tonumber(time) or 0 end
  37.         },
  38.        
  39.         init = function(self, time)
  40.             self.duration = time
  41.         end,
  42.     },
  43.     ["%"] = {
  44.         args = {
  45.             function(pitch) return math.Clamp(tonumber(pitch) or 100, 0, 255) end,
  46.             function(time) return tonumber(time) or 0 end,
  47.         },
  48.        
  49.         init = function(self, pitch, time)         
  50.             self.duration = self.duration / (pitch / 100)                          
  51.             self.duration = self.duration + time
  52.         end,
  53.        
  54.         think = function(self, pitch, time)        
  55.             if self.csp then
  56.                 self.csp:ChangePitch(pitch, time)
  57.             end
  58.         end,
  59.     },
  60.     ["^"] = {
  61.         args = {
  62.             function(volume) return math.Clamp(tonumber(volume) or 100, 0, 100) end,
  63.             function(time) return tonumber(time) or 0 end,
  64.         },
  65.        
  66.         think = function(self, volume, time)
  67.             if self.csp then
  68.                 if self.source_entity == LocalPlayer() then
  69.                     volume = volume / 2
  70.                 end
  71.                                                    
  72.                 self.csp:ChangeVolume(volume / 100, time)
  73.             end
  74.         end,
  75.     }
  76. }
  77.  
  78. local function PLAY_SOUND(self)
  79.     local csp = CreateSound(me, self.path)
  80.     csp:PlayEx(1, 0)
  81.     self.source_entity = me
  82.     self.csp = csp
  83. end
  84.  
  85. local function STOP_SOUND(self)
  86.     if self.csp then
  87.         self.csp:Stop()
  88.     end
  89. end
  90.  
  91. local function GET_DURATION(self)
  92.     return SoundDuration(self.path) or 0
  93. end
  94.  
  95. --
  96. local cache = {}
  97.  
  98. local function parse(str)
  99.     if cache[str] then return cache[str] end
  100.    
  101.     local out = {}
  102.    
  103.     -- lowercase it so we don't have to deal with case sensitivity
  104.     str = str:lower()
  105.    
  106.     -- add a last space so it matches the end as well
  107.     str = str .. " "
  108.    
  109.     -- split the input by either space or ;
  110.     for line in str:gmatch("(.-)%s") do
  111.         local key, mods = line:match("([a-z_]+)([%p%d]+)")
  112.                
  113.         -- if it doesn't have a modifier, use the whole line
  114.         if not key then
  115.             key = line
  116.         end
  117.        
  118.         local modifiers = {}
  119.        
  120.         if mods then           
  121.             for mod, args in mods:gmatch("(%p+)([%d%.,]+)") do         
  122.                 if args then
  123.                     -- add a last . so it matches the end as well
  124.                     args = args .. ","
  125.                    
  126.                     local temp = {}
  127.                    
  128.                     -- split the args by .
  129.                     for arg in args:gmatch("(.-),") do
  130.                        
  131.                         -- if it's a number make it a number, otherwise leave it as string
  132.                         arg = tonumber(arg) or arg
  133.                        
  134.                         table.insert(temp, arg)
  135.                     end    
  136.                    
  137.                     table.insert(modifiers, {mod = mod, args = temp})
  138.                 end        
  139.             end
  140.         end
  141.            
  142.         table.insert(out, {key = key, modifiers = modifiers})
  143.     end
  144.        
  145.     cache[str] = out
  146.    
  147.     return out
  148. end
  149.  
  150. local function play(sounds)
  151.     local id = "LOL_" ..tostring(sounds)
  152.     local active_sounds = {}
  153.    
  154.     hook.Add("RenderScene", id, function()
  155.         for key, data in pairs(active_sounds) do
  156.             if data.stop_time < SysTime() then
  157.                 if data.last_sound then
  158.                     print(data.active_sounds[key])
  159.                     data.active_sounds[key]:stop()
  160.                     hook.Remove("RenderScene", id)
  161.                 end
  162.                 active_sounds[key] = nil
  163.             else
  164.                 data:think()
  165.             end
  166.         end
  167.     end)
  168.    
  169.     -- copy the table since we're going to modify it
  170.     sounds = table.Copy(sounds)
  171.    
  172.     for i, sound in ipairs(sounds) do
  173.         local path = SOUNDS[sound.key]
  174.        
  175.         if path then
  176.             sound.path = path
  177.             sound.duration = GET_DURATION(sound)
  178.            
  179.             sound.play = function(self)
  180.                 PLAY_SOUND(self)
  181.             end
  182.            
  183.             sound.stop = function(self)
  184.                 STOP_SOUND(self)
  185.             end
  186.            
  187.             -- only add a think function if the modifier exists
  188.             if sound.modifiers then            
  189.                
  190.                 -- if args is defined use it to default and clamp the arguments
  191.                 for i, data in pairs(sound.modifiers) do
  192.                     local mod = MODIFIERS[data.mod]
  193.                     if mod and mod.args then
  194.                         for i, func in pairs(mod.args) do
  195.                             data.args[i] = func(data.args[i])
  196.                         end
  197.                     end
  198.                 end
  199.                
  200.                 sound.think = function(self)
  201.                     for i, data in pairs(self.modifiers) do
  202.                         local mod = MODIFIERS[data.mod]                    
  203.                         if mod and mod.think then
  204.                             mod.think(self, unpack(data.args))
  205.                         end
  206.                     end
  207.                 end
  208.             end
  209.         end
  210.     end
  211.        
  212.     -- play it
  213.     local duration = 0
  214.     local last
  215.    
  216.     for i, sound in ipairs(sounds) do
  217.        
  218.         if sound.play then
  219.                        
  220.             -- let it be able to think once first so we can modify duration and such when changing pitch
  221.             if sound.think then
  222.                 sound:think()
  223.             end
  224.            
  225.             for mod, data in pairs(sound.modifiers) do
  226.                 local mod = MODIFIERS[data.mod]                    
  227.                 if mod and mod.init then
  228.                     mod.init(sound, unpack(data.args))
  229.                 end
  230.             end
  231.                    
  232.             timer.Simple(duration, function()
  233.                 if last then
  234.                     last:stop()
  235.                 end
  236.                    
  237.                 sound.stop_time = SysTime() + sound.duration       
  238.                    
  239.                 sound:play()
  240.                                    
  241.                 if sound.think then
  242.                     table.insert(active_sounds, sound)
  243.                 end
  244.                    
  245.                 if i == #sounds then
  246.                     sound.last_sound = true
  247.                 end
  248.                    
  249.                 last = sound
  250.             end)
  251.                        
  252.             duration = duration + sound.duration           
  253.         end
  254.        
  255.     end
  256. end
  257.  
  258. local data = parse(
  259. [[
  260. boop%100=0.125
  261. boop%100=0.125
  262. boop%50=0.5
  263. boop%50=0.5
  264. boop%50=0.35
  265. boop%50=0.40
  266.  
  267. boop%100=0.125
  268. boop%100=0.125
  269. boop%50=0.5
  270. boop%50=0.5
  271. boop%25=0.35
  272. boop%50=0.4
  273.    
  274. boop%25=0.25
  275.  
  276. boop%25==0.25
  277. boop%25==0.125
  278. boop%25==0.25
  279. boop%25==0.125
  280.    
  281. boop%50=0.35
  282. boop%50=0.40
  283. boop%50=0.40
  284.    
  285. boop%25==0.25
  286. boop%25==0.125
  287. boop%25==0.25
  288. boop%25==0.125
  289.    
  290. boop%50=0.35
  291. boop%50=0.40
  292. boop%50=0.40
  293. ]])
  294.  
  295. play(data)
  296.  
  297. local data = parse(
  298. [[
  299.     kick%150=0.25^0
  300.     kick%150=0.5
  301.     kick%150=0.5
  302.     kick%150=0.5
  303.     kick%150=0.5
  304.     kick%150=0.5
  305.     kick%150=0.5
  306.     kick%150=0.5
  307.     kick%150=0.5
  308.     kick%150=0.5
  309.     kick%150=0.5
  310.     kick%150=0.5
  311.     kick%150=0.5
  312.     kick%150=0.5
  313.     kick%150=0.5
  314.     kick%150=0.5
  315.     kick%150=0.5
  316. ]]
  317. )
  318.  
  319. play(data)
  320.  
  321. local data = parse(
  322. [[
  323. oh%255=0.125^100
  324. oh%255=0.125^75
  325. oh%255=0.125^50
  326. oh%255=0.125^25
  327.    
  328.     oh%255=0.125^100
  329. oh%255=0.125^75
  330. oh%255=0.125^50
  331. oh%255=0.125^25
  332.    
  333.     oh%255=0.125^100
  334. oh%255=0.125^75
  335. oh%255=0.125^50
  336. oh%255=0.125^25
  337.    
  338.     oh%255=0.125^100
  339. oh%255=0.125^75
  340. oh%255=0.125^50
  341. oh%255=0.125^25
  342.    
  343.     oh%255=0.125^100
  344. oh%255=0.125^75
  345. oh%255=0.125^50
  346. oh%255=0.125^25
  347.    
  348.     oh%255=0.125^100
  349. oh%255=0.125^75
  350. oh%255=0.125^50
  351. oh%255=0.125^25
  352.    
  353.     oh%255=0.125^100
  354. oh%255=0.125^75
  355. oh%255=0.125^50
  356. oh%255=0.125^25
  357.    
  358.     oh%255=0.125^100
  359. oh%255=0.125^75
  360. oh%255=0.125^50
  361. oh%255=0.125^25
  362.    
  363.     oh%255=0.125^100
  364. oh%255=0.125^75
  365. oh%255=0.125^50
  366. oh%255=0.125^25
  367.    
  368.     oh%255=0.125^100
  369. oh%255=0.125^75
  370. oh%255=0.125^50
  371. oh%255=0.125^25
  372.    
  373.     oh%255=0.125^100
  374. oh%255=0.125^75
  375. oh%255=0.125^50
  376. oh%255=0.125^25
  377.    
  378.     oh%255=0.125^100
  379. oh%255=0.125^75
  380. oh%255=0.125^50
  381. oh%255=0.125^25
  382.    
  383.     oh%255=0.125^100
  384. oh%255=0.125^75
  385. oh%255=0.125^50
  386. oh%255=0.125^25
  387.    
  388.     oh%255=0.125^100
  389. oh%255=0.125^75
  390. oh%255=0.125^50
  391. oh%255=0.125^25
  392.    
  393.     oh%255=0.125^100
  394. oh%255=0.125^75
  395. oh%255=0.125^50
  396. oh%255=0.125^25
  397.    
  398.     oh%255=0.125^100
  399. oh%255=0.125^75
  400. oh%255=0.125^50
  401. oh%255=0.125^25
  402.    
  403.    
  404. ]])
  405.  
  406. play(data)
  407.  
  408. local data = parse(
  409. [[
  410. hi%100=0.5
  411. hi%100=0.5
  412. hi%100=0.5
  413. hi%100=0.5
  414. hi%100=0.5
  415. hi%100=0.5
  416. hi%100=0.5
  417. hi%100=0.5
  418. ]])
  419.  
  420. play(data)
  421.  
  422. local data = parse(
  423. [[
  424.     snare%255=0.75^0
  425.     snare%150=1^0
  426.     snare%150=1^0
  427.     snare%150=1^0
  428.     snare%150=1^0
  429.     snare%150=1
  430.     snare%150=1
  431.     snare%150=1
  432.     snare%150=1
  433. ]] 
  434. )
  435.  
  436. play(data)
  437.  
  438. local data = parse[[
  439.     wood%100=1
  440.     wood%125=1
  441.     wood%150=1
  442. ]]
  443.  
  444. play(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement