🎤Micdrop

Deepgram Aura-2 Text to Speech in Node.js

DeepgramTTS from @micdrop/deepgram speaks the answers of a @micdrop/server voice agent with the Aura-2 voices of Deepgram. It streams the text over the Deepgram WebSocket while the LLM writes it, and sends the audio back as raw PCM. Aura-2 speaks English, Spanish, Dutch, French, German, Italian and Japanese. The same package holds Nova-3 and Flux speech to text.

Installation

Terminal window
npm install @micdrop/server @micdrop/deepgram ws

Create an API key in the Deepgram console and set it as DEEPGRAM_API_KEY.

A complete voice agent with Deepgram

This server answers every browser that connects on port 8081, with Deepgram on both ends of the call: Nova-3 transcribes the user and Aura-2 speaks the answers an OpenAI model writes.

import { DeepgramSTT, DeepgramTTS } from '@micdrop/deepgram'
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, {
firstMessage: 'Hello, how can I help you today?',
stt: new DeepgramSTT({
apiKey: process.env.DEEPGRAM_API_KEY || '',
}),
agent: new OpenaiAgent({
apiKey: process.env.OPENAI_API_KEY || '',
systemPrompt: 'You are a friendly assistant. Answer in one or two sentences.',
}),
tts: new DeepgramTTS({
apiKey: process.env.DEEPGRAM_API_KEY || '',
voice: 'aura-2-thalia-en',
}),
})
})

The browser starts the call with @micdrop/web:

import { Micdrop } from '@micdrop/web'
button.onclick = () => Micdrop.start({ url: 'ws://localhost:8081' })

Voices

Each Aura-2 voice is a model whose name ends with its language: aura-2-thalia-en, aura-2-agathe-fr, aura-2-celeste-es, aura-2-julius-de, aura-2-melia-it, aura-2-beatrix-nl or aura-2-uzume-ja. listVoices() gives every one of them, with its gender, its accent and a sample:

import { DeepgramTTS } from '@micdrop/deepgram'
const voices = await DeepgramTTS.listVoices({
apiKey: process.env.DEEPGRAM_API_KEY || '',
language: 'es',
})
// [{ id: 'aura-2-celeste-es', name: 'Celeste', gender: 'female', ... }, ...]

The list is fetched once and shared by every call of the process for a day.

Options

OptionTypeDefaultDescription
apiKeystringRequiredYour Deepgram API key
voicestring'aura-2-thalia-en'Aura model of the voice
sampleRatenumber24000Rate of the audio in Hz: 8000, 16000, 24000, 32000 or 48000
speednumber1Speed of the voice, from 0.7 to 1.5
flushCharsnumber200Characters gathered before they are spoken, once the first sentence was
mipOptOutbooleanOptionalOpts out of the Deepgram Model Improvement Program
paramsRecord<string, ...>OptionalAny other query parameter of the endpoint, sent as is
apiUrlstringDeepgramAddress of a dedicated or self-hosted deployment, wss://api.deepgram.com by default
connectionTimeoutnumber5000Milliseconds a connection attempt may take
retryDelaynumber1000Milliseconds between two connection attempts
maxRetrynumber3Failed attempts in a row before Failed

A rate, a speed or a flushChars out of range throws when the instance is created.

How the answer is spoken

Deepgram synthesizes the text it holds when it receives a flush, and takes 20 flushes a minute on a connection. DeepgramTTS sends the answer sentence by sentence as the agent writes it, and flushes the first sentence alone, so the user hears it while the rest is being written. The next sentences are flushed in groups of flushChars characters, and whatever remains at the end of the answer. A long answer thus costs a handful of flushes, well within the limit.

When the user interrupts, cancel() sends Clear, which drops the text and the audio Deepgram holds, and the audio still on its way is ignored until Deepgram confirms. A new answer that starts while the previous one is still spoken clears it the same way.

Usage without MicdropServer

import { DeepgramTTS } from '@micdrop/deepgram'
import { Readable } from 'stream'
const tts = new DeepgramTTS({
apiKey: process.env.DEEPGRAM_API_KEY || '',
voice: 'aura-2-thalia-en',
})
// Audio is raw PCM, 16 bits, mono, at tts.sampleRate
tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))
tts.on('Failed', (texts) => console.error('Failed:', texts))
tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))

Events

EventPayloadDescription
AudioBufferA chunk of audio, PCM 16 bits, mono, at tts.sampleRate, ready to be played.
Failedstring[]Every reconnection attempt failed, with the text that stayed unspoken. The next answer connects again.

See the TTS interface for the full contract.

Connection and retries

The connection opens with the instance, so the first answer does not wait for it. A connection that drops in the middle of an answer opens again, and the text of that answer goes to the new one. After maxRetry failed attempts in a row, Failed hands the text back, ready for a FallbackTTS to speak it with another provider, and the next answer tries to connect again. A connection Deepgram closes while nothing is being said stays closed until the next answer opens a new one.