Add streamAudio method for real-time audio streaming - #12
Open
huynhquangtoan wants to merge 1 commit into
Open
Conversation
Owner
|
Could you provide an example demonstrating how to play audio using a streamAudio output buffer? |
Author
|
This is how I’m using app.post("/v1/audio/speech", async (req, res) => {
const input = req.body?.input ?? req.body?.text;
...
const tts = new EdgeTTS({
voice: edgeVoice,
lang: "en-US",
saveSubtitles: false,
rate,
timeout: 30000,
});
try {
if (streamFormat === "sse") {
res.setHeader("Content-Type", "text/event-stream");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders && res.flushHeaders();
for await (const chunk of tts.streamAudio(input)) {
const b64 = chunk.toString("base64");
res.write(
`event: speech.audio.delta\ndata: ${JSON.stringify({ delta: b64 })}\n\n`,
);
}
res.write(`event: speech.audio.done\ndata: {}\n\n`);
res.end();
} else {
const tmpFile = path.join(
os.tmpdir(),
`edge-tts-${Date.now()}-${Math.random().toString(36).slice(2)}.mp3`,
);
try {
await tts.ttsPromise(input, tmpFile);
const buf = await fs.readFile(tmpFile);
res.type("audio/mpeg").send(buf);
} finally {
fs.unlink(tmpFile).catch(() => {});
}
}
} catch (err) {
const message = err.message || "TTS failed";
return sendError(res, 502, "tts_failed", message.slice(0, 200));
}
});Use case explanation
|
Owner
|
Okay, actually I was hoping you could provide a test, but that's okay, I'll add it later. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add
streamAudio()for SSE / real-time streamingSummary
This PR adds a new method
streamAudio(text)that streams TTS audio as it is received from the Edge WebSocket, without writing to a file. It is intended for servers that need to send audio over SSE (e.g.speech.audio.delta/speech.audio.done) or other real-time streaming use cases.What changed
New API:
streamAudio(text: string): AsyncGenerator<Buffer>Path:turn.endis received, constructortimeoutis reached, or the WebSocket errors.Buffer(payload afterPath:audio\r\nin the binary message).Existing behavior:
ttsPromise(text, audioPath)is unchanged and still writes to a file.Docs: README updated with a "streamAudio (streaming / SSE)" section: usage, chunk format, and example with
for await.Why
stream_format: "sse") can consumestreamAudio()and forward chunks as SSE events instead of buffering and then sending a file.ttsPromiseusage stays the same.Example