Convert mono audio input to iOS stereo output

I am trying to route mono audio input to stereo output. I have already looked into this similar question in detail , but there has never been a resolution.

First, I create an AVAudioSession using a category AVAudioSessionCategoryPlayAndRecord

, AVAudioSessionModeHearingAccessibility

and AVAudioSessionCategoryOptionAllowBluetoothA2DP

. Once it is active, I start configuring the I / O.

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;

      

Description of audio components:

    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 up an input channel map which I assume should convert 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, no sound is output and the mono audio input (iPhone microphone) is not converted to stereo. Does any envelope convert? I tried using a mixer but couldn't achieve this.

Is there any other way to copy all audio data from one channel to another?

+3


source to share





All Articles