Piper TTS in Node.js
Piper TTS is an open source text to speech engine that runs offline on a CPU.
Each voice is a small VITS model of a few dozen megabytes, for one language and
one speaker. The project lists 178 voices in 53 languages, French, German and Spanish among
them. @micdrop/piper drives the
Piper binary as a subprocess of
@micdrop/server, so a Node voice agent speaks any of these
languages with no API key.
Piper starts speaking about 390 ms after it receives a sentence and generates much faster than real time, even on a modest CPU. With its 53 languages, it is the local voice to pick for a language beyond the five of Phonon, and for a commercial product in a language other than English, since the Phonon models for those languages are free for non-commercial use only. Kokoro sounds better in English, the only language it speaks in Micdrop. Piper only reads text aloud, so pair it with Whisper to transcribe the user’s speech locally as well.
Installation
Install the package:
npm install @micdrop/piperInstall the binary, which needs Python. In a container, add a Python interpreter to the image and run the same command:
pip install piper-ttsThen download a voice. It comes as a pair of files that have to sit next to each other:
mkdir -p voices && cd voicesBASE=https://huggingface.co/rhasspy/piper-voices/resolve/main/fr/fr_FR/siwis/mediumcurl -LO $BASE/fr_FR-siwis-medium.onnxcurl -LO $BASE/fr_FR-siwis-medium.onnx.jsonPiper ships a download_voices module, but calling it means finding the exact
interpreter pip installed it under, which is rarely the python3 in your
path. Downloading the two files is shorter and gives the same result.
Usage with MicdropServer
If you have no Micdrop server yet, set up a first voice call and come back to swap its voice for Piper.
import { PiperTTS } from '@micdrop/piper'import { MicdropServer } from '@micdrop/server'
const tts = new PiperTTS({ modelPath: './voices/fr_FR-siwis-medium.onnx',})
// Use with MicdropServernew MicdropServer(socket, { tts, // ... other options})Usage without MicdropServer
import { PiperTTS } from '@micdrop/piper'import { Readable } from 'stream'
const tts = new PiperTTS({ modelPath: './voices/fr_FR-siwis-medium.onnx',})
// Audio is raw PCM, 16 bits, 16 kHz, monotts.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
| Event | Payload | Description |
|---|---|---|
Audio | Buffer | A chunk of audio, PCM 16 bits, 16 kHz, mono, ready to be played. |
Failed | string[] | The Piper process exited with an error, or the binary cannot be run (reported once). The payload stays empty. |
See the TTS interface for the full contract.
Options
| Option | Type | Default | Description |
|---|---|---|---|
modelPath | string | Required | Path to the .onnx voice file |
configPath | string | <modelPath>.json | Voice configuration |
binaryPath | string | 'piper' in PATH | Piper executable |
speaker | number | Optional | Speaker index, for voices with several speakers |
lengthScale | number | Optional | Duration multiplier, above 1 to slow the voice down |
noiseScale | number | Optional | Variability of the generated speech |
noiseWidth | number | Optional | Variability of the phoneme durations |
sentenceSilence | number | Optional | Silence added after each sentence, in seconds |
volume | number | Optional | Volume multiplier, 1 keeps the voice’s own level |
Piper TTS voices
Voices are named after their language, their speaker and their quality, such as
fr_FR-siwis-medium or en_GB-alba-medium. You can
listen to every Piper voice before
downloading it.
Piper has five French voices, siwis, upmc, tom, mls and gilles, each under its own license. We compared those licenses and Piper’s latency with Gradium’s Phonon and the hosted APIs to see which French text to speech engine suits a voice agent.
Every voice lives in the rhasspy/piper-voices repository on Hugging Face, under a path built from the same parts as its name:
<language>/<locale>/<speaker>/<quality>/<locale>-<speaker>-<quality>.onnxSo the British voice en_GB-alba-medium sits at
en/en_GB/alba/medium/en_GB-alba-medium.onnx, and its configuration at the
same path with .json appended. To download it, point the BASE line of the
installation commands at that folder.
PiperTTS needs both <name>.onnx and <name>.onnx.json. The JSON file
gives the sample rate the model generates at, 16 kHz for the low voices and 22.05 kHz for the others, along with the number of
speakers. Micdrop reads that file at startup and resamples the audio to the
16 kHz the client plays. For a voice with several speakers, pick one with the
speaker option.
What Piper TTS costs to run
Piper costs nothing per minute, since it runs on your own machine. It uses time and memory instead, measured here on the CPU of a MacBook Pro M2:
| Measure | Piper |
|---|---|
| First sound, voice loaded | ~390 ms |
| Loading the voice | ~1 s |
| Three sentences in one process | About the same time as one sentence |
| Memory | About the size of the voice file |
Three sentences take about as long as one because loading the voice takes up
almost all of that time. The Piper process holds about as much memory as its
voice file, a few dozen megabytes for a medium voice, outside the Node
process. With the transcription and the first tokens of a local LLM added, a
fully local call with Piper answers in about a
second.
One process for the whole call
Loading a voice takes about a second, while synthesizing a sentence adds almost
nothing on top of it. PiperTTS starts the process as soon as you construct it
and keeps it alive across sentences, so the voice loads once per call.
When the user interrupts while Piper is still writing audio, PiperTTS kills
the process to cut that audio at once, then starts a new one right away. The new
process loads its voice while the user is still speaking and is ready by the
time the next answer begins. A process that has finished writing, quiet for a
second, holds nothing back and stays as it is.
When the binary cannot be found or run, PiperTTS emits Failed once and
leaves the process stopped for the rest of the call.
The whole answer is usually ready before the user has finished hearing its first sentence.
Piper TTS vs the hosted providers
Piper starts speaking about as fast as the hosted voices. ElevenLabs takes around 400 ms and Gradium around 300 ms. Only Cartesia answers clearly sooner, around 150 ms. Piper is free, while their entry prices run from $1.80 to $3.60 per hour of audio.
Piper’s VITS models sound flatter than the hosted voices. Pick Piper when the audio has to stay on your machine, when the call runs offline, or when the volume makes an hourly bill add up. Pick a hosted voice when its quality matters more to your users than the cost. You can compare Piper with every text to speech provider Micdrop supports on latency, voices and price.
Documentation
Piper also works in a voice agent that runs entirely on local models, next to a local LLM and local speech to text. For local text to speech in Node, Piper, Kokoro and Pocket TTS differ on latency, memory and languages.
License
The Piper engine is released under GPL-3.0 since it embedded espeak-ng. The
archived rhasspy/piper repository was MIT. Micdrop drives the binary as a
separate process. If you redistribute that binary yourself, its GPL terms come
with it. Each voice carries its own license, some of them restrictive, so read
the model card of the voice you pick.