Grok Text to Speech API in Node.js
GrokTTS speaks the answers of the agent with the Grok streaming text to speech by xAI, for @micdrop/server. It reads the text while the agent writes it, and returns raw PCM at the rate of your choice.
Installation
npm install @micdrop/grok @micdrop/openaiThe same package also provides Grok Voice, a model that hears and speaks, and a speech to text.
Usage with MicdropServer
import { GladiaSTT } from '@micdrop/gladia'import { GrokTTS } from '@micdrop/grok'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 GrokTTS({ apiKey: process.env.XAI_API_KEY || '', voice: 'eve', // Default voice language: 'en', // Default: 'auto' }), })})Usage without MicdropServer
import { GrokTTS } from '@micdrop/grok'import { Readable } from 'stream'
const tts = new GrokTTS({ apiKey: process.env.XAI_API_KEY || '', voice: 'ara',})
tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))tts.on('Failed', (texts) => console.error('Failed:', texts))
tts.speak(Readable.from(['Hello! [laugh] ', '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 xAI API key |
voice | string | 'eve' | eve, ara, leo, rex, sal and twenty more, or a custom voice id |
language | string | 'auto' | Language of the text, as a BCP-47 code such as en, fr or pt-BR |
sampleRate | number | 24000 | Rate of the audio: 8000, 16000, 22050, 24000, 44100 or 48000 Hz |
speed | number | 1 | Speed of the voice, from 0.7 to 1.5 |
textNormalization | boolean | false | Reads numbers, abbreviations and symbols as they are spoken |
optimizeStreamingLatency | boolean | true | Sends a shorter first chunk so the voice starts sooner, false favors quality |
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 |
Speech Tags
The text can direct the voice with inline tags such as [pause], [laugh] or [sigh], and wrapping tags such as <whisper>…</whisper> or <slow>…</slow>. An agent prompted to use them makes the answers more expressive. The xAI documentation lists them all.
Interruptions and Reconnection
The API speaks one answer at a time on a connection, and stops one only by closing it. When the user interrupts, GrokTTS closes the connection and opens a new one right away, so the next answer starts clear of the old audio. A connection that drops mid answer is opened again, and the text read so far goes out again. After maxRetry failed attempts in a row, Failed hands the text back, for a FallbackTTS to speak it.
Listing Voices
GrokTTS.listVoices() lists the voices of the API, which Grok Voice speaks with too:
const voices = await GrokTTS.listVoices({ apiKey: process.env.XAI_API_KEY || '', language: 'en', // Optional})It returns MicdropVoice objects with id, name and languages. The list is fetched once, then shared by every call of the process for a day. Pass refresh: true to fetch it again.