-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound
More file actions
42 lines (33 loc) · 1.04 KB
/
Sound
File metadata and controls
42 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.LineUnavailableException;
public class Sound{
public static void main(String[] args){
try {
Sound.createTone(200, 100, 40000);
} catch (LineUnavailableException lue) {
System.out.println(lue);
}
}
public static void createTone(double Hertz, int volume, int duration)
throws LineUnavailableException {
float rate = 44100;
byte[] buf;
AudioFormat audioF;
buf = new byte[1];
audioF = new AudioFormat(rate,8,1,true,false);
SourceDataLine sourceDL = AudioSystem.getSourceDataLine(audioF);
sourceDL = AudioSystem.getSourceDataLine(audioF);
sourceDL.open(audioF);
sourceDL.start();
for(int i = duration; i<rate; i++){
double angle = (i/rate)*Hertz*2.0*Math.PI;
buf[0]=(byte)(Math.sin(angle)*volume);
sourceDL.write(buf,0,1);
}
sourceDL.drain();
sourceDL.stop();
sourceDL.close();
}
}