Skip to content

Build your personal knowledge base with Trilium Notes

feat(desktop): transcribe with Silero VAD and Parakeet v3 (PoC)

Elian Doran2af39e274c8a
diff --git a/apps/desktop/src/services/transcription.ts b/apps/desktop/src/services/transcription.ts
index 59754a45cd..bee7983ae2 100644
--- a/apps/desktop/src/services/transcription.ts
+++ b/apps/desktop/src/services/transcription.ts
@@ -5,13 +5,16 @@ import fs from "fs";
import path from "path";
const SAMPLE_RATE = 16000;
+/** Silero VAD consumes 512-sample windows at 16 kHz. */
+const VAD_WINDOW_SIZE = 512;
const SAMPLES_CHANNEL = "transcription-poc-samples";
const MODEL_DIR = path.join(dataDirs.TRILIUM_DATA_DIR, "transcription-model");
/**
- * Proof of concept: records the default microphone in a hidden window and logs what a
- * sherpa-onnx streaming recognizer hears. Runs only when `<data dir>/transcription-model`
- * holds a streaming Zipformer transducer (`sherpa-onnx-streaming-zipformer-en-20M-2023-02-17`).
+ * Proof of concept: records the default microphone in a hidden window, splits it into utterances
+ * with Silero VAD and logs what Parakeet hears in each. Runs only when
+ * `<data dir>/transcription-model` holds `sherpa-onnx-nemo-parakeet-tdt-0.6b-v3-int8` plus
+ * `silero_vad.onnx`.
*/
export async function startTranscription() {
const log = getLog();
@@ -21,23 +24,46 @@ export async function startTranscription() {
}
const mod = await import("sherpa-onnx-node");
- const { OnlineRecognizer } = mod.default ?? mod;
- const recognizer = new OnlineRecognizer({
+ const { OfflineRecognizer, Vad } = mod.default ?? mod;
+ const recognizer = await OfflineRecognizer.createAsync({
featConfig: { sampleRate: SAMPLE_RATE, featureDim: 80 },
modelConfig: {
transducer: {
- encoder: path.join(MODEL_DIR, "encoder-epoch-99-avg-1.int8.onnx"),
- decoder: path.join(MODEL_DIR, "decoder-epoch-99-avg-1.onnx"),
- joiner: path.join(MODEL_DIR, "joiner-epoch-99-avg-1.int8.onnx")
+ encoder: path.join(MODEL_DIR, "encoder.int8.onnx"),
+ decoder: path.join(MODEL_DIR, "decoder.int8.onnx"),
+ joiner: path.join(MODEL_DIR, "joiner.int8.onnx")
},
tokens: path.join(MODEL_DIR, "tokens.txt"),
+ modelType: "nemo_transducer",
numThreads: 2,
provider: "cpu"
},
- decodingMethod: "greedy_search",
- enableEndpoint: true
+ decodingMethod: "greedy_search"
});
- const stream = recognizer.createStream();
+ const vad = new Vad({
+ sileroVad: {
+ model: path.join(MODEL_DIR, "silero_vad.onnx"),
+ threshold: 0.5,
+ minSilenceDuration: 0.5,
+ minSpeechDuration: 0.25,
+ maxSpeechDuration: 20,
+ windowSize: VAD_WINDOW_SIZE
+ },
+ sampleRate: SAMPLE_RATE,
+ numThreads: 1,
+ provider: "cpu"
+ }, 60);
+
+ // Utterances are decoded one at a time so the log keeps their order.
+ let decoding = Promise.resolve();
+ const transcribe = async (start: number, samples: Float32Array) => {
+ const stream = recognizer.createStream();
+ stream.acceptWaveform({ samples, sampleRate: SAMPLE_RATE });
+ const text = (await recognizer.decodeAsync(stream)).text.trim();
+ if (text) {
+ log.info(`Transcription [${formatOffset(start)}]: ${text}`);
+ }
+ };
const captureWindow = await createCaptureWindow();
@@ -47,17 +73,16 @@ export async function startTranscription() {
}
try {
- stream.acceptWaveform({ samples, sampleRate: SAMPLE_RATE });
- while (recognizer.isReady(stream)) {
- recognizer.decode(stream);
+ for (let i = 0; i + VAD_WINDOW_SIZE <= samples.length; i += VAD_WINDOW_SIZE) {
+ vad.acceptWaveform(samples.subarray(i, i + VAD_WINDOW_SIZE));
}
- if (recognizer.isEndpoint(stream)) {
- const text = recognizer.getResult(stream).text.trim();
- if (text) {
- log.info(`Transcription: ${text}`);
- }
- recognizer.reset(stream);
+ while (!vad.isEmpty()) {
+ const segment = vad.front(false);
+ vad.pop();
+ decoding = decoding
+ .then(() => transcribe(segment.start, segment.samples))
+ .catch((e) => log.error(`Transcription failed: ${e}`));
}
} catch (e) {
log.error(`Transcription failed: ${e instanceof Error ? e.message : String(e)}`);
@@ -67,6 +92,11 @@ export async function startTranscription() {
log.info("Transcription: listening to the microphone.");
}
+function formatOffset(sampleIndex: number) {
+ const seconds = Math.floor(sampleIndex / SAMPLE_RATE);
+ return `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
+}
+
/**
* The window runs only {@link CAPTURE_PAGE}, a local file, so it gets Node integration to reach
* `ipcRenderer` without a preload. Its in-memory session grants the microphone and nothing else.
diff --git a/apps/desktop/src/sherpa-onnx-node.d.ts b/apps/desktop/src/sherpa-onnx-node.d.ts
index fac8342b9f..4811be0795 100644
--- a/apps/desktop/src/sherpa-onnx-node.d.ts
+++ b/apps/desktop/src/sherpa-onnx-node.d.ts
@@ -1,15 +1,19 @@
declare module "sherpa-onnx-node" {
- export interface OnlineStream {
+ export interface OfflineStream {
acceptWaveform(wave: { samples: Float32Array; sampleRate: number }): void;
}
- export class OnlineRecognizer {
- constructor(config: object);
- createStream(): OnlineStream;
- isReady(stream: OnlineStream): boolean;
- decode(stream: OnlineStream): void;
- isEndpoint(stream: OnlineStream): boolean;
- reset(stream: OnlineStream): void;
- getResult(stream: OnlineStream): { text: string };
+ export class OfflineRecognizer {
+ static createAsync(config: object): Promise<OfflineRecognizer>;
+ createStream(): OfflineStream;
+ decodeAsync(stream: OfflineStream): Promise<{ text: string }>;
+ }
+
+ export class Vad {
+ constructor(config: object, bufferSizeInSeconds: number);
+ acceptWaveform(samples: Float32Array): void;
+ isEmpty(): boolean;
+ front(enableExternalBuffer: boolean): { start: number; samples: Float32Array };
+ pop(): void;
}
}