#Realtime text to speech with javascript
1 messages · Page 1 of 1 (latest)
You can create real-time text-to-speech functionality using JavaScript and the ElevenLabs TypeScript SDK. Here's a basic example of how you might set this up:
app.ws('/call/connection', (ws: WebSocket) => {
ws.on('message', async (data: string) => {
const message: {
event: string;
start?: { streamSid: string; callSid: string };
} = JSON.parse(data);
if (message.event === 'start' && message.start) {
const streamSid = message.start.streamSid;
const response = await elevenlabs.textToSpeech.convert(voiceId, {
model_id: 'eleven_turbo_v2',
output_format: outputFormat,
text,
});
}
});
});
In this example, we're listening for messages that Twilio sends to our WebSocket endpoint. When we receive a start message event, we generate audio using the ElevenLabs SDK's textToSpeech.convert method. This method takes a voiceId, a model_id, an output_format, and the text to convert to speech.
Please note that you'll need to replace voiceId, outputFormat, and text with your own values. The voiceId is the identifier of the voice you want to use for speech synthesis, outputFormat is the format you want the audio to be in, and text is the text you want to convert to speech.
Title: Websockets - ElevenLabs
Path: https://elevenlabs.io/docs/api-reference/websockets
Title: Streaming - ElevenLabs
Path: https://elevenlabs.io/docs/api-reference/speech-to-speech-streaming
Title: How to send an AI message through a phone call using Twilio and ElevenLabs in Node.js - ElevenLabs
Path: https://elevenlabs.io/docs/api-reference/integrating-with-twilio
Title: Text-to-speech – ElevenLabs
Path: https://help.elevenlabs.io/hc/en-us/sections/23795023293073-Text-to-speech
Title: Feedback - ElevenLabs
Path: https://elevenlabs.io/docs/speech-synthesis/feedback
real time streaming with javascript