Voice Activity Detection (VAD)
Relevant source files
The following files were used as context for generating this wiki page:Voice Activity Detection (VAD) is a system within VoiceInk designed to identify the presence of human speech in an audio stream. Its primary purpose is to filter out silence from audio recordings before they are processed by a transcription model. This pre-processing step reduces the amount of data sent to the transcription engine, which can improve performance, reduce processing time, and potentially lower costs for cloud-based services.
VoiceInk employs VAD in multiple contexts, with distinct implementations for different transcription backends. The feature is generally controlled by a user setting and is enabled by default to optimize the transcription process.
VAD Implementations
VoiceInk utilizes two primary VAD strategies depending on the selected transcription service, along with a standalone detector class.
whisper.cpp VAD Integration
When using a local Whisper model, VAD is handled directly by the whisper.cpp library. The WhisperContext actor configures VAD parameters before initiating a transcription job.
The process is as follows:
- A user-configurable setting,
IsVADEnabled, is checked fromUserDefaults. - If enabled, the
vadflag inwhisper_full_paramsis set totrue. - The path to the VAD model is retrieved from
VADModelManagerand passed to the parameters. - Specific VAD thresholds and timing parameters are configured.
This diagram illustrates the configuration flow within WhisperContext.
The following parameters are set for the integrated whisper.cpp VAD:
| Parameter | Value | Description |
|---|---|---|
threshold | 0.50 | Speech probability threshold. |
min_speech_duration_ms | 250 | Minimum duration of a speech segment. |
min_silence_duration_ms | 100 | Minimum duration of a silence segment to split speech. |
max_speech_duration_s | Float.greatestFiniteMagnitude | No maximum duration for a speech segment. |
speech_pad_ms | 30 | Padding added to the start and end of a speech segment. |
samples_overlap | 0.1 | Overlap between audio samples. |
Sources: VoiceInk/Whisper/LibWhisper.swift:80-85
Parakeet VAD Integration
The ParakeetTranscriptionService uses a different VAD implementation based on the FluidAudio library. This service applies VAD to long audio files (20 seconds or more) to segment speech before transcription.
The process is as follows:
- The service checks if the audio duration is >= 20 seconds and if
IsVADEnabledis true. - A
VadManageris initialized. vadManager.segmentSpeechAudio()is called with the audio samples.- If segmentation is successful, the resulting speech segments are concatenated and sent for transcription.
- If VAD initialization or segmentation fails, the service falls back to transcribing the entire audio file.
This sequence diagram shows the interaction between the service and the VAD manager.
Standalone VoiceActivityDetector
VoiceInk includes a dedicated VoiceActivityDetector class that acts as a Swift wrapper around the C-level whisper_vad_* functions. While its direct usage in the main transcription flow is not specified in the provided files, it offers a more granular way to detect speech segments.
This class is initialized with a model path and provides a process(audioSamples:) method that returns an array of speech segments with start and end times.
This diagram shows the class structure.
The process method involves two main steps:
whisper_vad_detect_speech(): Detects speech and calculates probabilities.whisper_vad_segments_from_probs(): Extracts speech segments based on the calculated probabilities and a set of parameters.
The parameters used in this class differ slightly from the LibWhisper integration:
| Parameter | Value |
|---|---|
threshold | 0.45 |
min_speech_duration_ms | 150 |
min_silence_duration_ms | 750 |
max_speech_duration_s | Float.greatestFiniteMagnitude |
speech_pad_ms | 100 |
samples_overlap | 0.1 |
A notable implementation detail is the correction applied to the timestamps returned from the C API, which are divided by 100.0 to get the correct time in seconds.
Sources: VoiceInk/Services/VoiceActivityDetector.swift:49-56, 74-76
VAD Model Management
The VAD functionality relies on a machine learning model file. The management of this file is handled by the VADModelManager.
- Class:
VADModelManager - Responsibility: To locate and provide the file path for the VAD model.
- Model File: It specifically looks for
silero_vad.onnxwithin the application's main bundle. - Usage: The
WhisperContextcallsVADModelManager.shared.getModelPath()to get the path needed to configure thewhisper.cppVAD.
Sources: VoiceInk/Whisper/VADModelManager.swift:9-30, VoiceInk/Whisper/LibWhisper.swift:110-111
Configuration
The VAD feature can be toggled globally within the application.
- Control Mechanism: A
Boolvalue stored inUserDefaults. - Key:
IsVADEnabled - Default Value:
true(if the key is not set). - Usage: Both
LibWhisperandParakeetTranscriptionServicecheck this key to determine whether to apply VAD during their respective transcription processes.
Sources: VoiceInk/Whisper/LibWhisper.swift:73, VoiceInk/Services/ParakeetTranscriptionService.swift:46
Summary
Voice Activity Detection is a key optimization feature in VoiceInk that improves transcription efficiency. It is implemented differently for the two main local transcription engines, whisper.cpp and Parakeet, demonstrating a flexible approach to audio pre-processing. The system relies on a bundled silero_vad.onnx model and is controlled by a single user setting, ensuring that speech is accurately isolated from silence before being transcribed.