flo von der uni
flo von der uni

Reputation: 748

Stereo playback gets converted to mono (on iPad only) even when using stereo headphones

I'm developing an audio processing app using core audio that records sounds through the headset mic and plays them back trough the headphones.

I've added a feature for the balance, i.e. to shift the playback onto one ear only.

This works perfectly on the iPods and iPhones I've tested it on. But not on the iPad. On the iPad the location of the sound doesn't change at all.

This is the code used to render the audio output:

static OSStatus renderInput(void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData)
{
    // Get a pointer to the dataBuffer of the AudioBufferList
    AudioBuffer firstBuffer = ioData->mBuffers[0];
    AudioSampleType *outA = (AudioSampleType *)firstBuffer.mData;
    int numChannels = firstBuffer.mNumberChannels;
    NSLog(@"numChannels = %d, left = %d, right = %d", numChannels, leftBalVolume, rightBalVolume);
    // Loop through the callback buffer, generating samples
    for (UInt32 i = 0; i < inNumberFrames * numChannels; i += numChannels) {        
        int outSignal = getFilteredSampleData(sampleDataTail);
        outA[i] = (outSignal * leftBalVolume) / 32768;
        if (numChannels > 1) {
            outA[i + 1] = (outSignal * rightBalVolume) / 32768;    
        }
        sampleDataTail = (sampleDataTail + 1) % sampleDataLen;
    }
    return noErr;
}

The output from the NSLog is as follows:

numChannels = 2, left = 16557, right = 32767

...telling me that it is basically working in stereo mode, I should hear the audio slightly to the right. But even if I put it 100% to the right, I still hear the audio in the middle, same volume on both earphones.

Obviously, the iPad 2 mixes the audiosignal down to mono and then plays that on both earphones. I thought that it might have to do with the fact that the iPad has only one speaker and thus would usually mix to mono... but why does it do that, even when a stereo headphone is connected?

Any help is greatly appreciated!

Upvotes: 1

Views: 1204

Answers (1)

flo von der uni
flo von der uni

Reputation: 748

Found the culprit:

I've called

desc.SetAUCanonical(1, true);

on the StreamFormat descriptor of the mixer's output. Now I'm just setting values for every property, and it works on the iPad as well...

desc.mSampleRate         = kGraphSampleRate;
desc.mFormatID           = kAudioFormatLinearPCM;
desc.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
desc.mFramesPerPacket    = 1;
desc.mChannelsPerFrame   = 2;
desc.mBitsPerChannel     = 16;
desc.mBytesPerPacket     = 4;
desc.mBytesPerFrame      = 4;

It seems that SetAUCanonical does different things on the iPad vs. iPod Touch and iPhone

Upvotes: 3

Related Questions