6

I'm trying to route a mono audio unit input to a stereo output. I already looked at this similar question in depth but there was never any resolution.

I first create an AVAudioSession with the AVAudioSessionCategoryPlayAndRecord category, AVAudioSessionModeHearingAccessibility mode, and AVAudioSessionCategoryOptionAllowBluetoothA2DP option. After setting it to active, I start to configure the input / output.

My mono format:

AudioStreamBasicDescription monoFormat;
monoFormat.mSampleRate = 44100;
monoFormat.mFormatID = kAudioFormatLinearPCM;
monoFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
monoFormat.mBytesPerPacket = 4;
monoFormat.mFramesPerPacket = 1;
monoFormat.mBytesPerFrame = 4;
monoFormat.mChannelsPerFrame = 1; //mono
monoFormat.mBitsPerChannel = 32;
monoFormat.mReserved = 0;

My stereo format:

AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate = 44100;
audioFormat.mFormatID = kAudioFormatLinearPCM;
audioFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked | kAudioFormatFlagIsNonInterleaved;
audioFormat.mBytesPerPacket = 8;
audioFormat.mFramesPerPacket = 1;
audioFormat.mBytesPerFrame = 8;
audioFormat.mChannelsPerFrame = 2;//stereo
audioFormat.mBitsPerChannel = 32;
audioFormat.mReserved = 0;

Audio Component description:

    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;

AudioUnit unit;
AudioComponent comp = AudioComponentFindNext(NULL, &desc);
result = AudioComponentInstanceNew(comp, &rioUnit);
result = AudioUnitSetProperty(unit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, 1, &one, sizeof(one));
result = AudioUnitSetProperty(unit, kAudioUnitProperty_StreamFormat, kAudioUnitScope_Input, 0, &monoFormat, sizeof(monoFormat));

Setting the channel map of the input, which I believe should convert the mono input to stereo output? :

SInt32 channelMap[2] = { 0, 0 };
        result = AudioUnitSetProperty(unit,
                                      kAudioOutputUnitProperty_ChannelMap,
                                      kAudioUnitScope_Input,
                                      0 ,
                                      channelMap,
                                      sizeof(channelMap));

result = AudioOutputUnitInitialize(unit);
result = AudioOutputUnitStart(unit);

When I run this code, there is no audio being outputted, and the mono audio input (iPhone microphone) is not being converted to stereo. Does the conversion look wrong to anyone? I tried using a mixer but was not able to get that working.

Is there a different way to copy all the audio data in one channel into the other?

Surz
  • 984
  • 3
  • 11
  • 36

0 Answers0