Alex Rivera | Logout

How to generate sound effects in Java?

Asked 2008-11-17T21:47:08.093
18

I'm looking for Java code that can be used to generate sound at runtime - NOT playback of existing sound files.

For example, what's the best code for generating a sawtooth waveform at 440 Hz for a duration of 2 milliseconds? Source code appreciated!

I remember my Commodore 128 had a simple Sound command that took as parameters voice, frequency, waveform, and duration to define a sound. That worked great in a lot of simple cases (quick and dirty games, experiments with sound, etc).

I am looking specifically for sound-effect like sounds, not music or MIDI (which the JFugue library covers quite well).

Edit
Report

2 Answers

10

Here is a sample that might help. This generates sin waves:

package notegenerator;

import java.io.IOException;

/**
 * Tone generator and player.
 * 
 * @author Cesar Vezga vcesar@yahoo.com
 */
public class Main {

public static void main(String[] args) throws IOException {

    Player player = new Player();

    player.play(BeachRock.getTack1(),BeachRock.getTack2());

}

 }

 package notegenerator;

 public class BeachRock {

// GUITAR
static String gs1 = "T332 A4-E4 F#5-C6 E5-A5 T166 G5 A5 F#5 A5 F5 A5 E5-A5 E3 G3 G#3 ";
static String gs2 = "A3 A3 A3 G3 E3 E3 G3 G#3 ";
static String gs3 = "A3 A3 A3 G3 E3 A3 C4 C#4 ";
static String gs4 = gs2 + gs2 + gs2 + gs3;
static String gs5 = "D4 D4 D4 C4 A3 A3 C4 D#4 ";
static String gs6 = "D4 D4 D4 C4 A3 E3 G3 G#3 ";
static String gs7 = gs4 + gs5 + gs6 + gs2 + "A3 A3 A3 G3 E3 B3 D3 D#3 ";
static String gs8 = "E4 E4 E4 D4 B3 B3 E4 B3 " + gs6 + gs2;
static String gs9 = "x E3-B3 E3-B3 E3-B3 E3-B3 E3 G3 G#3 ";
static String gs10 = gs7 + gs8 + gs9;
static String gs11 = "A3-D4 X*7 X*16 X*5 E3 G3 G#3 ";
static String guitar = gs1 + gs10 + gs11 + gs10 + gs11 + "A3 A3 A3";

// DRUMS
static String ds1 = "D2 X D3 D3 X*2 D3 X ";
static String ds2 = "D2 X D3 D3 X D3 D3 D3 ";
static String ds3 = "D2 D3 D3 D3 D3 T83 D3 D3 T166 D3 ";
static String ds4 = ds1 + ds1 + ds1 + ds2;
static String ds5 = ds1 + ds1 + ds1 + ds3;
static String ds6 = "D2*2 D3 D3 X*2 D3*2 ";
static String ds7 = "D2*2 D3 D3 X D3 D3 D3 ";
static String ds8 = ds6 + ds6 + ds6 + ds7;

static String drums = "V25 T166 X*16 " + ds4 + ds4 + ds5 + ds8 + ds4 + ds4
        + ds5 + ds8;

public static String getTack1(){
    return guitar;
}

public static String getTack2(){
    return drums;
}


}

package notegenerator;

import java.util.HashMap;

/**
 * 
 * Physics of Music - Notes
 * 
 * Frequencies for equal-tempered scale
 * This table created using A4 = 440 Hz
 * Speed of sound = 345 m/s = 1130 ft/
answered 2012-08-15T17:58:00.403
4

The Java media framework does both. You can play back recorded sounds or use the MIDI interface to synthesize your own sounds and music. It also provides a mixer API.

Of course, if you know the details of the waveform you want to play, you can "sample" the function at regular intervals and pass the resulting samples to the playback API, as if it were a pre-recorded sound file.

The JMF isn't actively maintained by Sun, but there are functioning distributions for various platforms.

My first computer was the Commodore 64, and I remember Tears for Fears' "Everybody Wants to Rule the World" on pumping out of its SID chip. I can't tell if this pure Java SID emulator is open source or not, but it might give you some pointers on implementing higher-level Attack-Decay-Sustain-Release and waveform functionality.

answered 2008-11-17T22:50:10.913

Your Answer