-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.java
More file actions
70 lines (52 loc) · 1.77 KB
/
Server.java
File metadata and controls
70 lines (52 loc) · 1.77 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.net.* ;
import java.io.*;
import javax.sound.sampled.*;
public class Server extends Thread{
private final static int packetsize = 500 ;
SourceDataLine sourceDataLine;
AudioFormat audioFormat;
int port = 2000;
int index;
static byte tempBuffer[] = new byte[500];
public AudioFormat getAudioFormat() {
float sampleRate = 16000.0F;
int sampleSizeInBits = 16;
int channels = 2;
boolean signed = true;
boolean bigEndian = true;
return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
}
public void run(){
try{
DatagramSocket socket=new DatagramSocket(port);
System.out.println("The server is ready...") ;
DatagramPacket packet = new DatagramPacket( new byte[packetsize], packetsize );
try{
this.audioFormat = this.getAudioFormat();
DataLine.Info dataLineInfo1 = new DataLine.Info(SourceDataLine.class, this.audioFormat);
this.sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo1);
this.sourceDataLine.open(this.audioFormat);
this.sourceDataLine.start();
FloatControl control = (FloatControl)this.sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue(control.getMaximum());
}catch(Exception e){
e.printStackTrace();
}
for(;;){
socket.receive(packet);
tempBuffer = packet.getData();
this.sourceDataLine.write(tempBuffer, 4, 496);
index = byteArrayToInt(tempBuffer);
System.out.println("Sequence number: "+index);
}
}catch(Exception e){
e.printStackTrace();
}
}
public static int byteArrayToInt(byte[] b) {
return b[3] & 0xFF |
(b[2] & 0xFF) << 8 |
(b[1] & 0xFF) << 16 |
(b[0] & 0xFF) << 24;
}
}