Warp 1.5: Sound

Let me quickly introduce you to a convenient tool for generating sound output from a Java applet. The AudioDataStream class is part of the non-standard (and rather sparsely documented) sun.audio package. Basically, an AudioDataStream object works the same way as an InputStream (and is in fact a subclass of it), except that it can be fed into the AudioPlayer object (also part of sun.audio) and be heard as sound.

When used correctly, AudioDataStreams perform the same way as the more common AudioClips and take up as much memory. The difference is that you can't manipulate or create AudioClips inside a Java applet. They have to be created in advance and imported. But an AudioDataStream is just like any InputStream object. You can let an applet build one from scratch and fill it with whatever you like. That's what I did in this game.

The way sound data has to be coded in order to come out of the speakers as something recognizable is far from trivial, however. There is a rough conversion table listed on my Rainbow Notes melody editor documentation pages and you can check the source code of that applet to see how I compute it. A more time-consuming but easier way is to use, for example, Billy Donahue's (donahu@cooper.edu) lin2mu conversion function.

Now for something about the basic structure of my sound effects...

The sound of the spaceship firing is a square waveform with an exponentially decaying amplitude and a frequency dropping from very high to low, sort of like in this graph:

The crash & explosion sounds also have exponential decays, but their waveforms are (almost) random. That's usually referred to as noise. What you hear as a "plink" when a bullet hits an indestructible object is a slightly perturbed square waveform, with a faster decay.

The sound of an enemy bullet being fired is related to the spaceship firing sound, but starts out at a lower frequency and drops faster. Finally, the weird bonus sound has a frequency that quickly and repeatedly goes from low to high.

What is the advantage of using AudioDataStreams instead of AudioClips, since I could obviously have recorded or generated all of these sounds in advance? Well, for one thing you don't have to waste time downloading them over a slow Internet connection. Even on a relatively sluggish machine, the 44 kilobytes of sound data used in this game get computed in less than a second. Faster than a modem any day.