- The Core Audio Frameworks
- Core Audio Conventions
- Your First Core Audio Application
- Core Audio Properties
- Summary
Your First Core Audio Application
Now let’s get a feel for Core Audio code by actually writing some. The audio engine APIs have a lot of moving parts and are, therefore, more complex, so we’ll make trivial use of one of the helper APIs. In this first example, we’ll get some metadata (information about the audio) from an audio file.
Launch Xcode, go to File > New Project, select the Mac OS X Application templates, and choose the Command Line Tool template; select the Foundation type in the pop-up menu below the template icon. When prompted, call the project CAMetadata. The resulting project has one user-editable source file, main.m, and produces an executable called CAMetadata, which you can run from Xcode or in the Terminal.
Select the CAMetadata.m file. You’ll see that it has a single main() function that sets up an NSAutoReleasePool, prints a “Hello, World!” log message, and drains the pool before terminating. Replace the comment and the printf so that main() looks like Listing 1.1. We’ve added numbered comments to the ends of some of the statements as callouts so that we can explain what this code does, line by line.
Listing 1.1 Your First Core Audio Application
int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; if (argc < 2) { printf ("Usage: CAMetadata /full/path/to/audiofile\n"); return -1; } // 1 NSString *audioFilePath = [[NSString stringWithUTF8String:argv[1]] stringByExpandingTildeInPath]; // 2 NSURL *audioURL = [NSURL fileURLWithPath:audioFilePath]; // 3 AudioFileID audioFile; // 4 OSStatus theErr = noErr; // 5 theErr = AudioFileOpenURL((CFURLRef)audioURL, kAudioFileReadPermission, 0, &audioFile); // 6 assert (theErr == noErr); // 7 UInt32 dictionarySize = 0; // 8 theErr = AudioFileGetPropertyInfo (audioFile, kAudioFilePropertyInfoDictionary, &dictionarySize, 0); // 9 assert (theErr == noErr); // 10 CFDictionaryRef dictionary; // 11 theErr = AudioFileGetProperty (audioFile, kAudioFilePropertyInfoDictionary, &dictionarySize, &dictionary); // 12 assert (theErr == noErr); // 13 NSLog (@"dictionary: %@", dictionary); // 14 CFRelease (dictionary); // 15 theErr = AudioFileClose (audioFile); // 16 assert (theErr == noErr); // 17 [pool drain]; return 0; }
Now let’s walk through the code from Listing 1.1:
- As in any C program, the main() method accepts a count of arguments (argc) and an array of plain C-string arguments. The first string is the executable name, so you must look to see if there’s a second argument that provides the path to an audio file. If there isn’t, you print a message and terminate.
- If there’s a path, you need to convert it from a plain C string to the NSString/CFStringRef representation that Apple’s various frameworks use. Specifying the UTF-8 encoding for this conversion lets you pass in paths that use non-Western characters, in case (like us) you listen to music from all over the world. By using stringByExpandingTildeInPath, you accept the tilde character as a shortcut to the user’s home directory, as in ~/Music/....
- The Audio File APIs work with URL representations of file paths, so you must convert the file path to an NSURL.
- Core Audio uses the AudioFileID type to refer to audio file objects, so you declare a reference as a local variable.
- Most Core Audio functions signal success or failure through their return value, which is of type OSStatus. Any status other than noErr (which is 0) signals an error. You need to check this return value on every Core Audio call because an error early on usually makes subsequent calls meaningless. For example, if you can’t create the AudioFileID object, trying to get properties from the file that object was supposed to represent will always fail. In this example, we’ve used an assert() to terminate the program instantly if we ever get an error, in callouts 7, 10, 13, and 17. Of course, your application will probably want to handle errors with somewhat less brutality.
- Here’s the first Core Audio function call: AudioFileOpenURL. It takes four parameters, a CFURLRef, a file permissions flag, a file type hint, and a pointer to receive the created AudioFileID object. You do a toll-free cast of the NSURL to a CFURLRef to match the first parameter’s defined type. For the file permissions, you pass a constant to indicate read permission. You don’t have a hint to provide, so you pass 0 to make Core Audio figure it out for itself. Finally, you use the & (“address of”) operator to provide a pointer to receive the AudioFileID object that gets created.
- If AudioFileOpenURL returned an error, die.
- To get the file’s metadata, you will be asking for a metadata property, kAudioFilePropertyInfoDictionary. But that call requires allocating memory for the returned metadata in advance. So here, we declare a local variable to receive the size we’ll need to allocate.
- To get the needed size, call AudioFileGetPropertyInfo, passing in the AudioFileID, the property you want information about, a pointer to receive the result, and a pointer to a flag variable that indicates whether the property is writeable (because we don’t care, we pass in 0).
- If AudioFileGetPropertyInfo failed, terminate.
- The call to get a property from an audio file populates different types, based on the property itself. Some properties are numeric; some are strings. The documentation and the Core Audio header files describe these values. Asking for kAudioFilePropertyInfoDictionary results in a dictionary, so we set up a local variable instance of type CFDictionaryRef (which can be cast to an NSDictionary if needed).
- You’re finally ready to request the property. Call AudioFileGetProperty, passing in the AudioFileID, the property constant, a pointer to the size you’re prepared to accept (set up in callouts 8–10 with the AudioFileGetPropertyInfo call) and a pointer to receive the value (set up on the previous line).
- Again, check the return value and fail if it’s anything other than noErr.
- Let’s see what you got. As in any Core Foundation or Cocoa object, you can use "%@" in a format string to get a string representation of the dictionary.
- Core Foundation doesn’t offer autorelease, so the CFDictionaryRef received in callout 12 has a retain count of 1. CFRelease() releases your interest in the object.
- The AudioFileID also needs to be cleaned up but isn’t a Core Foundation object, per se; therefore, it doesn’t get CFRelease()’d. Instead, it has its own end-of-life function: AudioFileClose().
- AudioFileClose() is another Core Audio call, so you should continue to check return codes, though it’s arguably meaningless here because you’re two lines away from terminating anyway.
So that’s about 30 lines of code, but functionally, it’s all about setting up three calls: opening a file, allocating a buffer for the metadata, and getting the metadata.
Running the Example
That was probably more code than you’re used to writing for simple functionality, but it’s done now. Let’s try it out. Click build; you get compile errors. Upon inspection, you should see that all the Core Audio functions and constants aren’t being found.
This is because Core Audio isn’t included by default in Xcode’s command-line executable project, which imports only the Foundation framework. Add a second #import line:
#import <AudioToolbox/AudioToolbox.h>
Audio Toolbox is an “umbrella” header file that includes most of the Core Audio functionality you’ll use in your apps, which means you’ll be importing it into pretty much all the examples. You also need to add the framework to your project. Click the project icon in Xcode’s file navigator, select the CAMetadata target, and click the Build Phases tab. Expand the Link Binaries with Libraries section and click the + button to add a new library to be linked at build time. In the sheet that slides out, select the AudioToolbox.framework, as shown in Figure 1.1.
Figure 1.1 Adding the AudioToolbox.framework to an Xcode project
Now you should be able to build the application without any errors. To run it, you need to provide a path as a command-line argument. You can either open the Terminal and navigate to the project’s build directory or supply an argument with Xcode. Let’s do the latter:
- From the Scheme pop-up menu, select Edit Scheme.
- Select the Run CAMetadata item and click the Arguments tab.
- Press + to add an argument and supply the path to an audio file on your hard drive.
- If your path has spaces in it, use quotation marks. For example, we’re using an MP3 bought online, located at ~/Music/iTunes/iTunes Music/Amazon MP3/Metric/Fantasies/05 - Gold Guns Girls.mp3. Click OK to dismiss the Scheme Editor sheet.
- Bring up Xcode’s Console pane with Shift--C and click Run.
Assuming that your path is valid, your output will look something like this:
2010-02-18 09:43:17.623 CAMetadata[17104:a0f] dictionary: { album = Fantasies; "approximate duration in seconds" = "245.368"; artist = Metric; comments = "Amazon.com Song ID: 210266948"; copyright = "2009 Metric Productions"; genre = "Alternative Rock"; title = "Gold Guns Girls"; "track number" = "5/10"; year = 2009; }
Well, that’s pretty cool: You’ve got a nice dump of a lot of the same metadata that you’d see in an application such as iTunes. Now let’s check it out with an AAC song from the iTunes Store. Changing the command-line argument to something like ~/Music/iTunes/iTunes Music/Arcade Fire/Funeral/07 Wake Up.m4a gets you the following:
2010-02-18 09:48:15.421 CAMetadata[17665:a0f] dictionary: { "approximate duration in seconds" = "335.333"; }
Whoa! What happened to the metadata call?
Nothing, really: Nothing in the documentation promises what you can expect in the info dictionary. As it turns out, Core Audio offers richer support for ID3 tags in .mp3 files than the iTunes tagging found in .m4a files.