From 0429af667865f748f277cc1436fe91620115780c Mon Sep 17 00:00:00 2001 From: Beingpax Date: Fri, 12 Sep 2025 14:13:31 +0545 Subject: [PATCH] Use whisper module in VoiceActivityDetector to resolve calling-convention conflict with whisper_vad_default_params; no functional changes --- VoiceInk/Services/VoiceActivityDetector.swift | 67 +++---------------- 1 file changed, 11 insertions(+), 56 deletions(-) diff --git a/VoiceInk/Services/VoiceActivityDetector.swift b/VoiceInk/Services/VoiceActivityDetector.swift index 4937c2d..67cd0f3 100644 --- a/VoiceInk/Services/VoiceActivityDetector.swift +++ b/VoiceInk/Services/VoiceActivityDetector.swift @@ -1,6 +1,11 @@ import Foundation import AVFoundation import os.log +#if canImport(whisper) +import whisper +#else +#error("Unable to import whisper module. Please check your project configuration.") +#endif // MARK: - C API Bridge @@ -8,60 +13,6 @@ import os.log fileprivate typealias WhisperVADContext = OpaquePointer fileprivate typealias WhisperVADSegments = OpaquePointer -// Define the C function signatures for Swift, scoped to this file - -@_silgen_name("whisper_vad_default_params") -fileprivate func whisper_vad_default_params() -> whisper_vad_params - -@_silgen_name("whisper_vad_default_context_params") -fileprivate func whisper_vad_default_context_params() -> whisper_vad_context_params - -@_silgen_name("whisper_vad_init_from_file_with_params") -fileprivate func whisper_vad_init_from_file_with_params(_ path_model: UnsafePointer, _ params: whisper_vad_context_params) -> WhisperVADContext? - -@_silgen_name("whisper_vad_detect_speech") -fileprivate func whisper_vad_detect_speech(_ vctx: WhisperVADContext, _ samples: UnsafePointer, _ n_samples: Int32) -> Bool - -@_silgen_name("whisper_vad_n_probs") -fileprivate func whisper_vad_n_probs(_ vctx: WhisperVADContext) -> Int32 - -@_silgen_name("whisper_vad_probs") -fileprivate func whisper_vad_probs(_ vctx: WhisperVADContext) -> UnsafeMutablePointer - -@_silgen_name("whisper_vad_segments_from_probs") -fileprivate func whisper_vad_segments_from_probs(_ vctx: WhisperVADContext, _ params: whisper_vad_params) -> WhisperVADSegments? - -@_silgen_name("whisper_vad_segments_n_segments") -fileprivate func whisper_vad_segments_n_segments(_ segments: WhisperVADSegments) -> Int32 - -@_silgen_name("whisper_vad_segments_get_segment_t0") -fileprivate func whisper_vad_segments_get_segment_t0(_ segments: WhisperVADSegments, _ i_segment: Int32) -> Float - -@_silgen_name("whisper_vad_segments_get_segment_t1") -fileprivate func whisper_vad_segments_get_segment_t1(_ segments: WhisperVADSegments, _ i_segment: Int32) -> Float - -@_silgen_name("whisper_vad_free_segments") -fileprivate func whisper_vad_free_segments(_ segments: WhisperVADSegments) - -@_silgen_name("whisper_vad_free") -fileprivate func whisper_vad_free(_ ctx: WhisperVADContext) - -// Structs matching whisper.h, scoped to this file -fileprivate struct whisper_vad_params { - var threshold: Float - var min_speech_duration_ms: Int32 - var min_silence_duration_ms: Int32 - var max_speech_duration_s: Float - var speech_pad_ms: Int32 - var samples_overlap: Float -} - -fileprivate struct whisper_vad_context_params { - var n_threads: Int32 - var use_gpu: Bool - var gpu_device: Int32 -} - // MARK: - VoiceActivityDetector Class @@ -72,8 +23,12 @@ class VoiceActivityDetector { init?(modelPath: String) { var contextParams = whisper_vad_default_context_params() contextParams.n_threads = max(1, min(8, Int32(ProcessInfo.processInfo.processorCount) - 2)) - - guard let context = whisper_vad_init_from_file_with_params(modelPath, contextParams) else { + + let contextOpt: WhisperVADContext? = modelPath.withCString { cPath in + whisper_vad_init_from_file_with_params(cPath, contextParams) + } + + guard let context = contextOpt else { logger.error("Failed to initialize VAD context.") return nil }