Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Playing PCM buffers from a stream

Tags:

ios

audio

pcm

I'm receiving a series of UDP packets from a socket containing encoded PCM buffers. After decoding them, I'm left with an int16 * audio buffer, which I'd like to immediately play back.

The intended logic goes something like this:

init(){
   initTrack(track, output, channels, sample_rate, ...);
}

onReceiveBufferFromSocket(NSData data){
   //Decode the buffer
   int16 * buf = handle_data(data);

   //Play data
   write_to_track(track, buf, length_of_buf, etc);
}

I'm not sure about everything that has to do with playing back the buffers though. On Android, I'm able to achieve this by creating an AudioTrack object, setting it up by specifying a sample rate, a format, channels, etc... and then just calling the "write" method with the buffer (like I wish I could in my pseudo-code above) but on iOS I'm coming up short.

I tried using the Audio File Stream Services, but I'm guessing I'm doing something wrong since no sound ever comes out and I feel like those functions by themselves don't actually do any playback. I also attempted to understand the Audio Queue Services (which I think might be close to what I want), however I was unable to find any simple code samples for its usage.

Any help would be greatly appreciated, specially in the form of example code.

like image 690
Sergio Morales Avatar asked Jan 21 '13 22:01

Sergio Morales


2 Answers

You need to use some type of buffer to hold your incoming UDP data. This is an easy and good circular buffer that I have used.

Then to play back data from the buffer, you can use Audio Unit framework. Here is a good example project.

Note: The first link also shows you how to playback using Audio Unit.

like image 80
user523234 Avatar answered Sep 27 '22 18:09

user523234


You could use audioQueue services as well, make sure your doing some kind of packet re-ordering, if your using ffmpeg to decode the streams there is an option for this.

otherwise audio queues are easy to set up.

https://github.com/mooncatventures-group/iFrameExtractor/blob/master/Classes/AudioController.m

You could also use AudioUnits, a bit more complicated though.

like image 41
Michelle Cannon Avatar answered Sep 27 '22 20:09

Michelle Cannon