ComputerCraft/MusicPlayer/Client/client.lua
2024-03-10 23:45:44 +03:00

95 lines
No EOL
2.8 KiB
Lua

local graphics = require("graphics")
local modem = peripheral.find('modem')
if not modem then error('No modem found') end
modem.open(7101)
local dfpwm = require("cc.audio.dfpwm")
local speaker = peripheral.find("speaker")
if not speaker then error('No speaker found') end
local decoder = dfpwm.make_decoder()
local status_vars = {
is_playing = true,
is_paused = false,
current_second = 0,
track_index = 1,
start_index = 1,
}
local cfg_vars = {
forward_skip = 5,
backward_skip = 5,
port = 7101,
chunk_size = 0.5
}
local function get_catalogue()
modem.transmit(7101, 7101, {type = 'query'})
local event, sides, channel, replyChannel, message, distance = os.pullEvent('modem_message')
return message.audio_catalogue
end
local audio_catalogue = get_catalogue()
local function music_loop()
local play_music = require("playback")
while true do
status_vars.current_second = play_music(status_vars, cfg_vars, audio_catalogue, modem)
sleep()
end
end
local function draw_ui_loop()
while true do
if not status_vars.is_playing then
graphics.draw_track_list(1, 1, audio_catalogue, status_vars.start_index, select(2, term.getSize()) - 1, term)
else
graphics.draw_track_list(1, 1, audio_catalogue, status_vars.start_index, select(2, term.getSize()) - 4, term)
end
sleep(1)
end
end
local function control_loop()
while true do
local event, button, x, y = os.pullEvent("mouse_click")
local btn_actions = {
pause = {
x = x + math.floor((select(1, term.getSize()) - 11) / 2) + 5,
y = select(2, term.getSize()) - 1,
func = function ()
status_vars.is_paused = not status_vars.is_paused
end
},
forward = {
x = math.floor((select(1, term.getSize()) - 11) / 2) + 9,
y = select(2, term.getSize()) - 1,
func = function ()
status_vars.current_second = status_vars.current_second + cfg_vars.forward_skip
end
},
backward = {
x = math.floor((select(1, term.getSize()) - 11) / 2) + 1,
y = select(2, term.getSize()) - 1,
func = function ()
status_vars.current_second = status_vars.current_second - cfg_vars.backward_skip
end
}
}
if x == 3 and y == 1 then return end
term.setCursorPos(btn_actions.pause.x, btn_actions.pause.y)
term.write('%')
for i, v in pairs(btn_actions) do
if x == v.x and y == v.y then
v.func()
break
end
end
end
end
term.clear() -- Clear terminal
parallel.waitForAny(music_loop, draw_ui_loop, control_loop) -- Start loops