Advertisement
CapsAdmin

Untitled

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