1

In this sample app, I was able to load in a file of stereo data and play it using the simulator. But it doesn't work on the device. I tried using a sound editor and convert the stereo clip to mono and changing the descriptor settings and it will work mono only. I had a hard time trying to find out why, I am guessing it has to do with my descriptor configuration problems.

This sample app is at https://github.com/peter7777usa/TestAudio

The PlayBack Function

static OSStatus playbackCallback(void *inRefCon,
                                 AudioUnitRenderActionFlags *ioActionFlags,
                                 const AudioTimeStamp *inTimeStamp,
                                 UInt32 inBusNumber,
                                 UInt32 inNumberFrames,
                                 AudioBufferList *ioData) {
    // Notes: ioData contains buffers (may be more than one!)
    // Fill them up as much as you can. Remember to set the size value in each buffer to match how
    // much data is in the buffer.
    NSLog(@"muffers %d", ioData->mNumberBuffers);
    UInt32 size = 2048;
    if (iosAudio->incomingCircularBuffer.fillCount>size){
        NSLog(@"Playing %d", iosAudio->incomingCircularBuffer.fillCount);
        iosAudio.pkgtotal -=2;
        int32_t availableBytes;
        SInt16 *databuffer = TPCircularBufferTail(&iosAudio->incomingCircularBuffer, &availableBytes);
        memcpy(ioData->mBuffers[0].mData, databuffer, size);
        ioData->mBuffers[0].mDataByteSize = size; // indicate how much data we wrote in the buffer
        TPCircularBufferConsume(&iosAudio->incomingCircularBuffer, size);
    }else{
    }

    return noErr;
}

The AudioStreamDescription

// Describe format
AudioStreamBasicDescription audioFormat;
bzero(&audioFormat, sizeof(AudioStreamBasicDescription));
UInt32 channelCount = 2;
UInt32 sampleSize = sizeof(UInt16);
audioFormat.mSampleRate         = 44100.00;
audioFormat.mFormatID           = kAudioFormatLinearPCM;
audioFormat.mFormatFlags        = kAudioFormatFlagsCanonical;;
audioFormat.mFramesPerPacket    = 1;
audioFormat.mChannelsPerFrame   = channelCount;
audioFormat.mBitsPerChannel     = sampleSize * 8;
audioFormat.mBytesPerPacket     = sampleSize * channelCount;
audioFormat.mBytesPerFrame      = sampleSize * channelCount;

// Apply format
status = AudioUnitSetProperty(audioUnit,
                              kAudioUnitProperty_StreamFormat,
                              kAudioUnitScope_Output,
                              kInputBus,
                              &audioFormat,
                              sizeof(audioFormat));
Jon Brooks
  • 2,472
  • 24
  • 32
Peter Fong
  • 11
  • 1
  • 2

0 Answers0