An Overview of the Sound File Player


The idea behind the sound file player is similar to the idea behind double buffered animation: provide separate buffers for sampled sound playback, then play back the sound buffers in continuous sequence as shown in Figure 1. While one buffer plays, the sound file player can safely write more of the sound file into buffers not being played. Because the spooler need only write to buffers at intervals (and not continuously), glitches caused by interrupted or slow disc access are unlikely. Playback continues smoothly from buffer to buffer without pause.

Graphic cannot be displayed

Figure 1: Playing sounds in a continuous sequence

The sound file player is build on top of a general sound spooler. See Using the Sound Spooler for a description.

A development note: the sound file player works better spooling a sound file from CD than it does spooling a sound file from the Macintosh in a development system because the file system on the CD uses DMA.

Sound File Player Operation

To play a sound file through once, follow these steps:

  1. Create a SoundFilePlayer and some sound buffers.

    sfp = CreateSoundFilePlayer ( NUMBUFFS, BUFSIZE, PassBuffers );
    CHECKPTR(sfp, "CreateSoundFilePlayer");

  2. Open the sound file and preload the first part of the file into the buffers.

    Result = LoadSoundFile( sfp, FileName );
    CHECKRESULT(Result,"LoadSoundFile");

  3. Start playback.

    Result = StartSoundFile( sfp, MAXAMPLITUDE );
    CHECKRESULT(Result,"StartSoundFile");

  4. Sit in a loop waiting for signals from sound file player and handing them off to the sound file player's service function.

    do
    {
    Result = ServiceSoundFile(sfp, SignalsIn, &SignalsNeeded);
    CHECKRESULT(Result,"ServiceSoundFile");
    } while ((SignalsNeeded);

  5. Stop playback.

    Result = StopSoundFile (sfp);
    CHECKRESULT(Result,"StopSoundFile");

  6. Close sound file.

    Result = UnloadSoundFile( sfp );
    CHECKRESULT(Result,"UnloadSoundFile");

  7. Dispose of the SoundFilePlayer and associated buffers.

DeleteSoundFilePlayer( sfp );
See playsoundfile.c in the Examples folder for a complete program listing.