Vai alla navigazione

Streaming

Scopri come trasmettere audio in tempo reale dall'API ElevenLabs usando la codifica di trasferimento chunked

L’API ElevenLabs supporta lo streaming audio in tempo reale per endpoint selezionati, restituendo byte audio raw (ad esempio, dati MP3) direttamente tramite HTTP usando la codifica di trasferimento chunked. In questo modo, i client possono elaborare o riprodurre l’audio in modo incrementale mentre viene generato.

Le nostre librerie ufficiali Node e Python includono utility per semplificare la gestione di questo flusso audio continuo.

Lo streaming è supportato per l’API Text to Speech, l’API Modificatore di Voce e l’API Isolatore Vocale. Questa sezione illustra il funzionamento dello streaming per le richieste inviate all’API Text to Speech.

In Python, una richiesta di streaming è così:

from elevenlabs import stream
from elevenlabs.client import ElevenLabs
elevenlabs = ElevenLabs()
audio_stream = elevenlabs.text_to_speech.stream(
text="This is a test",
voice_id="JBFqnCBsd6RMkjVDRZzb",
model_id="eleven_multilingual_v2"
)
# option 1: play the streamed audio locally
stream(audio_stream)
# option 2: process the audio bytes manually
for chunk in audio_stream:
if isinstance(chunk, bytes):
print(chunk)

In Node / Typescript, una richiesta di streaming è così:

import { ElevenLabsClient, stream } from "@elevenlabs/elevenlabs-js";
import { Readable } from "stream";
const elevenlabs = new ElevenLabsClient();
async function main() {
const audioStream = await elevenlabs.textToSpeech.stream("JBFqnCBsd6RMkjVDRZzb", {
text: "This is a test",
modelId: "eleven_v4",
});
// option 1: play the streamed audio locally
await stream(Readable.from(audioStream));
// option 2: process the audio manually
for await (const chunk of audioStream) {
console.log(chunk);
}
}
main();