Advertisement
MigasRocha

MusicPlayer/Computer ID:8

Dec 26th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | Gaming | 0 0
  1. -- Function to list available songs
  2. local function listSongs()
  3.     return {
  4.         "Anarchy",
  5.         "Daisuki",
  6.         "FriendlyCreatures",
  7.         "PsychoHead",
  8.         "Psychosocial",
  9.         "seila",
  10.         "SunnyDay",
  11.         "WhiteNight",
  12.         "Numa_Numa_Yei",
  13.         "Homem_Leme"
  14.     }
  15. end
  16.  
  17. -- Function to select a song
  18. local function selectSong()
  19.     print("Available songs:")
  20.     local songs = listSongs()
  21.     for i, song in ipairs(songs) do
  22.         print(i .. ". " .. song)
  23.     end
  24.     print("Enter the number of the song you want to play:")
  25.     local choice = tonumber(read())
  26.     if choice and choice >= 1 and choice <= #songs then
  27.         return songs[choice] .. ".dfpwm"
  28.     else
  29.         print("Invalid selection. Please try again.")
  30.         return selectSong()
  31.     end
  32. end
  33.  
  34. -- Main code
  35. local dfpwmFileName = selectSong()
  36. local dfpwmFile = fs.open(dfpwmFileName, "rb")
  37. if not dfpwmFile then
  38.     print("Failed to open " .. dfpwmFileName)
  39.     return
  40. end
  41. print("Opened file " .. dfpwmFileName)
  42.  
  43. local function findSpeakers()
  44.     local speakers = {}
  45.     local modemSide = "top"
  46.     local modem = peripheral.wrap(modemSide)
  47.     if not modem then
  48.         print("No modem found on side " .. modemSide)
  49.         return nil
  50.     end
  51.     print("Modem found on side " .. modemSide)
  52.  
  53.     local connectedPeripherals = modem.getNamesRemote()
  54.     for _, peripheralName in ipairs(connectedPeripherals) do
  55.         local speaker = peripheral.wrap(peripheralName)
  56.         if speaker and type(speaker) == "table" and speaker.playAudio then
  57.             table.insert(speakers, speaker)
  58.             print("Found valid speaker: " .. peripheralName)
  59.         else
  60.             print("Found invalid peripheral: " .. peripheralName)
  61.         end
  62.     end
  63.  
  64.     return speakers
  65. end
  66.  
  67. local speakers = findSpeakers()
  68. if #speakers == 0 then
  69.     print("No valid speakers found connected through wired modems.")
  70.     dfpwmFile.close()
  71.     return
  72. end
  73. print("Found " .. #speakers .. " valid speakers")
  74.  
  75. local dfpwm = require("cc.audio.dfpwm").make_decoder()
  76. local chunkSize = 16 * 1024  -- 16 KB
  77.  
  78. local function streamAndPlayAudio()
  79.     local totalBytesRead = 0
  80.     while true do
  81.         local chunk = dfpwmFile.read(chunkSize)
  82.         if not chunk then
  83.             break
  84.         end
  85.  
  86.         local decodedChunk = dfpwm(chunk)
  87.         for _, speaker in ipairs(speakers) do
  88.             local success, err = pcall(function() speaker.playAudio(decodedChunk) end)
  89.             if not success then
  90.                 print("Error playing audio on speaker: " .. err)
  91.             end
  92.         end
  93.         sleep(2.1)
  94.         totalBytesRead = totalBytesRead + #chunk
  95.     end
  96. end
  97.  
  98. print("Streaming and playing audio from", dfpwmFileName, "through wired modems...")
  99. streamAndPlayAudio()
  100.  
  101. dfpwmFile.close()
  102. print("Audio playback completed")
  103.  
  104. --GoogleDrive.. Audio Files:  https://drive.google.com/drive/folders/19y8S2p8WlPs2fDPxA-2xFLCXxfh2zaj4?usp=drive_link
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement