Audio Library Class Reference


Overview

Audio Library is a Cocoa interface to the Mac OS X Core Audio functions. The aim of the library is to provide a simple to use layer around Core Audio for accessing both built-in and external sound cards and sound files.

Audio Library can automatically manage various NSControls that are associated with sound cards, such as NSPopUpButtons for source selection and sampling rate selection, and NSSliders for volume controls. The controls that are managed by Audio Library automatically tracks changes that are made to a sound card by other processes. An NSPopUpButton for selecting sound cards can also be managed automatically; the menu is updated automatically when an external sound card is inserted or removed.

Audio Library can call a delegate whenever a parameter such as the sampling rate or the volume of the selected sound card is changed by a different process.

Both input and output sound card interfaces include sampling rate converters to allow a client to set a sampling rate that it wishes to use, irrespective of the hardware sampling rate of the sound card.

In addition to the usual Core Audio mechanism that "pulls" data from a client,
Audio Library includes a ring buffer for each output interface to allow data to be "pushed" to it. Each output sound card interface also includes a Varispeed Audio Unit that allows the output sampling rate to track the sampling rate of an input device. Together, the two Audio Library functions allow a "play though" from an input to an output sound card to be easily written.

The
Programming Guide section has code snippets showing examples of usage. See below for download instructions for both the Audio Library and complete example Xcode projects.



Requirements

Audio Library requires Snow Leopard (Base SDK: Mac OS X 10.6), or newer.


Usage

To facilitate easy debugging (breakpointing the library's code), in addition to the framework (AudioLibrary.framework) the
Audio Library is distributed as a collection of sources in an Audio Library folder.

Include
AudioLibrary.framework with your Xcode project, and additionally, include the following frameworks

AudioToolbox.framework
AudioUnit.framework
CoreAudio framework


To embed the
AudioLibrary framework into your application bundle (so the user will not need to add it to their /Library/Frameworks folder), follow the instructions here.



Example Code

The following six sample code projects show examples of connecting between an input sound card and an output sound card (Playthrough project), processing numerical data from an input sound card (Oscilloscope project), generating numerical data and sending to an output sound card (Generator project), reading an input sound file and writing to a sound card (Playfile project), reading input from an input sound card input and writing it to an output sound file (Recorder), and reading from a sound file and writing to an output sound file that has a different format (Format Converter project).

Playthrough

playthru

Playthrough is an application that uses the
"push" routine and rate scale parameter of AFSoundcard to play sound buffers from the default input sound card on the default output sound card. The example lets the Audio Library manage a set of volume control sliders.

The Xcode project for Playthrough is included in the Audio Library
download.

Oscilloscope

scope

Oscilloscope is an application that takes audio buffers from an input AFSoundcard and submit them to a subclass of NSView to display. The example lets the Audio Library manage the menu for selecting an input sound card.

The Xcode project for Oscilloscope is included in the Audio Library
download.

Audio Signal Generator

generator

Generator is an application that synthesizes audio samples for the default output AFSoundcard when buffers are requested. The example uses the channelMask in AFSoundcard to extract a single mono channel (either Left or Right channel of a stereo sound card). You can change the frequency, change the volume and you have the choice of generating a sine, square, or triangular waveform.

The Xcode project for Generator is included in the Audio Library
download.

Sound File Player

playfile

PlayFile is an application that plays a sound file from an AFSoundfile to the default output AFSoundcard. The example sets the resampling rate of both the AFSoundfile and the AFSoundcard to 48000. The example also uses the -elapse and -duration methods of the AFSoundfile to draw a progress bar, and using the file name from the -url method of AFSoundcard.

The Xcode project for PlayFile is included in the Audio Library
download.

Sound File Recorder

rec

Recorder is an application that records from the default input AFSoundcard to a sound file. The example sets the resampling rate of both the AFSoundfile and the AFSoundcard to 48000. The example also uses the -elapse to display the "size" of the file. The recording is initially written to /tmp. It allows multiple record/sop sessions to be appended to the sound file and when the application terminates, Recorder presents a file save dialog to let the user rename the file (or remove the file in /tmp if the user cancels the save dialog).

The Xcode project for Recorder is included in the Audio Library
download.

Audio Format Converter

fc

Format Converter is an application that reads from an input
AFSoundfile and writes to an output AFSoundFile. The two AFSoundfile objects can have different properties (format, sampling rate, etc) and the sampling rate conversion happens automatically by setting the resampling rate of the output AFSoundfile to the sampling rate of the input file. The format conversion is done automatically by selecting the format of the output file when it is created.


Download

The
Audio Library folder is in a disk image (dmg) which you can download from here. The disk image includes a folder of the Audio Library sources, AudioLibrary.framework and the sample code projects.