/
Main.java
146 lines (118 loc) · 3.56 KB
/
Main.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
package jAudioTransmitter;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
public class Main {
static List<Sender> senderList = new ArrayList<Sender>();
static MicrophoneReader mr;
static volatile Integer sendersCreated = 0;
static volatile Integer numBytesRead;
static volatile Integer senderNotReady = 0;
static volatile byte[] data;
static final Object monitor = new Object();
public static void main(String[] args) {
try {
mr = new MicrophoneReader();
mr.start();
ServerSocket ss = new ServerSocket(7373);
Scanner sc = new Scanner(System.in);
//while (!sc.next().equals("quit")) {
while (true) {
Socket s = ss.accept();
Sender sndr = new Sender(s);
senderList.add(sndr);
sndr.start();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
mr.setFinishFlag();
for (Sender sndr: senderList) {
sndr.setFinishFlag();
}
}
}
static class Sender extends Thread {
Socket s;
volatile boolean finishFlag;
int position;
int senderNumber;
public Sender(Socket s) {
this.s = s;
finishFlag = false;
System.out.print("Sender started: #");
senderNumber = ++sendersCreated;
System.out.println(senderNumber);
}
public void setFinishFlag() {
finishFlag = true;
}
public void run() {
try {
OutputStream os = s.getOutputStream();
while (!finishFlag) {
synchronized (monitor) {
senderNotReady++;
monitor.wait();
os.write(data, 0, numBytesRead);
os.flush();
senderNotReady--;
}
System.out.print("Sender #");
System.out.print(senderNumber);
System.out.print(": ");
System.out.print(numBytesRead);
System.out.println(" bytes sent");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
static class MicrophoneReader extends Thread {
volatile boolean finishFlag;
AudioFormat format = new AudioFormat(16000.0f, 16, 2, true, false);
//AudioFormat format = new AudioFormat(8000.0f, 8, 1, true, false);
int CHUNK_SIZE = 10240;
TargetDataLine microphone;
public MicrophoneReader() {
finishFlag = false;
System.out.println("Microphone reader started");
}
public void setFinishFlag() {
finishFlag = true;
}
public void run() {
try {
microphone = AudioSystem.getTargetDataLine(format);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
microphone = (TargetDataLine) AudioSystem.getLine(info);
microphone.open(format);
data = new byte[CHUNK_SIZE];
microphone.start();
while (!finishFlag) {
synchronized (monitor) {
if (senderNotReady==sendersCreated) {
monitor.notifyAll();
continue;
}
numBytesRead = microphone.read(data, 0, CHUNK_SIZE);
}
System.out.print("Microphone reader: ");
System.out.print(numBytesRead);
System.out.println(" bytes read");
}
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
}
}