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