
Build an Audio Waveform Display #75
Chapter 10, Audio
|
385
HACK
Seeing Is Believing
Now, you’re ready to run the hack. The main( ) method shown here is the
simulator code. Notice the creation of the
AudioInputStream and the cre-
ation of the container with the stream. All painting and management of
SingleWaveformPanels is encapsulated within the separate panel classes:
public static void main(String[] args) {
try {
JFrame frame = new JFrame("Waveform Display Simulator");
frame.setBounds(200,200, 500, 350);
File file = new File(args[0]);
AudioInputStream audioInputStream
= AudioSystem.getAudioInputStream(file);
WaveformPanelContainer container = new WaveformPanelContainer( );
container.setAudioToDisplay(audioInputStream);
frame.getContentPane().setLayout(new BorderLayout( ));
frame.getContentPane( ).add(container, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show( );
frame.validate( );
frame.repaint( );
} catch (Exception e){
e.printStackTrace( );
}
}
Then, just make sure to pass the audio filename in at the command line. Use
something like this:
java WaveformDisplaySimulator chord.wav
private JComponent createChannelDisplay(
SingleWaveformPanel waveformPanel,
int index) {
JPanel panel = new JPanel(new BorderLayout( ));
panel.add(waveformPanel, BorderLayout.CENTER);
JLabel label = new JLabel("Channel " + ++index);
panel.add(label, BorderLayout.NORTH);
return ...