I'm currently attempting to play back audio which I receive in a series of UDP packets. These are decoded into PCM frames with the following properties:
- 2 channels
- interleaved
- 2 bytes per sample in a single channel (so 4 bytes per frame)
- with a sample rate of 48000.
Every UDP packet contains 480 frames, so the buffer's size is 480 * 2(channels) * 2(bytes per channel).
I need to set up an Audio Unit to play back these packets. So, my first question is, how should I set up the AudioStreamBasicDescription struct for the Audio Unit? Looking at the documentation I'm not even sure if interleaved PCM is an acceptable format.
This is what I've got so far:
struct AudioStreamBasicDescription {
Float64 mSampleRate; //48000
UInt32 mFormatID; //?????
UInt32 mFormatFlags; //?????
UInt32 mBytesPerPacket; //Not sure what "packet" means here
UInt32 mFramesPerPacket; //Same as above
UInt32 mBytesPerFrame; //Same
UInt32 mChannelsPerFrame; //2?
UInt32 mBitsPerChannel; //16?
UInt32 mReserved; //???
};
typedef struct AudioStreamBasicDescription AudioStreamBasicDescription;
Secondly, after setting it up, I'm not sure how to get the frames from the UDP callback to the actual Audio Unit rendering function.
I currently have a callback function from the socket listener in which I generate the int16 * buffers that contain the audio I want to play. As I understand it, I also have to implement a render callback for the audio unit of the following form:
OSStatus RenderFrames(
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData)
{
//No idea what I should do here.
return noErr;
}
Putting it all together, I think what my socket reception callback should do is decode the frames, and put them in a buffer structure, so that the RenderFrames callback can fetch the frames from that buffer, and play them back. Is this correct? And if it is, once I fetch the next frame in the RenderFrames function, how do I actually "submit it" for playback?