CapsAdmin

Untitled

Jun 23rd, 2011
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.87 KB | None | 0 0
  1. if SERVER then return end
  2.  
  3. if not BASS then
  4.     require("bass")
  5.     if not BASS then return end -- uhh
  6. end
  7.  
  8. flomp = flomp or {} local f = flomp
  9.  
  10. flomp.emitter = ParticleEmitter(EyePos(), false)
  11.  
  12. f.error_enums = {
  13.     [1] = "mem",
  14.     [2] = "fileopen",
  15.     [3] = "driver",
  16.     [4] = "buflost",
  17.     [5] = "handle",
  18.     [6] = "format",
  19.     [7] = "position",
  20.     [8] = "init",
  21.     [9] = "start",
  22.     [14] = "already",
  23.     [18] = "nochan",
  24.     [19] = "illtype",
  25.     [20] = "illparam",
  26.     [21] = "no3d",
  27.     [22] = "noeax",
  28.     [23] = "device",
  29.     [24] = "noplay",
  30.     [25] = "freq",
  31.     [27] = "notfile",
  32.     [29] = "nohw",
  33.     [31] = "empty",
  34.     [32] = "nonet",
  35.     [33] = "create",
  36.     [34] = "nofx",
  37.     [37] = "notavail",
  38.     [38] = "decode",
  39.     [39] = "dx",
  40.     [40] = "timeout",
  41.     [41] = "fileform",
  42.     [42] = "speaker",
  43.     [43] = "version",
  44.     [44] = "codec",
  45.     [45] = "ended",
  46.     [-1] = "unknown",
  47. }
  48.  
  49. f.FFT = {}
  50. f.FFT_Bass_1 = 0
  51. f.FFT_Bass_2 = 0
  52.  
  53. f.max_volume = 0
  54. f.scale = 1
  55. f.eye_angles = Angle(0)
  56. f.eye_origin = vector_origin
  57. f.vol_multplier = 1
  58.  
  59. f.fft_detail = 6
  60. f.mul_fft = 1024/f.fft_detail
  61.  
  62. for i = 1, 1024 do
  63.     f.FFT[i] = 0
  64. end
  65.  
  66. function flomp.SetSource(var)
  67.     var = type(var) == "number" and Entity(var) or var
  68.     if type(var) == "Vector" or (IsEntity(var) and var:IsValid()) then
  69.         f.source = var
  70.     end
  71. end
  72.  
  73. function flomp.SetScale(n)
  74.     f.scale = n
  75. end
  76.  
  77. function flomp.SetVolumeInputScale(n)
  78.     f.vol_multplier = n
  79. end
  80.  
  81. function flomp.SetPow(n)
  82.     f.pow = n
  83. end
  84.  
  85. function flomp.GetPow()
  86.     return f.pow or 1.3
  87. end
  88.  
  89. function flomp.GetSourcePos()
  90.     return type(f.source) == "Vector" and f.source or IsEntity(f.source) and f.source:IsValid() and f.source:GetPos() or vector_origin
  91. end
  92.  
  93. function flomp.GetChannel()
  94.     return f.channel
  95. end
  96.  
  97. function flomp.PlaySound(path, restart, force_web)
  98.  
  99.     if restart == false and not f.IsNotReady() then return end 
  100.    
  101.     --if f.channel then f.channel:stop() end
  102.     f.channel = nil
  103.    
  104.    
  105.     if force_web or path:find("://", nil, true) then
  106.         BASS.StreamFileURL(path, -1, function(channel, err)
  107.             if err ~= 0 then
  108.                 error("[BASS] Flomp Stream Error: " .. f.error_enums[err])
  109.             end
  110.            
  111.             f.channel = f.channel or channel
  112.             f.channel:play()
  113.         end)
  114.     else
  115.         f.channel = f.channel or BASS.StreamFile(path)
  116.         if not f.channel then
  117.             error("[BASS Flomp could not open the file garrysmod/sound/" .. path)
  118.         return end
  119.         f.channel:play()
  120.     end
  121. end
  122.  
  123. function flomp.Stop()
  124.     if f.channel then
  125.         f.channel:stop()
  126.         f.channel = nil
  127.     end
  128. end
  129.  
  130. function flomp.GetAverage(istart, iend)
  131.     istart = math.Round(math.Clamp(istart, 1, 1024))
  132.     iend = math.Round(math.Clamp(iend, 1, 1024))
  133.     local n = 0
  134.     for i=istart, iend do
  135.         n = n + f.FFT[i]
  136.     end
  137.    
  138.     local div = (iend - istart)
  139.    
  140.     return div == 0 and 0 or (n / div)
  141. end
  142.  
  143. function flomp.IsAround(number, min, max)
  144.     return number > min and number < max and true or false
  145. end
  146.  
  147. function flomp.IsNotReady()
  148.     return
  149.         f.channel and not f.channel:getplaying() or
  150.         not f.channel or
  151.         not f.source or
  152.         IsEntity(f.source) and not f.source:IsValid()
  153. end
  154.  
  155. function flomp.ScaleVolume(volume)
  156.     return ((volume ^ f.GetPow()) * 100) * f.vol_multplier
  157. end
  158.  
  159. function flomp.Spectrum2D()
  160.    
  161.     if bawss and bawss.channel then bawss.channel:stop() bawss = nil end -- declan interuption protection
  162.     do return end
  163.    
  164.  
  165.     if f.IsNotReady() then return end
  166.    
  167.     local h = ScrH() + -40
  168.     local w = ScrW()
  169.     local volume = 0
  170.    
  171.     for fr = 1, 1024 do
  172.         volume = f.ScaleVolume(f.FFT[fr])
  173.        
  174.         surface.SetDrawColor(volume,volume,255*volume,255)
  175.         surface.DrawLine(
  176.             (w+fr)-780, h,
  177.             (w+fr)-780, h-(volume*50)
  178.         )
  179.     end
  180. end
  181.  
  182. function flomp.ParticleThink()
  183.     if f.IsNotReady() then return end
  184.    
  185.     if type(f.channel) == "Entity" and not f.channel:IsValid() then f.channel:stop() return end
  186.    
  187.  
  188.     f.FFT = f.channel:fft2048()
  189.    
  190.     --local overall_volume = flomp.GetAverage(1, 1024)
  191.        
  192.     --if overall_volume > f.max_volume then
  193.         --f.max_volume = overall_volume
  194.         --print(overall_volume)
  195.     --end
  196.    
  197.     f.FFT_Bass_1 = flomp.GetAverage(1, 15)
  198.                
  199.     for i = 1, 4 do
  200.        
  201.         local fr = (i * 256)
  202.        
  203.         --print(fr)
  204.        
  205.         local volume = f.ScaleVolume(f.FFT[math.Clamp(math.Round(i*f.mul_fft), 1, 1024)])
  206.                
  207.         if volume < 0.01 then continue end
  208.        
  209.         local n_fr = -(fr-30) + 1024 -- negative fr, 1024 to 0
  210.        
  211.         local f_fr = (fr-30)/1024 -- fraction fr, 0, 1
  212.         local nf_fr = n_fr/1024 -- negative fraction, 1, 0
  213.                
  214.         for i = 1, math.Clamp(math.Round(volume*8*f.scale),0,8) do
  215.        
  216.             local size = (f.FFT_Bass_1 * 30 ^ 2)
  217.             local color = HSVToColor(((CurTime()*10)+(f_fr*100))%360, 0.3, 1)
  218.             local velocity = ((f.eye_origin - f.GetSourcePos() ):Normalize() * 2 + VectorRand()):GetNormal()* volume
  219.            
  220.             local particle = f.emitter:Add("particle/Particle_Glow_04_Additive", f:GetSourcePos() + (velocity*80*f.scale))
  221.            
  222.             particle:SetVelocity(velocity*1600*f.scale)
  223.            
  224.             particle:SetLifeTime(0)
  225.             particle:SetDieTime(math.Clamp(volume^2, 0.1, 1))
  226.                                    
  227.             particle:SetStartLength(size*4*f.scale)
  228.             particle:SetStartSize(size*f.scale)
  229.             particle:SetEndSize(0)
  230.            
  231.             particle:SetStartAlpha(255)
  232.             particle:SetEndAlpha(0)
  233.                        
  234.             particle:SetAirResistance(math.Clamp((-size+800), 10, 1200)*f.scale)
  235.             --particle:SetGravity((VectorRand()*100)*f.scale)
  236.            
  237.             particle:SetColor(color.r, color.g, color.b)
  238.             particle:SetCollide(true)
  239.             particle:SetBounce(0.1)
  240.         end
  241.     end
  242. end
  243.  
  244. f.smooth_pp = 0
  245.  
  246. function flomp.DrawPostProcess()
  247.     if f.IsNotReady() then return end
  248.    
  249.     local w = ScrW()
  250.     local h = ScrH()
  251.    
  252.     local vec2 = f.GetSourcePos():ToScreen()
  253.            
  254.     local m = math.max(f.eye_angles:Forward():DotProduct((f.GetSourcePos()-f.eye_origin):Normalize()), 0) ^ 3
  255.     m = m * math.Clamp((-f.eye_origin:Distance(f.GetSourcePos()) / 6000) + 1, 0, 1)
  256.    
  257.     f.channel:setvolume(math.Clamp(m*10, 0, 1))
  258.    
  259.     if m < 0.001 then return end
  260.    
  261.     m = math.Clamp(m, 0, 1)
  262.    
  263.     local vol = f.FFT_Bass_1 * f.vol_multplier
  264.    
  265.     local avr = math.Clamp(vol * 2  - 0.1,0,1)
  266.     local blur = math.Clamp((vol*-10)+1, 0.2, 1)
  267.     local invert = (vol*-10+1)
  268.     local darkness = (-m+1)
  269.    
  270.     local angle = VectorRand() * f.GetAverage(1, 5) ^ 2.2 * 60 * m
  271.     angle.x = math.Clamp(angle.x, -0.2, 0.2)
  272.     angle.y = math.Clamp(angle.y, -0.2, 0.2)
  273.     angle.z = 0
  274.     LocalPlayer():SetEyeAngles(LocalPlayer():EyeAngles() + angle)
  275.  
  276.     f.smooth_pp = f.smooth_pp + ((avr - f.smooth_pp)*FrameTime()*10)       
  277.    
  278.     --local mscale = m * f.vol_multplier
  279.    
  280.     local tbl= {}
  281.     tbl[ "$pp_colour_addr" ] = 3/255*m
  282.     tbl[ "$pp_colour_addg" ] = 0
  283.     tbl[ "$pp_colour_addb" ] = 4/255*m
  284.     tbl[ "$pp_colour_brightness" ] = Lerp(m, 0, -0.11)
  285.     tbl[ "$pp_colour_contrast" ] = Lerp(m, 2, 1.4)
  286.     tbl[ "$pp_colour_colour" ] = 1.6
  287.     tbl[ "$pp_colour_mulr" ] = 0
  288.     tbl[ "$pp_colour_mulg" ] = 0
  289.     tbl[ "$pp_colour_mulb" ] = 0
  290.        
  291.  
  292.     DrawMotionBlur(blur, 0.5, 0)
  293.     DrawSunbeams(darkness,math.max(vol*0.2, 0.1), 0.005 * (vol * 50), vec2.x / ScrW(), vec2.y / ScrH())
  294.     DrawBloom(darkness, invert*(m/10), math.max(invert*40+2, 5), math.max(invert*40+2, 5), 4, 3, 1, 1, 1 )
  295.     DrawColorModify(tbl)
  296.     --print(blur)
  297.  
  298. end
  299.  
  300. function flomp.Calc3DSound(ply, origin, angles, fov)
  301.     f.eye_origin = origin
  302.     f.eye_angles = angles
  303.     if f.IsNotReady() then return end
  304.     if f.channel then
  305.         if IsEntity(f.source) then
  306.             f.channel:set3dposition(origin, angles:Forward()*-1, vector_origin or (f.source:GetVelocity()/100))
  307.         else
  308.             f.channel:set3dposition(origin, angles:Forward()*-1, vector_origin)
  309.         end
  310.     end
  311.     BASS.SetPosition(origin, vector_origin or (ply:GetVelocity()/100), angles:Forward()*-1, angles:Up())
  312. end
  313.  
  314. --hook.Add("HUDPaint", "flomp_Helper", flomp.Spectrum2D)
  315. hook.Add("Think", "flomp_Think", flomp.ParticleThink)
  316. hook.Add("RenderScreenspaceEffects", "flomp_RenderScreenspaceEffects", flomp.DrawPostProcess)
  317. hook.Add("CalcView", "flomp_CalcView", flomp.Calc3DSound)
  318.  
  319. --flomp.SetSource(Vector(9663.96875,-6569.4130859375,-9824.2373046875))
  320. --flomp.PlaySound("http://listen.trancebase.fm/tunein-dsl-asx", false)
  321. flomp.SetVolumeInputScale(20)
  322. flomp.SetPow(1.4)
Advertisement
Add Comment
Please, Sign In to add comment