How can I write samples to an AudioBuffer in Core Audio?

608 Views Asked by At

I have code that needs to write 16 bit PCM samples from memory into an AudioBuffer.

// Completely override the output callback function
- (void)
                 output:(EZOutput *)output
callbackWithActionFlags:(AudioUnitRenderActionFlags *)ioActionFlags
            inTimeStamp:(const AudioTimeStamp *)inTimeStamp
            inBusNumber:(UInt32)inBusNumber
         inNumberFrames:(UInt32)inNumberFrames
                 ioData:(AudioBufferList *)ioData {
    for (int i = 0; i < ioData->mNumberBuffers; i++) {
        AudioBuffer audioBuffer = ioData->mBuffers[i];
        for (int j = 0; j < audioBuffer.mDataByteSize; j++) {
            audioBuffer.mData[j];  //void* how do I know what this expects??
        }
    }
    // Fill the ioData with your audio data from anywhere
}

I'm using EZAUdio framework from https://github.com/syedhali/EZAudio to help playback some PCM audio data I download using a properietary mechanism.

How am I supposed to file a buffer of type void*? Isnt that just a pointer to an arbitrary location.

1

There are 1 best solutions below

0
On

The format of the data the buffer expects is given by -audioStreamBasicDescription in EZOutput. This returns an AudioStreamBasicDescription struct that you can read to determine the number of channels, bits per channel, etc.

To write to a void * you cast it to appropriate type before writing, if you're handling individual samples, or you call memcpy if your audio is already prepared.