C H A P T E R 9
■ ■ ■
Effect: Audio Visualizer
I have always been amazed how the human mind is capable of connecting sounds we hear with
something that we see. When my cat meows, I hear the sound and see the motion of the cat, and
somehow these two different sensory experiences are combined into a single event. Computers have
been used for years to visualize audio data, and being able to see the data update as you hear the sound
being analyzed provides insight into sounds that would not be possible by just listening alone. JavaFX is
an excellent tool for graphics, and Java has passable media support; this chapter will show how to
combine these tools to create our own live graphical representations of audio.
We will explore how to create an audio visualization in JavaFX. We will discuss a little bit about what
digital audio is in the first place and what it means to visualize sound. We will take a quick look at media
support in JavaFX and see that it won't give us access to the live raw data we need. We will then explore
the Java Sound API to learn how to create our own audio processing thread, which will enable us to
perform calculations on the raw audio data as it is being played.
Since we will be working with both Java and JavaFX, we will look at how these two environments can
work together to create a JavaFX-friendly audio API. The end of the chapter will then use our new JavaFX
audio API to make a simple player and three different examples of audio visualizations.
What Is an Audio Visualizer?
In the simplest terms, an audio visualizer is any graphic that is derived from audio data. To understand
what that means, it is worth starting from the beginning and describing a little bit about what sound is
and how digital audio works. In the most basic terms, sound is a change of air pressure on our eardrums.
When we speak, our throat and mouths rapidly change the air pressure around us, and this change in
pressure is propagated through the air and is eventually detected by our listener's ears.
Understanding that a particular sound correlated to a pattern in air pressure allowed early inventors
to create ways of recording sounds and playing it back. If we consider the phonograph, we can see that the
cylinder that holds the recording has captured a particular pattern of changing air pressure in its grooves.
When the needle of a phonograph is vibrated by those grooves, it re-creates the original sound by moving a
speaker, which in turn re-creates the changes in air pressure, which comprised the original sound.
Digital audio works by measuring the change in pressure several thousand times a second and
saving those measurements in a digital file. So when digital audio is played back, a computer reads each
of those values in the file and creates a voltage in a speaker wire proportional to that value. The voltage
in the wire then moves the membrane of a speaker by a proportional amount. The movement of the
speaker moves the air around it, which eventually moves the air in our ears. So, in essence, each value