Local Transcription (whisper.cpp)
Relevant source files
The following files were used as context for generating this wiki page:
The local transcription feature in VoiceInk provides a privacy-first, offline voice-to-text capability. It is built upon whisper.cpp, a high-performance C++ implementation of OpenAI's Whisper model. This system is responsible for managing local AI models, processing audio data, and generating accurate transcriptions directly on the user's machine without sending any data to the cloud.
The architecture is designed to be robust, handling everything from downloading and storing models to managing the transcription lifecycle. It integrates with the application's main state management (WhisperState) to provide a seamless experience for both live recording and file-based transcription. The core transcription logic is encapsulated in a dedicated service, LocalTranscriptionService, which interacts with a Swift wrapper around the underlying whisper.cpp library.
Sources: README.md, VoiceInk/Whisper/LibWhisper.swift, VoiceInk/Services/LocalTranscriptionService.swift
Core Architecture
The local transcription system is composed of several key components that work together to manage models and process audio. The central manager is WhisperState, which coordinates the efforts of the LocalTranscriptionService and the WhisperContext.
WhisperState: The main observable object that manages the application's recording state, selected transcription model, and the lifecycle of theWhisperContext.LocalTranscriptionService: A dedicated service that handles the specifics of transcribing an audio file using a local model. It is responsible for loading the correct model into aWhisperContextand processing the audio samples.WhisperContext: A thread-safeactorthat directly wraps thewhisper.cppC library functions. It manages the model context pointer, configures transcription parameters, and executes thewhisper_fullfunction.- Model Management (
WhisperState+LocalModelManager.swift): An extension onWhisperStatethat contains all logic for downloading, storing, importing, and deleting localwhisper.cppmodels.
Sources: VoiceInk/Whisper/WhisperState.swift, VoiceInk/Services/LocalTranscriptionService.swift, VoiceInk/Whisper/LibWhisper.swift
Component Interaction Diagram
This diagram shows the high-level interaction between the main components of the local transcription system.
Model Management
The application provides a comprehensive system for managing local transcription models, handled primarily by the WhisperState+LocalModelManager.swift extension.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift
Model Storage
Local models are stored in a dedicated directory within the application's support folder: ~/Library/Application Support/com.prakashjoshipax.VoiceInk/WhisperModels. This directory is created automatically if it doesn't exist.
Sources: VoiceInk/Whisper/WhisperState.swift:78-82, VoiceInk/Whisper/WhisperState+LocalModelManager.swift:33-37
Model Data Structures
Several structs define the properties of local models.
| Struct | Description | Provider | Source File |
|---|---|---|---|
LocalModel | Represents a predefined whisper.cpp model available for download from Hugging Face. | .local | VoiceInk/Models/TranscriptionModel.swift:126-143 |
WhisperModel | Represents a downloaded local model on disk, tracking its file URL and any associated Core ML assets. | N/A | VoiceInk/Whisper/WhisperState+LocalModelManager.swift:6-28 |
ImportedLocalModel | Represents a user-imported .bin model file. It derives its properties from the file's base name. | .local | VoiceInk/Models/TranscriptionModel.swift:146-160 |
Model Downloading
Users can download predefined ggml models directly within the app. The download process supports both the main model file (.bin) and optional Core ML encoder models for optimized performance on Apple Silicon.
The download flow is as follows:
- A download request is initiated for a
LocalModel. - The main
.binmodel is downloaded from the Hugging Face URL specified inLocalModel.downloadURL. - If the model supports Core ML (i.e., it's not a quantized
q5orq8model), the corresponding-encoder.mlmodelc.zipfile is also downloaded. - The Core ML zip file is unzipped into the models directory.
- The new
WhisperModelis added to theavailableModelslist inWhisperState.
This diagram illustrates the model download process, including the optional Core ML path.
Importing Local Models
The application allows users to import their own custom whisper.cpp models.
- The user selects a
.binfile via anNSOpenPanel. - The file must have a
.binextension. - The file is copied into the
WhisperModelsdirectory. - If a model with the same name already exists, the import is aborted to prevent overwriting.
- A new
ImportedLocalModelinstance is created and added to the list of available models.
Sources: VoiceInk/Whisper/WhisperState+LocalModelManager.swift:317-353, VoiceInk/Views/AI Models/ModelManagementView.swift:251-264
Transcription Process
The transcription process can be initiated either through live recording or by processing an existing audio file. In both cases, the LocalTranscriptionService is the component responsible for the core transcription task.
Transcription Flow
The following sequence diagram details the steps involved when transcribing a recorded audio file using a local model.
WhisperContext Wrapper
The LibWhisper.swift file provides the WhisperContext actor, which is a crucial, thread-safe wrapper around the whisper.cpp C API. Using an actor ensures that the underlying C context, which is not thread-safe, is only accessed from a single thread at a time.
Sources: VoiceInk/Whisper/LibWhisper.swift:9-13
Initialization
A WhisperContext is created via the static createContext(path:) method. This method initializes the whisper.cpp context from a model file (.bin) using whisper_init_from_file_with_params. On non-simulator builds, it enables Metal GPU acceleration by setting flash_attn = true.
Sources: VoiceInk/Whisper/LibWhisper.swift:110-129
Transcription Parameters
The fullTranscribe method configures various parameters for the whisper_full function call. These parameters control the behavior of the transcription process.
| Parameter | Value/Setting | Description | Source |
|---|---|---|---|
strategy | WHISPER_SAMPLING_GREEDY | Uses greedy sampling for decoding. | VoiceInk/Whisper/LibWhisper.swift:20 |
n_threads | max(1, min(8, cpuCount() - 2)) | Sets the number of threads for processing, optimized for the system's CPU core count. | VoiceInk/Whisper/LibWhisper.swift:19, 40 |
language | From UserDefaults (SelectedLanguage), defaults to "auto". | Sets the target language for transcription or enables auto-detection. | VoiceInk/Whisper/LibWhisper.swift:23-31 |
initial_prompt | From WhisperContext.prompt property. | Provides an initial prompt to guide the model's transcription. | VoiceInk/Whisper/LibWhisper.swift:33-41 |
print_timestamps | true | Enables timestamp generation (though they are not directly used in the final string). | VoiceInk/Whisper/LibWhisper.swift:44 |
translate | false | Disables translation to English. | VoiceInk/Whisper/LibWhisper.swift:45 |
no_context | true | Disables the use of context from previous audio segments. | VoiceInk/Whisper/LibWhisper.swift:48 |
single_segment | false | Allows the output to be split into multiple segments. | VoiceInk/Whisper/LibWhisper.swift:49 |
temperature | 0.2 | Sets a low temperature for more deterministic and less random output. | VoiceInk/Whisper/LibWhisper.swift:50 |
vad | true if VAD is enabled and model is available. | Enables Voice Activity Detection (VAD) to filter out silence. | VoiceInk/Whisper/LibWhisper.swift:55-70 |
Building the whisper.cpp Dependency
VoiceInk relies on a pre-compiled whisper.xcframework which is not included in the repository. To build the project from source, developers must first build this framework from the ggerganov/whisper.cpp repository.
The process involves:
- Cloning the
whisper.cpprepository. - Running the
./build-xcframework.shscript provided in that repository. - This script compiles the C++ code and creates a universal
whisper.xcframeworkin thebuild-apple/directory. - This framework must then be manually added to the VoiceInk Xcode project.
Sources: BUILDING.md:12-16
Summary
The local transcription system in VoiceInk is a well-architected feature that leverages the power of whisper.cpp for fast, private, and accurate offline speech-to-text. It features robust model management, including downloading, importing, and dynamic loading. The core transcription logic is safely handled by a Swift actor that wraps the non-thread-safe C++ library, ensuring stability and performance. This offline-first approach is a cornerstone of the application's commitment to user privacy.