-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadFromMic.java
136 lines (91 loc) · 3.11 KB
/
ReadFromMic.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
/*
* ReadFromMic.java
*
* Version:
* 1.0
*
* Revisions:
* 0
*/
import java.io.ByteArrayOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.TargetDataLine;
/**
* Class used to create the instance of a task to read the input
* from the microphone. This task is then supplied to a thread for
* concurrent execution.
*
* @author Siddharth Kalluru
* @author Vijaykumar Chandrashekar
*/
public class ReadFromMic implements Runnable{
boolean readFlag;
InetAddress ipOfServer;
int portOfStreamOnServer;
DatagramSocket objStreamSock;
ByteArrayOutputStream bytArrOpStream;
AudioFormat objAudioFormat;
/**
* Constructor to initialize the instance of HostUI
*
* @param objStreamSock Datagramsocket to stream voice to remote host
*
* @param ip ip address of the remote host
*
* @param portToSendStream data-gram socket to stream voice
* to remote host
*
* @exception UnknownHostException
*/
ReadFromMic(DatagramSocket objStreamSock, InetAddress ip, int portToSendStream)
throws UnknownHostException{
ipOfServer = ip;
this.portOfStreamOnServer = portToSendStream;
this.objStreamSock = objStreamSock;
readFlag = true;
bytArrOpStream = new ByteArrayOutputStream();
//Format of audio recorded - Sampled @ 16Khz, SampleSize(PCM), Mono
objAudioFormat = new AudioFormat(16000.0f, 16, 1, true, true);
}
@Override
public void run() {
TargetDataLine objMic;
try{
objMic = AudioSystem.getTargetDataLine(objAudioFormat);
DataLine.Info objInfo = new DataLine.Info(TargetDataLine.class, objAudioFormat);
objMic = (TargetDataLine) AudioSystem.getLine(objInfo);
objMic.open(objAudioFormat,16000); //Prep the buffer to accept data from mic
int packetSize = 3000;
System.out.println("Mic Buffer size: " + objMic.getBufferSize());
byte [] arrBuffer = new byte[objMic.getBufferSize() / 5];
int numOfBytesRead = 0, totalBytesRead = 0;
objMic.start(); //start Recording
int i=0;
System.out.println("Mic recording begun.");
//Read till read flag is set to false
while(readFlag){
//Read bytes from buffer of Mic
//equal to the packet size
numOfBytesRead = objMic.read(arrBuffer, 0, packetSize);
totalBytesRead = totalBytesRead + numOfBytesRead;
bytArrOpStream.write(arrBuffer, 0, numOfBytesRead); //Writing to op stream
//Add to data gram packet and send it
objStreamSock.send(new DatagramPacket(arrBuffer, arrBuffer.length,
ipOfServer, portOfStreamOnServer));
System.out.println("Stream packet : "+ ++i + "sent");
}
objMic.drain();
objMic.close();
System.out.println("Recording stopped.");
}
catch(Exception e){
e.printStackTrace();
}
}
}