-
Notifications
You must be signed in to change notification settings - Fork 2
/
Peer2.java
147 lines (121 loc) · 4.71 KB
/
Peer2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import java.io.*;
import java.text.*;
import java.util.*;
import java.net.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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.Line;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Mixer;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
// Client class
public class Peer2
{
boolean stopCapture = false;
ByteArrayOutputStream byteArrayOutputStream;
AudioFormat audioFormat;
TargetDataLine targetDataLine;
AudioInputStream audioInputStream;
SourceDataLine sourceDataLine;
byte tempBuffer[] = new byte[500];
byte playBuffer[] = new byte[500];
final DataInputStream dis;
final DataOutputStream dos;
final Socket s;
public Peer2(Socket s, DataInputStream dis, DataOutputStream dos){
this.s = s;
this.dis = dis;
this.dos = dos;
}
private 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);
}
private void captureAudio() {
try {
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo(); //get available mixers
System.out.println("Available mixers:");
Mixer mixer = null;
for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
System.out.println(cnt + " " + mixerInfo[cnt].getName());
mixer = AudioSystem.getMixer(mixerInfo[cnt]);
Line.Info[] lineInfos = mixer.getTargetLineInfo();
if (lineInfos.length >= 1 && lineInfos[0].getLineClass().equals(TargetDataLine.class)) {
System.out.println(cnt + " Mic is supported!");
break;
}
}
audioFormat = getAudioFormat(); //get the audio format
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, audioFormat);
targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
targetDataLine.open(audioFormat);
targetDataLine.start();
DataLine.Info dataLineInfo1 = new DataLine.Info(SourceDataLine.class, audioFormat);
sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo1);
sourceDataLine.open(audioFormat);
sourceDataLine.start();
//Setting the maximum volume
FloatControl control = (FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
control.setValue(control.getMaximum());
captureAndPlay(); //playing the audio
} catch (LineUnavailableException e) {
System.out.println(e);
System.exit(0);
}
}
private void captureAndPlay() {
byteArrayOutputStream = new ByteArrayOutputStream();
stopCapture = false;
try {
int seq = 0;
//Record non-stop
while (!stopCapture) {
//Read from mic and store in temp buffer
targetDataLine.read(tempBuffer, 0, tempBuffer.length); //capture sound into tempBuffer
seq = seq%16;
tempBuffer[499] = (byte)seq++;
System.out.println(tempBuffer[499]);
//Send whats in buffer to the server using sockets
dos.write(tempBuffer);
}
byteArrayOutputStream.close();
} catch (IOException e) {
System.out.println(e);
System.exit(0);
}
}
public static void main(String[] args) throws IOException
{
try
{
// getting localhost ip
InetAddress ip = InetAddress.getByName("localhost");
// establish the connection with server port 5056
Socket s = new Socket(ip, 5000);
// obtaining input and out streams
DataInputStream dis = new DataInputStream(s.getInputStream());
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
System.out.println("Connection Created");
Peer2 peer = new Peer2(s,dis,dos);
peer.captureAudio();
// the following loop performs the exchange of
// information between client and client handler
// closing resources
s.close();
dis.close();
dos.close();
}catch(Exception e){
e.printStackTrace();
}
}
}