import javax.sound.sampled.*;
//jdk1.3
public class Tone {
public static float SAMPLE_RATE = 8000f;
public static void sound(int hz, int msecs)
throws LineUnavailableException {
byte[] buf = new byte[1];
AudioFormat af = new AudioFormat(SAMPLE_RATE,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i<msecs*8; i++) {
double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 110.0);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();
}
public static void main(String[] args) {
try {
Tone.sound(1000,100);
Tone.sound(100,1000);
Tone.sound(5000,100);
} catch (LineUnavailableException lue) {
System.out.println(lue);
}
}
}Written and compiled by Réal Gagnon ©1998-2005
[ home ]