# Palomino - Sound Module

©2004,2009 Jim E. Brooks http://www.palomino3d.org

- Index
- Overview
- Class Design
- SoundBase
- Sample
- SDL Implementation
- PLIB Implementation

## Overview

[2007/12..2009/12]

Sound class plays sound files, can loop the same sound, and can adjust volume. Program will initiate asynchronous playing of a sound file (sample) by calling Sound::Play().

## Class Design

[2009/12]

A derivative of SoundBase initializes the sound system and controls the master volume.
A derivative of Sample plays sound files.
SoundBase provides caching of Samples.
SoundBase is able to define the Sample-related methods, but defers other functionality to a SoundBase derivative.

## SoundBase

[2009/12]

SoundBase provides generic functionality. SoundBase controls sound samples in terms of the base Sample class.

## Sample

[2009/12]

Sample means a waveform which is typically loaded from a sound file. The base Sample class defines a generic interface used by the SoundBase class. The derivatives of the Sample class are specific to a sound system and do the actual playing of a sound.

## SDL Implementation

[2009/12]

SDL_mixer is used. The SDL_RWop mechanism is used to read gzip-compressed sound files. SoundSDL creates either a SampleSDLWave or SampleSDLMusic object according to the kind of sound file. Difference between WAV/MUS is how many and which channels are used.

### SDL wave vs. music, SDL misnomers

[2009/12]

Wave means a sample/chunk. A SDL wave object is bound to a channel. A SDL music object isn't. Whether a sound file is a wave or music is determined when trying to use a SDL WAV() or MUS() function. SDL sound functions are misnomers, for example, SDL_*WAV() can load OGG etc music files too.

### SDL's underlying sound libraries

[2009/12]

- What works well:

Playing WAV and OGG.

- What doesn't:

Note that SDL_mixer is linked with various OPTIONAL LIBRARIES. The underlying SMPEG library is flakey and won't play all kinds of MPEGs. Playing MIDI does work well but requires timidity. On Debian 5 Linux, timidity has to run as a daemon.

- Summary:

Prefer .wav since SDL_mixer contains WAV code. Per the code of Mix_LoadWAV_RW(), no other kinds of sound is intrinsic.

## PLIB Implementation

[2007/12]

The developers of PLIB decided to require the application to perodically pulse PLIB to play sounds, deferring the choice of the frequency of the tick to the app developer. PLIB will play a segment of a sample once every tick. Therefore, the Sound class has a timer-tick functor.

PLIB alters a sample's volume by modifying its waveform (slow operation). The Sound class avoids PLIB distorting the waveform by restoring the original waveform (so changing volume becomes even slower).

PLIB will cause program exit if a sample is deleted while still playing. stopSample() and update() don't quite work according to the PLIB docs. The reliable way is to enqueue zombie samples then deleted them when their "play count" reaches zero.

Last modified: Wed Dec 16 16:53:16 CST 2009