-
Notifications
You must be signed in to change notification settings - Fork 9
/
MinimalEchoExample.java
48 lines (36 loc) · 1.46 KB
/
MinimalEchoExample.java
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
43
44
45
46
47
48
package net.labymod.opus;
import javax.sound.sampled.*;
import java.io.IOException;
public class MinimalEchoExample {
private static AudioFormat format =
new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 48000, 16, 1, 2, 48000, false);
public static void main(String[] args) throws LineUnavailableException, IOException {
OpusCodec.setupWithTemporaryFolder();
OpusCodec codec = OpusCodec.createDefault();
/*
Could also use the builder to customize the codec.
OpusCodec.newBuilder()
.withSampleRate(24000)
...
.build();
*/
// get default microphone and open it at the selected format
TargetDataLine microphone = AudioSystem.getTargetDataLine(format);
microphone.open(microphone.getFormat());
microphone.start();
// get default speaker and open it at the selected format
SourceDataLine speaker = AudioSystem.getSourceDataLine(format);
speaker.open(microphone.getFormat());
speaker.start();
while (true) {
//Reading microphone data
byte[] data = new byte[codec.getChannels() * codec.getFrameSize() * 2];
microphone.read(data, 0, data.length);
//Encoding PCM data chunk
byte[] encode = codec.encodeFrame(data);
//Decoding PCM data chunk
byte[] decoded = codec.decodeFrame(encode);
speaker.write(decoded, 0, decoded.length);
}
}
}