fix: audiomanager fix

This commit is contained in:
Owen Gretzinger
2025-07-11 15:39:03 -04:00
parent 858f934fb0
commit 19d7ae340b
+11 -10
View File
@@ -25,10 +25,11 @@ class AudioManager: NSObject, ObservableObject {
override init() { override init() {
super.init() super.init()
NotificationCenter.default.addObserver(self, NotificationCenter.default.addObserver(forName: .AVAudioEngineConfigurationChange,
selector: #selector(handleAudioEngineConfigurationChange), object: audioEngine,
name: .AVAudioEngineConfigurationChange, queue: .main) { [weak self] _ in
object: audioEngine) self?.handleAudioEngineConfigurationChange()
}
} }
deinit { deinit {
@@ -260,7 +261,7 @@ class AudioManager: NSObject, ObservableObject {
} }
private func processAudioBuffer(_ buffer: AVAudioPCMBuffer, converter: AVAudioConverter, targetFormat: AVAudioFormat, source: AudioSource) { private func processAudioBuffer(_ buffer: AVAudioPCMBuffer, converter: AVAudioConverter, targetFormat: AVAudioFormat, source: AudioSource) {
var processBuffer = buffer let processBuffer = buffer
// Convert to target format (16kHz int16 mono) in a single step AVAudioConverter will handle resampling and downmixing // Convert to target format (16kHz int16 mono) in a single step AVAudioConverter will handle resampling and downmixing
let outputFrameCapacity = AVAudioFrameCount(Double(processBuffer.frameLength) * targetFormat.sampleRate / processBuffer.format.sampleRate) let outputFrameCapacity = AVAudioFrameCount(Double(processBuffer.frameLength) * targetFormat.sampleRate / processBuffer.format.sampleRate)
@@ -385,6 +386,11 @@ class AudioManager: NSObject, ObservableObject {
} }
} }
} }
private func handleAudioEngineConfigurationChange() {
print("🔔 Audio engine configuration changed - restarting mic")
restartMicrophone()
}
} }
// MARK: - SCStreamDelegate & SCStreamOutput // MARK: - SCStreamDelegate & SCStreamOutput
@@ -425,9 +431,4 @@ extension CMSampleBuffer {
return AVAudioPCMBuffer(pcmFormat: format, bufferListNoCopy: audioBufferList.unsafePointer) return AVAudioPCMBuffer(pcmFormat: format, bufferListNoCopy: audioBufferList.unsafePointer)
} }
} }
}
@objc private func handleAudioEngineConfigurationChange(_ notification: Notification) {
print("🔔 Audio engine configuration changed - restarting mic")
restartMicrophone()
} }