tags:

views:

108

answers:

3

Hey guys, i created a pong clone, i would like to add some sound effects when collisions occur my problem is every example i could find about synthesizing sound takes about 30 lines of code considering my whole application is 90 lines of code i am looking for a simpler approach. Is there a simple way to create a beep sound of different tones, duration does not matter just a series of beeps with different tones?

+2  A: 

java.awt.Toolkit.getDefaultToolkit().beep()

series of beeps?

int numbeeps = 10;

for(int x=0;x<numbeeps;x++)
{
  java.awt.Toolkit.getDefaultToolkit().beep();
}
Sev
"Different tones" He said.
Martijn Courteaux
+1  A: 

Here's a small example taken (and shortened) from Java Sound - Example: Code to generate audio tone

    byte[] buf = new byte[ 1 ];;
    AudioFormat af = new AudioFormat( (float )44100, 8, 1, true, false );
    SourceDataLine sdl = AudioSystem.getSourceDataLine( af );
    sdl = AudioSystem.getSourceDataLine( af );
    sdl.open( af );
    sdl.start();
    for( int i = 0; i < 1000 * (float )44100 / 1000; i++ ) {
        double angle = i / ( (float )44100 / 440 ) * 2.0 * Math.PI;
        buf[ 0 ] = (byte )( Math.sin( angle ) * 100 );
        sdl.write( buf, 0, 1 );
    }
    sdl.drain();
    sdl.stop();
    sdl.close();
tangens
A: 

You can use JSyn. This is a lib you have to install (with a .DLL and a .JAR). But very simple to create diffrent tones.

Link (Also tutorials available)

This is an example:

public static void main(String[] args) throws Exception {
    SawtoothOscillatorBL osc;
    LineOut lineOut;
    // Start JSyn synthesizer.
    Synth.startEngine(0);

    // Create some unit generators.
    osc = new SawtoothOscillatorBL();
    lineOut = new LineOut();

    // Connect oscillator to both left and right channels of output.
    osc.output.connect(0, lineOut.input, 0);
    osc.output.connect(0, lineOut.input, 1);

    // Start the unit generators so they make sound.
    osc.start();
    lineOut.start();

    // Set the frequency of the oscillator to 200 Hz.
    osc.frequency.set(200.0);
    osc.amplitude.set(0.8);

    // Sleep for awhile so we can hear the sound.
    Synth.sleepForTicks(400);

    // Change the frequency of the oscillator.
    osc.frequency.set(300.0);
    Synth.sleepForTicks(400);

    // Stop units and delete them to reclaim their resources.
    osc.stop();
    lineOut.stop();
    osc.delete();
    lineOut.delete();

    // Stop JSyn synthesizer.
    Synth.stopEngine();
}

Martijn

Martijn Courteaux