WhisperEngine

whisper.cpp speech-to-text engine.

Create instances with the WhisperEngine factory function. Engines are ordinary objects — never singletons — and any number of them (with the same or different models and backends) can coexist and transcribe concurrently: each owns its native whisper context and a dedicated single-threaded dispatcher (SerialExecutor) that serializes model loading, transcribe, reinitialize and the native release in close.

Audio crosses into native code zero-copy: 16 kHz mono FloatArrays are read in place by whisper.cpp; other rates / channel counts and ShortArray input are converted natively (pinned input, one output buffer).

Cancellation is cooperative and prompt: cancelling the coroutine that called transcribe sets the native abort flag, which whisper.cpp polls inside every ggml graph computation, so decoding stops within one graph step and the call throws CancellationException. The engine stays usable afterwards.

Live audio is transcribed with transcribeStream, which turns a Flow of PCM chunks into a Flow<TranscriptionEvent> without writing any audio to disk. Each collection owns its own native whisper_state, so streams, one-shot transcribe calls and other streams on the same engine never disturb each other's decoder state; their decodes are serialized on the engine's thread like every other native call.

Types

Link copied to clipboard
object Companion

Constants and factory functions of WhisperEngine.

Properties

Link copied to clipboard
open override val activeBackend: ComputeBackend

The backend whisper.cpp actually runs on (never ComputeBackend.AUTO).

Link copied to clipboard
open override val activeDeviceName: String?

The GPU whisper.cpp runs on, or null on the CPU (see InferenceEngine.activeDeviceName).

Link copied to clipboard
open override val backendConfig: BackendConfig

The configuration of the currently loaded model (see reinitialize).

Link copied to clipboard
open override val backendFallbackReason: String?

Why activeBackend is not the requested backend, or null (see InferenceEngine.backendFallbackReason).

Link copied to clipboard
open override val isClosed: Boolean

true once close has been called.

Link copied to clipboard

true while a transcribe call or a transcribeStream decode is executing native code.

Link copied to clipboard

Path of the loaded whisper ggml model (.bin).

Link copied to clipboard

Whisper-specific construction options.

Link copied to clipboard

whisper_context_params.use_gpu of the loaded model: always false for ComputeBackend.CPU_ONLY.

Functions

Link copied to clipboard

Requests the native abort of the transcription currently executing, if any; it then fails with InferenceCancelledException (a transcribeStream decode ends its stream with a terminal TranscriptionEvent.Error). Safe from any thread. Queued calls are not affected.

Link copied to clipboard
open override fun close()

Closes the engine: aborts a running transcription, rejects queued and future calls with IllegalStateException, ends running transcribeStream collections with a terminal TranscriptionEvent.Error (InferenceCancelledException), frees their native states and then the native context on the engine's thread, and stops that thread. Idempotent and safe from any thread.

Link copied to clipboard
open suspend override fun closeAndAwait()

closes the engine and suspends until its native memory has been freed on the engine thread (after a running call has been aborted and left native code). Idempotent; cancelling the caller stops the wait, not the release. See InferenceEngine.closeAndAwait.

Link copied to clipboard

Reloads the model with backendConfig on this engine's thread, after any transcription already queued. The old native context is freed before the new one is created, so GPU resources are released promptly (e.g. when switching to ComputeBackend.CPU_ONLY as an iOS app enters the background).

Link copied to clipboard
suspend fun transcribe(samples: FloatArray, sampleRate: Int = SAMPLE_RATE, channels: Int = 1, options: TranscriptionOptions = TranscriptionOptions.DEFAULT): TranscriptionResult

Transcribes interleaved float PCM in [-1, 1].

suspend fun transcribe(samples: ShortArray, sampleRate: Int = SAMPLE_RATE, channels: Int = 1, options: TranscriptionOptions = TranscriptionOptions.DEFAULT): TranscriptionResult

Transcribes interleaved signed 16-bit PCM (normalized as sample / 32768). The array is read in place by the native converter; see the FloatArray overload for the remaining semantics.

Link copied to clipboard
fun transcribeStream(audioFlow: Flow<FloatArray>, sampleRate: Int = SAMPLE_RATE, channels: Int = 1, options: StreamingOptions = StreamingOptions.DEFAULT): Flow<TranscriptionEvent>

Transcribes a live stream of interleaved float PCM chunks in [-1, 1] into a cold Flow of TranscriptionEvents. Nothing is written to disk: audio flows from audioFlow through the native streaming resampler (skipped for 16 kHz mono) into an in-memory ring buffer, a voice-activity turn detector, and whisper_full_with_state() on a whisper_state owned by this collection.

@JvmName(name = "transcribeStreamPcm16")
fun transcribeStream(audioFlow: Flow<ShortArray>, sampleRate: Int = SAMPLE_RATE, channels: Int = 1, options: StreamingOptions = StreamingOptions.DEFAULT): Flow<TranscriptionEvent>

transcribeStream for interleaved signed 16-bit PCM chunks (normalized natively as sample / 32768). See the FloatArray overload for the event contract. (On the JVM this overload is named transcribeStreamPcm16, since both overloads erase to Flow.)