-
Notifications
You must be signed in to change notification settings - Fork 3
/
Server.java
66 lines (50 loc) · 1.81 KB
/
Server.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.io.ByteArrayInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.SourceDataLine;
class Server {
AudioInputStream audioInputStream;
static AudioInputStream ais;
static AudioFormat format;
static boolean status = true;
static int port = 1234;
static int sampleRate = 48000;/**8000, 16000, 22050, 24000, 32000, 44100, 48000 Choose the best for the device and the bandwidth **/
static DataLine.Info dli;
static SourceDataLine sdl;
public static void main(String args[]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(port);
byte[] rData = new byte[4096];
format = new AudioFormat(sampleRate, 16, 1, true, false);
dli = new DataLine.Info(SourceDataLine.class, format);
sdl = (SourceDataLine) AudioSystem.getLine(dli);
sdl.open(format);
sdl.start();
FloatControl volume = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
volume.setValue(1.00f);
DatagramPacket rPacket = new DatagramPacket(rData,
rData.length);
ByteArrayInputStream baiss = new ByteArrayInputStream(
rPacket.getData());
System.out.println(baiss);
while (status == true) {
serverSocket.receive(rPacket);
System.out.println(rPacket);
ais = new AudioInputStream(baiss, format, rPacket.getLength());
Sstream(rPacket.getData());
}
sdl.drain();
sdl.close();
}
public static void Sstream(byte soundbytes[]) {
try {
sdl.write(soundbytes, 0, soundbytes.length);
} catch (Exception e) {
e.printStackTrace();
}
}
}