Previous Book Contents Book Index Next

Inside Macintosh: Sound /
Chapter 2 - Sound Manager / Using the Sound Manager


Legacy Documentclose button

Important: Inside Macintosh: Sound is deprecated as of Mac OS X v10.5. For new audio development in Mac OS X, use Core Audio. See the Audio page in the ADC Reference Library.

Compressing and Expanding Sounds

Some of the capabilities provided by MACE are transparently available to your application. For example, if you pass the SndPlay function a handle to an 'snd ' resource that contains a compressed sampled sound, the Sound Manager automatically expands the sound data for playback in real time. Your application does not need to know whether the 'snd ' resource contains compressed or noncompressed samples when it calls SndPlay. This is because sufficient information is in the resource itself to allow the Sound Manager to determine whether it should expand the data samples.

However, aside from expansion playback, all of the MACE capabilities need to be specifically requested by your application. For example, you can use the procedure Comp3to1 or Comp6to1 if you want to compress a sampled sound (for example, to create an 'snd ' resource containing compressed audio data). You can use the procedures Exp1to3 and Exp1to6 to expand compressed audio data.

All of these procedures require you to specify both an input and an output buffer, from and to which the sampled-sound data to be converted is read and written. Your application must allocate the appropriate amount of storage for each buffer. For example, if you want to expand a buffer of compressed monophonic sampled-sound data by using Exp1to6, the output buffer must be at least six times the size of the input buffer.

The MACE compression and expansion routines can work on only one channel of sound. The numChannels parameter of all four procedures allows you to specify how many channels are in the original sample, and the whichChannel parameter allows you to specify which channel you wish to compress or expand. Because the MACE routines can compress or expand only one channel of sound, you must make adjustments when allocating an output buffer for stereo sound. For example, if you are compressing two-channel sound using the Comp3to1 procedure, your output buffer need only be one-sixth the size of your input buffer.

Often when compressing polyphonic sound, being able to compress only one channel is not a problem, because you lose sound quality during compression anyway. However, you might at times wish to maintain more than one channel of a multichannel sound even after compression and expansion. For example, two channels of a stereo sound might be quite different and might both be necessary to achieve a full sound after expansion. In these cases, you can compress each channel of a multichannel sound individually and then manually interleave the samples on a packet basis. When you expand polyphonic compressed sound data, you must interleave the channels of sound on a sample frame basis.

The MACE routines work only with sampled-sound data in offset binary format. If you are compressing data in a sound file, you must convert that data from linear, two's complement format to binary offset format before compression.

When calling the MACE routines, you can also specify addresses of two small buffers (128 bytes each) that the Sound Manager uses to maintain state information about the compression or expansion process. When you first call a MACE routine, the state buffers should be filled with zeros to initialize the state information. When you subsequently call another MACE routine, you can use the same state buffers. You can pass NIL for both buffers if you do not want to save state information across calls to the MACE routines. Listing 2-33 illustrates the use of the Comp3to1 procedure when using state buffers.

Listing 2-33 Compressing audio data

PROCEDURE MyCompressBy3 (inBuf: Ptr; outBuf: Ptr; numSamp: LongInt);
CONST 
   kStateBufferSize = 128;
VAR
   myInState:        Ptr;     {input state buffer}
   myOutState:       Ptr;     {output state buffer}
BEGIN
   myInState := NewPtrClear(kStateBufferSize);
   myOutState := NewPtrClear(kStateBufferSize);
   IF (myInState <> NIL) AND (myOutState <> NIL) THEN
      Comp3to1(inBuf, outBuf, numSamp, myInState, myOutState, 1, 1);
END;
Because the last two parameters (numChannels and whichChannel) are both set to 1, MyCompressBy3 compresses monophonic audio data.

In practice, compressing a sound resource or sound file is considerably more complex than calling the MyCompressBy3 procedure defined in Listing 2-33. To compress a sound resource containing monophonic sampled-sound data, you would need to

Techniques for compressing sound files and for expanding both sound resources and sound files are analogous to that sketched here. Remember that after compressing or expanding each channel of polyphonic sampled-sound data, you must interleave frames of sound data, on a packet basis after compression or on a sample basis after expansion.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
2 JUL 1996