Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to list available songs
- local function listSongs()
- return {
- "Anarchy",
- "Daisuki",
- "FriendlyCreatures",
- "PsychoHead",
- "Psychosocial",
- "seila",
- "SunnyDay",
- "WhiteNight",
- "Numa_Numa_Yei",
- "Homem_Leme"
- }
- end
- -- Function to select a song
- local function selectSong()
- print("Available songs:")
- local songs = listSongs()
- for i, song in ipairs(songs) do
- print(i .. ". " .. song)
- end
- print("Enter the number of the song you want to play:")
- local choice = tonumber(read())
- if choice and choice >= 1 and choice <= #songs then
- return songs[choice] .. ".dfpwm"
- else
- print("Invalid selection. Please try again.")
- return selectSong()
- end
- end
- -- Main code
- local dfpwmFileName = selectSong()
- local dfpwmFile = fs.open(dfpwmFileName, "rb")
- if not dfpwmFile then
- print("Failed to open " .. dfpwmFileName)
- return
- end
- print("Opened file " .. dfpwmFileName)
- local function findSpeakers()
- local speakers = {}
- local modemSide = "top"
- local modem = peripheral.wrap(modemSide)
- if not modem then
- print("No modem found on side " .. modemSide)
- return nil
- end
- print("Modem found on side " .. modemSide)
- local connectedPeripherals = modem.getNamesRemote()
- for _, peripheralName in ipairs(connectedPeripherals) do
- local speaker = peripheral.wrap(peripheralName)
- if speaker and type(speaker) == "table" and speaker.playAudio then
- table.insert(speakers, speaker)
- print("Found valid speaker: " .. peripheralName)
- else
- print("Found invalid peripheral: " .. peripheralName)
- end
- end
- return speakers
- end
- local speakers = findSpeakers()
- if #speakers == 0 then
- print("No valid speakers found connected through wired modems.")
- dfpwmFile.close()
- return
- end
- print("Found " .. #speakers .. " valid speakers")
- local dfpwm = require("cc.audio.dfpwm").make_decoder()
- local chunkSize = 16 * 1024 -- 16 KB
- local function streamAndPlayAudio()
- local totalBytesRead = 0
- while true do
- local chunk = dfpwmFile.read(chunkSize)
- if not chunk then
- break
- end
- local decodedChunk = dfpwm(chunk)
- for _, speaker in ipairs(speakers) do
- local success, err = pcall(function() speaker.playAudio(decodedChunk) end)
- if not success then
- print("Error playing audio on speaker: " .. err)
- end
- end
- sleep(2.1)
- totalBytesRead = totalBytesRead + #chunk
- end
- end
- print("Streaming and playing audio from", dfpwmFileName, "through wired modems...")
- streamAndPlayAudio()
- dfpwmFile.close()
- print("Audio playback completed")
- --GoogleDrive.. Audio Files: https://drive.google.com/drive/folders/19y8S2p8WlPs2fDPxA-2xFLCXxfh2zaj4?usp=drive_link
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement