I am currently trying to play the audio that I receive in a series of UDP packets. They are decoded into PCM frames with the following properties:
- 2 channels
- interleaved
- 2 bytes per sample in one channel (so 4 bytes per frame)
- with a sampling rate of 48000.
Each UDP packet contains 480 frames, so the buffer size is 480 * 2 (channels) * 2 (bytes per channel).
I need to configure the Audio Unit to play these packages. So my first question is: how do I set up the AudioStreamBasicDescription structure for the Audio Unit? Looking at the documentation, I'm not even sure that interleaved PCM is an acceptable format.
This is what I have 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 up, I'm not sure how to get frames from a UDP callback to the actual audio rendering function.
I currently have a callback function from a socket listener in which I generate int16 * buffers containing the audio I want to play. As I understand it, I also need to implement a render callback for an audio unit of the following form:
OSStatus RenderFrames( void *inRefCon, AudioUnitRenderActionFlags *ioActionFlags, const AudioTimeStamp *inTimeStamp, UInt32 inBusNumber, UInt32 inNumberFrames, AudioBufferList *ioData) {
Combining all this, I think that a socket reception callback should be executed, it is to decode the frames and put them in the buffer structure so that the RenderFrames callback can extract the frames from this buffer and play them back. Is it correct? And if so, as soon as I get the next frame in the RenderFrames function , how do I actually βsend itβ to play ?