-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.java
300 lines (251 loc) · 7.84 KB
/
Player.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package CFB.Blind.Recorder;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.*;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;
import javax.sound.sampled.AudioFormat.Encoding;
import CFB.Blind.Recorder.AudioApplet.uploadThread;
public class Player extends Applet implements KeyListener{
private static final long serialVersionUID = 1L;
//Global declarations
protected boolean running;
int uploadstatestaus=0;
ByteArrayOutputStream out = null;
AudioFileFormat.Type fileType;
Object lock = new Object();
TargetDataLine line = null;
SourceDataLine sline = null;
volatile boolean paused = false;
boolean first;
TextArea displayArea;
int dis=0;
private AudioClip clip;
private AppletContext context;
URL url=null;
String result="";
public void init () {
context = this.getAppletContext();
// Construct the button
Button beep = new Button("Beep");
// add the button to the layout
this.add(beep);
displayArea = new TextArea(5, 20);
displayArea.setEditable(false);
this.add(displayArea);
// specify that action events sent by this
// button should be handled by a new BeepAction object
beep.addKeyListener(this);
context = this.getAppletContext();
try{
//uri=this.getDocumentBase();
}
catch (Exception e)
{
}
}
public void keyReleased(KeyEvent e) {
//displayInfo(e, "KEY RELEASED: ");
dis=0;
displayArea.append("\nKey release");
System.out.print("STOP AUDIO");
stopAudio();
uploadAudio();
System.out.print("End Upload");
System.out.print("result is "+result);
}
/** Handle the button click. */
public void actionPerformed(ActionEvent e) {
//Clear the text components.
displayArea.setText("");
}
public void keyPressed(KeyEvent e) {
if(dis==0)
{
dis=1;
displayArea.append("\nKey Pressed");
recordAudio();
}
}
private void recordAudio()
{
first=true;
try {
final AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
final AudioFormat format = getFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
line = (TargetDataLine)AudioSystem.getLine(info);
line.open(format);
line.start();
Runnable runner = new Runnable() {
int bufferSize = (int) format.getSampleRate()* format.getFrameSize();
byte buffer[] = new byte[bufferSize];
public void run() {
out = new ByteArrayOutputStream();
running = true;
try {
while (running) {
int count = line.read(buffer, 0, buffer.length);
if (count > 0) {
out.write(buffer, 0, count);
InputStream input = new ByteArrayInputStream(buffer);
final AudioInputStream ais = new AudioInputStream(input, format, buffer.length /format.getFrameSize());
}
}
out.close();
}catch (IOException e) {
System.exit(-1);
}
}
};
Thread recordThread = new Thread(runner);
recordThread.start();
}catch(LineUnavailableException e) {
System.err.println("Line Unavailable:"+ e);
e.printStackTrace();
System.exit(-2);
}
catch (Exception e) {
System.out.println("Direct Upload Error");
e.printStackTrace();
}
}
private AudioFormat getFormat()
{
Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
float sampleRate= 16000.0F;
int sampleSizeInBits = 16;
int channels = 1;
int frameSize = 2;
float frameRate = 16000.0F;
boolean bigEndian = false;
return new AudioFormat(encoding, sampleRate, sampleSizeInBits, channels, frameSize, frameRate, bigEndian);
}//End of getAudioFormat method
class uploadThread extends Thread{
public void run(){
AudioFileFormat.Type fileType = AudioFileFormat.Type.WAVE;
byte audio[] = out.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
final AudioFormat format = getFormat();
final AudioInputStream in = new AudioInputStream(input, format, audio.length /format.getFrameSize());
try {
URL servlet=new URL("http://localhost:8080/TGMC-version1/AudioUpload");
HttpURLConnection con = (HttpURLConnection) servlet
.openConnection();
//con.setRequestProperty("Content-type","sumon");
con.setRequestProperty("Content-Type",getContentType(format));
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setChunkedStreamingMode(2048);
con.connect();
OutputStream out = con.getOutputStream();
byte[] buffer = new byte[10240];
int totalRead = 0;
while (true) {
int numRead = in.read(buffer);
if (numRead < 0) {
break;
}
out.write(buffer, 0, numRead);
out.flush();
totalRead += numRead;
}
out.close();
in.close();
System.out.println("Posted total of " + totalRead
+ " audio bytes");
System.out.println("Http response line: "
+ con.getResponseMessage());
InputStream instr = con.getInputStream();
ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
try {
result = (String) inputFromServlet.readObject();
System.out.print("Result is+"+result);
displayArea.append("\nResult:'"+result+"'");
/*
AudioClip clip;
AppletContext context;
context = this.getAppletContext();*/
System.out.print("Upload Rnd");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
System.out.print(e);
e.printStackTrace();
}
inputFromServlet.close();
instr.close();
} catch (IOException e) {
e.printStackTrace();
}
uploadstatestaus=0;
}
}
class speak extends Thread
{
public void run()
{
System.out.print("Speak");
clip.loop();
}
}
private String getContentType(AudioFormat format) {
String encoding = null;
if (format.getEncoding() == AudioFormat.Encoding.ULAW) {
encoding = "MULAW";
} else if (format.getEncoding() == AudioFormat.Encoding.PCM_SIGNED) {
encoding = "L16";
}
System.out.print("AUDIO=" + encoding + "; CHANNELS=" + format.getChannels()
+ "; RATE=" + (int) format.getSampleRate() + "; BIG="
+ format.isBigEndian());
return "AUDIO=" + encoding + "; CHANNELS=" + format.getChannels()
+ "; RATE=" + (int) format.getSampleRate() + "; BIG="
+ format.isBigEndian();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
private void stopAudio() {
if (sline != null) {
sline.stop();
sline.close();
}else {
line.stop();
line.close();
}
}
private void uploadAudio() {
uploadstatestaus=1;
//th.stop();
//clip.loop();
//th=new speak();
Thread th= new uploadThread();
th.start();
while(th.isAlive());
String audioURL = "http://localhost:8080/TGMC-version1/abc.wav?Text=You%20said%20"+result;
System.out.print(audioURL);
try {
url = new URL(this.getDocumentBase(), audioURL);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
clip = context.getAudioClip(url);
clip.loop();
}
}