Prianka Liz Kariat
YML Innovation Lab
Published in
2 min readFeb 10, 2019

--

Changing the Format of iOS AVAudioEngine Mic Input

AVAudioEngine mic input is always rendered at 44 kHZ , 32 bit. Specifying a different format when you tap the mic input does not take effect. There might be some specific instances in which we want to record the mic input in a different format, maybe to input it to a machine learning model that requires audio in a specific format or simply because you want to perform some specific calculations.

Manipulating the audio buffer input can be a solution but is not a trivial one. Today we are going to see a code snippet that will actually help you convert the mic input to a different format as and when we receive it.

AVAudioConverter

We are going to use AVAudioConverter for converting the audio input into the specified format.

The following steps need to be performed to get the audio engine up and receive audio in the requested format.

  1. Check Audio Permissions
  2. Install Tap on Input Bus
  3. Use AVAudioConverter to convert the audio as and when we receive the input buffer
  4. Request Audio Permissions

You need to check for audio permissions and request it from the OS if not granted. Once permissions are granted, we start tapping the microphone input.

2. Start Tapping Microphone Input

AVAudioEngine handles all mic input and audio output.

Once the audio engine is set up, you can start tapping the microphone input.

3. Use AVAudioConverter to convert the audio as and when we receive the input buffer

We use AVAudioConverter to convert the audio input

In this sample, we are going to convert the audio to pcm 16 bit Int at a specified sample rate. We declare the recording format and define an audio converter that converts the audio from the input format to the recording format.

Now let us see the code for conversion to be called in the call back for installing tap

AVAudioConverter’s convert method accepts an AVAudioConverterInputBlock. This input block is responsible for accepting the input buffer to the AVAudioConverter. We input the buffer returned by the audio engine as and when received to this input block.

We also declare an output AVAudioPCMBuffer in the recording format, that will hold the converted buffer once converter finishes running.

The frame capacity for the output pcm buffer is kept at double the output sample rate to be fail safe.

Once we get the output buffer, we are converting the buffer into raw array values for further manipulation.

These steps concretely outline how we can obtain raw audio data in a specific format from the input mic.

References

--

--