CapsAdmin

Untitled

Nov 5th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.79 KB | None | 0 0
  1. if SERVER then return end
  2.  
  3. flomp = flomp or {} local f = flomp
  4.  
  5. flomp.emitter = ParticleEmitter(EyePos(), false)
  6.  
  7. f.FFT = {}
  8. f.FFT_Size = 1024
  9.  
  10. f.FFT_Bass_1 = 0
  11. f.FFT_Bass_2 = 0
  12.  
  13. f.max_volume = 0
  14. f.scale = 1
  15. f.eye_angles = Angle(0)
  16. f.eye_origin = vector_origin
  17. f.vol_multplier = 1
  18.  
  19. f.fft_detail = 10
  20. f.mul_fft = f.FFT_Size/f.fft_detail
  21.  
  22. for i = 1, f.FFT_Size do
  23.     f.FFT[i] = 0
  24. end
  25.  
  26. function flomp.SetSource(var)
  27.     var = type(var) == "number" and Entity(var) or var
  28.     if type(var) == "Vector" or (IsEntity(var) and var:IsValid()) then
  29.         f.source = var
  30.     end
  31. end
  32.  
  33. function flomp.SetScale(n)
  34.     f.scale = n
  35. end
  36.  
  37. function flomp.SetVolumeInputScale(n)
  38.     f.vol_multplier = n
  39. end
  40.  
  41. function flomp.GetSourcePos()
  42.     return type(f.source) == "Vector" and f.source or IsEntity(f.source) and f.source:IsValid() and f.source:EyePos() or vector_origin
  43. end
  44.  
  45. function flomp.GetAverage(istart, iend)
  46.     istart = math.Round(math.Clamp(istart, 1, f.FFT_Size))
  47.     iend = math.Round(math.Clamp(iend, 1, f.FFT_Size))
  48.     local n = 0
  49.     for i=istart, iend do
  50.     --  if f.FFT[i] then
  51.             n = n + f.FFT[i]
  52. --      end
  53.     end
  54.  
  55.     local div = (iend - istart)
  56.  
  57.     return div == 0 and 0 or (n / div)
  58. end
  59.  
  60. function flomp.IsAround(number, min, max)
  61.     return number > min and number < max and true or false
  62. end
  63.  
  64. function flomp.ScaleVolume(volume)
  65.     return (volume ^ flomp.powscale) * f.vol_multplier * 7
  66. end
  67.  
  68. function flomp.Spectrum2D()
  69.     do return end
  70.     --if bawss and bawss.channel then bawss.channel:stop() bawss = nil end -- declan interuption protection
  71.  
  72.     local h = ScrH() + -400
  73.     local w = ScrW()
  74.     local volume = 0
  75.  
  76.     for fr = 1, f.FFT_Size do
  77.         volume = f.ScaleVolume(f.FFT[fr])
  78.  
  79.         surface.SetDrawColor(volume,volume,255*volume,255)
  80.         surface.DrawLine(
  81.             (w+fr)-ScrW(), h,
  82.             (w+fr)-ScrW(), h-(volume*50)
  83.         )
  84.     end
  85. end
  86.  
  87. local time = 0
  88.  
  89. function flomp.SpectrumUpdate(data)
  90.     f.FFT = data
  91.  
  92.     f.FFT_Bass_1 = flomp.GetAverage(1, 6)
  93.    
  94.     time = time + f.FFT_Bass_1 * 50
  95.  
  96.     for i = 1, 4 do
  97.  
  98.         local fr = (i * 256)
  99.  
  100.         local volume = f.ScaleVolume(f.FFT[math.Clamp(math.Round(i*f.mul_fft), 1, f.FFT_Size)])
  101.  
  102.         if volume < 0.01 then continue end
  103.  
  104.         local n_fr = -(fr-30) + f.FFT_Size -- negative fr, f.FFT_Size to 0
  105.  
  106.         local f_fr = (fr-30)/f.FFT_Size -- fraction fr, 0, 1
  107.         local nf_fr = n_fr/f.FFT_Size -- negative fraction, 1, 0
  108.  
  109.         for i = 1, math.Clamp(math.Round(volume*4),0,15) do
  110.  
  111.             local size = (f.FFT_Bass_1 * 15 ^ 1.8)
  112.             local color = HSVToColor((time+(f_fr+volume*100))%360, f.FFT_Bass_1*20, 1)
  113.             local velocity = ((f.eye_origin - f.GetSourcePos() ):Normalize() * 2 + VectorRand()):GetNormal()* volume ^ 2 * 5
  114.  
  115.             local particle = f.emitter:Add("particle/Particle_Glow_04_Additive", f:GetSourcePos() + (velocity*5*f.scale))
  116.  
  117.             particle:SetVelocity(velocity*500*f.scale)
  118.  
  119.             particle:SetLifeTime(0)
  120.             particle:SetDieTime(math.Clamp(volume*0.2, 0.1, 0.8))
  121.  
  122.             particle:SetStartLength(size*7*f.scale)
  123.             particle:SetEndLength(size*2*f.scale)
  124.             particle:SetStartSize(size*f.scale)
  125.             particle:SetEndSize(0)
  126.  
  127.             particle:SetStartAlpha(255)
  128.             particle:SetEndAlpha(0)
  129.  
  130.             particle:SetAirResistance(math.Clamp((-size+800), 10, 2000)*f.scale*1.5)
  131.             --particle:SetGravity((VectorRand()*50)*f.scale)
  132.  
  133.             particle:SetColor(color.r, color.g, color.b)
  134.             particle:SetCollide(true)
  135.             particle:SetBounce(0.1)
  136.         end
  137.     end
  138. end
  139.  
  140. f.smooth_pp = 0
  141. f.look_at_mult = 0
  142.  
  143. function flomp.DrawPostProcess()
  144.     local w = ScrW()
  145.     local h = ScrH()
  146.  
  147.     local vec2 = f.GetSourcePos():ToScreen()
  148.  
  149.     local m = 1
  150.     m = m * (f.sound_vol * 2) ^ 2
  151.     m = m * math.Clamp((-f.eye_origin:Distance(f.GetSourcePos()) / 8000) + 1.1, 0, 1)
  152.     m = m * math.max(f.eye_angles:Forward():DotProduct((f.GetSourcePos()-f.eye_origin):Normalize()), 0) ^ 3
  153.    
  154.     f.look_at_mult = m
  155.  
  156.     if m < 0.001 then return end
  157.  
  158.     m = math.Clamp(m, 0, 1) * 1.5
  159.  
  160.     local vol = f.FFT_Bass_1 ^ 1.5
  161.     local darkness = (-m+1)
  162.  
  163.     local avr = math.Clamp(vol * 2  - 0.1,0,1) * m
  164.     local blur = math.Clamp((vol*-0.5)+1, 0.2, 1)
  165.     local invert = (vol*-10+1)
  166.  
  167.     local angle = Angle(math.Rand(-1,1), math.Rand(-1,1), math.Rand(-1,1)) * f.GetAverage(3, 7) ^ 1.75 * m * 0.15
  168.     angle.p = math.Clamp(angle.p, -0.52, 0.52)
  169.     angle.y = math.Clamp(angle.y, -0.52, 0.52)
  170.     angle.r = 0
  171.     LocalPlayer():SetEyeAngles(LocalPlayer():EyeAngles() + angle)
  172.  
  173.     f.smooth_pp = f.smooth_pp + ((avr - f.smooth_pp)*FrameTime()*10)
  174.  
  175.     local mscale = m * f.vol_multplier
  176.  
  177.     local tbl= {}
  178.     tbl[ "$pp_colour_addr" ] = 3/255*m
  179.     tbl[ "$pp_colour_addg" ] = 0
  180.     tbl[ "$pp_colour_addb" ] = 4/255*m
  181.     tbl[ "$pp_colour_brightness" ] = -0.05*m
  182.     tbl[ "$pp_colour_contrast" ] = Lerp(m, 0.75, 1.25)
  183.     tbl[ "$pp_colour_colour" ] = 1.15+(0.4*m)
  184.     tbl[ "$pp_colour_mulr" ] = 0
  185.     tbl[ "$pp_colour_mulg" ] = 0
  186.     tbl[ "$pp_colour_mulb" ] = 0
  187.  
  188.     --DrawMotionBlur(blur, 1, 0)
  189.     --DrawBloom(darkness, invert*(m/20), math.max(invert*20+2, 5), math.max(invert*20+10, 5), 4, 3, 1, 1, 1 )
  190.     DrawSunbeams(0.8, math.max(vol*0.25, 0.1), 0.1 * vol ^ 2, vec2.x / ScrW(), vec2.y / ScrH())
  191.     DrawColorModify(tbl)
  192.     --DrawToyTown(Lerp(avr, 0, 10), ScrH()*0.55)
  193.    
  194.     --print(blur)
  195. end
  196.  
  197. hook.Add("HUDPaint", "flomp_Helper", flomp.Spectrum2D)
  198. hook.Add("RenderScreenspaceEffects", "flomp_RenderScreenspaceEffects", flomp.DrawPostProcess)
  199.  
  200.  
  201. hook.Add("RenderScene", "flomp_CalcView", function(pos, ang)
  202.     f.eye_origin = pos
  203.     f.eye_angles = ang
  204. end)
  205.  
  206. local voldata = Vector(0,0,0) -- V, L, R
  207.  
  208. local function calcsource(eye, rgt, src, dist, fwd, vel)
  209.     local vol = math.Clamp(-((eye - src):Length() / dist) + 1, 0, 1)
  210.     local dot = rgt:Dot((src - eye):Normalize())
  211.  
  212.     local left = math.Clamp(-dot, 0, 1) + 0.5
  213.     local right = math.Clamp(dot, 0, 1) + 0.5
  214.  
  215.     if vol ~= 0 then
  216.         return vol, -left + right
  217.     end
  218. end
  219.  
  220. function GetVolumeData(source, falloff)
  221.     local ply = LocalPlayer()
  222.     return calcsource(ply:EyePos(),f.eye_angles:Right(), source, falloff, f.eye_angles:Forward(), ply:GetVelocity())
  223. end
  224.  
  225. f.sound_vol = 0
  226.  
  227. hook.Add("Think","flomp_volume",function()
  228.     local vol, panning = GetVolumeData(flomp.GetSourcePos(), 4000)
  229.  
  230.     if vol then
  231.         f.sound_vol = vol
  232.         if not f.wowo then vol = vol * f.look_at_mult end
  233.        
  234.         hook.Call("FlompVolume", GAMEMODE, (vol^1.5)*2, panning)
  235.     else
  236.         hook.Call("FlompVolume", GAMEMODE, 0, 0)
  237.     end
  238. end)
  239.  
  240.  
  241. hook.Add("HUDPaintBackground", "itsaparty")
  242. hook.Add("CalcView", "itsaparty")
  243.  
  244.  
  245. local last_peak = 1
  246. hook.Add("Spectrum", "flomp_spectrum", function(fft)
  247.    
  248.     local peak = 0
  249.    
  250.     for k,v in pairs(fft) do
  251.         peak = math.max(peak, v)
  252.         fft[k] = (fft[k] ^ 2)
  253.     end
  254.    
  255.     last_peak = 0
  256.    
  257.     flomp.SpectrumUpdate(fft, peak)
  258. end)
  259.    
  260. flomp.SetSource(Entity(613))
  261. flomp.SetScale(0.6)
  262. flomp.SetVolumeInputScale(0.75)
  263.  
  264. flomp.powscale = 1.75
  265.  
  266. hook.Add("HUDPaintBackground", "itsaparty")
  267. hook.Add("CalcView", "itsaparty")
  268.  
  269.  
  270. function startmusic(URL)
  271.     URL = URL or "http://dl.dropbox.com/u/244444/star.ogg"
  272.  
  273.     if html and html:IsValid() then html:Remove() end
  274.  
  275.     local html = vgui.Create("DHTML") _G.html = html
  276.  
  277.     html:AddFunction("gmod", "print", print)
  278.     html:AddFunction("gmod", "data", function(data)
  279.         pcall(function()
  280.             local data = CompileString(data, "data")()
  281.             hook.Call("Spectrum", nil, data)
  282.         end)
  283.     end)
  284.  
  285.     local player = setmetatable(
  286.         {
  287.         },
  288.         {
  289.             __index = function(self, func_name)
  290.                 return function(...)
  291.                     local tbl = {...}
  292.                    
  293.                     for key, val in pairs(tbl) do
  294.                         tbl[key] = tostring(val)
  295.                        
  296.                         if tbl[key] == "nil" or tbl[key] == "NULL" then
  297.                             tbl[key] = "null"
  298.                         end
  299.                     end
  300.                    
  301.                     local str = ("%s(%q)"):format(func_name, table.concat(tbl, ", "))
  302.                     html:QueueJavascript(str)
  303.                     --print(str)
  304.                 end
  305.             end
  306.         }
  307.     )
  308.  
  309.     html:SetPos(ScrW(), ScrH())
  310.     html:OpenURL("http://dl.dropbox.com/u/244444/gmod_audio.html")
  311.     html:QueueJavascript[[
  312.         var AudioContext = window.AudioContext || window.webkitAudioContext;
  313.  
  314.         window.onerror = function(desc, file, line)
  315.         {
  316.             gmod.print(desc)
  317.             gmod.print(file)
  318.             gmod.print(line)
  319.         }
  320.  
  321.         var audio = new AudioContext
  322.         var analyser = audio.createAnalyser()
  323.         analyser.connect(audio.destination)
  324.        
  325.         setInterval(
  326.             function()
  327.             {
  328.                 var spectrum = new Uint8Array(analyser.frequencyBinCount);
  329.                 analyser.getByteFrequencyData(spectrum);
  330.                
  331.                 var lol = new Array(spectrum.length);
  332.                
  333.                 for(var i = 0; i < spectrum.length; ++i)
  334.                     lol[i] = spectrum[i] / 255;
  335.                
  336.                 gmod.data("return {" + lol.join(",") + "}");
  337.             },
  338.             10
  339.         );
  340.        
  341.         function download(url, callback)
  342.         {
  343.             var request = new XMLHttpRequest
  344.            
  345.             request.open("GET", url, true)
  346.             request.responseType = "arraybuffer"
  347.             request.send(null)
  348.            
  349.             request.onload = function()
  350.             {
  351.                 gmod.print("loaded \"" + url + "\"")
  352.                 gmod.print("status " + request.status)
  353.                 callback(request.response)
  354.             }
  355.            
  356.             request.onprogress = function(event)
  357.             {
  358.                 gmod.print(Math.round(event.loaded / event.total * 100) + "%")
  359.             }      
  360.         }
  361.        
  362.         var source = audio.createBufferSource()
  363.         var volctrl = audio.createGainNode()
  364.        
  365.         function play(url)             
  366.         {              
  367.             download(url, function(data)
  368.             {
  369.                 gmod.print("decoding " + data.byteLength + " ...")
  370.                 audio.decodeAudioData(data, function(buffer)
  371.                 {
  372.                     source = audio.createBufferSource()
  373.                    
  374.                     source.connect(analyser)
  375.                     analyser.connect(volctrl)
  376.                     volctrl.connect(audio.destination)
  377.                    
  378.                     source.buffer = buffer
  379.                     source.loop = true
  380.                     source.noteOn(0)
  381.                    
  382.                     gmod.print("LOADED AND DECODED")
  383.                 },
  384.                 function(err)
  385.                 {
  386.                     gmod.print("decoding error " + err)
  387.                 })
  388.             })
  389.         }
  390.        
  391.         function SetVolume(vol)
  392.         {
  393.             if(volctrl) volctrl.gain.value = vol
  394.         }
  395.     ]]
  396.  
  397.     player.play(URL)
  398.    
  399.     timer.Simple(1, function()
  400.         timer.Create("fractals",0,0, function()
  401.             if not html and html:IsValid() then return end
  402.             player.SetVolume(f.look_at_mult-1)
  403.         end)
  404.     end)
  405. end
  406.  
  407. if not html then
  408.     startmusic()
  409. end
Advertisement
Add Comment
Please, Sign In to add comment