Inworld Text to Speech in Node.js
InworldTTS speaks the answers of the agent with Inworld TTS, for @micdrop/server. It streams the text to Inworld while the agent writes it, over a single WebSocket, and returns raw PCM at the rate of your choice.
Installation
npm install @micdrop/inworldUsage with MicdropServer
import { GladiaSTT } from '@micdrop/gladia'import { InworldTTS } from '@micdrop/inworld'import { OpenaiAgent } from '@micdrop/openai'import { MicdropServer } from '@micdrop/server'import { WebSocketServer } from 'ws'
const wss = new WebSocketServer({ port: 8081 })
wss.on('connection', (socket) => { new MicdropServer(socket, { stt: new GladiaSTT({ apiKey: process.env.GLADIA_API_KEY || '' }), agent: new OpenaiAgent({ apiKey: process.env.OPENAI_API_KEY || '', systemPrompt: 'You are a helpful assistant', }), tts: new InworldTTS({ apiKey: process.env.INWORLD_API_KEY || '', voiceId: 'Dennis', // Default voice modelId: 'inworld-tts-2', // Default model }), })})The API key is the one the Inworld portal shows in its Basic form, a base64 string, passed as is.
Usage without MicdropServer
import { InworldTTS } from '@micdrop/inworld'import { Readable } from 'stream'
const tts = new InworldTTS({ apiKey: process.env.INWORLD_API_KEY || '', voiceId: 'Ashley',})
tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))tts.on('Failed', (texts) => console.error('Failed:', texts))
tts.speak(Readable.from(['Hello! ', 'How can I help you today?']))Events
| Event | Payload | Description |
|---|---|---|
Audio | Buffer | A chunk of the voice, PCM 16 bits mono at sampleRate. |
Failed | string[] | The synthesis failed, with the text that was not spoken. |
See the TTS interface for the full contract.
Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your Inworld API key, in its Basic form |
voiceId | string | 'Dennis' | Voice, such as Dennis or Ashley, or the id of a custom voice |
modelId | string | 'inworld-tts-2' | inworld-tts-2, inworld-tts-2-flash, or a TTS 1.5 model |
sampleRate | number | 24000 | Rate of the audio, from 8000 to 48000 Hz |
language | string | undefined | Language of the text, as a BCP-47 code, detected when unset |
speakingRate | number | 1 | Speed of the voice, from 0.5 to 1.5 |
deliveryMode | string | 'BALANCED' | How freely inworld-tts-2 interprets the text: STABLE, BALANCED or CREATIVE |
temperature | number | 1 | Randomness of the voice, from 0 to 2, read by the TTS 1.5 models |
textNormalization | boolean | undefined | Reads numbers, dates and symbols as they are spoken, left to Inworld when unset |
connectionTimeout | number | 5000 | Time to open the connection, in milliseconds |
retryDelay | number | 1000 | Delay between two connection attempts, in milliseconds |
maxRetry | number | 3 | Failed attempts in a row before Failed |
Models
inworld-tts-2 gives the most natural voice, and keeps a consistent delivery across the sentences of an answer. inworld-tts-2-flash starts sooner and costs less. Both find the sentences in the text themselves, so InworldTTS hands them the words as the agent writes them. The TTS 1.5 models get a flush at the end of each sentence instead.
Answers and Interruptions
Each answer gets a context of its own on the WebSocket, closed when the agent is done writing, and Inworld speaks what is left of the text before it ends. When the user interrupts, the context is cancelled: Inworld stops the synthesis and drops the text not spoken yet, which is not billed.
A new context receives the text of the last answers, up to five of them, so that Inworld detects the language and reads short answers in their context. A connection that drops mid answer is opened again, and the context is created again with the text read so far. After maxRetry failed attempts in a row, Failed hands the text back, for a FallbackTTS to speak it.
Listing Voices
InworldTTS.listVoices() lists the voices of Inworld and those of the account:
const voices = await InworldTTS.listVoices({ apiKey: process.env.INWORLD_API_KEY || '', language: 'fr', // Optional})It returns MicdropVoice objects with id, name, languages, gender, description, and custom for a voice of the account. The list is fetched once, then shared by every call of the process for a day. Pass refresh: true to fetch it again.