Greetings all
I'm trying to find a tutorial that shows me how to make a Frequency/tone generator in java.
Example I would like to have a slider that changes the value x for the formula y=sin(x)+cos(x) so if the slider is moved
to 440 it would play a 440hz sin wave out the speaker does such a tutorial exist if so can someone post a link to it
tia sal22
PS: I'm using netbeans 6.9.1 if anyone wants to know
|
|
0
|
|
|
|
Reply
|
ratullloch_delthis
|
11/28/2010 8:33:32 PM |
|
On 11/28/2010 12:33 PM, ratullloch_delthis wrote:
> Greetings all
> I'm trying to find a tutorial that shows me how to make a Frequency/tone generator in java.
> Example I would like to have a slider that changes the value x for the formula y=sin(x)+cos(x) so if the slider is moved
> to 440 it would play a 440hz sin wave out the speaker does such a tutorial exist if so can someone post a link to it
> tia sal22
>
> PS: I'm using netbeans 6.9.1 if anyone wants to know
>
Here is a method I wrote to create a single tone.
import java.util.*;
import javax.sound.sampled.*;
public class Tone {
public static float SAMPLE_RATE = 8000f;
public static void sound(int hz, int msecs, double vol)
throws LineUnavailableException {
if (hz <= 0)
throw new IllegalArgumentException("Frequency <= 0 hz");
if (msecs <= 0)
throw new IllegalArgumentException("Duration <= 0 msecs");
if (vol > 1.0 || vol < 0.0)
throw new IllegalArgumentException("Volume out of range 0.0
- 1.0");
byte[] buf = new byte[(int)SAMPLE_RATE * msecs / 1000];
for (int i=0; i<buf.length; i++) {
double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
buf[i] = (byte)(Math.sin(angle) * 127.0 * vol);
}
// shape the front and back 10ms of the wave form
for (int i=0; i < SAMPLE_RATE / 100.0 && i < buf.length / 2; i++) {
buf[i] = (byte)(buf[i] * i / (SAMPLE_RATE / 100.0));
buf[buf.length-1-i] =
(byte)(buf[buf.length-1-i] * i / (SAMPLE_RATE / 100.0));
}
AudioFormat af = new AudioFormat(SAMPLE_RATE,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
sdl.write(buf,0,buf.length);
sdl.drain();
sdl.close();
}
public static void main(String[] args) throws
LineUnavailableException {
Tone.sound(800,1000,0.8);
}
}
--
Knute Johnson
s/nospam/knute2010/
|
|
0
|
|
|
|
Reply
|
Knute
|
11/28/2010 10:48:43 PM
|
|
On Sun, 28 Nov 2010 20:33:32 GMT, ratullloch_delthis
<ratullloch_delthis@gmail.com> wrote, quoted or indirectly quoted
someone who said :
>I'm trying to find a tutorial that shows me how to make a Frequency/tone generator in java.
>Example I would like to have a slider that changes the value x for the formula y=sin(x)+cos(x) so if the slider is moved
>to 440 it would play a 440hz sin wave out the speaker does such a tutorial exist if so can someone post a link to it
>tia sal22
see sample code at http://mindprod.com/jgloss/sound.html
--
Roedy Green Canadian Mind Products
http://mindprod.com
In programming, and documenting programs, keep vocabulary consistent and precisely defined! Variation in vocabulary to relieve the tedium is for novels.
|
|
0
|
|
|
|
Reply
|
Roedy
|
12/2/2010 1:13:18 AM
|
|