Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local dfpwm = require("cc.audio.dfpwm")
- local linc = require("/lib/linc")
- local speaker = peripheral.find("speaker")
- local function merge(t1, t2)
- for i=1, #t2 do
- t1[#t1+1] = t2[i]
- end
- return t1
- end
- local audios = fs.find("/*.dfpwm")
- audios = merge(audios, fs.find("/*/*.dfpwm"))
- audios = merge(audios, fs.find("/*/*/*.dfpwm"))
- audios = merge(audios, fs.find("/*/*/*/*.dfpwm"))
- local w, h = term.getSize()
- local nOption = 1
- local isPlayingAudio = false
- local audioLength = 0
- local currentChunk = 0
- local isRepeating = false
- local audio;
- local title = "TuneBox"
- local function drawMusicSelector()
- linc.drawTopBar(title)
- print()
- for i, v in ipairs(audios) do
- if nOption == i then
- term.setBackgroundColor(colors.lightGray)
- term.setTextColor(colors.black)
- term.clearLine()
- print(" > "..v.." ("..tostring(math.floor(fs.getSize(v) / 1024)).." KB)")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- else
- print(" "..v.." ("..tostring(math.floor(fs.getSize(v) / 1024)).." KB)")
- end
- end
- end
- local function drawButton(str, bgcolor, fgcolor, keycolor)
- local bg = term.getBackgroundColor()
- local fg = term.getTextColor()
- write(" ")
- term.setBackgroundColor(bgcolor)
- term.setTextColor(fgcolor)
- term.write("[ ")
- term.setTextColor(keycolor)
- term.write(str:sub(1,1))
- term.setTextColor(fgcolor)
- term.write(str:sub(2, #str) .. " ]")
- term.setBackgroundColor(bg)
- term.setTextColor(fg)
- write(" ")
- end
- local function lerp(a, b, t)
- return a + (b - a) * t
- end
- local function drawPlayer()
- linc.drawTopBar(title)
- print()
- print(" Now playing: \""..audio:sub(1,#audio - 6).."\"\n")
- write(" ")
- for i = 1, w - 2 do
- if i < math.floor(lerp(0, w - 2, currentChunk / audioLength)) then
- term.setTextColor(colors.green)
- write("|")
- term.setTextColor(colors.white)
- else
- term.setTextColor(colors.white)
- write("|")
- end
- end
- print("\n")
- linc.drawButton("Prev", colors.gray, colors.lightGray, colors.white)
- linc.drawButton("Stop", colors.gray, colors.lightGray, colors.white)
- linc.drawButton("Next", colors.gray, colors.lightGray, colors.white)
- if not isRepeating then
- linc.drawButton("Repeat", colors.gray, colors.lightGray, colors.white)
- else
- --term.setTextColor(colors.black)
- linc.drawButton("Repeat", colors.green, colors.gray, colors.white)
- --term.setTextColor(colors.white)
- end
- end
- local function playAudioSchedule()
- local decoder = dfpwm.make_decoder()
- while true do
- if isPlayingAudio then
- decoder = dfpwm.make_decoder()
- audioLength = 0
- for _ in io.lines(audios[nOption], 16 * 1024) do
- audioLength = audioLength + 1
- end
- currentChunk = 1
- for chunk in io.lines(audios[nOption], 16 * 1024) do
- local buffer = decoder(chunk)
- if not isPlayingAudio then
- break
- end
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- currentChunk = currentChunk + 1
- drawPlayer()
- end
- if not isRepeating then
- sPlayingAudio = false
- end
- end
- sleep(0)
- end
- end
- local function handleInput()
- while true do
- local event, key = os.pullEvent("key")
- if event == "key" then
- if not isPlayingAudio then
- if keys.getName(key) == "up" then
- if nOption > 1 then
- nOption = nOption - 1
- end
- elseif keys.getName(key) == "down" then
- if nOption < #audios then
- nOption = nOption + 1
- end
- elseif keys.getName(key) == "enter" then
- isPlayingAudio = true
- audio = fs.getName(audios[nOption])
- goto continue
- end
- drawMusicSelector()
- else
- if keys.getName(key) == "r" then
- isRepeating = not isRepeating
- drawPlayer()
- elseif keys.getName(key) == "p" then
- if nOption > 1 then
- nOption = nOption - 1
- audio = fs.getName(audios[nOption])
- isPlayingAudio = false
- sleep(3)
- isPlayingAudio = true
- end
- elseif keys.getName(key) == "n" then
- if nOption < #audios then
- nOption = nOption + 1
- audio = fs.getName(audios[nOption])
- isPlayingAudio = false
- sleep(3)
- isPlayingAudio = true
- end
- elseif keys.getName(key) == "s" then
- isPlayingAudio = false
- sleep(3)
- drawMusicSelector()
- end
- end
- end
- ::continue::
- end
- end
- local function handleExit()
- while true do
- local event = os.pullEventRaw("terminate")
- if event == "terminate" then
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- break
- end
- end
- end
- if speaker == nil then
- error("Cannot find speaker!")
- end
- drawMusicSelector()
- parallel.waitForAny(handleInput, playAudioSchedule, handleExit)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement