-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.txt
351 lines (272 loc) · 11.1 KB
/
main.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
--@name Jukebox V0.2
--@author Kanderman
--@shared
--@include lib/music/fft_decoder.txt
--@include lib/wire/writefuncs.txt
if SERVER then
require("lib/wire/writefuncs.txt")
wire.adjustInputs({"QueueScreen"}, {"entity"})
chip():isWeldedTo():linkComponent(chip())
chip():setColor(Color(0,0,0,0))
local songQueue = {}
local songLen = 0
local songStartT = 0
local ltqsu = 0
local playing = false
local function chathandler(ply, msg)
local msgData = string.split(msg, " ")
if msgData[1] == "/vol" then
local aha = tonumber(msgData[2])
if aha == nil then
aha = 0
end
net.start("setting_set")
net.writeString("vol")
net.writeFloat(aha*0.01)
net.send(ply)
elseif msgData[1] == "/quota" then
local aha = tonumber(msgData[2])
if aha == nil then
aha = 40
end
net.start("setting_set")
net.writeString("quota")
net.writeFloat(math.clamp(aha*0.01, 0, 0.8))
net.send(ply)
elseif msgData[1] == "/play" then
local songd = {}
local breh = msgData[2]
local s, _, _ = string.find(breh, "?", 1)
if s ~= nil then
breh = string.sub(breh, 1, s - 1)
end
local thing = string.split(breh, "/")
songd.name = string.stripExtension(thing[#thing])
songd.req = ply:getName()
songd.url = breh
songQueue[#songQueue + 1] = songd
elseif msgData[1] == "/skip" then
if playing then
table.remove(songQueue, 1)
playing = false
end
if songQueue[1] ~= nil then
net.start("song_cmd")
net.writeString("play")
net.writeTable(songQueue[1])
net.send()
net.start("update")
net.writeString("queue")
net.writeTable(songQueue)
net.send()
playing = true
songLen = 10
songStartT = timer.curtime()
end
end
end
local function update()
if timer.curtime() > songStartT + songLen then
if playing then
table.remove(songQueue, 1)
playing = false
end
if songQueue[1] ~= nil then
net.start("song_cmd")
net.writeString("play")
net.writeTable(songQueue[1])
net.send()
net.start("update")
net.writeString("queue")
net.writeTable(songQueue)
net.send()
playing = true
songLen = 10
songStartT = timer.curtime()
end
end
if timer.curtime() > ltqsu + 2 and wire.ports["QueueScreen"]:isValid() then
ltqsu = timer.curtime()
local queueScreen = wire.getWirelink(wire.ports["QueueScreen"])
for x = 1, 18 do
local song = songQueue[x]
if song == nil then
song = {}
song.name = ""
end
if x == 1 then
for i = 0, 29 do
local c = 333
if i <= ((timer.curtime() - songStartT)/songLen)*30 then c = 059 end
writeString(queueScreen, string.format("%d: %s ", x, song.name)[i+1], i, x - 1, 999, c, 0)
end
else
writeString(queueScreen, string.format("%d: %s ", x, song.name), 0, x - 1, 999, 000, 0)
end
end
end
end
if wire.ports["QueueScreen"]:isValid() then
local queueScreen = wire.getWirelink(wire.ports["QueueScreen"])
queueScreen[2041] = 1
queueScreen[2041] = 0
end
hook.add("think", "update", update)
hook.add("PlayerSay", "chatcmds", chathandler)
net.receive("songLength", function()
songLen = net.readFloat()
songStartT = timer.curtime()
end)
else
require("lib/music/fft_decoder.txt")
local settings = {}
settings.volume = 0.25
settings.quota = 0.4
local queue = {}
local songData = {}
songData.name = "none"
songData.req = "unknown"
songData.url = ""
local song = nil
local steps = 50
local mul = 50
local pcm = {}
local fft = {}
local loudness = 1
local function customSoundFade( SoundObject, Minimum, Maximum, Distance, MaxVolume )
SoundObject:setFade( Maximum, Maximum )
local Max = Maximum - math.clamp( Minimum, 0, Maximum - 1 )
local SetVolume = 1 - ( math.clamp( Distance - Minimum, 0, Max ) / Max )
local Volume = math.clamp( SetVolume * MaxVolume, 0, MaxVolume )
SoundObject:setVolume( Volume )
SoundObject:setPos(chip():getPos())
end
local function draw3DCircle(pos, radius, resol)
local be = 1/resol
for t = 1, resol do
render.draw3DLine(pos + Vector(0,radius,0):rotateAroundAxis(Vector(0,0,1), 360*(t*be)), pos + Vector(0,radius,0):rotateAroundAxis(Vector(0,0,1), 360*((t+1)*be)))
end
end
local function drawfunc()
--sound data
if song ~= nil then
if song:isValid() then
fft = song:getFFT(0)
pcm = fftToPcm(fft, 44100, 0.5, steps, mul)
local l, r = song:getLevels()
--loudness = math.lerp(0.25, loudness, (l+r)*0.33)
else
for x = 1, steps do
pcm[x] = 0
end
end
else
for x = 1, steps do
pcm[x] = 0
end
end
--background
render.setColor(Color(40,40,40)*0.8)
render.draw3DQuad(Vector(0,0,0), Vector(50,0,200), Vector(50,512,200), Vector(0,512,0))
render.draw3DQuad(Vector(468,512,200), Vector(468,0,200), Vector(512,0,0), Vector(512,512,0))
render.setColor(Color(40,40,40)*0.9)
render.draw3DQuad(Vector(50,0,200), Vector(468,0,200), Vector(468,512,200), Vector(50,512,200))
render.setColor(Color(40,40,40)*0.7)
render.draw3DQuad(Vector(0,0,0), Vector(512,0,0), Vector(468,0,200), Vector(50,0,200))
render.setColor(Color(40,40,40))
render.draw3DQuad(Vector(0,512,0), Vector(50,512,200), Vector(468,512,200), Vector(512,512,0))
--visualiser
local ld = 0
for k = 1,#fft do
local v = fft[k]
render.setColor(Color(190,1-v*2,1):hsvToRGB())
render.draw3DBox(Vector(k*4 - 2,512,0), Angle(0,0,0), Vector(-1.5,-5 - v*440,10), Vector(1.5,-5,0))
if v > 64 then continue end
ld = ld + v*((64-v)/64)
end
loudness = math.lerp(0.125, loudness, ld/6)
render.setColor(Color(255,255,255))
local sp = 1/(steps-1)
for p = 1, steps - 1 do
render.draw3DLine(Vector(128+(p-1)*sp*256, 256 + pcm[p]*128, 60*(1-loudness)), Vector(128+p*sp*256, 256 + pcm[p+1]*128, 60*(1-loudness)))
end
render.setColor(Color(0,212,255))
draw3DCircle(Vector(256, 256, 50*(1-loudness)), 128*(1+loudness*0.15), 32)
render.setColor(Color(0,212,255)*0.8)
draw3DCircle(Vector(256, 256, 30*(1-loudness)), 132*(1+loudness*0.165), 32)
render.setColor(Color(0,212,255)*0.6)
draw3DCircle(Vector(256, 256, 15*(1-loudness)), 138*(1+loudness*0.18), 32)
render.setColor(Color(0,212,255)*0.4)
draw3DCircle(Vector(256, 256, 0), 146*(1+loudness*0.2), 32)
--render.drawCircle(256, 256, 128)
--render.drawCircle(256, 256, 130)
--render.drawCircle(256, 256, 132)
--song info
render.setColor(Color(255,255,255))
render.setFont("DermaLarge")
render.drawText(10, 10, string.format("Song: %s\nBy: %s", songData.name, songData.req))
--special info
render.setColor(Color(255*(1-steps/800),255*(steps/800),0))
render.setFont("DermaDefault")
render.drawText(10,65, string.format("Line resolution: %d", steps))
--command info
render.setColor(Color(255,255,255))
render.drawText(10,450, "Chat commands:\n/play {url} (queues a soundfile from url)\n/vol {0-200} (sets the volume for you)\n/quota {0-100} (sets the cpu usage target)")
steps = math.floor(math.clamp(steps + 1500*(cpuMax()*settings.quota - cpuUsed()), 8, 800))
end
local function songStart(Musix, error)
if error == 0 then
song = Musix
song:play()
song:setPos(chip():getPos())
song:setLooping(false)
hook.add("think", "updatesounds", function()
customSoundFade(song, 250, 750, player():getShootPos():getDistance( chip():getPos() ), settings.volume)
if song:getTime() >= song:getLength() then
song:stop()
hook.remove("think", "updatesounds")
songData.name = "none"
songData.req = "unknown"
songData.url = ""
end
end)
if player() ~= owner() then return end
net.start("songLength")
net.writeFloat(song:getLength())
net.send()
elseif player() == owner() then
net.start("songLength")
net.writeFloat(0)
net.send()
end
end
local function netHandler(name)
if name == "setting_set" then
local sett = net.readString()
if sett == "vol" then
settings.volume = net.readFloat()
elseif sett == "quota" then
settings.quota = net.readFloat()
end
elseif name == "song_cmd" then
local cmd = net.readString()
if cmd == "play" then
songData = net.readTable()
if song ~= nil then
if song:isValid() then
song:stop()
hook.remove("think", "updatesounds")
end
end
try(function() bass.loadURL(songData.url, "3d noblock", songStart) end)
end
elseif name == "update" then
local type = net.readString()
if type == "queue" then
queue = net.readTable()
end
end
end
hook.add("render" , "screendraw", drawfunc)
hook.add("net", "recievedata", netHandler)
end