Optimize recorder start, cleanup logs
This commit is contained in:
parent
1753d4ff83
commit
073fa0e701
@ -34,7 +34,6 @@ class Recorder: ObservableObject {
|
|||||||
private func handleDeviceChange() async {
|
private func handleDeviceChange() async {
|
||||||
guard !isReconfiguring else { return }
|
guard !isReconfiguring else { return }
|
||||||
isReconfiguring = true
|
isReconfiguring = true
|
||||||
logger.notice("🔄 Handling device change...")
|
|
||||||
|
|
||||||
if engine != nil {
|
if engine != nil {
|
||||||
let currentURL = file?.url
|
let currentURL = file?.url
|
||||||
@ -44,7 +43,6 @@ class Recorder: ObservableObject {
|
|||||||
if let url = currentURL {
|
if let url = currentURL {
|
||||||
do {
|
do {
|
||||||
try await startRecording(toOutputFile: url)
|
try await startRecording(toOutputFile: url)
|
||||||
logger.notice("✅ Successfully restarted recording after device change.")
|
|
||||||
} catch {
|
} catch {
|
||||||
logger.error("❌ Failed to restart recording after device change: \(error.localizedDescription)")
|
logger.error("❌ Failed to restart recording after device change: \(error.localizedDescription)")
|
||||||
}
|
}
|
||||||
@ -53,28 +51,27 @@ class Recorder: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
isReconfiguring = false
|
isReconfiguring = false
|
||||||
logger.notice("✅ Device change handled.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func configureAudioSession(with deviceID: AudioDeviceID) async throws {
|
private func configureAudioSession(with deviceID: AudioDeviceID) async throws {
|
||||||
logger.info("🔊 Configuring audio session for device ID: \(deviceID)...")
|
|
||||||
try? await Task.sleep(nanoseconds: 50_000_000)
|
|
||||||
do {
|
do {
|
||||||
_ = try AudioDeviceConfiguration.configureAudioSession(with: deviceID)
|
_ = try AudioDeviceConfiguration.configureAudioSession(with: deviceID)
|
||||||
try AudioDeviceConfiguration.setDefaultInputDevice(deviceID)
|
try AudioDeviceConfiguration.setDefaultInputDevice(deviceID)
|
||||||
logger.info("✅ Audio session configured successfully.")
|
|
||||||
} catch {
|
} catch {
|
||||||
logger.error("❌ Failed to configure audio session: \(error.localizedDescription)")
|
logger.error("❌ Failed to configure audio session: \(error.localizedDescription)")
|
||||||
throw error
|
throw error
|
||||||
}
|
}
|
||||||
try? await Task.sleep(nanoseconds: 50_000_000)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startRecording(toOutputFile url: URL) async throws {
|
func startRecording(toOutputFile url: URL) async throws {
|
||||||
deviceManager.isRecordingActive = true
|
logger.notice("▶️ startRecording called for: \(url.lastPathComponent)")
|
||||||
logger.notice("▶️ Attempting to start recording to: \(url.lastPathComponent)")
|
|
||||||
|
deviceManager.isRecordingActive = true
|
||||||
|
|
||||||
|
Task {
|
||||||
|
_ = await mediaController.muteSystemAudio()
|
||||||
|
}
|
||||||
|
|
||||||
let wasMuted = await mediaController.muteSystemAudio()
|
|
||||||
let deviceID = deviceManager.getCurrentDevice()
|
let deviceID = deviceManager.getCurrentDevice()
|
||||||
if deviceID != 0 {
|
if deviceID != 0 {
|
||||||
do {
|
do {
|
||||||
@ -87,7 +84,15 @@ class Recorder: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
engine = AVAudioEngine()
|
engine = AVAudioEngine()
|
||||||
let inputNode = engine!.inputNode
|
|
||||||
|
guard let engine = self.engine else {
|
||||||
|
logger.error("❌ AVAudioEngine is nil after creation attempt.")
|
||||||
|
await mediaController.unmuteSystemAudio()
|
||||||
|
stopRecording()
|
||||||
|
throw RecorderError.couldNotStartRecording
|
||||||
|
}
|
||||||
|
|
||||||
|
let inputNode = engine.inputNode
|
||||||
let inputFormat = inputNode.outputFormat(forBus: 0)
|
let inputFormat = inputNode.outputFormat(forBus: 0)
|
||||||
|
|
||||||
let whisperSettings: [String: Any] = [
|
let whisperSettings: [String: Any] = [
|
||||||
@ -110,7 +115,7 @@ class Recorder: ObservableObject {
|
|||||||
do {
|
do {
|
||||||
file = try AVAudioFile(forWriting: url, settings: whisperSettings)
|
file = try AVAudioFile(forWriting: url, settings: whisperSettings)
|
||||||
} catch {
|
} catch {
|
||||||
logger.error("Failed to create audio file: \(error.localizedDescription)")
|
logger.error("❌ Failed to create audio file: \(error.localizedDescription)")
|
||||||
await mediaController.unmuteSystemAudio()
|
await mediaController.unmuteSystemAudio()
|
||||||
stopRecording()
|
stopRecording()
|
||||||
throw RecorderError.couldNotStartRecording
|
throw RecorderError.couldNotStartRecording
|
||||||
@ -176,8 +181,8 @@ class Recorder: ObservableObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
do {
|
do {
|
||||||
try engine!.start()
|
try engine.start()
|
||||||
logger.notice("✅ Recording started successfully.")
|
logger.notice("✅ Recording engine started successfully.")
|
||||||
} catch {
|
} catch {
|
||||||
logger.error("❌ Failed to start audio engine: \(error.localizedDescription)")
|
logger.error("❌ Failed to start audio engine: \(error.localizedDescription)")
|
||||||
await mediaController.unmuteSystemAudio()
|
await mediaController.unmuteSystemAudio()
|
||||||
@ -196,8 +201,6 @@ class Recorder: ObservableObject {
|
|||||||
|
|
||||||
if wasRunning {
|
if wasRunning {
|
||||||
logger.notice("⏹️ Recording stopped.")
|
logger.notice("⏹️ Recording stopped.")
|
||||||
} else {
|
|
||||||
logger.info("ℹ️ stopRecording called, but engine was not running.")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
audioMeter = AudioMeter(averagePower: 0, peakPower: 0)
|
audioMeter = AudioMeter(averagePower: 0, peakPower: 0)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user