package com.javaforum.audio;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.AudioFormat;
public class ToneGeneratorExample {
public byte[] getMonoSinusTone(int frequency, AudioFormat af) {
byte sampleSize = (byte) (af.getSampleSizeInBits() / 8);
byte[] data = new byte[(int) af.getSampleRate() * sampleSize];
double step_width = (2 * Math.PI) / af.getSampleRate();
double x = 0;
for (int i = 0; i < data.length; i += sampleSize) {
int sample_max_value = (int) Math.pow(2, af.getSampleSizeInBits()) / 2 - 1;
int value = (int) (sample_max_value * Math.sin(frequency * x));
for (int j = 0; j < sampleSize; j++) {
byte sample_byte = (byte) ((value >> (8 * j)) & 0xff);
data[i + j] = sample_byte;
}
x += step_width;
}
return data;
}
public byte[] getStereoSinusTone(int frequency1, int frequency2, AudioFormat af) {
byte sampleSize = (byte) (af.getSampleSizeInBits() / 8);
byte[] data = new byte[(int) af.getSampleRate() * sampleSize * 2];
double stepWidth = (2 * Math.PI) / af.getSampleRate();
double x = 0;
for (int i = 0; i < data.length; i += sampleSize * 2) {
int sample_max_value = (int) Math.pow(2, af.getSampleSizeInBits()) / 2 - 1;
int value = (int) (sample_max_value * Math.sin(frequency1 * x));
for (int j = 0; j < sampleSize; j++) {
byte sampleByte = (byte) ((value >> (8 * j)) & 0xff);
data[i + j] = sampleByte;
}
value = (int) (sample_max_value * Math.sin(frequency2 * x));
for (int j = 0; j < sampleSize; j++) {
byte sampleByte = (byte) ((value >> (8 * j)) & 0xff);
int index = i + j + sampleSize;
data[index] = sampleByte;
}
x += stepWidth;
}
return data;
}
public void play(int frequenzy) {
AudioFormat af = new AudioFormat(44100, 16, 1, true, false);
byte[] data = getMonoSinusTone(frequenzy, af);
try {
Clip c = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
c.open(af, data, 0, data.length);
c.loop(1);
while(c.isRunning()) {
try {
Thread.sleep(50);
} catch (Exception ex) {}
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
}
public void play(int frequenzyLeft, int frequencyRight) {
AudioFormat af = new AudioFormat(44100, 16, 2, true, false);
byte[] data = getStereoSinusTone(frequenzyLeft, frequencyRight, af);
try {
Clip c = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
c.open(af, data, 0, data.length);
c.loop(1);
while(c.isRunning()) {
try {
Thread.sleep(50);
} catch (Exception ex) {}
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
ToneGeneratorExample tge = new ToneGeneratorExample();
if (args.length == 1) {
tge.play(Integer.parseInt(args[0]));
} else if (args.length == 2) {
tge.play(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
} else {
System.out.println("Mono Ton");
tge.play(500);
System.out.println("Ton auf dem linken Kanal");
tge.play(500, 0);
System.out.println("Ton auf dem rechten Kanal");
tge.play(0, 500);
System.out.println("Ton auf dem beiden Kanälen");
tge.play(500, 1300);
System.out.println("Fertig");
}
}
}