-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplayer.lua
More file actions
418 lines (356 loc) · 10.7 KB
/
player.lua
File metadata and controls
418 lines (356 loc) · 10.7 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
--[[
Quartz Player (c) AlexDevs
https://alexdevs.me
https://github.com/Ale32bit/Quartz
GNU GPLv3 License
https://github.com/Ale32bit/Quartz/blob/main/LICENSE
]]
local emulateSmallTerm = false
settings.define("quartz.right", {
description = "Right speaker",
default = "right",
type = "string",
})
settings.define("quartz.left", {
description = "Left speaker",
default = "left",
type = "string",
})
settings.define("quartz.distributed", {
description = "Play mono audio from all speakers connected to the network",
default = false,
type = "boolean",
})
settings.define("quartz.autoplay", {
description = "Autoplay the track on start",
default = true,
type = "boolean",
})
settings.define("quartz.volume", {
description = "Default audio volume. Range: 0.0 - 1.0",
default = 1,
type = "number"
})
settings.define("quartz.distance", {
description = "Default audio distance. Range: 0 - 128",
default = 1,
type = "number"
})
settings.define("quartz.loop", {
description = "Restart track or playlist when it ends",
default = true,
type = "boolean"
})
settings.define("quartz.raw", {
description = "Play unfiltered audio",
default = false,
type = "boolean"
})
local quartz = {
version = "0.6.4",
modules = {},
drivers = {},
args = table.pack(...),
}
local running = true
local current = term.current()
if emulateSmallTerm then
term.setBackgroundColor(colors.brown)
term.clear()
-- turtle / ni term
current = window.create(term.current(), 1, 1, 39, 13, true)
end
local w, h = current.getSize()
quartz.logWindow = window.create(current, 1, 1, w, h, true)
quartz.guiWindow = window.create(current, 1, 1, w, h, false)
quartz.termWindow = current
quartz.guiWindow.setCursorPos(1, 1)
quartz.guiWindow.write("Quartz Player " .. quartz.version)
quartz.guiWindow.setCursorPos(1, 2)
quartz.guiWindow.write("GUI not loaded.")
quartz.guiWindow.setCursorPos(1, 3)
quartz.guiWindow.write("Press F1 to switch to log screen.")
function quartz.log(...)
local oldTerm = term.redirect(quartz.logWindow)
print(...)
term.redirect(oldTerm)
end
function quartz.logError(...)
local oldTerm = term.redirect(quartz.logWindow)
printError(...)
term.redirect(oldTerm)
end
function quartz.switchToLogScreen()
term.redirect(quartz.logWindow)
quartz.guiWindow.setVisible(false)
quartz.logWindow.setVisible(true)
quartz.logWindow.redraw()
end
function quartz.switchToGuiScreen()
term.redirect(quartz.guiWindow)
quartz.guiWindow.setVisible(true)
quartz.logWindow.setVisible(false)
quartz.guiWindow.redraw()
end
quartz.log("Quartz Player " .. quartz.version .. " by AlexDevs")
quartz.log("https://github.com/Ale32bit/Quartz")
local UI = require("quartz.lib.ui")
local rawDfpwm = require("quartz.lib.rawDfpwm")
local speakers = {
left = peripheral.wrap(settings.get("quartz.left")),
right = peripheral.wrap(settings.get("quartz.right")),
}
quartz.speakers = speakers
speakers.left = speakers.left or speakers.right
speakers.right = speakers.right or speakers.left
speakers.isMono = speakers.left == speakers.right
speakers.volume = settings.get("quartz.volume")
speakers.distance = settings.get("quartz.distance")
speakers.distributedMode = settings.get("quartz.distributed")
speakers.distributedSpeakers = {}
if speakers.distributedMode then
speakers.distributedSpeakers = { peripheral.find("speaker") }
end
if not speakers.left and not speakers.right and not speakers.distributedMode then
printError("The configured speakers could not be found.")
print(
"Configure the speakers by setting the peripheral names to the settings \"quartz.left\" and/or \"quartz.right\" with the \"set\" command.")
return
end
if speakers.distributedMode and #speakers.distributedSpeakers == 0 then
printError("There are no speakers attached to the network.")
return
end
local mode
if speakers.distributedMode then
mode = "distributed"
else
mode = speakers.isMono and "mono" or "stereo"
end
quartz.log("Speaker configuration:", mode)
if speakers.distributedMode then
for i, speaker in ipairs(speakers.distributedSpeakers) do
speaker.stop()
end
else
speakers.left.stop()
speakers.right.stop()
end
function quartz.make_decoder()
return rawDfpwm.make_decoder()
end
function quartz.setDecoderRawMode(enable)
rawDfpwm.set_raw_mode(enable)
end
function quartz.getDecoderRawMode()
return rawDfpwm.get_raw_mode()
end
quartz.setDecoderRawMode(settings.get("quartz.raw", false))
quartz.log("Loading playback drivers...")
local driversPath = "/quartz/drivers"
for i, fileName in ipairs(fs.list(driversPath)) do
local isValid = fileName:match("%.lua$") ~= nil
local file = fileName:gsub("%.lua$", "")
if isValid then
local path = fs.combine(driversPath, file):gsub("/", ".")
local driver = require(path)
quartz.drivers[driver.type] = driver
quartz.log("Found driver:", driver.type)
end
end
local modulesPath = "/quartz/modules"
for i, fileName in ipairs(fs.list(modulesPath)) do
local isValid = fileName:match("%.lua$") ~= nil
local file = fileName:gsub("%.lua$", "")
if isValid then
local path = fs.combine(modulesPath, file):gsub("/", ".")
local module = require(path)
quartz.modules[file] = module
quartz.log("Found module:", file)
end
end
local tasks = {}
local filters = {}
function quartz.addTask(func)
local thread = coroutine.create(func)
local pid = #tasks + 1
tasks[pid] = thread
return thread, pid
end
function quartz.killTask(pid)
if not pid then
return
end
tasks[pid] = nil
filters[pid] = nil
end
quartz.ui = UI(quartz.guiWindow, quartz.addTask)
quartz.track = nil
quartz.trackMeta = nil
quartz.trackSource = nil
local trackPid = nil
function quartz.play()
quartz.log(string.format("Playing %s - %s", quartz.trackMeta.artist, quartz.trackMeta.title))
quartz.track:play()
end
function quartz.stop(dispose)
if quartz.track then
quartz.log("Stopping playback")
quartz.track:stop()
if dispose then
quartz.log("Disposing playback")
pcall(function() quartz.track:dispose() end)
quartz.track = nil
quartz.killTask(trackPid)
os.queueEvent("quartz_dispose")
end
end
end
function quartz.loadTrack(tr, src)
if quartz.track then
quartz.stop(true)
end
quartz.track = tr
quartz.trackMeta = quartz.track:getMeta()
quartz.trackSource = src
quartz.log(string.format("[%s] Loaded track: %s - %s (%s)",
quartz.track.type, quartz.trackMeta.artist, quartz.trackMeta.title, quartz.trackMeta.album))
os.queueEvent("quartz_load", quartz.trackMeta, quartz.track, quartz.trackSource)
trackPid = quartz.addTask(function()
quartz.track:run()
end)
quartz.play()
end
function quartz.setVolume(vol)
if vol > 1 then
vol = 1
end
if vol < 0 then
vol = 0
end
speakers.volume = vol
os.queueEvent("quartz_volume", vol)
end
function quartz.setDistance(dist)
if dist > 128 then
dist = 128
end
if dist < 0 then
dist = 0
end
speakers.distance = dist
os.queueEvent("quartz_distance", dist)
end
function quartz.loadDriver(handle, name, source, altMeta)
if not source then
if debug and debug.getinfo then
local debugSource = debug.getinfo(2, "S").source
source = debugSource:match("/(%w+)%.lua$")
end
end
local compatibleDrivers = {}
for _, driver in pairs(quartz.drivers) do
local isCompatible, weight = driver.checkCompatibility(handle, name)
if isCompatible then
table.insert(compatibleDrivers, {
driver = driver,
weight = weight,
})
end
end
table.sort(compatibleDrivers, function(a, b)
return a.weight > b.weight
end)
local comp = compatibleDrivers[1]
if comp then
local track = comp.driver.new(handle, name, quartz.speakers, quartz.make_decoder, altMeta or {})
quartz.loadTrack(track, source)
return true
end
return false
end
function quartz.exit()
running = false
quartz.guiWindow.setVisible(false)
quartz.stop(true)
term.redirect(quartz.termWindow)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1, 1)
end
quartz.log("Loading modules...")
for name, moduleContext in pairs(quartz.modules) do
quartz.log("Loading module", name)
local init = (type(moduleContext) == "table" and moduleContext.init)
or (type(moduleContext) == "function" and moduleContext)
if type(init) == "function" then
local ok, err = pcall(init, quartz)
if not ok then
quartz.logError(err)
end
end
end
quartz.addTask(function()
while true do
local ev, key = os.pullEvent("key")
if key == keys.f1 then
if quartz.logWindow.isVisible() then
quartz.switchToGuiScreen()
else
quartz.switchToLogScreen()
end
end
end
end)
quartz.addTask(function()
quartz.log("Ready")
quartz.log("Press F1 to toggle the log screen")
os.queueEvent("quartz_ready")
end)
quartz.addTask(function()
local counter = 0
while true do
if not quartz.track or quartz.track.state == "paused" then
os.pullEvent("quartz_play")
end
parallel.waitForAny(function()
sleep(1)
counter = counter + 1
end, function()
os.pullEvent("quartz_pause")
end, function()
os.pullEvent("quartz_driver_end")
counter = 0
end)
--quartz.log(counter)
end
end)
local event = {}
while running do
for i, thread in pairs(tasks) do
if not running then
break
end
if coroutine.status(thread) == "dead" then
tasks[i] = nil
filters[i] = nil
else
if filters[i] == nil or event[1] == filters[i] or event[1] == "terminate" then
local ok, par = coroutine.resume(thread, table.unpack(event))
if ok then
filters[i] = par
else
running = false
quartz.exit()
print("Quartz has crashed!")
print("Version:", quartz.version)
printError(debug.traceback(thread, par))
-- skip pulling event
error()
end
end
end
end
event = table.pack(coroutine.yield())
end