Implementation:0xsline OpenChatCut NativeAsrWorker
| Knowledge Sources | |
|---|---|
| Domains | Transcription, Speech_Recognition, Desktop |
| Last Updated | 2026-10-22 14:00 GMT |
Overview
Concrete desktop worker that runs whisper.cpp (ggml) speech recognition via a persistent `whisper-server` (model loaded once) with `whisper-cli` spawn fallback, replacing the older transformers.js ONNX pipeline on the desktop path.
Description
The NativeAsrWorker runs in an Electron `UtilityProcess` and provides on-device speech recognition:
- Persistent whisper-server: Spawns a `whisper-server` process that loads the ggml model once and serves transcription requests over a local TCP socket. This avoids re-loading the model for each transcription.
- whisper-cli fallback: If the whisper-server fails to start (timeout 20s), falls back to spawning `whisper-cli` per request (timeout 45 minutes).
- Model management: Uses `resolveGgmlPath` to find the cached ggml model file. Tracks the loaded engine (model ID, revision, ggml path, backend).
- Audio processing: Extracts PCM from the source audio via `extractPcm` at 16 kHz sample rate. Writes WAV format for whisper input.
- Result parsing: Converts whisper.cpp JSON output to `DesktopAsrChunk[]` via `whisperWordsToChunks` or `whisperTokensToChunks`.
- Lifecycle: `NativeAsrWorkerLifecycle` manages initialization, request queuing, and shutdown.
This is a Wrapper Doc — it wraps whisper.cpp (a C++ binary) with Node.js process management, audio preprocessing, and result normalization.
Usage
- Desktop transcription: The `NativeAsrService` (in `desktop/native-asr-service.ts`) spawns this worker via Electron `UtilityProcess` and forwards transcription requests.
- Offline ASR: Provides on-device transcription without cloud API calls.
Code Reference
Source Location
- Repository: OpenChatCut
- File:*** desktop/native-asr-worker.ts
Signature
interface NativeWorkerData {
readonly cacheDir: string;
readonly ffmpegPath: string;
readonly whisperCliPath: string;
readonly platform: NodeJS.Platform;
}
interface LoadedEngine {
readonly modelId: string;
readonly revision: string;
readonly ggmlPath: string;
readonly backend: DesktopAsrBackend;
}
// Worker message protocol (via Electron parentPort):
// Initialize: { type: 'init', cacheDir, ffmpegPath, whisperCliPath, platform }
// Transcribe: { type: 'transcribe', audio, model, language, ... }
// Preload: { type: 'preload', modelId, ... }
Import
// This is an Electron UtilityProcess worker — not imported directly.
// Spawned by desktop/native-asr-service.ts via `utilityProcess.fork()`.
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| cacheDir | string | Yes | Model cache directory |
| ffmpegPath | string | Yes | FFmpeg binary path for PCM extraction |
| whisperCliPath | string | Yes | whisper.cpp CLI binary path |
| platform | NodeJS.Platform | Yes | OS platform |
| audio | Buffer | Yes | Raw audio bytes to transcribe |
| model | string | Yes | ggml model ID (e.g., 'base.en') |
| language | string | Yes | Language code or 'auto' |
Outputs
| Name | Type | Description |
|---|---|---|
| DesktopAsrResponse | object | Transcription result with word-level chunks |
| DesktopModelLoadResponse | object | Model load status |
Usage Examples
Spawned by NativeAsrService
// In desktop/native-asr-service.ts (simplified):
import { utilityProcess } from 'electron';
const child = utilityProcess.fork('native-asr-worker.js');
child.postMessage({
type: 'init',
cacheDir: modelCachePath,
ffmpegPath: ffmpegBin,
whisperCliPath: whisperCliBin,
platform: process.platform,
});
// Later:
child.postMessage({ type: 'transcribe', audio: pcmBuffer, model: 'base.en', language: 'en' });
Related Pages
Implements Principle
Requires Environment
- Environment:0xsline_OpenChatCut_Electron
- Environment:0xsline_OpenChatCut_WhisperCpp
- Environment:0xsline_OpenChatCut_FFmpeg