O'Reilly logo

Swing Hacks by Chris Adamson, Joshua Marinacci

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 panel;
}
}
Example 10-7. Testing out the waveform display (continued)

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required