Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
262 lines (205 loc) · 6.53 KB
/
Copy pathMain.java
File metadata and controls
262 lines (205 loc) · 6.53 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
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
// onlinebin/java-online-audio-client/src/Main.java
// Copyright 2013 Polish-Japanese Institute of Information Technology (author: Danijel Korzinek)
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.UIManager;
@SuppressWarnings(value={"serial","unchecked"})
public class Main extends JFrame {
public static Main main_frame = null;
private KaldiASR asr = null;
private MLF mlf = null;
private JComboBox tfInputFile;
private JRadioButton rbFile, rbScp;
private JCheckBox cbMLF, cbHTK, cbTextGrid, cbWebVTT;
private JTextArea taLog;
private JProgressBar progress;
Main() {
super("KALDI Online Audio Client");
JPanel pMain = new JPanel();
pMain.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
add(pMain);
pMain.setLayout(new BoxLayout(pMain, BoxLayout.PAGE_AXIS));
JPanel pInput = new JPanel();
pInput.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
pInput.setLayout(new BoxLayout(pInput, BoxLayout.LINE_AXIS));
pInput.add(new JLabel("Input:"));
tfInputFile = new JComboBox();
tfInputFile.setEditable(true);
pInput.add(tfInputFile);
rbFile = new JRadioButton("RAW");
rbScp = new JRadioButton("SCP");
ButtonGroup bgFile = new ButtonGroup();
bgFile.add(rbFile);
bgFile.add(rbScp);
rbFile.setSelected(true);
pInput.add(rbFile);
pInput.add(rbScp);
JPanel pOutput = new JPanel();
pOutput.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
pOutput.setLayout(new BoxLayout(pOutput, BoxLayout.LINE_AXIS));
pOutput.add(new JLabel("Output:"));
cbMLF = new JCheckBox("MLF");
cbHTK = new JCheckBox("HTK");
cbTextGrid = new JCheckBox("TextGrid");
cbWebVTT = new JCheckBox("WebVTT");
pOutput.add(cbMLF);
pOutput.add(cbHTK);
pOutput.add(cbTextGrid);
pOutput.add(cbWebVTT);
JPanel pRun = new JPanel();
pRun.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
pRun.setLayout(new BoxLayout(pRun, BoxLayout.LINE_AXIS));
JButton bRun = new JButton("Run");
bRun.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
JButton bStop = new JButton("Stop");
bStop.setMaximumSize(new Dimension(Short.MAX_VALUE, 25));
pRun.add(bRun);
pRun.add(bStop);
taLog = new JTextArea();
JScrollPane spLog = new JScrollPane(taLog);
progress = new JProgressBar();
pMain.add(pInput);
pMain.add(pOutput);
pMain.add(pRun);
pMain.add(spLog);
pMain.add(progress);
File curr_dir = new File(".");
String filenames[] = curr_dir.list();
for (String name : filenames) {
if (name.endsWith(".raw") || name.endsWith(".scp"))
tfInputFile.addItem(name);
}
bRun.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (asr != null)
return;
new Thread() {
public void run() {
try {
asr = new KaldiASR(Options.KALDI_HOST, Options.KALDI_PORT);
if (cbMLF.isSelected())
mlf = new MLF();
if (rbFile.isSelected()) {
recognize(new File(tfInputFile.getSelectedItem().toString()));
} else if (rbScp.isSelected()) {
SCPFile scp = new SCPFile(new File(tfInputFile.getSelectedItem().toString()));
for (String filename : scp) {
recognize(new File(filename));
}
}
if (asr != null) {
asr.close();
asr = null;
}
if (cbMLF.isSelected())
mlf.save(new File(Options.MLF_FILENAME));
} catch (Exception e) {
Main.error(e);
}
}
}.start();
}
});
bStop.addActionListener(new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
if (asr != null) {
try {
asr.close();
} catch (IOException e1) {
}
asr = null;
}
}
});
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(250, 250, 600, 600);
setVisible(true);
}
public static void log(String msg) {
main_frame.taLog.append(msg + "\n");
}
public static void error(Throwable e) {
main_frame.taLog.append("ERROR: " + e.getMessage() + "\n");
e.printStackTrace();
}
public static void resetProgress(int max) {
main_frame.progress.setMaximum(max);
main_frame.progress.setValue(0);
}
public static void progress(int val) {
main_frame.progress.setValue(val);
}
public void recognize(File raw_file) {
try {
String path = raw_file.getAbsolutePath();
log("Recognizing: " + path);
long input_size = raw_file.length();
FileInputStream file_stream = new FileInputStream(raw_file);
OutputProcess output = new OutputProcess();
if (cbTextGrid.isSelected()) {
File textgrid = new File(path.substring(0, path.length() - 4) + ".TextGrid");
output.saveTextGrid(textgrid);
}
if (cbWebVTT.isSelected()) {
File webvtt = new File(path.substring(0, path.length() - 4) + ".vtt");
output.saveWebVVT(webvtt);
}
if (cbHTK.isSelected()) {
File htk = new File(path.substring(0, path.length() - 4) + ".lab");
output.saveHTK(htk);
}
asr.recognize(file_stream, input_size, output);
output.finalize();
if (cbMLF.isSelected()) {
String filename = raw_file.getName();
mlf.add(filename.substring(0, filename.length() - 4) + ".lab", output);
}
file_stream.close();
} catch (Exception e) {
Main.error(e);
}
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
if (Options.propertiesFileExists())
Options.load();
else
Options.save();
main_frame = new Main();
} catch (Exception e) {
e.printStackTrace();
}
}
}