Advertisement
CapsAdmin

Untitled

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