Speechmatics Real-Time Speech to Text in Node.js
SpeechmaticsSTT transcribes the user with the Speechmatics Realtime API, in 55+ languages, for @micdrop/server.
Its sessions run on the EU region by default, and the same code reaches a Speechmatics container hosted on your own servers. With SpeechmaticsSTT as the only component, the server transcribes the call without answering.
Installation
npm install @micdrop/speechmaticsUsage with MicdropServer
import { MistralAgent } from '@micdrop/mistral'import { MicdropServer } from '@micdrop/server'import { SpeechmaticsSTT } from '@micdrop/speechmatics'import { GradiumTTS } from '@micdrop/gradium'import { WebSocketServer } from 'ws'
const wss = new WebSocketServer({ port: 8081 })
wss.on('connection', (socket) => { new MicdropServer(socket, { stt: new SpeechmaticsSTT({ apiKey: process.env.SPEECHMATICS_API_KEY || '', language: 'fr', }), agent: new MistralAgent({ apiKey: process.env.MISTRAL_API_KEY || '', systemPrompt: 'Tu es un assistant vocal. Réponds en une ou deux phrases.', settings: { reasoningEffort: 'none' }, }), tts: new GradiumTTS({ apiKey: process.env.GRADIUM_API_KEY || '', voiceId: process.env.GRADIUM_VOICE_ID || '', region: 'eu', }), })})The client connects to this server and streams the microphone.
Usage without MicdropServer
import { SpeechmaticsSTT } from '@micdrop/speechmatics'import { createReadStream } from 'fs'
const stt = new SpeechmaticsSTT({ apiKey: process.env.SPEECHMATICS_API_KEY || '',})
stt.on('Transcript', (text) => console.log('Transcript:', text))stt.on('Failed', (chunks) => console.error('Failed:', chunks.length, 'chunks'))
// Audio is raw PCM, 16 bits, 16 kHz, monostt.transcribe(createReadStream('speech.pcm'))Events
| Event | Payload | Description |
|---|---|---|
Transcript | string | Transcription of one utterance. The text is empty when nothing was recognized. |
Failed | Buffer[] | Transcription gave up after its retries, with the audio chunks left pending. |
See the STT interface for the full contract.
Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | Required | Your Speechmatics API key |
region | 'eu' | 'us' | 'au' | 'global' | 'eu' | Region of the endpoint. global may route the session to any region |
url | string | Optional | Address of a Speechmatics container on your own servers, in place of the region |
language | string | 'en' | Language of the speech, a pack such as cmn_en, or multi |
model | string | 'enhanced' | standard, enhanced, melia-1 or oak-1 |
maxDelay | number | 0.7 | Longest delay before a word is final, in seconds, from 0.7 to 4 |
settings | DeepPartial<SpeechmaticsTranscriptionConfig> | Optional | Any other field of transcription_config, which wins over the above |
connectionTimeout | number | 5000 | Time to open the session, in milliseconds |
transcriptionTimeout | number | 4000 | Time an utterance waits for its transcript once it ended, in milliseconds |
retryDelay | number | 1000 | Delay between two connection attempts, in milliseconds |
maxRetry | number | 3 | Failed attempts in a row before Failed |
How an Utterance Ends
Each stream given to transcribe() is one utterance and gets one Transcript event. When the stream ends, SpeechmaticsSTT sends ForceEndOfUtterance: Speechmatics finalizes every word heard so far at once, and answers with an EndOfUtterance that releases the transcript. The transcript comes as soon as the turn of the user closes, without waiting for maxDelay nor for a silence on the Speechmatics side.
Speechmatics finalizes the words in the order they were heard, so a second utterance that starts before the first transcript keeps its own words. A stream that carries no audio leaves no utterance behind, and an utterance Speechmatics never ends is emitted after transcriptionTimeout with the words it finalized.
Models and Languages
The standard and enhanced models transcribe one language, given as an ISO code such as en, fr, de or cmn. Bilingual packs such as cmn_en or ar_en follow speakers who switch between two languages, and Spanish with settings: { domain: 'bilingual-en' } follows English mixed in.
melia-1 and oak-1 transcribe 55+ languages in the same session, with language: 'multi', which SpeechmaticsSTT sets for them. They refuse max_delay, so it is left out of their session.
const stt = new SpeechmaticsSTT({ apiKey: process.env.SPEECHMATICS_API_KEY || '', model: 'melia-1', settings: { language_hints: ['fr', 'en'] },})Custom Vocabulary and Other Settings
settings takes any field of the transcription config, such as the words to recognize, a domain or the spelling of the output:
const stt = new SpeechmaticsSTT({ apiKey: process.env.SPEECHMATICS_API_KEY || '', language: 'en', settings: { output_locale: 'en-GB', additional_vocab: [ 'Micdrop', { content: 'Speechmatics', sounds_like: ['speech matics'] }, ], domain: 'finance', },})The Micdrop turns end each utterance, so the silence trigger of Speechmatics (conversation_config.end_of_utterance_silence_trigger) stays off. When it is set, the ends it finds split the speech without closing the utterance.
Data Residency
The eu region keeps the audio in Speechmatics’ EU data centers, and Speechmatics keeps no audio nor transcript of a real-time session once it ends. The global endpoint may route a session to any region, outside Europe included. Speechmatics also ships its engine as a container for your own servers: pass its address as url.
Connection and Retries
SpeechmaticsSTT opens one session per call and keeps it between the turns, with a ping every minute so that Speechmatics does not close it while nobody speaks. A session that drops is opened again, and the audio of every utterance still waiting for its transcript goes out again to the new one. After maxRetry failed attempts in a row, Failed hands that audio back, for a FallbackSTT to transcribe it, and the next utterance tries to connect again.