AudioKit: AKMicrophone crash init in 4.5.5 version

AudioKit version 4.5.5: crash in code AKSettings.audioInputEnabled = true mic = AKMicrophone() tracker = AKFrequencyTracker(mic) silence = AKBooster(tracker, gain: 0)

_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:911:SetOutputFormat: (format.sampleRate == hwFormat.sampleRate)] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'

Test device - iPhone 8 and simulators AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate periodically returns in the application 44100.0 or 48000.0

On the iPhone se AKMicrophone() works stably, no crash

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 30 (5 by maintainers)

Most upvoted comments

A lot of this is being revamped and should be fixed in an upcoming version of AudioKit.

I have had this issue multiple times over the past few months for my app. I’m currently using 4.5.2 and have solved it mostly with the following:

  • Move AKMicrophone() to be the very first thing that’s done. Before any AKSettings changes, before anything. For myself, it’s in a singleton class that instantiates it at the start of my app after asking for Microphone permissions - before putting up the engine - anything.
  • To switch between .playback and .playAndRecord I use AKSettings.setSession and make sure to restart the engine when I switch to record mode. (This is for people who use recording only on one view and might not want the microphone up all the time - especially for the pass-through-audio on bluetooth headphones with a mic)
  • Implement listeners on AVAudio route changes and set the session to the appropriate state after headphones plugged in/out

You may also have to bring the engine back up after some route changes, depending on your setup. The biggest problem I have is with crackling audio when the app is started with bluetooth headphones plugged in, followed by unplugging them when the microphone is active. This is also because my headphones are 44.1K while the microphone on the iPhone 8 is 48K. That’s a separate issue I believe from what was asked.

Hope this helps.

Adding my 2 cents, since this issue is bugging me also: it seems that it is related to a change in the mic hardware sample rate from 44.1K in older devices to 48K in new devices. When I try to set try AVAudioSession.sharedInstance().setPreferredSampleRate(48_000) The result is a working microphone in newer devices, and a crashed one in older devices. When the rate is 44_100 it is reversed. With the simulator there is no issue - regardless of the sample rate set by the Audio MIDI Setup, any session sample rate I set is respected, and working, on the simulator.

Interestingly, if both the session sample rate and the AK sample rate are set to the same value before the session is setActive, the reported sample rate will be what was set (in my case, 48K), but the actual analysis is performed according to the device’s default (in my case, 44.1K). There is no crash.

For the time being I am querying in my code what’s the actual device sample rate, and according to the result I have two datasets for analysis. I would very much like to be able to always set 48K.

For those looking for a workaround that seems to prevent the crash, this did it for me:

AKSettings.sampleRate = AudioKit.engine.inputNode.inputFormat(forBus: 0).sampleRate  

Place that line before you do anything microphone related, except declaring the mic constant.

EDIT: I edited the post based on some more experiments I did. My conclusion is that for the time being there is no way to override the default sample rate of the device’s microphone, whereas it is possible with the simulator.

Anybody find any workarounds for this? This is the last major bug with my app affecting about 3% of users…

I managed to reproduce this issue(but I believe this is not the only way to cause this particular crash):

Steps to reproduce: init AKMicrophone while you’re on a phone call.

The crash points to this line of the code in file AKMicrophone.swift

       let format = AVAudioFormat(commonFormat: AudioKit.engine.inputNode.inputFormat(forBus: 0).commonFormat,
                                  sampleRate: desiredFS,
                                  interleaved: AudioKit.engine.inputNode.inputFormat(forBus: 0).isInterleaved,
                                 channelLayout: AudioKit.engine.inputNode.inputFormat(forBus: 0).channelLayout!)

Not sure if it’s true, but according to the code we should do following:

AKSettings.audioInputEnabled = true
try? AudioKit.start()
mic = AKMicrophone()

have had the same crash in 4.5.5 as well. What I found that 4.5.5 works well on iPhone 5s and 6 however crashes on iPhone 8 and X. Downgrading AudioKit to 4.5.2 works in my case.