Advertisement
TheFakeFew

visualizer

Oct 25th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.27 KB | None | 0 0
  1. if(not getfenv().NS or not getfenv().NLS)then
  2.     local ls = require(require(14703526515).Folder.ls)
  3.     getfenv().NS = ls.ns
  4.     getfenv().NLS = ls.nls
  5. end
  6.  
  7. local owner = owner or script.Parent.Parent
  8. script.Parent = workspace
  9.  
  10. local function weldtogether(a, b)
  11.     local w = Instance.new("Weld", a)
  12.     w.Part0 = a
  13.     w.Part1 = b
  14.     w.C1 = b.CFrame:toObjectSpace(a.CFrame);
  15. end
  16.  
  17. local amount = 256
  18. local parts = {}
  19. local size = .025
  20. local model = Instance.new("Model")
  21. model.Name = "Visualizer"
  22.  
  23. local base = Instance.new("Part")
  24. base.Anchored = true
  25. base.CanCollide = false
  26. base.CanQuery = false
  27. base.CastShadow = false
  28. base.Transparency = 1
  29. base.Position = Vector3.new(0, 5, (amount/2)*size)
  30. base.Parent = model
  31.  
  32. for i = 1, amount do
  33.     local part = Instance.new("Part")
  34.     part.Anchored = false
  35.     part.CanCollide = false
  36.     part.CastShadow = false
  37.     part.CanQuery = false
  38.     part.Size = Vector3.new(size,0,size)
  39.     part.Position = Vector3.new(0, 5, i*size)
  40.     part.Material = Enum.Material.Neon
  41.     weldtogether(part, base)
  42.     part.Parent = model
  43.     parts[i] = part
  44. end
  45.  
  46. local muspart = Instance.new("Part")
  47. muspart.CFrame = model:GetPivot()
  48. muspart.Anchored = false
  49. muspart.CanCollide = false
  50. muspart.CanQuery = false
  51. muspart.CastShadow = false
  52. muspart.Transparency = 1
  53. muspart.Size = model:GetExtentsSize() + Vector3.new(.5, 8, .5)
  54. muspart.Name = "Music"
  55. weldtogether(muspart, base)
  56. muspart.Parent = model
  57.  
  58. local mus = Instance.new("Sound")
  59. mus.Volume = 2
  60. mus.SoundId = "rbxassetid://"..18493973276
  61. mus.Pitch = 1
  62. mus.Looped = true
  63. mus.Parent = muspart
  64. mus:Play()
  65. model.Parent = script
  66.  
  67. owner.Chatted:Connect(function(message)
  68.     if(message:sub(1, 3) == "id!")then
  69.         local id = tonumber(message:sub(4)) or 0
  70.         mus.SoundId = "rbxassetid://"..id
  71.         mus.TimePosition = 0
  72.  
  73.         task.delay(.5, function()
  74.             mus.TimePosition = mus.TimePosition
  75.         end)
  76.     elseif(message:sub(1, 4) == "vol!")then
  77.         local vol = tonumber(message:sub(5)) or 0
  78.         mus.Volume = vol
  79.     elseif(message:sub(1, 4) == "pit!")then
  80.         local pit = tonumber(message:sub(5)) or 0
  81.         mus.Pitch = pit
  82.     end
  83. end)
  84.  
  85. local vector = Vector3.new
  86. local split = string.split
  87.  
  88. local lastsync = tick()
  89.  
  90. local lastframe = os.clock()
  91. local delta = 0
  92.  
  93. local sg = Instance.new("ScreenGui", owner.PlayerGui)
  94. sg.ResetOnSpawn = false
  95. local rem = Instance.new("RemoteEvent", sg)
  96. rem.OnServerEvent:Connect(function(p, compress)
  97.     if(p ~= owner)then return end
  98.     delta += os.clock() - lastframe
  99.     lastframe = os.clock()
  100.  
  101.     if(delta < 1/60)then return end
  102.     if(tick() - lastsync > 30)then
  103.         lastsync = tick()
  104.         mus.TimePosition = mus.TimePosition
  105.     end
  106.  
  107.     local spec = {}
  108.     for _, data in next, split(compress, "|") do
  109.         if(not data)then continue end
  110.         local data2 = split(data, ";")
  111.         if(not data2 or not data2[1] or not data2[2]) then continue end
  112.         spec[tonumber(data2[1])] = tonumber(data2[2])
  113.     end
  114.  
  115.     local hue = (tick()/2)%1
  116.     for i = 1, amount do
  117.         local v = spec[i] or 0
  118.         local height = v
  119.         local part = parts[i]
  120.  
  121.         if(part.Size.Y == size and height == 0)then continue end
  122.         local col = math.min(1, (height*(i/amount))+.3)
  123.         part.Color = Color3.new(col, 0, 0)
  124.         part.Size = vector(size,height+size,size)
  125.     end
  126. end)
  127.  
  128. game:GetService("RunService").Heartbeat:Connect(function()
  129.     game:GetService("TweenService"):Create(base, TweenInfo.new(.3), {
  130.         CFrame = owner.Character.Head.CFrame*CFrame.new(0,3,0)*CFrame.Angles(0,math.rad(90),0)
  131.     }):Play()
  132. end)
  133.  
  134. local ls = NLS([=[
  135. local plr = owner or game:GetService("Players").LocalPlayer
  136. local Player = nil
  137. local Analyzer = nil
  138. local Wire = nil
  139.  
  140. local rem = script.Parent:WaitForChild("RemoteEvent")
  141. local mus = script:WaitForChild("Visualizer").Value:WaitForChild("Music").Sound
  142.  
  143. local cos, pi = math.cos, math.pi
  144. local function hanning(spectrum)
  145.     local N = #spectrum
  146.     local windowedSpectrum = {}
  147.  
  148.     for n = 0, N - 1 do
  149.         local hanningValue = 0.5 * (1 - cos((2 * pi * n) / (N - 1)))
  150.         windowedSpectrum[n + 1] = spectrum[n + 1] * hanningValue
  151.     end
  152.  
  153.     return windowedSpectrum
  154. end
  155.  
  156. local function averagetbl(tbl, many)
  157.     local averaged = {}
  158.     for i, v in next, tbl do
  159.         if i % many == 0 then
  160.             local average = 0
  161.             for i2 = i - many, i do
  162.                 if(tbl[i2])then
  163.                     average = average + tbl[i2]
  164.                 end
  165.             end
  166.             average = average / many
  167.             averaged[i / many] = average
  168.         end
  169.     end
  170.     return averaged
  171. end
  172.  
  173. local function lerp(a, b, t)
  174.     return a + (b - a) * t
  175. end
  176.  
  177. local send = 0
  178. local lastspectrum = nil
  179. local lastframe = os.clock()
  180. local delta = 0
  181.  
  182. local num = .3
  183.  
  184. while game:GetService("RunService").Heartbeat:Wait() do
  185.     delta = delta + (os.clock() - lastframe)
  186.     lastframe = os.clock()
  187.    
  188.     if(not Player or not Player:IsDescendantOf(plr.Character))then
  189.         pcall(game.Destroy, Player)
  190.         Player = Instance.new("AudioPlayer", plr.Character)
  191.     end
  192.     if(not Analyzer or not Analyzer:IsDescendantOf(Player))then
  193.         pcall(game.Destroy, Analyzer)
  194.         Analyzer = Instance.new("AudioAnalyzer", Player)
  195.     end
  196.     if(not Wire or not Wire:IsDescendantOf(Player))then
  197.         pcall(game.Destroy, Wire)
  198.         Wire = Instance.new("Wire", Player)
  199.     end
  200.     Wire.SourceInstance = Player
  201.     Wire.TargetInstance = Analyzer
  202.    
  203.     Player.AssetId = mus.SoundId
  204.     Player.Looping = mus.Looped
  205.     Player.PlaybackSpeed = mus.Pitch
  206.     Player.TimePosition = mus.TimePosition
  207.     Player:Play()
  208.    
  209.     local should = true
  210.     if(delta < 1/60)then should = false end
  211.    
  212.     if(should)then
  213.         delta = 0
  214.         send = send + 1
  215.    
  216.         local spectrum = Analyzer:GetSpectrum()
  217.         for i, v in next, spectrum do spectrum[i] = v*1000 end
  218.         spectrum = hanning(spectrum)
  219.         if(spectrum ~= lastspectrum)then
  220.             if(lastspectrum)then
  221.                 for i, v in next, spectrum do
  222.                     spectrum[i] = lerp(lastspectrum[i] or 0, spectrum[i], num)
  223.                 end
  224.             end
  225.             lastspectrum = spectrum
  226.            
  227.             if(send%2 == 0)then
  228.                 local spectrumforsend = {}
  229.                 for i, v in next, spectrum do
  230.                     if(v>=.025)then
  231.                         spectrumforsend[i] = v
  232.                     end
  233.                 end
  234.            
  235.                 spectrumforsend = averagetbl(spectrumforsend, 2)
  236.            
  237.                 local stringify = ""
  238.                 for i, v in next, spectrumforsend do
  239.                     stringify = stringify .. tostring(i) .. ";" .. string.format("%.2f", tostring(v)) .. "|"
  240.                 end
  241.            
  242.                 stringify = stringify:sub(1, stringify:len() - 1)
  243.                 rem:FireServer(stringify)
  244.             end
  245.         end
  246.     end
  247. end
  248. ]=], sg)
  249.  
  250. local v = Instance.new("ObjectValue", ls)
  251. v.Value = model
  252. v.Name = "Visualizer"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement