August 02, 2021, 02:02:29 AM

Author Topic: Godaigo's plea for help!  (Read 19649 times)

0 Members and 1 Guest are viewing this topic.

Offline godaigo

  • Global Moderator
  • Member
  • *****
  • Posts: 205
  • Country:
  • I love YaBB SE!
Re: Godaigo's plea for help!
« Reply #15 on: May 02, 2009, 01:18:21 AM »
Godaigo
All's fair in Love and Brewing.

Offline benthehutt

  • Global Moderator
  • Seasoned Poster
  • *****
  • Posts: 368
  • Country:
  • "Drugs? You'd better not be!" - my mom
    • Echelon9
Re: Godaigo's plea for help!
« Reply #16 on: May 02, 2009, 11:46:43 AM »
Yeah! That's a pretty sweet app. I'm more interested in actual synthesis than just adding sound files to an application. Anyway, I started some work on this, but I've got some questions on Java as a whole. What is the difference between Java and J2EE? Also, what good is the Runnable class? I've seen some examples where a guy instantiates a Runnable object and I don't know what the point was.

Here is my code so far. It's pretty simple, it just generates a sine wave and a square wave and plays them back. But let me know if I'm doing anything in Java that's stupid (I'm using Eclipse 3.4.1 Ganymede):

Code: [Select]
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();
}

}

Also, what is the difference between an ActionListener and an EventListener?

Any help would be appreciated. Thanks!
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.