400
|
Chapter 10, Audio
#77 Show Audio Information While Playing Sound
HACK
Running the Hacked Hack
When you run this hack, you finally get a player with a level meter, as seen
in Figure 10-9.
While this looks OK in a book, it really isn’t very satisfactory when you’re
watching the audio as it plays. It doesn’t seem to relate to the music that
closely; that is, it seems to follow softer music OK, but it really falls apart on
rock music.
Part of the reason is that this “maximum” algorithm is quite crude; an
approach such as averaging the samples in the buffer might be more realistic.
But the real problem is that the access JavaSound gives you is doomed to be
hopelessly behind what’s being played. Think about the
available( )
method, which reports how much you can write to the SourceDataLine’s
buffer without blocking. What’s happening is that you’re refilling one end of
its buffer, while it drains out the other end to the speakers—or more accu-
rately, to the native sound system (which may have its own buffers, and thus
more latency). This arrangement is illustrated in Figure 10-10.
So, you can calculate the level for the samples in the buffer, but it will be
some time until those samples are played, so you have a mismatch of what’s
being measured and what’s actually being played. What you need is access
to the
SourceDataLine’s buffer, so you could run the level check on the bytes
that are just about to be played. Until and unless that’s available, the sug-
gested workaround isn’t really going to work.
Figure 10-9. Audio player display with format information and level meter
Figure 10-10. Flow of samples through buffers in JavaSound
read()
Your buffer
write()
Available To native soundPreviously written data
SourceDataLine’s buffer

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.