CapsAdmin

Untitled

Oct 14th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.59 KB | None | 0 0
  1. if html and html:IsValid() then html:Remove() end
  2.  
  3. local html = vgui.Create("DHTML") _G.html = html
  4.  
  5. html:AddFunction("gmod", "print", print)
  6. html:AddFunction("gmod", "data", function(data)
  7.     pcall(function()
  8.         local data = CompileString(data, "data")()
  9.         hook.Call("Spectrum", nil, data)
  10.     end)
  11. end)
  12.  
  13. local player = setmetatable(
  14.     {
  15.     },
  16.     {
  17.         __index = function(self, func_name)
  18.             return function(...)
  19.                 local tbl = {...}
  20.                
  21.                 for key, val in pairs(tbl) do
  22.                     tbl[key] = tostring(val)
  23.                    
  24.                     if tbl[key] == "nil" or tbl[key] == "NULL" then
  25.                         tbl[key] = "null"
  26.                     end
  27.                 end
  28.                
  29.                 local str = ("%s(%q)"):format(func_name, table.concat(tbl, ", "))
  30.                 html:QueueJavascript(str)
  31.                 --print(str)
  32.             end
  33.         end
  34.     }
  35. )
  36.  
  37. html:SetPos(ScrW(), ScrH())
  38. html:OpenURL("http://dl.dropbox.com/u/244444/gmod_audio.html")
  39. html:QueueJavascript[[
  40.     var AudioContext = window.AudioContext || window.webkitAudioContext;
  41.  
  42.     if(!AudioContext) {
  43.         throw "CHROME PLZ";
  44.     }
  45.  
  46.     var audio = null
  47.     var analyser = null
  48.  
  49.     function player()
  50.     {
  51.         if (!audio)
  52.         {
  53.             audio = new AudioContext;
  54.            
  55.             analyser = audio.createAnalyser()
  56.             analyser.connect(audio.destination)
  57.            
  58.             setInterval(
  59.                 function()
  60.                 {
  61.                     var spectrum = new Uint8Array(analyser.frequencyBinCount);
  62.                     analyser.getByteFrequencyData(spectrum);
  63.                    
  64.                     var lol = new Array(spectrum.length);
  65.                    
  66.                     for(var i = 0; i < spectrum.length; ++i)
  67.                         lol[i] = spectrum[i] / 255;
  68.                    
  69.                     gmod.data("return {" + lol.join(",") + "}");
  70.                 },
  71.                 0
  72.             );
  73.         }
  74.    
  75.         return audio
  76.     }
  77.    
  78.     function download(url, callback)
  79.     {
  80.         var request = new XMLHttpRequest
  81.        
  82.         request.open("GET", url, true)
  83.         request.responseType = "arraybuffer"
  84.         request.send(null)
  85.        
  86.         request.onload = function()
  87.         {
  88.             gmod.print("loaded \"" + url + "\"")
  89.             callback(request.response)
  90.         }
  91.        
  92.         request.onprogress = function(event)
  93.         {
  94.             gmod.print(Math.round(event.loaded / event.total * 100) + "%")
  95.         }      
  96.     }
  97.    
  98.     function play(url)
  99.     {
  100.         download(url, function(data)
  101.         {
  102.             gmod.print("decoding " + data.byteLength + " ...")
  103.             player().decodeAudioData(data, function(buffer)
  104.             {
  105.                 gmod.print("asdasds")
  106.                 var source = audio.createBufferSource()
  107.                 source.buffer = buffer
  108.                 source.loop = true
  109.                 source.connect(analyser)
  110.                 source.noteOn(0)
  111.                
  112.                 gmod.print("LOADED AND DECODED")
  113.             })
  114.         })
  115.     }
  116.    
  117.     window.onerror = function(desc, file, line)
  118.     {
  119.         gmod.print(desc)
  120.         gmod.print(file)
  121.         gmod.print(line)
  122.     }
  123. ]]
  124.  
  125. player.play("http://dl.dropbox.com/u/244444/beetle.mp3")
  126.  
  127. print("hi")
Advertisement
Add Comment
Please, Sign In to add comment