I am currently using the AVAudioEngine to grab raw int16Channel data from the inputNode. The default sample rate for the AVAudioEngine online claim it is 44 kHz with bit depth of 32. I forcefully tap the inputNode with an AVAudioFormat which is initialized with a sample rate of 44100 rather than using inputNode.sampleRate since my program will only run with a sampleRate of 44100. The odd thing about the crash I am experiencing, is that it only seems to occur on newer devices.
Devices I tested this on:
iPhone 6s: No Crash
iPhone 7: No Crash
iPhone 8: No Crash
iPhone SE 2: No Crash
iPhone 11 Pro: Crash
iPhone 11: Crash
iPhone 12: Crash
I do not have any other devices so I was not able to pinpoint what generation this occurred at. I am pretty sure the AVAudioEngine switched its default format when connected to bluetooth devices (from 44.1 kHz to 16 kHz), but I am not sure what is causing this. Below is the specific crash log:
2021-05-25 13:04:31.908675-0400 Pump Beta[57851:6521568] [avae] AVAEInternal.h:76 required condition is false: [AVAudioEngineGraph.mm:2027:InstallTapOnNode: (format.sampleRate == inputHWFormat.sampleRate)]
2021-05-25 13:04:31.912024-0400 Pump Beta[57851:6521568] * Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == inputHWFormat.sampleRate'
* First throw call stack:
(0x1829ad86c 0x1979c6c50 0x1828b3000 0x193393e18 0x1933d8fa8 0x1934557fc 0x1934360b8 0x104a13eb8 0x104921e40 0x10d367bcc 0x10d3696c0 0x10d371354 0x10d3720c0 0x10d37e644 0x1ce459814 0x1ce46076c)
libc++abi.dylib: terminating with uncaught exception of type NSException
Code Block swift struct BaseView: View { let audioEngine = AVAudioEngine() let inputFormat = AVAudioFormat(commonFormat: .pcmFormatInt16, sampleRate: 44100, channels: 1, interleaved: false) let analysisQueue = DispatchQueue(label: "com.dev.pump.analysisQueue", qos: .background) var body: some View { VStack { Text("Audio Engine Test") .onTapGesture { Log.info("Tapped") audioEngine.inputNode do { try audioEngine.start() } catch { Log.error("Error in audio engine start", error) fatalError() } analysisQueue.async { audioEngine.inputNode.installTap(onBus: 0, bufferSize: 11025, format: inputFormat) {_,_ in Log.info("I am running") } } } } } }
Devices I tested this on:
iPhone 6s: No Crash
iPhone 7: No Crash
iPhone 8: No Crash
iPhone SE 2: No Crash
iPhone 11 Pro: Crash
iPhone 11: Crash
iPhone 12: Crash
I do not have any other devices so I was not able to pinpoint what generation this occurred at. I am pretty sure the AVAudioEngine switched its default format when connected to bluetooth devices (from 44.1 kHz to 16 kHz), but I am not sure what is causing this. Below is the specific crash log:
2021-05-25 13:04:31.908675-0400 Pump Beta[57851:6521568] [avae] AVAEInternal.h:76 required condition is false: [AVAudioEngineGraph.mm:2027:InstallTapOnNode: (format.sampleRate == inputHWFormat.sampleRate)]
2021-05-25 13:04:31.912024-0400 Pump Beta[57851:6521568] * Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == inputHWFormat.sampleRate'
* First throw call stack:
(0x1829ad86c 0x1979c6c50 0x1828b3000 0x193393e18 0x1933d8fa8 0x1934557fc 0x1934360b8 0x104a13eb8 0x104921e40 0x10d367bcc 0x10d3696c0 0x10d371354 0x10d3720c0 0x10d37e644 0x1ce459814 0x1ce46076c)
libc++abi.dylib: terminating with uncaught exception of type NSException
** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == inputHWFormat.sampleRate'
There is no default sample rate for the audio engine itself, only for the device's input hardware. The newer devices you mention (the ones where your code crashes) currently have a default sample rate of 48KHz for their built-in microphones, not 44.1KHz.
Your code will have to deal with the actual format of the input node (that is, the format of output bus 0). If you need the sample rate to be 44.1KHz, you'll have to use something like AVAudioConverter to resample the audio data to your rate.
Your code will have to deal with the actual format of the input node (that is, the format of output bus 0). If you need the sample rate to be 44.1KHz, you'll have to use something like AVAudioConverter to resample the audio data to your rate.