Skip to main content

createSTT()

Creates an offline speech-to-text engine instance for transcribing complete audio files or buffers. Call destroy() when done to free native resources.
function createSTT(
  options: STTInitializeOptions | ModelPathConfig
): Promise<SttEngine>

Parameters

options
STTInitializeOptions | ModelPathConfig
required
Configuration object or simple model path. See Configuration for full options.

Returns

Promise resolving to an SttEngine instance.

Example

const stt = await createSTT({
  modelPath: { type: 'asset', path: 'models/whisper-tiny' },
  modelType: 'whisper',
  modelOptions: {
    whisper: {
      language: 'en',
      task: 'transcribe'
    }
  }
});

const result = await stt.transcribeFile('/path/to/audio.wav');
console.log(result.text);

await stt.destroy();

SttEngine

Offline STT engine interface returned by createSTT().

Properties

instanceId
string
Unique identifier for this engine instance.

Methods

transcribeFile()

Transcribe audio from a file path.
transcribeFile(filePath: string): Promise<SttRecognitionResult>
filePath
string
required
Absolute path to audio file (WAV, FLAC, etc.).
Returns: Promise resolving to SttRecognitionResult. Example:
const result = await stt.transcribeFile('/sdcard/recording.wav');
console.log('Text:', result.text);
console.log('Tokens:', result.tokens);
console.log('Language:', result.lang);

transcribeSamples()

Transcribe audio from PCM samples in memory.
transcribeSamples(
  samples: number[],
  sampleRate: number
): Promise<SttRecognitionResult>
samples
number[]
required
PCM audio samples as float values in range [-1, 1].
sampleRate
number
required
Sample rate in Hz (e.g. 16000, 44100).
Returns: Promise resolving to SttRecognitionResult. Example:
const samples = new Float32Array(audioBuffer);
const result = await stt.transcribeSamples(
  Array.from(samples),
  16000
);
console.log(result.text);

setConfig()

Update runtime configuration (hotwords, decoding method, etc.) without recreating the engine.
setConfig(config: SttRuntimeConfig): Promise<void>
config
SttRuntimeConfig
required
Runtime configuration options. See SttRuntimeConfig.
Example:
await stt.setConfig({
  decodingMethod: 'modified_beam_search',
  maxActivePaths: 8,
  hotwordsFile: '/path/to/hotwords.txt',
  hotwordsScore: 2.0
});

destroy()

Release native resources. The engine cannot be used after calling this method.
destroy(): Promise<void>
Example:
await stt.destroy();

detectSttModel()

Detect STT model type and structure without initializing the recognizer. Stateless utility function.
function detectSttModel(
  modelPath: ModelPathConfig,
  options?: { preferInt8?: boolean; modelType?: STTModelType }
): Promise<{
  success: boolean;
  detectedModels: Array<{ type: string; modelDir: string }>;
  modelType?: string;
}>

Parameters

modelPath
ModelPathConfig
required
Model path configuration (asset, file, or auto).
options
object
Optional detection options.

Returns

Object with detection results:
  • success: Whether detection succeeded
  • detectedModels: Array of detected model types and directories
  • modelType: Primary detected model type

Example

const result = await detectSttModel({
  type: 'asset',
  path: 'models/sherpa-onnx-whisper-tiny-en'
});

if (result.success && result.detectedModels.length > 0) {
  console.log('Detected:', result.modelType);
  console.log('Models:', result.detectedModels);
}

Build docs developers (and LLMs) love