whisper.rn uses a context-based architecture to manage speech recognition and voice activity detection resources. Contexts encapsulate native resources (models, memory, compute threads) and must be explicitly managed to prevent memory leaks.
Every WhisperContext instance has these properties:
interface WhisperContext { id: number // Unique context identifier (index.ts:262) ptr: number // Native context pointer (index.ts:260) gpu: boolean // Whether GPU/Metal acceleration is active (index.ts:264) reasonNoGPU: string // Explanation if GPU is not available (index.ts:266)}
GPU Acceleration Status:Check if hardware acceleration is active:
if (whisperContext.gpu) { console.log('Using GPU/Metal acceleration')} else { console.log('GPU not available:', whisperContext.reasonNoGPU) // Common reasons: "Core ML not enabled", "Metal not supported", "GPU disabled in options"}
When using ArrayBuffer, the data is transferred directly to native code via JSI (index.ts:402-405), bypassing JSON serialization for better performance.
Always release contexts when done to free native resources (index.ts:617-619):
// Release a single contextawait whisperContext.release()// Release all active contextsimport { releaseAllWhisper } from 'whisper.rn'await releaseAllWhisper() // Calls RNWhisper.releaseAllContexts() (index.ts:718-722)
Failing to release contexts causes memory leaks. The native models remain in memory until explicitly released.
VAD contexts have similar properties to Whisper contexts (index.ts:751-762):
interface WhisperVadContext { id: number // Unique context identifier gpu: boolean // Whether GPU acceleration is active reasonNoGPU: string // Explanation if GPU is not available}
Release VAD contexts when done (index.ts:821-823):
// Release a single VAD contextawait vadContext.release()// Release all active VAD contextsimport { releaseAllWhisperVad } from 'whisper.rn'await releaseAllWhisperVad() // Calls RNWhisper.releaseAllVadContexts() (index.ts:870-874)
Contexts are thread-safe for concurrent transcription jobs:
// Multiple transcriptions can run in parallel on the same contextconst job1 = whisperContext.transcribe(audio1, options)const job2 = whisperContext.transcribe(audio2, options)const [result1, result2] = await Promise.all([ job1.promise, job2.promise,])
Jobs are managed internally with unique jobId values (index.ts:288, index.ts:423). The native layer handles concurrent job execution.