🎤Micdrop

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

Terminal window
npm install @micdrop/grok @micdrop/openai

The 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

EventPayloadDescription
AudioBufferA chunk of the voice, PCM 16 bits mono at sampleRate.
Failedstring[]The synthesis failed, with the text that was not spoken.

See the TTS interface for the full contract.

Options

OptionTypeDefaultDescription
apiKeystringRequiredYour xAI API key
voicestring'eve'eve, ara, leo, rex, sal and twenty more, or a custom voice id
languagestring'auto'Language of the text, as a BCP-47 code such as en, fr or pt-BR
sampleRatenumber24000Rate of the audio: 8000, 16000, 22050, 24000, 44100 or 48000 Hz
speednumber1Speed of the voice, from 0.7 to 1.5
textNormalizationbooleanfalseReads numbers, abbreviations and symbols as they are spoken
optimizeStreamingLatencybooleantrueSends a shorter first chunk so the voice starts sooner, false favors quality
connectionTimeoutnumber5000Time to open the connection, in milliseconds
retryDelaynumber1000Delay between two connection attempts, in milliseconds
maxRetrynumber3Failed 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.