Skip to content

Run Whisper and Piper locally from TypeScript

Transcribe with whisper.cpp and speak with Piper on your own machine from TypeScript, with no account and no audio leaving the host, using local() from VoiceAI SDK.

To run Whisper and Piper locally from TypeScript, start whisper.cpp and Piper as child processes and pipe audio through them. local() from VoiceAI SDK does that for you and puts both behind the same session API as a cloud provider: transcribe and listen go to Whisper, and speak goes to Piper. Once the models are downloaded, no audio leaves the machine.

VoiceAI SDK is a developer preview, and the examples are written against its planned 1.0 API. They were not timed or benchmarked. How fast Whisper and Piper run depends on your hardware and the models you pick, so this post gives no speed figures.

Local and cloud at a glance

local()A cloud provider, such as deepgram()
Account and keyNoneAn API key on your server
Where audio goesOver pipes to two processes on the same machineTo the provider's API
Setupwhisper-cli and piper on PATH; models downloaded on first useSet the key
SpeedDepends on your hardware and model sizeDepends on the provider and the network
Partial transcriptsFrom re-running the model on a sliding window, less oftenDepends on the provider
Speaker labelsNo; diarization fails with unsupportedDepends on the provider
Speech-to-speechNo; always Whisper, then your handler, then PiperOnly with openai()

Install the binaries and check them

The SDK doesn't bundle the programs. Install whisper-cli from whisper.cpp and piper, and put both on PATH, or pass their locations as whisperPath and piperPath. Then let doctor check them, and try both directions from the CLI:

npm install voiceai-sdk
npx voiceai-sdk doctor
npx voiceai-sdk transcribe hello.wav --provider local
npx voiceai-sdk say "The build passed." --provider local --voice en_US-amy-medium --out passed.wav

Models are downloaded to modelDir on first use: ~/.cache/voiceai-sdk by default, or the path in VOICEAI_MODEL_DIR. The first run takes longer for that reason.

Transcribe with Whisper

import { readFile } from "node:fs/promises";
import { createSession } from "voiceai-sdk";
import { local } from "voiceai-sdk/local";

await using session = await createSession(local({ model: "base.en" }));

const result = await session.transcribe(await readFile("standup.wav"));

if (!result.ok) {
  console.log("nothing recognized:", result.reason);
} else {
  for (const word of result.words) console.log(`${word.startMs} ms  ${word.text}`);
}

base.en is the model from the SDK's examples. Larger Whisper models are more accurate and slower, and which one fits depends on your machine. Incoming audio is resampled to 16 kHz mono PCM before it reaches Whisper. threads sets how many CPU threads the model uses; the default is half the cores.

Speak with Piper

speak sends text to Piper and returns audio. listVoices lists the Piper voices installed in modelDir, and voice picks one.

import { createSession, listVoices } from "voiceai-sdk";
import { local } from "voiceai-sdk/local";

const provider = local({ voice: "en_US-amy-medium" });
console.log((await listVoices(provider)).map((v) => v.id));

await using session = await createSession(provider);

const speech = await session.speak("The nightly build finished. Two tests failed.");
console.log(speech.ok, speech.durationMs, speech.characters);

Piper produces raw PCM, and the SDK wraps it in the format you ask for with format. Iterate the Speech instead of awaiting it to get chunks as Piper produces them. To write a file from the command line, use say --out as above.

Hold a conversation offline

local() streams in both directions, so converse works the same as on a cloud provider. This server takes 16 kHz PCM over a WebSocket and answers each sentence:

import { createSession } from "voiceai-sdk";
import { local } from "voiceai-sdk/local";
import { webSocketAudio } from "voiceai-sdk/ws";
import { answer } from "./agent";

Bun.serve({
  port: 3000,
  fetch: (req, server) => (server.upgrade(req) ? undefined : new Response("upgrade required", { status: 426 })),
  websocket: {
    async open(ws) {
      await using session = await createSession(local({ model: "base.en", voice: "en_US-amy-medium" }), {
        sampleRate: 16000,
        encoding: "pcm16",
      });
      session.connect(webSocketAudio(ws));

      await session.say`Hi, I'm listening.`;
      await session.converse(answer);
    },
  },
});

answer is your converse handler. If it calls a model over the network, the speech stays local but the text doesn't. Keep that in mind if privacy is the reason you chose local().

Run it in CI

local() needs no keys, which makes it a fit for end-to-end checks on real recordings in CI. Small models are the practical choice on CPU-only runners. Cache the model directory between runs so the model isn't downloaded every time:

# Point the CI cache at this directory
export VOICEAI_MODEL_DIR="$PWD/.cache/voiceai-sdk"

npx voiceai-sdk doctor || exit 1
npx voiceai-sdk transcribe fixtures/hello.wav --provider local --json > hello.json

Keep unit tests on mock(), which runs in memory and needs no binaries, and use local() for the few checks that need real speech. See Testing voice agents without a microphone.

When a model won't load

session.native holds the two child processes. Read session.native.whisper.stderr to see why whisper.cpp failed, such as a missing model file. doctor exits with 1 when a provider you configured can't connect, so it is a quick first check in a CI job.

Common questions

Does any audio leave my machine? Not with local(). After the models are downloaded, audio goes over pipes to two processes on the same host. The SDK has no service of its own in the path.

Can Whisper label speakers here? No. whisper.cpp doesn't label speakers, so diarization fails with unsupported. Use a cloud provider that supports speaker labels if you need them.

Which Whisper model should I use? Start with base.en from the examples, then try larger ones on your own recordings. Larger models are more accurate and slower; whether the trade is worth it depends on your hardware and your audio.

Can I move to a cloud provider later? Yes. Change the import and the factory. Results have the same shape, so the code after createSession stays the same.

Next steps

  • Local (Whisper): every option of local(), and how it runs the binaries.
  • Quickstart: the same code in memory, on your machine, then on a cloud provider.
  • CLI: doctor, transcribe and say from the command line.

More guides on the VoiceAI SDK blog, or start with the quickstart.