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.

Share and Enjoy:
  • StumbleUpon
  • Digg
  • Reddit
  • del.icio.us
  • Suggest to Techmeme via Twitter
  • Technorati
  • Slashdot
  • HackerNews
  • Twitter
  • Facebook
  • Print this article!

RSS feed | Trackback URI

5 Comments »

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 [...]

 
Comment by Charles Dumont
2008-12-07 21:24:10

Thanks for these tutorials. Any idea why I get this error after building up to Part 2?

2008-12-07 20:20:28.416 WorldJam[603:20b] Error loading /Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder: dlopen(/Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder, 262): Symbol not found: _SCDynamicStoreCopyConsoleUser
Referenced from: /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/LangAnalysis.framework/Versions/A/LangAnalysis
Expected in: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.sdk/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration
=shlibs-removed,shlib-info=[num="49",name="DivX Decoder",kind="-",dyld-addr="0x6d9000",reason="dyld",requested-state="E",state="E",path="/Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder",description="/Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder",loaded_addr="0x6d9000",slide="0x6d9000",prefix=""]

This is the offending statement which is pasted from your code:

OSStatus status = AudioQueueNewInput(&recordState.dataFormat,
AudioInputCallback,
&recordState,
CFRunLoopGetCurrent(),
kCFRunLoopCommonModes,
0,
&recordState.queue);

 
Pingback by ??iPhone????? Subscribed to comments via email
2009-03-04 22:59:42

[...] ?????http://trailsinthesand.com/exploring-iphone-audio-part-2/ [...]

 
Comment by Durgesh Subscribed to comments via email
2009-07-27 00:50:00

Main jat yamla pagla deewana O rabba itti si baat na jana ke ke..ke o menu pyar kardi hai, oye hoye hoye, sadde utte o mardi hai…burrra

 
Comment by durgesh Subscribed to comments via email
2009-09-04 22:00:00

durga naam hai mera. durga… Mar dalunga sab ko mar dalunga… Anpa, gaga, Shabo, rajo, anna. sab ko mar dalunga.. Salon ne meri le rakhi hai…. Pehli meri mannu ne li jab main xwalker pe tha. ab ye sale meri GOM pe le rahe hain…

Ye mat socho ki maine shadi karva li to ab main badal gya hun… Main nahi badla durga naam hai mera… Sab se badla lunga chun chun ke marunga…

Hu ha ha ha hu ha ha….

Oye gopi tu ki dekh rya hain…. Tenu vi dekh lavan ga…

Jai durga maiya ki… bolo.. bolo… jai durga maiya ki.

 
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> <pre lang="" line="" escaped=""> in your comment.