将单声道音频输入转换为立体声输出iOS

时间:2017-06-30 09:11:08

标签: ios avaudioplayer core-audio audiounit

我试图将单声道音频单元输入路由到立体声输出。我已经深入研究了this similar question,但从未有任何解决方案。

我首先使用AVAudioSessionCategoryPlayAndRecord类别,AVAudioSessionModeHearingAccessibility模式和AVAudioSessionCategoryOptionAllowBluetoothA2DP选项创建AVAudioSession。将其设置为活动后,我开始配置输入/输出。

我的单一格式:

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;

我的立体声格式:

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;

音频组件说明:

    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));

设置输入的通道映射,我认为应该将单声道输入转换为立体声输出? :

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

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

当我运行此代码时,没有输出音频,并且单声道音频输入(iPhone麦克风)未转换为立体声。转换看起来对任何人都不对吗?我尝试使用调音台,但无法正常工作。

是否有不同的方法将一个频道中的所有音频数据复制到另一个频道?

0 个答案:

没有答案