December 01, 1992
Sound As a Data TypeSOUND AS A DATA TYPE
QuickTime makes it easy
Aaron E. Walsh
Aaron is cofounder and CEO of Fore-Runner Development and Consulting Corp., a Boston-based software firm. He can be reached at 1357 Washington St., West Newton, MA 02135. Programming for sound has never been simple. In the Macintosh world, for instance, programmers used to face the onerous task of programming the Sound Driver which was, to be kind, a challenge. Nevertheless, until Apple released the Sound Manager in System 6.0.2, there were no alternatives. Although difficult to master, the sound manager was a dramatic improvement over previous methods of sound programming. You still had to have an intimate knowledge of complex sound-data structures for anything other than simply playing a sound at preset volumes. Playing a sound asynchronously, altering the playback volume, or even playing two different sounds at the same time often required more effort than it was worth. QuickTime, Apple's system-wide architecture for handling dynamic data--animation, video, and audio--goes beyond the Sound Manager by defining a data type called a movie for dealing with dynamic data. A movie is simply a structure containing 0 or more data streams, known as tracks. Tracks reference (via pointers) a single-track media, which in turn references the raw data samples comprising a movie. Figure 1 illustrates the basic QuickTime movie structure. (For more information on QuickTime movies, see "Programming QuickTime," DDJ, July 1992.) QuickTime 1.0 is incapable of directly playing the popular sound resource (snd) data type developers rely on as the Macintosh sound format standard. To do so, snd resource data structures must be manually extracted into the QuickTime sound format. Documentation for converting snd data types to the QuickTime sound format is nonexistent as of this article. The code and article presented here demonstrate a technique for converting traditional and sound resources to QuickTime sound data samples. QuickTime itself does not provide direct support for specific media types. This is the responsibility of media handler components, to which QuickTime provides a number of interface routines. When accessing and manipulating movie data via media handlers, you are not required to have detailed knowledge about the data itself. In this respect, graphic and audio elements are treated as data types for which a number of standard routines are provided. Compared to the original Sound Driver and Sound Manager routines, QuickTime makes sound management on the Macintosh amazingly simple:
Sound Description RecordTo add sound to a QuickTime movie, the sound-media handlers must be given a description of the sound data for which they will be responsible. A Sound Description record contains information such as sample size, sample rate, compression details, and so on. Figure 2 details the sound-description structure. When you provide raw data samples and a corresponding description of that data, QuickTime can add an audio track to any movie.
Figure 2: QuickTime Sound Description record.
struct SoundDescription
{
long descSize; /* total size in bytes of this SoundDescription
structure */
long dataFormat; /* describes sample encoding technique (i.e.,
off-set binary, twos-complement) */
long resvd1; /* reserved by Apple. Set to 0 in your sound
descriptions */
short resvd2; /* reserved by Apple. Set to 0 in your sound
descriptions */
short dataRefIndex; /* reserved. Set to 1 in your sound descriptions
*/
short version; /* reserved by Apple. Set to 0 in your sound
descriptions */
short revlevel; /* reserved by Apple. Set to 0 in your sound
descriptions */
long vendor; /* reserved by Apple. Set to 0 in your sound
descriptions */
short numChannels; /* number of channels of sound. Set to 1 for
monaural, set to 2 for stereo */
short sampleSize; /* number of bits per sample. Set to 8 for 8-bit
sound, 16 for 16-bit sound */
short compressionID; /* reserved. Set to 0 in your sound descriptions
*/
short packetSize; /* reserved. Set to 0 in your sound descriptions
*/
Fixed sampleRate; /* rate at which samples were obtained. Field
contains an unsigned, fixed point number. */
};
You're responsible for filling in the sound description structure, for which the fields in Table 1 must be set corresponding to the original audio sample; the remaining fields are reserved for use by Apple. Set each of these to 0, with the exception of dataRefIndex, which should be set to 1; see Figure 2.
Table 1: Fields of the sound-description structures.
Function Description ----------------------------------------------------------------------
Listing One (page 102) details how to create a sound-description record based on the data structures of a sound (snd) resource. This technique is an effective means of bridging the sound-format incompatibility between QuickTime and the Sound Manager. On a computer, sound data is stored as a series of digital samples, each specifying the amplitude of the sound at a given time. This format is commonly known as pulse-code modulation (PCM). The amplitude values in a sample are typically encoded in one of two ways: offset-binary or twos-complement:
The number of bits used to encode the amplitude value for each sample is known as the sample size. A larger sample size provides more bits to encode the amplitude value. Hence, the size of a sample corresponds directly to the quality of sound. The standard Macintosh hardware is limited to 8-bit samples, where QuickTime supports up to 32-bit samples. When playing larger samples on a standard Macintosh, QuickTime converts the samples to 8-bit format for compatibility. Also influencing the quality of sound is the sample rate. The sample rate represents the number of samples captured in one second. A higher sample rate can more accurately describe the original sound waveform. The standard Macintosh hardware is capable of an output sampling rate of 22.2545 KHz, where QuickTime supports up to 65.535 KHz. QuickTime will convert higher sample rates to accommodate the playback hardware. Sound can be added to a QuickTime movie in real time, in which case an audio digitizing device is required to convert the analog sound wave into its digital representation. Alternately, sound can be added directly from a disk file.
Creating a Sound TrackThe code in Listing One demonstrates the technique of taking an existing sound resource (snd resource type) from a disk file and incorporating it into a QuickTime movie as a new sound track. The user is prompted to select a file containing the sound resource from which data samples will be extracted, and prompted again to select the movie file into which the sound will be added. ConvertSndToMovie() opens the selected movie file and adds a new track for the sound data. Using the sound-description structure created by CreateSndDescriptor(), ConvertSndToMovie() then adds sound samples to the track media. CreateSndDescriptor() creates a sound-description structure based on data in the original sound (snd) resource. This routine handles only type 1 snd resources containing uncompressed raw data stored in offset-binary format. While the majority of sound resources fall into this category, you may wish to enhance the code to handle MACE compressed resources. Once the sound track has been added to the movie file, execution of the application is terminated. The original sound resource and its associated file remain unchanged, while the QuickTime movie now has a new sound track. The sound samples are saved directly into the movie file itself, although QuickTime allows data samples to be saved as an external file as well.
Manipulating QuickTime SoundVolume and balance are the two main attributes of sound in QuickTime. Essentially, QuickTime treats sound as a data type. A variety of routines are provided which simplify the interaction between the programmer and the data. Among these are:
ConclusionQuickTime sound support is simple, once the sound data has been successfully saved into the movie format. The current version of QuickTime, Version 1.0, does not provide an API for converting snd sound resources or AIFF sound files to the movie sound format. Ideally, adding sounds to a QuickTime movie should require no more effort than simply specifying the sound file to play. Until this is possible, the developer must devise techniques (such as the one presented here) for converting the non-QuickTime sound formats into the QuickTime format. With QuickTime, Apple has come a long way towards making the sound-data structure a standard data type on the Macintosh. However, the issue of interformat sound-data exchange remains, though I find it difficult to complain when much of the headache of sound manipulation has been resolved by QuickTime.
[LISTING ONE]
Copyright © 1992, Dr. Dobb's Journal
|
|
||||||||||||||||||||||||||||
|
|
|
|