Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- test_tts.lua
- -- Simple local TTS test script for speakers (with speaker reset)
- local decoder = require("cc.audio.dfpwm").make_decoder()
- print("=== TTS Speaker Test ===")
- print("Enter text to speak (or type 'exit' to quit):")
- -- Get all speakers
- local speakers = { peripheral.find("speaker") }
- if #speakers == 0 then
- print("No speaker found. Please connect one and restart.")
- return
- end
- -- Function to stop any currently playing sounds
- local function stopAllSpeakers()
- for _, sp in ipairs(speakers) do
- -- Some sounds might still be playing; stop them
- pcall(function()
- sp.stop() -- stop all current sounds
- end)
- end
- -- Wait a moment to ensure speakers flush buffers
- sleep(0.2)
- end
- while true do
- io.write("> ")
- local message = read()
- if message == "exit" then
- print("Exiting test.")
- break
- end
- stopAllSpeakers() -- ðŸ”¹æ¸…ç©ºæ‰€æœ‰æ‰¬å£°å™¨ç¼“å†²ï¼Œé˜²æ¢æ®‹ç•™å£°éŸ³
- local url = "https://music.madefor.cc/tts?text=" .. textutils.urlEncode(message)
- print("Requesting TTS audio...")
- local response, err = http.get({ url = url, binary = true })
- if not response then
- print("HTTP request failed: " .. tostring(err))
- else
- print("Playing TTS audio...")
- while true do
- local chunk = response.read(16 * 1024)
- if not chunk then break end
- local buffer = decoder(chunk)
- for _, sp in ipairs(speakers) do
- while not sp.playAudio(buffer) do
- os.pullEvent("speaker_audio_empty")
- end
- end
- end
- response.close()
- print("Playback finished.")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment