playTTS

View as Markdown

playTTS

Plays text-to-speech.

Parameters

params
objectRequired

Object containing the parameters for playing text-to-speech.

text
stringRequired

Text to play. SSML may be entered as a string wrapped in <speak> tags. See our supported voices and languages documentation for usage and supported tags.

language
stringDefaults to en-US

Language of the text in ISO 639-1 (language name) + ISO 3166 (country code). Supported languages can be found here.

gender
"male" | "female"Defaults to female

Gender of the voice.

voice
string

Voice to use (takes precedence over gender). Supported voices can be found here.

volume
numberDefaults to 0

Volume value between -40dB and +40dB where 0 is unchanged.

listen
object

Callback to listen for events. List of playback events can be found here. Example event: onStarted.

Returns

Promise<CallPlayback>

A promise that resolves to a CallPlayback object that you can use to view the current state and results of the play session.

Examples

In this example, we dial a phone number and play a TTS message. Once the TTS message is finished playing, we hangup the call.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Listen for incoming calls
8await voiceClient.listen({
9 topics: ["office"],
10 onCallReceived: async (call) => {
11 console.log("Call received");
12 // Answer the call
13 call.answer();
14 // Play TTS on the call
15 await call.playTTS({
16 text: "Hello, this is a test call from SignalWire",
17 listen: {
18 onStarted: () => console.log("TTS started"),
19 onFailed: () => console.log("TTS failed"),
20 onUpdated: (tts) => console.log("TTS state:", tts.state),
21 onEnded: () => {
22 console.log("TTS ended");
23 // Hangup the call
24 call.hangup();
25 }
26 }
27 }).onStarted();
28 }
29});

In this example, we are using SSML to play a TTS message. Once the TTS message is finished playing, we hangup the call.

1import { SignalWire } from "@signalwire/realtime-api";
2
3const client = await SignalWire({ project: "ProjectID Here", token: "Token Here" })
4
5const voiceClient = client.voice;
6
7// Listen for incoming calls
8await voiceClient.listen({
9 topics: ["office"],
10 onCallReceived: async (call) => {
11 console.log("Call received");
12 // Answer the call
13 call.answer();
14 // Play TTS on the call using SSML. Listens for playback events. Ends the call after the TTS is finished playing.
15 await call.playTTS({
16 text: `<speak>
17 Here are <say-as interpret-as="characters">SSML</say-as> samples.
18 I can pause <break time="3s"/>.
19 I can speak in cardinals. Your number is <say-as interpret-as="cardinal">10</say-as>.
20 Or I can speak in ordinals. You are <say-as interpret-as="ordinal">10</say-as> in line.
21 Or I can even speak in digits. The digits for ten are <say-as interpret-as="characters">10</say-as>.
22 I can also substitute phrases, like the <sub alias="World Wide Web Consortium">W3C</sub>.
23 Finally, I can speak a paragraph with two sentences.
24 <p><s>This is sentence one.</s><s>This is sentence two.</s></p>
25 </speak>
26 `,
27 voice: "polly.Joey",
28 listen: {
29 onStarted: () => console.log("TTS started"),
30 onFailed: () => console.log("TTS failed"),
31 onUpdated: (tts) => console.log("TTS state:", tts.state),
32 onEnded: () => {
33 console.log("TTS ended");
34 call.hangup();
35 }
36 }
37 }).onStarted();
38 }
39});