rhowells
rhowells

Reputation: 1

AudioUnit recording glitches every 30 seconds

I've used this sample code to create an audio recorder. http://www.stefanpopp.de/capture-iphone-microphone/

I'm finding I get glitches about every 30 seconds. They sound a bit like buffer glitches to me, although I might be wrong. I've tried contacting the author of the article but not having much success. I'm really struggling to follow some of this code. I think it's missing a circular buffer but I'm not sure how important that is here. I'm hoping someone can point me in the right direction to either:

Why aren't I using AVAudioSession? I need the user to be able to set mic level while recording and to be able to listen back at the same time. Previously I did this with AVAudioSession but on more recent devices isInputGainSettable returns NO. It also returns NO for many hardware mics plugged in via lightning cable, which we're seeing more and more now the headphone jack is gone.

Upvotes: 0

Views: 183

Answers (2)

Arno van Goch
Arno van Goch

Reputation: 128

In my experience (iPhone 6) sample rate from microphone can be 48000 when a headset is not plugged in, and change to 44100 when a headset is plugged in.

If your audiounit is expecting a samplerate of 44100 then glitches like these are to be expected. To verify, you could try if your problem remains when you plug in a headset.

A workaround for the glitch problem seems to be to use an AVAudioEngine. Connect its inputNode to its mainMixerNode using the inputFormat of the inputNode. Connect the mainMixerNode to your AudioUnit in your desired format. Connect your AudioUnit to outputNode of the AVAudioEngine.

Using this mixerNode between inputNode and audioUnit is essential in this workaround.

Upvotes: 0

hotpaw2
hotpaw2

Reputation: 70703

Several problems.

Apple recommends that object methods not be called in the audio context (the callbacks). Your code has several. Use C functions instead.

Newer iOS devices likely use a hardware sample rate of 48000, not 44100. Resampling potentially causes buffers to change sizes.

The code seems to assume that the play callback buffer was the same size as the input callback buffer. This is not guaranteed. Thus the playback might end up with too few samples, causing periodic glitches.

Upvotes: 1

Related Questions