Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local peripheral = peripheral or require("peripheral")
- local http = http or require("http")
- local dfpwm = require("cc.audio.dfpwm")
- local speaker = peripheral.find("speaker")
- if not speaker then
- print("Speaker not found. Please make sure a speaker is connected.")
- return
- end
- local function stringChunks(str)
- local chunkSize = 16 * 1024
- 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
- local function saveToFile(filename, data)
- local file = fs.open(filename, "w")
- file.write(data)
- file.close()
- 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 response then
- local responseData = response.readAll()
- response.close()
- local decoder = dfpwm.make_decoder()
- print("Press Enter to start playing the music.")
- read()
- for chunk in stringChunks(responseData) do
- local buffer = decoder(chunk)
- while not speaker.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- saveToFile("output_audio.dfpwm", chunk)
- end
- print("Music finished playing.")
- else
- print("Request failed. HTTP Error: " .. (response and response.getResponseCode() or "Unknown"))
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement