Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local peripheral = peripheral
- local dfpwm = require("cc.audio.dfpwm")
- local modem = peripheral.wrap("back")
- modem.open(11)
- -- Function to find connected speakers
- local function findConnectedSpeakers()
- local speakers = {}
- local allPeripherals = peripheral.getNames()
- for _, pName in ipairs(allPeripherals) do
- local pType = peripheral.getType(pName)
- if pType == "speaker" then
- table.insert(speakers, peripheral.wrap(pName))
- end
- end
- return speakers
- end
- -- Function to read chunks from a string
- local function stringChunks(str, chunkSize)
- local start = 1
- return function()
- if start > #str then
- return nil
- else
- local chunk = str:sub(start, start + chunkSize - 1)
- start = start + chunkSize
- return chunk
- end
- end
- end
- -- Main function to play music on connected speakers
- local function playMusicOnSpeakers()
- local connectedSpeakers = findConnectedSpeakers()
- if #connectedSpeakers == 0 then
- print("No speakers found. Please make sure speakers are connected.")
- return
- end
- print("Please enter the URL of the music file you want to play:")
- local url = read()
- local response = http.get(url, nil, true)
- if not response then
- print("Request failed. HTTP Error: " .. (response and response.getResponseCode() or "Unknown"))
- return
- end
- local responseData = response.readAll()
- response.close()
- local decoder = dfpwm.make_decoder()
- local chunkSize = 16 * 1024
- local chunkGenerator = stringChunks(responseData, chunkSize)
- local chunk = chunkGenerator()
- while chunk do
- local buffer = decoder(chunk)
- for _, speaker in ipairs(connectedSpeakers) do
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- chunk = chunkGenerator()
- end
- print("Music successfully played on all speakers.")
- end
- -- Call the main function to play music
- playMusicOnSpeakers()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement