Frequency/tone generator in java tia sal22

R

ratullloch_delthis

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
 
K

Knute Johnson

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 = (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 = (byte)(buf * 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);
}
}
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top