From c148acefbd9ae673ad7e1076ef12742c5c182443 Mon Sep 17 00:00:00 2001 From: Beingpax Date: Wed, 12 Mar 2025 14:47:43 +0545 Subject: [PATCH] perf: faster recording start time --- VoiceInk/Whisper/WhisperState.swift | 37 +++++++++++++++++------------ 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/VoiceInk/Whisper/WhisperState.swift b/VoiceInk/Whisper/WhisperState.swift index 42444b4..1dfee51 100644 --- a/VoiceInk/Whisper/WhisperState.swift +++ b/VoiceInk/Whisper/WhisperState.swift @@ -197,23 +197,10 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate { self.recordedFile = file self.transcriptionStartTime = Date() - await ActiveWindowService.shared.applyConfigurationForCurrentApp() - - if let enhancementService = self.enhancementService, - enhancementService.isEnhancementEnabled && - enhancementService.useScreenCaptureContext { - await enhancementService.captureScreenContext() + Task.detached(priority: .background) { + await self.performBackgroundTasks() } - if let currentModel = self.currentModel, self.whisperContext == nil { - do { - try await self.loadModel(currentModel) - } catch { - await MainActor.run { - self.messageLog += "Error preloading model: \(error.localizedDescription)\n" - } - } - } } catch { self.messageLog += "\(error.localizedDescription)\n" self.isRecording = false @@ -227,6 +214,26 @@ class WhisperState: NSObject, ObservableObject, AVAudioRecorderDelegate { } } + private func performBackgroundTasks() async { + if let currentModel = self.currentModel, self.whisperContext == nil { + do { + try await self.loadModel(currentModel) + } catch { + await MainActor.run { + self.messageLog += "Error preloading model: \(error.localizedDescription)\n" + } + } + } + + await ActiveWindowService.shared.applyConfigurationForCurrentApp() + + if let enhancementService = self.enhancementService, + enhancementService.isEnhancementEnabled && + enhancementService.useScreenCaptureContext { + await enhancementService.captureScreenContext() + } + } + private func requestRecordPermission(response: @escaping (Bool) -> Void) { #if os(macOS) response(true)