ストリーミング

チャンク転送エンコーディングを使用してElevenLabs APIからリアルタイムオーディオをストリーミングする方法を確認

ElevenLabs APIは、一部のエンドポイントでリアルタイムオーディオストリーミングをサポートしています。チャンク転送エンコーディングを使用して、生のオーディオバイト(例:MP3データ)をHTTP経由で直接返します。これにより、生成中のオーディオをクライアント側で段階的に処理または再生できます。

公式のNodeおよびPythonライブラリには、この連続オーディオストリームの処理を簡素化するユーティリティが含まれています。

ストリーミングは、テキスト読み上げAPI、ボイスチェンジャーAPI、オーディオ分離APIでサポートされています。このセクションでは、テキスト読み上げAPIへのリクエストでストリーミングがどのように機能するかに焦点を当てます。

Pythonでのストリーミングリクエストは次のとおりです。

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)

Node/TypeScriptでのストリーミングリクエストは次のとおりです。

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_v3",
});
// 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();