CapsAdmin

Untitled

Mar 22nd, 2012
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. local time = 0
  2.  
  3. local function getpitch(offset)
  4.     return 440 * 2 ^ ((offset - 48) / 12)
  5. end
  6.  
  7. local function saw(offset)
  8.     return (time * getpitch(offset))%1
  9. end
  10.  
  11. local function pwm(offset, w)
  12.     w = w or 0.5
  13.  
  14.     return (time * getpitch(offset))%1 > w and 1 or 0
  15. end
  16.  
  17. local function sin(offset)
  18.     return math.sin(time * math.pi * 2 * getpitch(offset))
  19. end
  20.  
  21. local v
  22. local function tri(offset)
  23.     v = (time * getpitch(offset))%1
  24.  
  25.     return v > 0.5 and (-v + 1) or v
  26. end
  27.  
  28. local function super(func, offset, detune, amount)
  29.     local v = 0
  30.     for i = -amount, amount do
  31.         v = v + func(offset + (i / detune))
  32.     end
  33.  
  34.     return v
  35. end
  36.  
  37. local voldata = Vec3(1,1,1) -- V, L, R
  38. local should_play
  39.  
  40. local function calcsource(eye, rgt, src, dist, fwd)
  41.     local vol = math.clamp(-((eye - src):GetLength() / dist) + 1, 0, 1)
  42.     local dot = rgt:Dot((src - eye):Normalize())
  43.  
  44.     voldata.x = vol * 2
  45.  
  46.     voldata.y = math.clamp(-dot, 0, 1) + 0.5
  47.     voldata.z = math.clamp(dot, 0, 1) + 0.5
  48.  
  49.     should_play = vol ~= 0
  50. end
  51.  
  52. local source = Vec3(866.255005, 327.433594, 175.739883)
  53. local function GetVolumeData()
  54.     local ply = entities.GetLocalPlayer()
  55.     local ang = ply:GetViewRotation():GetAng3()
  56.     return calcsource(ply:GetEyePos(), ang:GetRight(), source, 10, ang:GetForward())
  57. end
  58.  
  59. local function waveform()
  60.     local t = (time*8)
  61.     local w = 0 --pwm(30, math.tan(t))
  62.  
  63.     if t%1 > 0.9 and t%1 < 0.95 then
  64.         w = w + (math.random() * math.sin(t))
  65.     end
  66.  
  67.     if t%8 < 0.5 then
  68.         w = w + math.random()
  69.     end
  70.  
  71.     if (t%4 > 2 and t%4 < 2.6) then
  72.         w = w + pwm(12)
  73.     end
  74.  
  75.     if (t%4 > 2.6 and t%4 < 3) then
  76.         w = w + pwm(0)
  77.     end
  78.  
  79.     if should_play then
  80.         w = w * voldata.x
  81.  
  82.         return w * voldata.y, w * voldata.z
  83.     end
  84.  
  85.     return 0,0
  86. end
  87.  
  88. hook.Add("AudioSample", 1, function(pos)
  89.     time = pos
  90.     return waveform()
  91. end)
  92.  
  93. aniaudio.Start(0.1)
  94. hook.Add("PostGameUpdate", "aniaudio", function()
  95.     GetVolumeData()
  96.     aniaudio.Process()
  97. end)
Advertisement
Add Comment
Please, Sign In to add comment