Advertisement
Guest User

play.lua

a guest
Feb 3rd, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.97 KB | None | 0 0
  1. local dfpwm = require("cc.audio.dfpwm")
  2. local speakers = { peripheral.find("speaker") }
  3. local drive = peripheral.find("drive")
  4. local decoder = dfpwm.make_decoder()
  5.  
  6. local menu = require "menu"
  7.  
  8. local uri = nil
  9. local volume = settings.get("media_center.volume")
  10. local selectedSong = nil
  11.  
  12. if drive == nil or not drive.isDiskPresent() then
  13.     local savedSongs = fs.list("songs/")
  14.  
  15.     if #savedSongs == 0 then
  16.         error("ERR - No disk was found in the drive, or no drive was found. No sound files were found saved to device.")
  17.     else
  18.         local entries = {
  19.             [1] = {
  20.                 label = "[CANCEL]",
  21.                 callback = function()
  22.                     error()
  23.                 end
  24.             }
  25.         }
  26.  
  27.         for i, fp in ipairs(savedSongs) do
  28.             table.insert(entries, {
  29.                 label = fp:match("^([^.]+)"),
  30.                 callback = function()
  31.                     selectedSong = fp
  32.  
  33.                     menu.exit()
  34.                 end
  35.             })
  36.         end
  37.  
  38.         menu.init({
  39.             main = {
  40.                 entries = entries
  41.             }
  42.         })
  43.  
  44.         menu.thread()
  45.  
  46.         if selectedSong ~= nil then
  47.             local fp = "songs/" .. selectedSong
  48.  
  49.             if fs.exists(fp) then
  50.                 local file = fs.open(fp, "r")
  51.  
  52.                 uri = file.readAll()
  53.  
  54.                 file.close()
  55.             else
  56.                 print("Song was not found on device!")
  57.  
  58.                 return
  59.             end
  60.         else error() end
  61.     end
  62. else
  63.     local songFile = fs.open("disk/song.txt", "r")
  64.     uri = songFile.readAll()
  65.  
  66.     songFile.close()
  67. end
  68.  
  69. --if uri == nil or not uri:find("^https") then
  70.     --print("ERR - Invalid URI!")
  71.     --return
  72. --end
  73.  
  74. function playChunk(chunk)
  75.     local returnValue = nil
  76.     local callbacks = {}
  77.  
  78.     for i, speaker in pairs(speakers) do
  79.         if i > 1 then
  80.             table.insert(callbacks, function()
  81.                 speaker.playAudio(chunk, volume or 1.0)
  82.             end)
  83.         else
  84.             table.insert(callbacks, function()
  85.                 returnValue = speaker.playAudio(chunk, volume or 1.0)
  86.             end)
  87.         end
  88.     end
  89.  
  90.     parallel.waitForAll(table.unpack(callbacks))
  91.  
  92.     return returnValue
  93. end
  94.  
  95. print("Playing '" .. selectedSong or drive.getDiskLabel() .. "' at volume " .. (volume or 1.0))
  96.  
  97. local quit = false
  98.  
  99. function play()
  100.     while true do
  101.         --local response = http.get(uri, nil, true)
  102.  
  103.   local response = uri
  104.         local chunkSize = 4 * 1024
  105.         local chunk = response.read(chunkSize)
  106.         while chunk ~= nil do
  107.             local buffer = decoder(chunk)
  108.  
  109.             while not playChunk(buffer) do
  110.                 os.pullEvent("speaker_audio_empty")
  111.             end
  112.  
  113.             chunk = response.read(chunkSize)
  114.         end
  115.     end
  116. end
  117.  
  118. function readUserInput()
  119.     local commands = {
  120.         ["stop"] = function()
  121.             quit = true
  122.         end
  123.     }
  124.  
  125.     while true do
  126.         local input = string.lower(read())
  127.         local commandName = ""
  128.         local cmdargs = {}
  129.  
  130.         local i = 1
  131.         for word in input:gmatch("%w+") do
  132.             if i > 1 then
  133.                 table.insert(cmdargs, word)
  134.             else
  135.                 commandName = word
  136.             end
  137.         end
  138.  
  139.         local command = commands[commandName]
  140.  
  141.         if command ~= nil then
  142.             command(table.unpack(cmdargs))
  143.         else print('"' .. cmdargs[1] .. '" is not a valid command!') end
  144.     end
  145. end
  146.  
  147. function waitForQuit()
  148.     while not quit do
  149.         sleep(0.1)
  150.     end
  151. end
  152.  
  153. parallel.waitForAny(play, readUserInput, waitForQuit)
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement