Show Audio Information While Playing Sound #77
Chapter 10, Audio
|
395
HACK
Testing It Out
Launch the DataLineGUI application and you’ll get a file-selection dialog.
Choose a suitable AIFF or WAV, and you’ll see the GUI shown in
Figure 10-8.
This is all well and good for a simple GUI, but there’s one problem: where
the heck is our level meter?! It should be between the filename and the text
area, but it’s totally not there!
Initially, I suspected my repaint code was hosed, but it all seemed correct.
So, right after figuring out the meter width, I added a sanity-check debug
line:
System.out.println ("level = " + level);
And when I ran it, I got a result that I really didn’t want to see:
[aeris:HacksBook/Media/x11] cadamson% java DataLineInfoGUI
got PCM format
got info
got line
opened line
level = 0.0
level = 0.0
level = 0.0
level = 0.0
level = 0.0
And that was on a really loud song, so it wasn’t just a slow fade in. I looked
around to see if there was something special you have to do for
getLevel( )
to work, but there wasn’t.
Figure 10-8. Audio player display with format information
396
|
Chapter 10, Audio
#77 Show Audio Information While Playing Sound
HACK
Then I Googled, and found this post to the javasound-interest mailing list
from February 2003:
Date: Mon, 17 Feb 2003 22:31:21 -0800
Reply-To: Discussion list for JavaSound API
<JAVASOUND-INTEREST@JAVA.SUN.COM>
Sender: Discussion list for JavaSound API
<JAVASOUND-INTEREST@JAVA.SUN.COM>
From: Florian Bomers <Florian.Bomers@SUN.COM>
Organization: Sun Microsystems Inc.
Subject: Re: DataLine.getLevel( )?
Comments: To: knute@frazmtn.com
Content-Type: text/plain; charset=us-ascii
Unfortunately, it is not implemented. (actually, in my private opinion, it
is a questionable method anyway: usually soundcard drivers do not provide
such a primitive, so the Java Sound implementation has to calculate this
"level" on its own. But there are many different algorithms to do so, suited
depending for what the "level" is needed for, and it would possibly eat
unnecessarily processor resources. So I guess it's best if everybody does
the calculation of the "level" on his own on the buffers received by the TDL
or written to the SDL, respectively. Easy and fast algorithms are maximum,
moving average,block average, power).
sorry...
Florian
Knute Johnson wrote:
>
> Anybody know if DataLine.getLevel( ) is implemented? All I get is 0.0
> on SourceDataLines and -1.0 on TargetDataLines.
>
> Thanks,
>
> Knute Johnson
In fact, a little further research shows that the fact that DataLine.getLevel( )
always returns UNKNOWN_LEVEL was filed as bug 4297101 in the Java Bug
Parade on December 6, 1999. Five years later, it’s still not fixed, though it
looks like there was at least an attempt to fix it for Tiger (J2SE 5.0)—a fix
that was abandoned in August 2003.
By the way, wouldn’t it have saved a lot of people a lot of
time if they disclosed in the Javadoc that this method is a no-
op? But I digress….
So, the level meter is not going to work—not because of the graphics, but
because there’s no way to get an accurate level. Or is there?

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.