views:

393

answers:

1

I am writing an iPhone application that needs to record audio from the built-in microphone and then send that audio data to a server for processing.

The application uses a socket connection to connect to the server and Audio Queue Services to do the recording. What I am unsure of is when to actually send the data. Audio Queue Services fires a callback each time it has filled a buffer with some audio data. NSOutputStream fires an event each time it has space available.

My first thought was to send the data to the server on the Audio Queue callback. But it seems like this would run into a problem if the NSOutputStream does not have space available at that time.

Then I thought about buffering the data as it comes back from the Audio Queue and sending some each time the NSOutputStream fires a space available event. But this would seem to have a problem if the sending to the server gets ahead of the audio recording then there will be a situation where there is nothing to write on the space available event, so the event will not be fired again and the data transfer will effectivly be stalled.

So what is the best way to handle this? Should I have a timer that fires repeatedly and see if there is space available and there is data that needs to be sent? Also, I think I will need to do some thread synchronization so that I can take chunks of data out of my buffer to send across the network but also add chunks of data to the buffer as the recording proceeds without risking mangling my buffer.

+2  A: 

You could use a ring buffer to hold a certain number of audio frames and drop frames if the buffer exceeds a certain size. When your stream-has-space-available callback gets called, pull a frame off the ring buffer and send it.

CHDataStructures provides a few ring-buffer (which it calls “circular buffer”) classes.

Peter Hosey
Thanks Peter, I have not come across these data structures before. It seems like this could be useful, although I am doing speech recognition on the server side, so I don't know that I can safely drop frames without effecting accuracy. Do you know of a thread safe queue structure for objective-c. I've been looking around and not finding anything. Maybe I have to write my own.
Mark Kanof
I have no idea about the thread safety about the classes in the framework I linked to. You should read its documentation and see what it says.
Peter Hosey