-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathflite_main.c
439 lines (405 loc) · 13.9 KB
/
flite_main.c
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*************************************************************************/
/* */
/* Language Technologies Institute */
/* Carnegie Mellon University */
/* Copyright (c) 2001 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/*************************************************************************/
/* Author: Alan W Black (awb@cs.cmu.edu) */
/* Date: January 2001 */
/*************************************************************************/
/* */
/* Simple top level program */
/* */
/*************************************************************************/
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include "flite.h"
#include "flite_version.h"
cst_val *flite_set_voice_list(const char *voxdir);
#ifdef WASM32_WASI
void flite_set_lang_list(void);
#else
void *flite_set_lang_list(void);
#endif
void cst_alloc_debug_summary();
/* Its not very appropriate that these are declared here */
void usenglish_init(cst_voice *v);
cst_lexicon *cmu_lex_init(void);
static void flite_version()
{
printf(" Carnegie Mellon University, Copyright (c) 1999-2016, all rights reserved\n");
printf(" version: %s-%s-%s %s (http://cmuflite.org)\n",
FLITE_PROJECT_PREFIX,
FLITE_PROJECT_VERSION,
FLITE_PROJECT_STATE,
FLITE_PROJECT_DATE);
}
static void flite_usage()
{
printf("flite: a small simple speech synthesizer\n");
flite_version();
printf("usage: flite TEXT/FILE [WAVEFILE]\n"
" Converts text in TEXTFILE to a waveform in WAVEFILE\n"
" If text contains a space the it is treated as a literal\n"
" textstring and spoken, and not as a file name\n"
" if WAVEFILE is unspecified or \"play\" the result is\n"
" played on the current systems audio device. If WAVEFILE\n"
" is \"none\" the waveform is discarded (good for benchmarking)\n"
" Other options must appear before these options\n"
" --version Output flite version number\n"
" --help Output usage string\n"
" -o WAVEFILE Explicitly set output filename\n"
" -f TEXTFILE Explicitly set input filename\n"
" -t TEXT Explicitly set input textstring\n"
" -p PHONES Explicitly set input textstring and synthesize as phones\n"
" --set F=V Set feature (guesses type)\n"
" -s F=V Set feature (guesses type)\n"
" --seti F=V Set int feature\n"
" --setf F=V Set float feature\n"
" --sets F=V Set string feature\n"
" -ssml Read input text/file in ssml mode\n"
" -b Benchmark mode\n"
" -l Loop endlessly\n"
" -voice NAME Use voice NAME (NAME can be pathname/url to flitevox file)\n"
" -voicedir NAME Directory containing (clunit) voice data\n"
" -lv List voices available\n"
" -add_lex FILENAME add lex addenda from FILENAME\n"
" -pw Print words\n"
" -ps Print segments\n"
" -psdur Print segments and their durations (end-time)\n"
" -pr RelName Print relation RelName\n"
" -voicedump FILENAME Dump selected (cg) voice to FILENAME\n"
" -v Verbose mode\n");
exit(0);
}
static void flite_voice_list_print(void)
{
cst_voice *voice;
const cst_val *v;
printf("Voices available: ");
for (v=flite_voice_list; v; v=val_cdr(v))
{
voice = val_voice(val_car(v));
printf("%s ",voice->name);
}
printf("\n");
return;
}
static cst_utterance *print_info(cst_utterance *u)
{
cst_item *item;
const char *relname;
int printEndTime = 0;
int printStress = 0;
relname = utt_feat_string(u,"print_info_relation");
if (cst_streq(relname, "SegmentEndTime"))
{
relname = "Segment";
printEndTime = 1;
}
if (cst_streq(relname, "SegmentStress"))
{
relname = "Segment";
printStress = 1;
}
for (item=relation_head(utt_relation(u,relname));
item;
item=item_next(item))
{
if (!printEndTime)
printf("%s",item_feat_string(item,"name"));
else
printf("%s:%1.3f",item_feat_string(item,"name"), item_feat_float(item,"end"));
if (printStress == 1)
{
if (cst_streq("+",ffeature_string(item,"ph_vc")))
printf("%s",ffeature_string(item,"R:SylStructure.parent.stress"));
}
printf(" ");
}
printf("\n");
return u;
}
static void ef_set(cst_features *f,const char *fv,const char *type)
{
/* set feature from fv (F=V), guesses type if not explicit type given */
const char *val;
char *feat;
const char *fname;
if ((val = strchr(fv,'=')) == 0)
{
fprintf(stderr,
"flite: can't find '=' in featval \"%s\", ignoring it\n",
fv);
}
else
{
feat = cst_strdup(fv);
feat[cst_strlen(fv)-cst_strlen(val)] = '\0';
fname=feat_own_string(f,feat);
val = val+1;
if ((type && cst_streq("int",type)) ||
((type == 0) && (cst_regex_match(cst_rx_int,val))))
feat_set_int(f,fname,atoi(val));
else if ((type && cst_streq("float",type)) ||
((type == 0) && (cst_regex_match(cst_rx_double,val))))
feat_set_float(f,fname,atof(val));
else
feat_set_string(f,fname,val);
cst_free(feat);
}
}
int main(int argc, char **argv)
{
struct timeval tv;
cst_voice *v;
const char *filename;
const char *outtype;
cst_voice *desired_voice = 0;
const char *voicedir = NULL;
int i;
float durs;
double time_start, time_end;
int flite_verbose, flite_loop, flite_bench;
int explicit_filename, explicit_text, explicit_phones, ssml_mode;
#define ITER_MAX 3
int bench_iter = 0;
cst_features *extra_feats;
const char *lex_addenda_file = NULL;
const char *voicedumpfile = NULL;
cst_audio_streaming_info *asi;
filename = 0;
outtype = "play"; /* default is to play */
flite_verbose = FALSE;
flite_loop = FALSE;
flite_bench = FALSE;
explicit_text = explicit_filename = explicit_phones = FALSE;
ssml_mode = FALSE;
extra_feats = new_features();
flite_init();
flite_set_lang_list(); /* defined at compilation time */
for (i=1; i<argc; i++)
{
if (cst_streq(argv[i],"--version"))
{
flite_version();
return 1;
}
else if (cst_streq(argv[i],"-h") ||
cst_streq(argv[i],"--help") ||
cst_streq(argv[i],"-?"))
flite_usage();
else if (cst_streq(argv[i],"-v"))
flite_verbose = TRUE;
else if (cst_streq(argv[i],"-lv"))
{
if (flite_voice_list == NULL)
flite_set_voice_list(voicedir);
flite_voice_list_print();
exit(0);
}
else if (cst_streq(argv[i],"-l"))
flite_loop = TRUE;
else if (cst_streq(argv[i],"-b"))
{
flite_bench = TRUE;
break; /* ignore other arguments */
}
else if ((cst_streq(argv[i],"-o")) && (i+1 < argc))
{
outtype = argv[i+1];
i++;
}
else if ((cst_streq(argv[i],"-voice")) && (i+1 < argc))
{
if (flite_voice_list == NULL)
flite_set_voice_list(voicedir);
desired_voice = flite_voice_select(argv[i+1]);
i++;
}
else if ((cst_streq(argv[i],"-voicedir")) && (i+1 < argc))
{
voicedir = argv[i+1];
if (flite_voice_list == NULL)
flite_set_voice_list(voicedir);
i++;
}
else if ((cst_streq(argv[i],"-add_lex")) && (i+1 < argc))
{
lex_addenda_file = argv[i+1];
i++;
}
else if (cst_streq(argv[i],"-f") && (i+1 < argc))
{
filename = argv[i+1];
explicit_filename = TRUE;
i++;
}
else if (cst_streq(argv[i],"-pw"))
{
feat_set_string(extra_feats,"print_info_relation","Word");
feat_set(extra_feats,"post_synth_hook_func",
uttfunc_val(&print_info));
}
else if (cst_streq(argv[i],"-ps"))
{
feat_set_string(extra_feats,"print_info_relation","Segment");
feat_set(extra_feats,"post_synth_hook_func",
uttfunc_val(&print_info));
}
else if (cst_streq(argv[i],"-psdur"))
{
// Added by AUP Mar 2013 for extracting durations (end-time) of segments
// (useful in talking heads, etc.)
feat_set_string(extra_feats,"print_info_relation","SegmentEndTime");
feat_set(extra_feats,"post_synth_hook_func",
uttfunc_val(&print_info));
}
else if (cst_streq(argv[i],"-psstress"))
{
feat_set_string(extra_feats,"print_info_relation","SegmentStress");
feat_set(extra_feats,"post_synth_hook_func",
uttfunc_val(&print_info));
}
else if (cst_streq(argv[i],"-ssml"))
{
ssml_mode = TRUE;
}
else if (cst_streq(argv[i],"-pr") && (i+1 < argc))
{
feat_set_string(extra_feats,"print_info_relation",argv[i+1]);
feat_set(extra_feats,"post_synth_hook_func",
uttfunc_val(&print_info));
i++;
}
else if (cst_streq(argv[i],"-voicedump") && (i+1 < argc))
{
voicedumpfile = argv[i+1];
i++;
}
else if ((cst_streq(argv[i],"-set") || cst_streq(argv[i],"-s"))
&& (i+1 < argc))
{
ef_set(extra_feats,argv[i+1],0);
i++;
}
else if (cst_streq(argv[i],"--seti") && (i+1 < argc))
{
ef_set(extra_feats,argv[i+1],"int");
i++;
}
else if (cst_streq(argv[i],"--setf") && (i+1 < argc))
{
ef_set(extra_feats,argv[i+1],"float");
i++;
}
else if (cst_streq(argv[i],"--sets") && (i+1 < argc))
{
ef_set(extra_feats,argv[i+1],"string");
i++;
}
else if (cst_streq(argv[i],"-p") && (i+1 < argc))
{
filename = argv[i+1];
explicit_phones = TRUE;
i++;
}
else if (cst_streq(argv[i],"-t") && (i+1 < argc))
{
filename = argv[i+1];
explicit_text = TRUE;
i++;
}
else if (filename)
outtype = argv[i];
else
filename = argv[i];
}
if (filename == NULL) filename = "-"; /* stdin */
if (flite_voice_list == NULL)
flite_set_voice_list(voicedir);
if (desired_voice == 0)
desired_voice = flite_voice_select(NULL);
v = desired_voice;
feat_copy_into(extra_feats,v->features);
durs = 0.0;
if (voicedumpfile != NULL)
{
flite_voice_dump(v,voicedumpfile);
exit(0);
}
if (lex_addenda_file)
flite_voice_add_lex_addenda(v,lex_addenda_file);
if (cst_streq("stream",outtype))
{
asi = new_audio_streaming_info();
asi->asc = audio_stream_chunk;
feat_set(v->features,"streaming_info",audio_streaming_info_val(asi));
}
if (flite_bench)
{
outtype = "none";
filename = "A whole joy was reaping, but they've gone south, you should fetch azure mike.";
explicit_text = TRUE;
}
loop:
gettimeofday(&tv,NULL);
time_start = (double)(tv.tv_sec)+(((double)tv.tv_usec)/1000000.0);
if (explicit_phones)
durs = flite_phones_to_speech(filename,v,outtype);
else if ((strchr(filename,' ') && !explicit_filename) || explicit_text)
{
if (ssml_mode)
durs = flite_ssml_text_to_speech(filename,v,outtype);
else
durs = flite_text_to_speech(filename,v,outtype);
}
else
{
if (ssml_mode)
durs = flite_ssml_file_to_speech(filename,v,outtype);
else
durs = flite_file_to_speech(filename,v,outtype);
}
gettimeofday(&tv,NULL);
time_end = ((double)(tv.tv_sec))+((double)tv.tv_usec/1000000.0);
if (flite_verbose || (flite_bench && bench_iter == ITER_MAX))
printf("times faster than real-time: %f\n(%f seconds of speech synthesized in %f)\n",
durs/(float)(time_end-time_start),
durs,
(float)(time_end-time_start));
if (flite_loop || (flite_bench && bench_iter++ < ITER_MAX))
goto loop;
delete_features(extra_feats);
delete_val(flite_voice_list); flite_voice_list=0;
/* cst_alloc_debug_summary(); */
return 0;
}