RSS

Exploring iPhone Audio Part 2

March 25th, 2008 Posted in Software Development, iPhone

Old Fashioned Microphone

Last time we created the RecordState structure to keep track of the recording state. We also configured the recording parameter to record 8000 samples per second, 16 bit, mono audio.







Now we can create the audio output queue with the following call:

1
2
3
4
5
6
7
8
OSStatus status = AudioQueueNewInput(
                &recordState.dataFormat, // 1
                AudioInputCallback, // 2
                &recordState,  // 3
                CFRunLoopGetCurrent(),  // 4
                kCFRunLoopCommonModes, // 5
                0,  // 6
                &recordState.queue);  // 7
  1. The first parameter is a pointer to the dataFormat structure in our RecordState structure.
  2. This is a callback function that will be defined in the next article of this series.
  3. Parameter 3 allows you to pass a pointer to whatever you want. We will pass a pointer to our RecordState structure.
  4. CFRunLoopGetCurrent causes the callback to be called on the main application thread. Passing NULL for this parameter will cause the callback to be called on the audio queue’s internal thread. We may explore the ramifications of changing this in a future article.
  5. To tell you the truth I’m not really sure what this is for. For now I’m content to leave it set to kCFRunLoopCommonModes.
  6. Reserved. Must be zero.
  7. A pointer to the input audio queue reference in our RecordState structure. This reference will be populated if the call succeeds.

The status return variable will be set to zero if the call succeeds.

Next we need to allocate memory for buffers and queue up the buffers.

1
2
3
4
5
6
7
for(int i = 0; i < NUM_BUFFERS; i++)
{
	AudioQueueAllocateBuffer(recordState.queue,
             16000, &recordState.buffers[i]);
	AudioQueueEnqueueBuffer(recordState.queue,
             recordState.buffers[i], 0, NULL);
}

Each buffer is given 16000 bytes of data which for 8000 16 bit (2 byte) samples per second will give us 1 second worth of recording space per buffer. For now this is hard coded. In later articles we’ll get more elaborate on calculating the buffer size.

Each time the callback specified in the second parameter of the AudioQueueNewInput function is called we will get a filled buffer. The callback function will re-enqueue the buffer by calling AudioQueueEnqueueBuffer again for the completed buffer. This allows large amounts of audio data to be captured without using large amounts of memory. This is very important for a device like the iPhone which has limited memory.

The next article will discuss how to handle the audio data in the callback function.

[del.icio.us] [Digg] [Reddit] [Technorati]

RSS feed | Trackback URI

1 Comment »

2008-03-26 13:45:40

[...] the last article we learned how to open a new audio input queue, allocate some buffers and enqueue the buffers for [...]

 
Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.