Java Reference
In-Depth Information
The
createEQBands
method begins by getting the list of
EqualizerBand
s from the
AudioEqualizer
associated
with the current
MediaPlayer
. The list is then cleared to make way for the bands that we want to create. The minimum
and maximum gains for an
EqualizerBand
on the current platform are accessible through the
EqualizerBand.MIN_
GAIN
and
EqualizerBand.MAX_GAIN
constants. These constants should always be used in calculations involving the
range of valid gain values. We use them to define the min, max, and mid (the midpoint of the gain range) variables. A
for
loop then creates the number of
EqualizerBand
s defined by the
BAND_COUNT
constant, setting their gain values in
a nice cosine wave pattern. This pattern emphasizes the bass and high-range frequencies, which makes a nice set of
default values.
Afterward, a second
for
loop iterates through the list of
EqualizerBand
instances we just created and creates
a corresponding
Slider
control and frequency
Label
for each one. Each
Slider
is created by the
createEQSlider
method. The new
Slider
's
value
property is bidirectionally bound to the
EqualizerBand
's
gain
property inside this
method. This is the crucial part that gives the user control of the band's gain. Note that each
Slider
is also given a
style class of
eqSlider
. We use this class later to add some nice styling to our equalizer display.
The final step in the
createEQBands
method is to set the alignment and grow constraints of the
Slider
and
Label
and add them to the
GridPane
layout node. The
Slider
s are set to grow horizontally so that the
GridPane
will expand
to fill the width of the window. Of course, the same result could have been achieved by setting the
Label
s to grow, but
then we would also have had to reset the
Label
's maximum size because all
Label
s, by default, have their maximum
size set equal to their preferred size. The
Label
s and
Slider
s populate rows one and two in the
GridPane
, leaving row
zero for the spectrum display.
MediaPlayer
has four properties that deal with the frequency spectrum of the current audio track:
audioSpectrumListener
,
audioSpectrumInterval
,
audioSpectrumNumBands
, and
audioSpectrumThreshold
. The
audioSpectrumListener
property is an instance of the
AudioSpectrumListener
interface. You use
MediaPlayer
's
setAudioSpectrumListener
method to attach the listener, which then enables the computation of spectrum data and
sends periodic updates to the listener's
spectrumDataUpdate
method. Calling
setAudioSpectrumListener
again with
a null value turns off audio spectrum calculations and updates.
The
audioSpectrumInterval
property lets you control the interval at which updates are sent to the
AudioSpectrumListener
. The value is specified in seconds, and its default value is 0.1 seconds, which means that
by default your listener will receive 10 spectrum updates per second. The number of spectrum bands that will be
reported to the listener is controlled by the
audioSpectrumNumBands
property. The
audioSpectrumThreshold
property
defines the minimum value that will be reported for a given spectral band. This value has a unit of decibels and must
be less than zero.
Listing 9-24 highlights the additions that need to be made to the
createEQInterface
method to create the
controls that display the audio frequency spectrum.
Listing 9-24.
Creating the Frequency Spectrum Display
private void createEQInterface() {
final GridPane gp = (GridPane) getViewNode();
final MediaPlayer mp = songModel.getMediaPlayer();
createEQBands(gp, mp);
createSpectrumBars(gp, mp);
spectrumListener = new SpectrumListener(START_FREQ, mp, spectrumBars);
}
private void createSpectrumBars(GridPane gp, MediaPlayer mp) {
spectrumBars = new SpectrumBar[BAND_COUNT];
for (int i = 0; i < spectrumBars.length; i++) {
spectrumBars[i] = new SpectrumBar(100, 20);
spectrumBars[i].setMaxWidth(44);
Search WWH ::
Custom Search