wss://api.pascalai.co/v1/text-to-speech/realtime

Text to speech realtime

Send a finished sentence and receive audio on the same WebSocket.

wss://api.pascalai.co/v1/text-to-speech/realtime?model=sarvam-bulbul-live

Send an API key as Authorization: Bearer. A browser sends the key as api_key on the query string. File synthesis stays on Text to speech.

FieldTypeDescription
modelRequiredstringCatalogue id of the live model. piper-en-us-lessac-medium, matcha-en-ljspeech, or pocket-tts.
api_keystringAPI key, sent on the query string.

Client messages

Wait for session.created. Send one session.update, then each speak. session.update is accepted only before the first sentence. The server answers with binary PCM16 frames.

FieldTypeDescription
session.updateobjectJSON text frame. session holds the fields in the Session section. Send it once, before the first speak.
speakobjectJSON text frame. text is one finished sentence.
session.closeobjectJSON text frame. Ends the session and closes the socket.

Session

These fields are the session object inside session.update.

FieldTypeDescription
speak.textRequiredstringThe words to speak. Same field on every live TTS model.
session.speed
Default 1
numberPiper and Matcha only. 0.5 to 2. Default 1. Omit on Pocket.
session.voice
Default alba
stringPocket TTS Realtime only. One of 26 speakers. Default alba. Omit on Piper and Matcha.

Server events

FieldTypeDescription
session.createdobjectThe socket is open. session.id, session.model, and session.prepaid_seconds.
binary framebytesLittle-endian PCM16, mono. A JSON frame carries sample_rate before the audio.
response.doneobjectThe current sentence has finished.
errorobjecterror.type, error.code, error.message, and error.param. The socket then closes.

Billing

A session is charged $0.05 plus the audio produced, at $0.00333 per minute. 60 seconds of audio is one minute. 61 seconds is two minutes. The text length is checked before the sentence starts, at about 14 characters per second. A session runs for up to 5 hours of audio. The socket closes after 30 seconds without a message.

Example

const socket = new WebSocket(
  'wss://api.pascalai.co/v1/text-to-speech/realtime?model=sarvam-bulbul-live&api_key=' + key
);
socket.binaryType = 'arraybuffer';
socket.onmessage = (event) => {
  if (typeof event.data !== 'string') return;
  const payload = JSON.parse(event.data);
  if (payload.type === 'session.created') {
    socket.send(JSON.stringify({ type: 'session.update', session: { speed: 1 } }));
    socket.send(JSON.stringify({
    type: 'speak',
    text: 'Keep walking toward the river.',
  }));
  }
};
// binary Int16 PCM16 frames, then response.done

Models