
Show Audio Information While Playing Sound #77
Chapter 10, Audio
|
397
HACK
Hacking the Hack
Florian’s message to javasound-interest says it is best if “everybody does
the calculation of the ‘level’ on his own[, based] on the buffers received by
the TDL [(
TargetDataLine
, usually used by capture devices)] or written to
the SDL [(
SourceDataLine
)], respectively.”
Setting aside the argument of duplication of effort, note that the buffers he
speaks of are available in the hack code; it’s what the
PCMFilePlayer reads
from the file and writes to the
Line (specifically, a SourceDataLine, as
Florian’s message notes). So, in theory at least, this can be done. But it’s not
going to be pretty.
First, create a new
DataLineInfoGUI2 class that is identical to the one from
earlier in this hack, except that instead of using a
PCMFilePlayer, it uses a
PCMFilePlayerLeveler, a class that will be defined next.
This new class is pretty much the same as the old
PCMFilePlayer, except that
on each time through the while loop, as it reads the buffer and writes it to
the line, it will call a method to scan through the buffer and determine a
level for this group of samples. So, after reading the bytes from the input
stream but before writing them to the line, add:
// calculate level
calculateLevel (buffer, readPoint, leftover);
As Florian argues in his message, the idea of a level is up for interpretation,
but there is a general sense ...