Exploring iPhone Audio Part 3
March 26th, 2008 Posted in Software Development, iPhone
In the last article we learned how to open a new audio input queue, allocate some buffers and enqueue the buffers for recording.
Now we are going to see an extremely simple audio input callback function and learn how to start recording.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | void AudioInputCallback( void *inUserData, // 1 AudioQueueRef inAQ, // 2 AudioQueueBufferRef inBuffer, // 3 const AudioTimeStamp *inStartTime, // 4 UInt32 inNumberPacketDescriptions, // 5 const AudioStreamPacketDescription *inPacketDescs) // 6 { static int count = 0; RecordState* recordState = (RecordState*)inUserData; AudioQueueEnqueueBuffer(recordState->queue, inBuffer, 0, NULL); ++count; printf("Got buffer %d\n", count); } |
- The first parameter is a void pointer that will actually point to our RecordState structure that was passed into AudioQueueNewInput. We’ll see in later articles how we can use this to keep track of the state of our recording.
- This is a reference to the audio input queue which is also in our RecordState structure.
- This is the buffer that has just been filled. In our case this will contain 1 second worth of audio.
- inStartTime is a timestamp value that can be used to syncronize audio.
- The number of packet descriptions in parameter 6
- An array of packet descriptions. We’ll discuss this in later articles.
The first thing this callback function does is cast the inUserData void pointer to a pointer to our RecordState structure. This simple callback function doesn’t do much. It re-enqueues the buffer that was filled so that it can be re-used. To show you that things are actually working it will print a message to the debug console for each packet recorded. Shift-Command-R will open your debug console.
We can start recording with the following simple call:
1 | OSStatus status = AudioQueueStart(recordState.queue, NULL); |
If AudioQueueStart is successful the status will be zero. Recording can be stopped with a call to AudioQueueStop. Recording can be paused with a call to AudioQueuePause.
In the next article we’ll look at how to do something with the audio buffers that are being sent to the callback function. We will open an audio file and write the recorded packets to that file.

![[del.icio.us]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/digg.png)
![[Reddit]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/reddit.png)
![[Technorati]](http://trailsinthesand.com/wp-content/plugins/bookmarkify/technorati.png)
Related Articles:

[...] working on the Exploring iPhone Audio Trail I wanted to look at the audio files that I was recording. My application is getting it’s [...]
[...] Part 3 of this series of articles we created the AudioInputCallback function that does nothing more than [...]