
386
|
Chapter 10, Audio
#76 Play Non-Trivial Audio
HACK
This hack shows you how to do all of the sample conversion
and painting you need to display a waveform very simplisti-
cally. However, you should address a few key issues before
using this in an audio application; for example, this only
deals with 16-bit audio. You probably would want to build
something a little more generic to deal with other sample
sizes. You may also want to deal with compression, so you
can display waveforms for MP3 files. That said, this hack
still gives you a good idea of how to dig into raw audio data
and get your audio visualization on.
—Jonathan Simon
H A C K
#76
Play Non-Trivial Audio Hack #76
When loading an entire audio clip into memory is a bad idea (or just
impossible), you have to take JavaSound responsibilities into your own
hands.
Playing JavaSound audio with a Clip [Hack #71] is a pretty convenient way to
play a short sound, like a sound effect for a desktop application. The only
problem is that the
Clip loads all the audio into memory, which could have
a couple of bad side effects:
• It makes your application use more memory, which could cause problems.
• The audio you need might not fit into memory at all.
You might have run into this second point if you tried to load a really big
audio file into a
Clip. For example, I took a 3 minute, 45 second track from
a CD and converted it to 8-bit mono PCM in an AIFF file, which ...