Audiokit: AKMicrophone Init lead crash on simulator (AudioKit 4.6.3)

Created on 22 Mar 2019  路  5Comments  路  Source: AudioKit/AudioKit

Initializing microphone is leading crash on the simulator.
On hardware iPhone5 + iPad 4 gen. it is not crashing.

Producing the crash is very easy.
Adding an instance of AKMicrophone
let mic = AKMicrophone()
anywhere before/after AudioKit init/start, before/after AKSettings.setSession set, gives the same result..
To make sure I've created brand new project, one line of code let mic = AKMicrophone() is enough to crash.

2019-03-22 01:08:19.024293+0000 AudioRecorder[15720:235382] [avae] AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:911:SetOutputFormat: (format.sampleRate == hwFormat.sampleRate)] 2019-03-22 01:08:19.031799+0000 AudioRecorder[15720:235382] *** Terminating app due to uncaught exception 'com.apple.coreaudio.avfaudio', reason: 'required condition is false: format.sampleRate == hwFormat.sampleRate'
I've checked all about format.sampleRate == hwFormat.sampleRate and previously posted AKMicrophone crashs but I couldn't find a solution.

I set sample rate manually etc... no luck.
I'm stuck. Is anyone tried the AKMicrophone on AudioKit 4.6.3?
I'm wondering if it is my computer setup. MBP:Internal Speaker
Screen Shot 2019-03-22 at 01 30 56
Screen Shot 2019-03-22 at 01 31 32
Screen Shot 2019-03-22 at 01 31 57

I saw a long discussion on #1664 should I have write there?

Most helpful comment

You should make sure your hardware devices are all using the same sample rate.
I was having similar problems with format.sampleRate == hwFormat.sampleRate crashes.

Fixing for me involved going to Launchpad > Other > Audio MIDI Setup and changing the sample rate on my monitor (previously it was 48kHz, incompatible with AudioKits default 44.1kHz).

audio_devices

All 5 comments

Just to add to the discussion, anecdotally, this seems like an issue that plagues most new users (myself included) continuously through the last few months. Some people it's iPhones, some people (like myself) iPads, and then macOS. I know there are very successful long term users of AudioKit who do not have this problem, and perhaps we could just update the guides to help users with the right protocol for enabling the microphone?

Currently on an iPad 7th gen, I get this crash, but on iPad Pro 2nd & 3rd gen, no crash. My simulator works fine, but my collogues simulator it crashes every time no matter what simulator. Just so perplexing.

More importantly, is there any way we could at least wrap the microphone initialization in optionals or something so at least we can avoid a crash and just pass an error before there is any long term solution?

I'm assuming it's here on the force unwrapping of format:

 /// Initialize the microphone
    @objc override public init() {
        super.init()
        self.avAudioNode = mixer
        AKSettings.audioInputEnabled = true

        #if os(iOS)
        let format = getFormatForDevice()
        // we have to connect the input at the original device sample rate, because once AVAudioEngine is initialized, it reports the wrong rate
        setAVSessionSampleRate(sampleRate: AudioKit.deviceSampleRate)
        AudioKit.engine.attach(avAudioUnitOrNode)
        AudioKit.engine.connect(AudioKit.engine.inputNode, to: self.avAudioNode, format: format!)
        setAVSessionSampleRate(sampleRate: AKSettings.sampleRate)
        #elseif !os(tvOS)
        AudioKit.engine.inputNode.connect(to: self.avAudioNode)
        #endif

I'd be happy to make the fix so that at least have the init method return an error if the format is wrong, it would require require having everyone wrap their init with a do/catch, if that is agreeable?

You should make sure your hardware devices are all using the same sample rate.
I was having similar problems with format.sampleRate == hwFormat.sampleRate crashes.

Fixing for me involved going to Launchpad > Other > Audio MIDI Setup and changing the sample rate on my monitor (previously it was 48kHz, incompatible with AudioKits default 44.1kHz).

audio_devices

Hi @bakeddean, you're advise _"make sure your hardware devices are all using the same sample rate"_ has fixed the simulator crash thanks.

Hi @maksutovic I do agree for finding an error catching procedure would be a good approach because this issue looks like will appear in future again and again as a fragile point.

I think it's all about to detect correct sampling rate of device/simulator. Apparently this is the reason why it _crashs | not crashs_ on different iOS hardware.

I dug in
/// Initialize the microphone @objc override public init() ....
But I couldn't find a nil value which would cause the crash, it could be easy to patch it. The sampling rate is there but simulator _(in my case)_ don't like this sampling rate because of other different sampling rate devices in my computer _(in my case)_ thanks to @bakeddean 's point I could fix it for now on simulator.

I just submitted a pull request to make AKMicrophone() a fail-able initializer, this should at least help with the hard crashing. Once it's confirmed into master it should help those of us with these problems. We should close this issue for now!

Okay, and I'll be pushing 4.6.4 later today.

Was this page helpful?
0 / 5 - 0 ratings