Testing the Loaders


Loader Design and Implementation Issues

Before embarking on a detailed examination of LoadersTests, I'd like to explain some of the design and implementation decisions that underpin ClipsLoader, MidisLoader, and their support classes, ClipsInfo and MidisInfo:


Background music

I'm assuming that a MIDI sequence will be utilized for background music (an action-packed, adrenaline-fueled technobeat or, perhaps, "I'm just a little black rain cloud"). The use of a sequence makes sense because MIDI files are considerably smaller than sampled audio recordings of the same music. As I mentioned in the previous chapter, high-quality stereo sampled audio may require about 10 MB per minute of sound, but a typical MIDI sequence may need less than 10 KB. A long piece of music should be encoded as a MIDI sequence to save disk space and load time. Since a game needs one background tune at a time, I've restricted MidisLoader to play only one sequence at a time. As a consequence, only one Sequencer object needs to be created at runtime, which reduces the loader's processing overheads.


Audio effects

Sound clips will be employed for audio effects, such as explosions, laser swooshes, and anguished criesshort-lived sounds for the most part. Several sound effects may need to be played at the same time, or overlap in time, so ClipsLoader supports concurrent playing of several clips. However, a proviso is built into the Java Sound API: one instance of a Clip object can be executing at a time. This means that an explosion clip, for example, cannot be played three times at once (you might want to do this to create an overlapping effect of multiple blasts). The solution is to load the explosion three times into ClipsLoader, with different names, creating three distinct Clip objects that can be played together.

This coding strategy is used for the fireball sounds in the JumpingJack example in Chapter 12.


Though I've assumed that clips will be short pieces of audio, longer sounds can be loaded and played; ClipsLoader's pause and resume features are useful for these longer files.


Preloading

Multiple clips and sequences can be loaded at startup based on filename lists supplied in text files or loaded via method calls during execution.

Most programmers will want to preload their audio so the sounds are ready to play during the game. I haven't included any methods for unloading clips or sequences, but they would be easy to add.



Sound formats

ClipsLoader supports more sound formats than WAV. The playing of ULAW or ALAW files is dealt with by converting them into PCM format as they're read in. This is achieved using the conversion code I wrote for the PlayClip class in Chapter 7.


The WAV bug

I've chosen not to code around the WAV file bug in Java Sound in J2SE 5.0, described in the last chapter. Instead, ClipsLoader issues a warning message if a clip is one second long or less. It's up to the programmer to lengthen short sounds, perhaps with a quiet hiss, to get their length up to at least 1.1 seconds.


Callbacks

Java Sound's Clip and Sequencer classes support looping, but they're inadequate for my purposes. I want the program to be notified when a loop iteration finishes, so the program has the option of carrying out some activity, such as stopping the sound or changing a visual element in the game. Therefore, when iteration ends in ClipsLoader and MidisLoader, a callback method named atSequenceEnd( ) will automatically be invoked.

The callback reports when a clip or sequence has finished. This occurs during the end of a loop iteration and the end of the audio and is distinguished by constants passed to the atSequenceEnd( ) call.


Similar APIs

Some effort has been made to make the APIs offered by ClipsLoader and MidisLoader as similar as possible, which is why, for example, they offer the atSequenceEnd( ) callback.


Copyright

One serious issue to consider before releasing a game with numerous sounds is copyright.

In Windows, a sound's copyright can be viewed as a side effect of playing it in the Windows Media Player: the details appear in the GUI. For example, the "Bach Sheep" and "Farmer in the Dell" sequences used in LoadersTests are copyrighted by David E Lovell and Diversified Software, respectively.

In general, I advise a do-it-yourself (DIY) policy on audio elements. Many sound effects can be generated by recording noises through a PC's microphone, then distorting them in a package such as WavePad. MIDI sequences should be created from scratch, perhaps with the help of a musician.



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net