Advertisement
GroupXyz

Audioplayer 2.1

Dec 14th, 2024
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.43 KB | Source Code | 0 0
  1. -- Audioplayer script by GroupXyz
  2.  
  3. -- Version: 2.1
  4. -- Play audio files!
  5.  
  6. function loadFileList(interface)
  7.     local fileList = {}
  8.     for _, file in ipairs(fs.list(interface)) do
  9.       if not fs.isDir(interface.."/"..file) then
  10.         table.insert(fileList, file)
  11.       end
  12.     end
  13.     return fileList
  14.   end
  15.  
  16.   function displayFileList(fileList)
  17.     term.clear()
  18.     term.setCursorPos(1, 1)
  19.     print("----------Audioplayer by GroupXyz----------")
  20.     print("Please place your audio files in the folder 'sounds/'")
  21.     print("Files:")
  22.     for i, file in ipairs(fileList) do
  23.       print(i..". "..file)
  24.     end
  25.   end
  26.  
  27.   function playFile(interface, selectedFile, loop)
  28.     local filePath = interface.."/"..selectedFile
  29.     if fs.exists(filePath) and not fs.isDir(filePath) then
  30.       local speaker = peripheral.find("speaker")
  31.       if speaker then
  32.         local dfpwm = require("cc.audio.dfpwm")
  33.         local decoder = dfpwm.make_decoder()
  34.  
  35.         local function playBuffer(buffer)
  36.           while not speaker.playAudio(buffer) do
  37.             os.pullEvent("speaker_audio_empty")
  38.           end
  39.         end
  40.  
  41.         if loop then
  42.           while true do
  43.             for chunk in io.lines(filePath, 16 * 1024) do
  44.               local buffer = decoder(chunk)
  45.               playBuffer(buffer)
  46.               sleep(0.05)
  47.             end
  48.           end
  49.         else
  50.           for chunk in io.lines(filePath, 16 * 1024) do
  51.             local buffer = decoder(chunk)
  52.             playBuffer(buffer)
  53.             sleep(0.05)
  54.           end
  55.         end
  56.  
  57.         print("File played successfully.")
  58.       else
  59.         print("Speaker not found.")
  60.       end
  61.     else
  62.       print("File not found.")
  63.     end
  64.   end
  65.  
  66.   function createInterface(interface)
  67.     if not fs.exists(interface) then
  68.       fs.makeDir(interface)
  69.     end
  70.   end
  71.  
  72.   local queue = {}
  73.  
  74.   function showInstructions()
  75.     if fs.exists("hideinstructions") then
  76.       return
  77.     end
  78.  
  79.     print("Instructions:")
  80.     print("1. Type the file number to play a file.")
  81.     print("2. Type 'repeat' to loop a file.")
  82.     print("3. Type 'queue' to add a file to the queue.")
  83.     print("4. Type 'playqueue' to play the queue.")
  84.     print("5. Type 'skip' to skip the current file in the queue.")
  85.     print("6. Type 'w' to scroll up and 's' to scroll down.")
  86.     print("7. Type '0' to exit.")
  87.     print("Type 'ok' to continue or 'hide' to disable instructions permanently.")
  88.     while true do
  89.       local input = read()
  90.       if input == "ok" then
  91.         break
  92.       elseif input == "hide" then
  93.         local file = fs.open("hideinstructions", "w")
  94.             if not file then
  95.                 term.clear()
  96.                 print("Failed to open file for writing, disk space full? Waiting 5s")
  97.                 sleep(5)
  98.                 return
  99.             end
  100.             file.write("delete this file to show instructions again")
  101.             file.close()
  102.             term.clear()
  103.             print("Instructions hidden, delete the file hideinstructions to reset. Waiting 3s")
  104.             sleep(3)
  105.         break
  106.       else
  107.         print("Please type 'ok' to continue or 'hide' to disable instructions permanently.")
  108.       end
  109.     end
  110.   end
  111.  
  112.   function addToQueue(file)
  113.     table.insert(queue, file)
  114.     print(file .. " added to queue.")
  115.   end
  116.  
  117.   function playQueue(interface)
  118.     while #queue > 0 do
  119.       local file = table.remove(queue, 1)
  120.       playFile(interface, file, false)
  121.     end
  122.   end
  123.  
  124.   function skipQueue()
  125.     if #queue > 0 then
  126.       table.remove(queue, 1)
  127.       print("Skipped current file in queue.")
  128.     else
  129.       print("Queue is empty.")
  130.     end
  131.   end
  132.  
  133.   function scrollFileList(fileList, startIndex, endIndex)
  134.     term.clear()
  135.     term.setCursorPos(1, 1)
  136.     for i = startIndex, endIndex do
  137.       if fileList[i] then
  138.         print(i .. ": " .. fileList[i])
  139.       end
  140.     end
  141.   end  
  142.  
  143.   -- Main program
  144.   local interface = "sounds/"
  145.   createInterface(interface)
  146.   local fileList = loadFileList(interface)
  147.   local startIndex = 1
  148.   local endIndex = 10
  149.  
  150.   showInstructions()
  151.  
  152. while true do
  153.   scrollFileList(fileList, startIndex, endIndex)
  154.   --print("Which file do you want to play? (Type 'repeat' to loop, 'queue' to add to queue, 'playqueue' to play queue, 'skip' to skip current file in queue, 'scroll' to scroll, 0 to exit)")
  155.   local choice = read()
  156.  
  157.   if choice == "0" then
  158.     break
  159.   elseif choice == "repeat" then
  160.     print("Enter the file number to repeat:")
  161.     local repeatChoice = tonumber(read())
  162.     if fileList[repeatChoice] then
  163.       playFile(interface, fileList[repeatChoice], true)
  164.     else
  165.       print("Invalid selection.")
  166.     end
  167.   elseif choice == "queue" then
  168.     print("Enter the file number to add to queue:")
  169.     local queueChoice = tonumber(read())
  170.     if fileList[queueChoice] then
  171.       addToQueue(fileList[queueChoice])
  172.     else
  173.       print("Invalid selection.")
  174.     end
  175.   elseif choice == "playqueue" then
  176.     playQueue(interface)
  177.   elseif choice == "skip" then
  178.     skipQueue()
  179.   elseif choice == "w" and startIndex > 1 then
  180.     startIndex = startIndex - 10
  181.     endIndex = endIndex - 10
  182.   elseif choice == "s" and endIndex < #fileList then
  183.     startIndex = startIndex + 10
  184.     endIndex = endIndex + 10
  185.   elseif tonumber(choice) and fileList[tonumber(choice)] then
  186.     playFile(interface, fileList[tonumber(choice)], false)
  187.   else
  188.     print("Invalid selection.")
  189.   end
  190. end
  191.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement