Control mono playback output with Core Audio

2019-07-23 05:00发布

问题:

I'm developing an application for iOS, that uses the RemoteIO audio unit to record audio from the microphone, process it and output to the speakers (headset). Currently I use a single channel (mono) for input and output.

What I'd like to do, is to allow the users to choose an output speaker: left-only, right-only or both. My current code supports only the "both" setting - the same sound is coming from both speakers.

Here's how I set the stream format (kAudioUnitProperty_StreamFormat) of the input and output bus:

AudioStreamBasicDescription ASBD = {0};
size_t bytesPerSample = sizeof(SInt16);
ASBD.mFormatID = kAudioFormatLinearPCM;
ASBD.mFormatFlags = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
ASBD.mSampleRate = 44100;
ASBD.mFramesPerPacket = 1;
ASBD.mBytesPerFrame = bytesPerSample;
ASBD.mBytesPerPacket = bytesPerSample;
ASBD.mBitsPerChannel = 8 * bytesPerSample;
ASBD.mChannelsPerFrame = 1;

And my render callback (kAudioUnitProperty_SetRenderCallback) looks roughly like this:

AudioUnitRender(remoteIO, ioActionFlags, inTimeStamp, inputBus, inNumberFrames, ioData);
SInt16 *renderBuffer = ioData->mBuffers[0].mData;
// Process renderBuffer and modify the samples

What would be the simplest way to implement the left-only/right-only settings? I don't mind changing a device setting if there's anything relevant.

回答1:

The easiest way to do this is to use a stereo ASBD on both RemoteIO elements you control (i.e., the output element of the input scope and the input element of the output scope), and just send zeros as the samples for the inactive channel, if any.

The RemoteIO unit has limited capacity for on-the-fly format conversion, but it can split the mono stream from the microphone into a stereo stream using its internal format converter.

When this is configured correctly, depending on the exact ASBD, your render callback will receive buffer lists that have separate buffers for each channel (non-interleaved), or a single, interleaved buffer. You'll need to bzero one of the buffers (non-interleaved case), or iterate over the samples and mute them (set to zero; interleaved case).

(NOTE: The selective channel muting will presumably take place after your own processing, so you may need to change your own code to accommodate stereo data. Or, set the output element of the input scope of the RemoteIO unit to emit mono samples, do your processing there, and use your own AudioConverter to split the stream to stereo afterward.)