CapsAdmin

Untitled

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