2

I am using playing audio as soon as we received any input from microphone. I am using OSStatus for recording and playing audio. As the recording and playing are working fine.

I have to active left side headphone, right side headphone or center as per user select. As I research AudioBuffer, we have to set mNumberChannels for left, right and center headphone. Here is my code for playing audio.

    AudioBuffer buffer;

//    // 1 - Left
//    // 2 - Right
//    // 3 - Center
//    
    buffer.mNumberChannels = 0;


    buffer.mDataByteSize = inNumberFrames * 2;
    buffer.mData = malloc( inNumberFrames * 2 );

    // Put buffer in a AudioBufferList
    AudioBufferList bufferList;
    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0] = buffer;

    // Obtain recorded samples

    OSStatus status;

    status = AudioUnitRender([iosAudio audioUnit], 
                             ioActionFlags, 
                             inTimeStamp, 
                             inBusNumber, 
                             inNumberFrames, 
                             &bufferList);
    checkStatus(status);



    // Now, we have the samples we just read sitting in buffers in bufferList
    // Process the new data
    [audioProcess processAudio:&bufferList];

    free(bufferList.mBuffers[0].mData);

    return noErr;

But I am not able to enable left, right or center if I changed mNumberChannels. Can anyone help to point out my mistake?

Nidhi Patel
  • 1,222
  • 11
  • 17
Nirmalsinh Rathod
  • 5,079
  • 4
  • 26
  • 56

1 Answers1

0

The correct value mNumberChannels for an AudioBuffer can be derived from the mChannelsPerFrame and the interleaved flag. For non interleaved formats, mNumberChannels is always 1. For interleaved formats, mNumberChannels is equal to mChannelsPerFrame.

So mNumberChannels is for specifying the number of channels in the processing buffers, NOT for panning or routing audio to "left"-"center"-"right". So setting it left=1, right=2, center=3 is wrong.

After recording into the audiobuffer, to do panning (selecting audio playback via left/center/right), you should connect your current processing unit to a Multichannel Mixer unit (subtype kAudioUnitSubType_MultiChannelMixer) which takes any number of mono or stereo streams and combines them into a single stereo output and supports stereo panning for each input.

With this mixer unit, you can use a property like kMultiChannelMixerParam_Pan, to control left/center/right.

ruoho ruotsi
  • 1,283
  • 14
  • 13