views:

1927

answers:

3

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).

+1  A: 

What you want is probably not a sound API but some kind of synthesizer code, I'm pretty sure you need more low-level sound driver control than Java would allow (it being a interpreted language normally running in a "sandbox").

But the good news is that a quick search for "java sound synthesizing" on google revealed a plugin called JSyn wich uses native C methods (wich I guessed was one way of doing it) to generate sound. It seems to be free for non-commercial use and available in commercial licenses as well. :)

Stein G. Strindhaug
+3  A: 

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.

erickson
The code of the emulator is available as JaC64 at SourceForge.
Burkhard
A: 

You can easily generate sampled sound data in Java and play it back without using native code. If you're talking MIDI things may get tricky, but I've not dabbled in that area.

To generate sampled sound data, you have to thing of the process backwards. We're going to act like the A-to-D and sample a continuous sound function over time. Your sound card does the same thing for audio through a mic or line in.

First, choose a sample rate (NOT the freq of the tone we're generating). Let's go with 44100 hz since that's likely the sound card playback rate (thus no sample rate conversion, that's not easy unless hardware does it).

// in hz, number of samples in one second
sampleRate = 44100

// this is the time BETWEEN Samples
samplePeriod = 1.0 / sampleRate

// 2ms
duration = 0.002;
durationInSamples = Math.ceil(duration * sampleRate);

time = 0;
for(int i = 0; i < durationInSamples; i++)
{
  // sample a sine wave at 440 hertz at each time tick
  // substitute a function that generates a sawtooth as a function of time / freq
  // rawOutput[i] = function_of_time(other_relevant_info, time);
  rawOutput[i] = Math.sin(2 * Math.PI * 440 * time);
  time += samplePeriod;
}

// now you can playback the rawOutput
// streaming this may be trickier
basszero
For dummies out here like me, once you've generated it, how do you play it?
skiphoppy