Advertisement
GroupXyz

Audioplayer 3.1

Dec 12th, 2024 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.61 KB | None | 0 0
  1. -- Synchronized Audioplayer script by GroupXyz
  2. -- Version: 3.1
  3.  
  4. -- Required peripherals: modem (for networking), speaker
  5.  
  6. local dfpwm = require("cc.audio.dfpwm")
  7. local decoder = dfpwm.make_decoder()
  8.  
  9. -- Find all modems (wired and wireless)
  10. local modems = {}
  11. for _, name in ipairs(peripheral.getNames()) do
  12.     if peripheral.getType(name) == "modem" then
  13.         table.insert(modems, peripheral.wrap(name))
  14.     end
  15. end
  16.  
  17. if #modems == 0 then
  18.     error("No modems found. Please attach at least one modem.")
  19. end
  20.  
  21. -- Open channel 1234 on all modems
  22. for _, modem in ipairs(modems) do
  23.     modem.open(1234)
  24. end
  25.  
  26. local currentPlayingFile = nil
  27. local lastSignalTime = nil
  28.  
  29. function loadFileList(interface)
  30.     local fileList = {}
  31.     for _, file in ipairs(fs.list(interface)) do
  32.         if not fs.isDir(interface.."/"..file) then
  33.             table.insert(fileList, file)
  34.         end
  35.     end
  36.     return fileList
  37. end
  38.  
  39. --function displayFileList(fileList)
  40. --    term.clear()
  41. --    term.setCursorPos(1, 1)
  42. --    print("----------Audioplayer by GroupXyz----------")
  43. --    print("Please place your audio files in the folder 'sounds/'")
  44. --    print("Files:")
  45. --    for i, file in ipairs(fileList) do
  46. --        print(i..". "..file)
  47. --    end
  48. --    if currentPlayingFile then
  49. --        print("\nCurrently playing: " .. currentPlayingFile)
  50. --    end
  51. --end
  52.  
  53. local currentPage = 1
  54. local itemsPerPage = 10
  55.  
  56. function paginate(fileList, page, itemsPerPage)
  57.     local startIdx = (page - 1) * itemsPerPage + 1
  58.     local endIdx = math.min(page * itemsPerPage, #fileList)
  59.     local paginatedList = {}
  60.     for i = startIdx, endIdx do
  61.         table.insert(paginatedList, fileList[i])
  62.     end
  63.     return paginatedList
  64. end
  65.  
  66. function displayFileList(fileList)
  67.     term.clear()
  68.     term.setCursorPos(1, 1)
  69.     print("----------Audioplayer by GroupXyz----------")
  70.     print("Please place your audio files in the folder 'sounds/'")
  71.     print("Files (Page " .. currentPage .. "):")
  72.    
  73.     local paginatedList = paginate(fileList, currentPage, itemsPerPage)
  74.     for i, file in ipairs(paginatedList) do
  75.         print((i + (currentPage - 1) * itemsPerPage) .. ". " .. file)
  76.     end
  77.    
  78.     if currentPlayingFile then
  79.         print("\nCurrently playing: " .. currentPlayingFile)
  80.     end
  81.    
  82.     print("\nUse 'n' for next page, 'p' for previous page")
  83. end
  84.  
  85. function playFile(interface, selectedFile, loop)
  86.     local filePath = interface.."/"..selectedFile
  87.     if fs.exists(filePath) and not fs.isDir(filePath) then
  88.         currentPlayingFile = selectedFile
  89.         local speaker = peripheral.find("speaker")
  90.         if speaker then
  91.             local function playBuffer(buffer)
  92.                 while not speaker.playAudio(buffer) do
  93.                     os.pullEvent("speaker_audio_empty")
  94.                 end
  95.             end
  96.  
  97.             if loop then
  98.                 while true do
  99.                     for chunk in io.lines(filePath, 16 * 1024) do
  100.                         local buffer = decoder(chunk)
  101.                         playBuffer(buffer)
  102.                         sleep(0.05)
  103.                     end
  104.                 end
  105.             else
  106.                 for chunk in io.lines(filePath, 16 * 1024) do
  107.                     local buffer = decoder(chunk)
  108.                     playBuffer(buffer)
  109.                     sleep(0.05)
  110.                 end
  111.             end
  112.         else
  113.             print("Speaker not found.")
  114.         end
  115.     else
  116.         print("File not found.")
  117.     end
  118. end
  119.  
  120. function sendSyncSignal(fileName)
  121.     for _, modem in ipairs(modems) do
  122.         modem.transmit(1234, 1234, { command = "play", file = fileName, timestamp = os.epoch("utc") })
  123.     end
  124.     playFile("sounds", fileName, false) -- Play locally as well
  125. end
  126.  
  127. function listenForCommands(interface)
  128.     while true do
  129.         local event, side, channel, replyChannel, message = os.pullEvent("modem_message")
  130.         if channel == 1234 and type(message) == "table" and message.command == "play" then
  131.             local fileName = message.file
  132.             local timestamp = message.timestamp
  133.  
  134.             -- Check if the signal is newer than the last signal
  135.             if not lastSignalTime or timestamp > lastSignalTime then
  136.                 lastSignalTime = timestamp
  137.                 print("Received play command for file: "..fileName)
  138.                 playFile(interface, fileName, false)
  139.             end
  140.         end
  141.     end
  142. end
  143.  
  144. -- Main program
  145. local interface = "sounds/"
  146. if not fs.exists(interface) then
  147.     fs.makeDir(interface)
  148. end
  149. local fileList = loadFileList("sounds")
  150. displayFileList(fileList)
  151.  
  152. parallel.waitForAny(function()
  153.     while true do
  154.         displayFileList(fileList)
  155.         print("Which file do you want to play? (Type 0 to exit)")
  156.         local choice = read()
  157.  
  158.         if choice == "0" then
  159.             break
  160.         elseif tonumber(choice) and fileList[tonumber(choice)] then
  161.             local selectedFile = fileList[tonumber(choice)]
  162.             if currentPlayingFile ~= selectedFile then
  163.                 print("Sending play command for file: "..selectedFile)
  164.                 sendSyncSignal(selectedFile)
  165.             else
  166.                 print("File is already playing.")
  167.             end
  168.         elseif choice == "n" then
  169.             if currentPage * itemsPerPage < #fileList then
  170.                 currentPage = currentPage + 1
  171.             end
  172.         elseif choice == "p" then
  173.             if currentPage > 1 then
  174.                 currentPage = currentPage - 1
  175.             end        
  176.         else
  177.             print("Invalid selection.")
  178.         end
  179.     end
  180. end, function()
  181.     listenForCommands(interface)
  182. end)
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement