import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class BasicSound {
// CD Quality audio (well, almost)
public final float SAMPLE_RATE = 44100;
// Main GUI component
JFrame mainFrame;
public static void main( String[] args ){
BasicSound my_sound = new BasicSound();
my_sound.showDialog();
}
public BasicSound(){
setUpDialog();
}
public void setUpDialog(){
// Main frame
mainFrame = new JFrame("Pure Wave Generator");
mainFrame.setSize( 300, 300 );
Container content = mainFrame.getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
// Frequency slider
final JSlider slider_hertz = new JSlider( JSlider.HORIZONTAL, 20, 20000, 440 );
slider_hertz.setPaintLabels( true );
slider_hertz.setToolTipText("In Hertz");
slider_hertz.setBorder( new TitledBorder( "Frequency" ) );
content.add( slider_hertz );
// Sine wave button
final JButton btn_play_sine = new JButton( "Play Sine Wave" );
btn_play_sine.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent actionEvent ){
try{
System.out.println("Play sine wave at " + slider_hertz.getValue() + " hertz.");
generateSineWave( slider_hertz.getValue() );
}catch( LineUnavailableException err ){
System.out.println("Line is unavailable.");
}
}
} );
content.add( btn_play_sine );
// Square wave button
final JButton btn_play_square = new JButton( "Play Square Wave" );
btn_play_square.addActionListener( new ActionListener(){
public void actionPerformed( ActionEvent actionEvent ){
try{
System.out.println("Play square wave at " + slider_hertz.getValue() + " hertz.");
generateSquareWave( slider_hertz.getValue() );
}catch( LineUnavailableException err ){
System.out.println("Line is unavailable.");
}
}
} );
content.add( btn_play_square );
mainFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void showDialog(){
mainFrame.setVisible( true );
}
// Generate a sine wave at a specified frequency
public void generateSineWave( float hertz ) throws LineUnavailableException{
byte[] buf = new byte[1];
AudioFormat af = new AudioFormat( SAMPLE_RATE, 8, 1, true, false );
SourceDataLine source_dl = AudioSystem.getSourceDataLine( af );
source_dl.open( af );
for( int i=0; i < SAMPLE_RATE; i++ ){
double angle = i / ( SAMPLE_RATE / hertz ) * 2.0 * Math.PI;
buf[0]=( byte )( Math.sin( angle ) * 100 );
source_dl.write( buf, 0, 1 );
}
System.out.println("Finished playback.");
source_dl.start();
source_dl.drain();
source_dl.stop();
source_dl.close();
}
// Generate a square wave at a specified frequency
public void generateSquareWave( float hertz ) throws LineUnavailableException{
byte[] buf = new byte[1];
AudioFormat af = new AudioFormat( SAMPLE_RATE, 8, 1, true, false );
SourceDataLine source_dl = AudioSystem.getSourceDataLine( af );
source_dl.open( af );
for( int i=0; i < SAMPLE_RATE; i++ ){
double angle = i / ( SAMPLE_RATE / hertz ) * 2.0 * Math.PI;
buf[0]=( byte )( ( Math.sin( angle ) > 0 ) ? 100 : -100 );
source_dl.write( buf, 0, 1 );
}
System.out.println("Finished playback.");
source_dl.start();
source_dl.drain();
source_dl.stop();
source_dl.close();
}
}