WhisperBackgroundFallback

class WhisperBackgroundFallback(engine: WhisperEngine, scope: CoroutineScope, abortInFlightOnBackground: Boolean = true, onError: (Throwable) -> Unit = {}) : AutoCloseable(source)

Keeps a WhisperEngine off the GPU while the app is in the background.

iOS revokes GPU access from background apps: a Metal command buffer submitted after the app has entered the background fails with MTLCommandBufferErrorDomain / accessRevoked and aborts the process. This controller reloads the engine with ComputeBackend.CPU_ONLY (use_gpu = false, Accelerate / NEON only) when the app enters the background and restores the previous configuration when it returns to the foreground.

On iOS, wire it to UIApplication notifications with WhisperEngine.observeAppLifecycle(...) (iosMain); on other platforms call onEnterBackground / onEnterForeground from your own lifecycle callbacks.

Transitions are applied in order on scope through WhisperEngine.reinitialize (i.e. on the engine's own thread, after transcriptions already queued). Rapid background/foreground toggles are conflated to the latest state. When abortInFlightOnBackground is true (default), a GPU transcription that is executing when the app enters the background is aborted immediately (it fails with com.donfuldauer.kggml.core.InferenceCancelledException); transcriptions queued behind it still run before the switch, so cancel those too if they must not touch the GPU.

Parameters

onError

receives failures of the reloads (the engine keeps its previous configuration on failure).

Constructors

Link copied to clipboard
constructor(engine: WhisperEngine, scope: CoroutineScope, abortInFlightOnBackground: Boolean = true, onError: (Throwable) -> Unit = {})

Properties

Link copied to clipboard

true while the engine runs on the CPU fallback installed by this controller.

Functions

Link copied to clipboard
open override fun close()

Stops observing transitions. Does not change the engine's current configuration.

Link copied to clipboard

Call when the app enters the background (e.g. UIApplicationDidEnterBackgroundNotification).

Link copied to clipboard

Call when the app returns to the foreground (e.g. UIApplicationWillEnterForegroundNotification).