-
Notifications
You must be signed in to change notification settings - Fork 8
/
phnrec.cpp
299 lines (267 loc) · 7.06 KB
/
phnrec.cpp
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
/**************************************************************************
* copyright : (C) 2004-2006 by Petr Schwarz & Pavel Matejka *
* UPGM,FIT,VUT,Brno *
* email : {schwarzp,matejkap}@fit.vutbr.cz *
**************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
**************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string>
//#include <getopt.h>
#include <map>
#include "config.h"
#include "srec.h"
#include "getopt.h"
void help()
{
puts("\nUSAGE: phnrec [options]\n");
puts(" -c dir configuration directory");
puts(" -l file list of files");
puts(" -i file input file");
puts(" -o file output file");
puts(" -m file output MLF");
puts(" -a live audio input");
puts(" -s fmt [waveform] source format (wf-waveform, par-parameters, post-posteriors)");
puts(" -t fmt [strings] target format (par-parameters, post-posteriors, str-strings)");
puts(" -w fmt [lin16] waveform format (lin16, alaw)");
puts(" -f fmt [str] live output format (str, strlen, lab)");
puts(" -p num [-3.8] phoneme insertion penalty");
puts(" -v verbose\n");
}
typedef enum {ofUnknown, ofLab, ofStr, ofStrLen} live_oformat;
live_oformat str2oform(char *str)
{
if(strcmp(str, "lab") == 0)
return ofLab;
else if(strcmp(str, "str") == 0)
return ofStr;
else if(strcmp(str, "strlen") == 0)
return ofStrLen;
else
{
fprintf(stderr, "ERROR: Invalid output format: %s. (can be 'lab', 'str', 'strlen')\n", str);
exit(1);
}
return ofUnknown;
}
typedef struct
{
live_oformat fmt;
char prevWord[256];
long_long prevStart;
bool dumped;
SpeechRec *SR;
bool kws;
} live_conf;
void live_callback(unsigned int message, char *word, long_long start, long_long stop, float score, void *tmp)
{
live_conf *cfg = (live_conf *)tmp;
live_oformat fmt = cfg->fmt;
if(message == DECMSG_NEWESTIM && strcmp(word, cfg->prevWord) == 0 &&
start == cfg->prevStart && cfg->dumped)
return;
#ifndef PHNREC_ONLY
if(cfg->kws && score < cfg->SR->THR.GetThreshold(word))
return;
#endif // PHNREC_ONLY
strcpy(cfg->prevWord, word);
cfg->prevStart = start;
cfg->dumped = true;
int len = (int)((stop - start)/100000 + 1);
switch(fmt)
{
case ofLab:
#ifdef _MSC_VER
fprintf(stdout, "%I64d %I64d", start, stop);
#else
fprintf(stdout, "%lli %lli", start, stop);
#endif
fprintf(stdout, " %s %f\n", word, score);
break;
case ofStr:
fprintf(stdout, " %s\n", word);
break;
case ofStrLen:
fprintf(stdout, " %s(%d)\n", word, len);
break;
case ofUnknown:
break;
}
}
int main(int argc, char *argv[])
{
char *config_dir = 0;
char *file_list = 0;
char *input_file = 0;
char *output_file = 0;
bool live_input = false;
char *output_mlf = 0;
char *wpenalty = 0;
bool verbose = false;
live_oformat format = ofStr;
SpeechRec::data_format iformat = SpeechRec::dfWaveform;
SpeechRec::data_format oformat = SpeechRec::dfStrings;
SpeechRec::wave_format wformat = SpeechRec::wfUnknown;
SpeechRec SR;
// command line parsing
if(argc == 1)
{
help();
exit(1);
}
optind = 0;
while (1)
{
int c = getopt(argc, argv, "-c:l:i:o:m:as:t:w:f:p:v");
if(c == -1)
break;
switch(c)
{
case 'c':
config_dir = optarg;
break;
case 'l':
file_list = optarg;
break;
case 'i':
input_file = optarg;
break;
case 'o':
output_file = optarg;
break;
case 'm':
output_mlf = optarg;
break;
case 'a':
live_input = true;
break;
case 's':
iformat = SR.Str2DataFormat(optarg);
if(iformat == SpeechRec::dfUnknown)
exit(1);
break;
case 't':
oformat = SR.Str2DataFormat(optarg);
if(oformat == SpeechRec::dfUnknown)
exit(1);
break;
case 'w':
wformat = SR.Str2WaveFormat(optarg);
if(wformat == SpeechRec::wfUnknown)
exit(1);
break;
case 'p':
wpenalty = optarg;
break;
case 'f':
format = str2oform(optarg);
break;
case 'v':
verbose = true;
break;
case '?':
fprintf(stderr, "ERROR: Error during command line parsing\n");
exit(1);
}
}
// set log handler according verbosity
if(!verbose)
SR.SetLogHandler(&SpeechRec::MDummy, 0);
// construct speech recognizer
if(!config_dir)
{
fprintf(stderr, "ERROR: Configuration directory is not set (-c)\n");
exit(1);
}
char config_file[1024];
strcpy(config_file, config_dir);
strcat(config_file, dirSep);
strcat(config_file, "config");
if(!SR.Init(config_file))
exit(1);
// set word insertion penalty
if(wpenalty)
{
float value;
if(sscanf(wpenalty, "%f", &value) != 1)
{
fprintf(stderr, "ERROR: Invalid argument for -p switch at command line: %s\n", wpenalty);
exit(1);
}
SR.DE->SetWPenalty(value);
}
// set waveform format
if(wformat != SpeechRec::wfUnknown)
SR.SetWaveFormat(wformat);
// check the input file name if an output is asked
if(output_file && !input_file)
{
fprintf(stderr, "ERROR: The input file is not specified (-i)\n");
exit(1);
}
if(!SR.CheckDataFormatConv(iformat, oformat))
{
fprintf(stderr, "ERROR: Unsupported data conversion (-s, -t)\n");
exit(1);
}
// process input file
if(input_file)
{
char line[1024];
strcpy(line, input_file);
if(output_file)
{
strcat(line, " ");
strcat(line, output_file);
}
if(!SR.ProcessFileListLine(iformat, oformat, line))
exit(1);
}
// process file list
if(file_list)
{
if(!SR.ProcessFileList(iformat, oformat, file_list, output_mlf))
exit(1);
}
// run the live audio
if(live_input)
{
live_conf cfg;
cfg.fmt = format;
const char *mode = SR.C.GetString("decoder", "mode");
if(strcmp(mode, "kws") == 0)
{
#ifndef PHNREC_ONLY
if(strcmp(SR.C.GetString("decoder", "type"), "stkint") == 0)
{
((StkInterface *)SR.DE)->SetImproveKwdEstim(false);
}
#endif // PHNREC_ONLY
cfg.prevWord[0] = '\0';
cfg.prevStart = -1;
cfg.dumped = false;
cfg.SR = &SR;
cfg.kws = true;
}
else
{
cfg.kws = false;
}
SR.DE->SetCallbackFunc(&live_callback, (void *)&cfg);
if(SR.C.GetInt("onlinenorm", "estim_interval") != 0)
{
printf("Estimation of normalization parameters, please speak ...\n");
}
if(!SR.RunLive())
exit(1);
}
return 0;
}