State Management (WhisperState)
Relevant source files
The following files were used as context for generating this wiki page:
- VoiceInk/Whisper/WhisperState.swift
- VoiceInk/Whisper/WhisperState+ModelManagement.swift
- VoiceInk/Whisper/WhisperState+UI.swift
- VoiceInk/Whisper/WhisperState+LocalModelManager.swift
- VoiceInk/Whisper/WhisperError.swift
- VoiceInk/VoiceInk.swift
- VoiceInk/Whisper/WhisperState+Parakeet.swift
- VoiceInk/Whisper/LibWhisper.swift
The WhisperState class is the central nervous system for the VoiceInk application's core transcription functionality. It is an ObservableObject that manages the entire lifecycle of voice recording, audio processing, transcription, and AI enhancement. It serves as the single source of truth for the recording state, loaded AI models, and user interface visibility.
WhisperState orchestrates interactions between the UI, the audio recorder, various transcription services (local, cloud, Parakeet), and the AI enhancement service. It is responsible for loading and managing transcription models, handling user actions like starting/stopping recordings, and processing the resulting audio files through the transcription and enhancement pipeline. Its state is observed by various SwiftUI views to update the UI in real-time.
Sources: VoiceInk/Whisper/WhisperState.swift:15-23, VoiceInk/VoiceInk.swift:51-54
Core Architecture
WhisperState is initialized at the application's root in VoiceInkApp and injected into the SwiftUI environment. This ensures a single instance is shared across all relevant views and services. It maintains references to key components like the Recorder, WhisperContext (for local transcription), and various transcription service handlers.
Sources: VoiceInk/VoiceInk.swift:51-64, VoiceInk/Whisper/WhisperState.swift:70-90
Key Properties
| Property | Type | Description |
|---|---|---|
recordingState | RecordingState | The current state of the recording and transcription process. |
isModelLoaded | Bool | Indicates if a transcription model is ready for use. |
currentTranscriptionModel | any TranscriptionModel | The currently selected model for transcription (local or cloud). |
availableModels | [WhisperModel] | A list of locally downloaded Whisper models. |
allAvailableModels | [any TranscriptionModel] | A comprehensive list of all possible models, including predefined, local, and custom. |
isMiniRecorderVisible | Bool | Controls the visibility of the recorder UI panel. |
whisperContext | WhisperContext? | An actor that wraps the whisper.cpp C++ context for thread-safe local transcription. |
recorder | Recorder | The object responsible for handling audio recording from the microphone. |
downloadProgress | [String: Double] | Tracks the download progress for models, keyed by model name. |
enhancementService | AIEnhancementService? | A reference to the service that handles AI-based text enhancement. |
Sources: VoiceInk/Whisper/WhisperState.swift:16-56
Component Relationships
The following diagram illustrates the primary components interacting with WhisperState.
Recording and Transcription Lifecycle
The core function of WhisperState is to manage the recording and transcription process. This is governed by a state machine and a sequence of asynchronous operations.
Recording State Machine
The recordingState property tracks the application's status through the transcription lifecycle.
| State | Description |
|---|---|
idle | The app is waiting for user input to start recording. |
recording | The app is actively recording audio from the microphone. |
transcribing | The recorded audio is being processed by the selected transcription model. |
enhancing | The transcribed text is being sent to an AI service for refinement. |
busy | A transient state used to prevent user interaction during cleanup. |
Sources: VoiceInk/Whisper/WhisperState.swift:9-14
This diagram shows the possible transitions between states.
Transcription Flow
The toggleRecord() method is the entry point for starting and stopping a recording. When a recording is stopped, the transcribeAudio() method is invoked to process the saved audio file.
This sequence diagram illustrates the end-to-end flow:
Model Management
WhisperState provides a comprehensive system for managing transcription models, including downloading, importing, loading, and deleting them. This logic is primarily encapsulated in the WhisperState+LocalModelManager.swift and WhisperState+ModelManagement.swift extensions.
Model Representation
Local Whisper models are represented by the WhisperModel struct.
| Property | Type | Description |
|---|---|---|
name | String | The base name of the model (e.g., "ggml-base.en"). |
url | URL | The local file URL for the model's .bin file. |
coreMLEncoderURL | URL? | The path to the unzipped Core ML encoder directory. |
downloadURL | String | The Hugging Face URL to download the main model file. |
coreMLZipDownloadURL | String? | The Hugging Face URL for the optional Core ML encoder zip file. |
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:8-28
Model Download Flow
Users can download models from a predefined list. The process handles both the main model file and an optional, associated Core ML model for performance enhancement.
This flow is managed by the downloadModel() and its private helper functions. It uses URLSession with progress observation to provide real-time feedback to the UI.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:144-239
Model Loading and Unloading
loadModel(_:): Asynchronously loads a localWhisperModelinto memory by creating aWhisperContext. This is a prerequisite for local transcription. It setsisModelLoadedtotrueupon success.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:65-82unloadModel(): Releases thewhisperContextand setsisModelLoadedtofalse. This frees up memory.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:311-320cleanupModelResources(): A more comprehensive cleanup function that releases thewhisperContext, resetsisModelLoaded, and also cleans up resources for the Parakeet service.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:331-338
User-Imported Models
The importLocalModel(from:) function allows users to import their own ggml compatible .bin files. The function copies the selected file into the app's WhisperModels directory and adds it to the list of available models. It prevents overwriting existing models with the same name.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:345-381
Parakeet Model Management
The app also supports Parakeet models, which have a distinct management flow handled in WhisperState+Parakeet.swift.
downloadParakeetModel(_:): Uses theFluidAudioframework'sAsrModels.downloadAndLoadfunction. Download status is tracked viaparakeetDownloadStatesanddownloadProgressproperties.deleteParakeetModel(_:): Removes the model's cache directory and updatesUserDefaultsto reflect its deletion.isParakeetModelDownloaded(_:): ChecksUserDefaultsto see if a specific Parakeet model has been successfully downloaded.
Sources: VoiceInk/Whisper/WhisperState+Parakeet.swift
UI Management
The WhisperState+UI.swift extension contains logic for managing the recorder's user interface.
showRecorderPanel()/hideRecorderPanel(): These methods are responsible for showing and hiding the recorder window, instantiatingNotchWindowManagerorMiniWindowManageras needed based on therecorderTypepreference.toggleMiniRecorder(): A high-level function that either starts a new recording session or cancels an existing one, depending on the current state.dismissMiniRecorder(): Handles the logic for gracefully dismissing the recorder panel, stopping any active recording, cleaning up resources, and resetting the state toidle.setupNotifications(): ObservesNotificationCenternotifications (e.g.,.toggleMiniRecorder,.promptDidChange) to trigger state changes from other parts of the app, such as global hotkeys.
Sources: VoiceInk/Whisper/WhisperState+UI.swift
Error Handling
Errors that occur during the transcription lifecycle are modeled by the WhisperStateError enum. This enum conforms to LocalizedError, providing user-friendly descriptions and recovery suggestions.
| Error Case | Description | Recovery Suggestion |
|---|---|---|
modelLoadFailed | Failed to load the transcription model. | Try selecting a different model or redownloading the current model. |
transcriptionFailed | Failed to transcribe the audio. | Check the default model and try again. If the problem persists, try another model. |
unzipFailed | Failed to unzip the downloaded Core ML model. | The archive might be corrupt. Try deleting and redownloading the model. |
Sources: VoiceInk/Whisper/WhisperError.swift
Conclusion
WhisperState is a critical, well-structured class that acts as the orchestrator for all recording and transcription-related activities in VoiceInk. By centralizing state management, it decouples the UI from the complex underlying services for audio recording, model management, and multi-provider transcription. Its use of a state machine, asynchronous tasks, and clear separation of concerns into extensions makes it a robust and maintainable foundation for the application's core features.