Debian Codegraph @

main/flite_main.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2001 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: January 2001 */
35 /*************************************************************************/
36 /* */
37 /* Simple top level program */
38 /* */
39 /*************************************************************************/
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45
46 #include "flite.h"
47 #include "flite_version.h"
48
49 cst_val *flite_set_voice_list(const char *voxdir);
50 void *flite_set_lang_list(void);
51
52 void cst_alloc_debug_summary();
53
54 /* Its not very appropriate that these are declared here */
55 void usenglish_init(cst_voice *v);
56 cst_lexicon *cmu_lex_init(void);
57
58 static void flite_version()
59 {
60 printf(" Carnegie Mellon University, Copyright (c) 1999-2016, all rights reserved\n");
61 printf(" version: %s-%s-%s %s (http://cmuflite.org)\n",
62 FLITE_PROJECT_PREFIX,
63 FLITE_PROJECT_VERSION,
64 FLITE_PROJECT_STATE,
65 FLITE_PROJECT_DATE);
66 }
67
68 static void flite_usage()
69 {
70 printf("flite: a small simple speech synthesizer\n");
71 flite_version();
72 printf("usage: flite TEXT/FILE [WAVEFILE]\n"
73 " Converts text in TEXTFILE to a waveform in WAVEFILE\n"
74 " If text contains a space the it is treated as a literal\n"
75 " textstring and spoken, and not as a file name\n"
76 " if WAVEFILE is unspecified or \"play\" the result is\n"
77 " played on the current systems audio device. If WAVEFILE\n"
78 " is \"none\" the waveform is discarded (good for benchmarking)\n"
79 " Other options must appear before these options\n"
80 " --version Output flite version number\n"
81 " --help Output usage string\n"
82 " -o WAVEFILE Explicitly set output filename\n"
83 " -f TEXTFILE Explicitly set input filename\n"
84 " -t TEXT Explicitly set input textstring\n"
85 " -p PHONES Explicitly set input textstring and synthesize as phones\n"
86 " --set F=V Set feature (guesses type)\n"
87 " -s F=V Set feature (guesses type)\n"
88 " --seti F=V Set int feature\n"
89 " --setf F=V Set float feature\n"
90 " --sets F=V Set string feature\n"
91 " -ssml Read input text/file in ssml mode\n"
92 " -b Benchmark mode\n"
93 " -l Loop endlessly\n"
94 " -voice NAME Use voice NAME (NAME can be pathname/url to flitevox file)\n"
95 " -voicedir NAME Directory containing (clunit) voice data\n"
96 " -lv List voices available\n"
97 " -add_lex FILENAME add lex addenda from FILENAME\n"
98 " -pw Print words\n"
99 " -ps Print segments\n"
100 " -psdur Print segments and their durations (end-time)\n"
101 " -pr RelName Print relation RelName\n"
102 " -voicedump FILENAME Dump selected (cg) voice to FILENAME\n"
103 " -v Verbose mode\n");
104 exit(0);
105 }
106
107 static void flite_voice_list_print(void)
108 {
109 cst_voice *voice;
110 const cst_val *v;
111
112 printf("Voices available: ");
113 for (v=flite_voice_list; v; v=val_cdr(v))
114 {
115 voice = val_voice(val_car(v));
116 printf("%s ",voice->name);
117 }
118 printf("\n");
119
120 return;
121 }
122
123 static cst_utterance *print_info(cst_utterance *u)
124 {
125 cst_item *item;
126 const char *relname;
127 int printEndTime = 0;
128 int printStress = 0;
129
130 relname = utt_feat_string(u,"print_info_relation");
131 if (cst_streq(relname, "SegmentEndTime"))
132 {
133 relname = "Segment";
134 printEndTime = 1;
135 }
136 if (cst_streq(relname, "SegmentStress"))
137 {
138 relname = "Segment";
139 printStress = 1;
140 }
141
142 for (item=relation_head(utt_relation(u,relname));
143 item;
144 item=item_next(item))
145 {
146 if (!printEndTime)
147 printf("%s",item_feat_string(item,"name"));
148 else
149 printf("%s:%1.3f",item_feat_string(item,"name"), item_feat_float(item,"end"));
150 if (printStress == 1)
151 {
152 if (cst_streq("+",ffeature_string(item,"ph_vc")))
153 printf("%s",ffeature_string(item,"R:SylStructure.parent.stress"));
154 }
155 printf(" ");
156 }
157 printf("\n");
158
159 return u;
160 }
161
162 static void ef_set(cst_features *f,const char *fv,const char *type)
163 {
164 /* set feature from fv (F=V), guesses type if not explicit type given */
165 const char *val;
166 char *feat;
167 const char *fname;
168
169 if ((val = strchr(fv,'=')) == 0)
170 {
171 fprintf(stderr,
172 "flite: can't find '=' in featval \"%s\", ignoring it\n",
173 fv);
174 }
175 else
176 {
177 feat = cst_strdup(fv);
178 feat[cst_strlen(fv)-cst_strlen(val)] = '\0';
179 fname=feat_own_string(f,feat);
180 val = val+1;
181 if ((type && cst_streq("int",type)) ||
182 ((type == 0) && (cst_regex_match(cst_rx_int,val))))
183 feat_set_int(f,fname,atoi(val));
184 else if ((type && cst_streq("float",type)) ||
185 ((type == 0) && (cst_regex_match(cst_rx_double,val))))
186 feat_set_float(f,fname,atof(val));
187 else
188 feat_set_string(f,fname,val);
189 cst_free(feat);
190 }
191 }
192
193 int main(int argc, char **argv)
194 {
195 struct timeval tv;
196 cst_voice *v;
197 const char *filename;
198 const char *outtype;
199 cst_voice *desired_voice = 0;
200 const char *voicedir = NULL;
201 int i;
202 float durs;
203 double time_start, time_end;
204 int flite_verbose, flite_loop, flite_bench;
205 int explicit_filename, explicit_text, explicit_phones, ssml_mode;
206 #define ITER_MAX 3
207 int bench_iter = 0;
208 cst_features *extra_feats;
209 const char *lex_addenda_file = NULL;
210 const char *voicedumpfile = NULL;
211 cst_audio_streaming_info *asi;
212
213 filename = 0;
214 outtype = "play"; /* default is to play */
215 flite_verbose = FALSE;
216 flite_loop = FALSE;
217 flite_bench = FALSE;
218 explicit_text = explicit_filename = explicit_phones = FALSE;
219 ssml_mode = FALSE;
220 extra_feats = new_features();
221
222 flite_init();
223 flite_set_lang_list(); /* defined at compilation time */
224
225 for (i=1; i<argc; i++)
226 {
227 if (cst_streq(argv[i],"--version"))
228 {
229 flite_version();
230 return 1;
231 }
232 else if (cst_streq(argv[i],"-h") ||
233 cst_streq(argv[i],"--help") ||
234 cst_streq(argv[i],"-?"))
235 flite_usage();
236 else if (cst_streq(argv[i],"-v"))
237 flite_verbose = TRUE;
238 else if (cst_streq(argv[i],"-lv"))
239 {
240 if (flite_voice_list == NULL)
241 flite_set_voice_list(voicedir);
242 flite_voice_list_print();
243 exit(0);
244 }
245 else if (cst_streq(argv[i],"-l"))
246 flite_loop = TRUE;
247 else if (cst_streq(argv[i],"-b"))
248 {
249 flite_bench = TRUE;
250 break; /* ignore other arguments */
251 }
252 else if ((cst_streq(argv[i],"-o")) && (i+1 < argc))
253 {
254 outtype = argv[i+1];
255 i++;
256 }
257 else if ((cst_streq(argv[i],"-voice")) && (i+1 < argc))
258 {
259 if (flite_voice_list == NULL)
260 flite_set_voice_list(voicedir);
261 desired_voice = flite_voice_select(argv[i+1]);
262 i++;
263 }
264 else if ((cst_streq(argv[i],"-voicedir")) && (i+1 < argc))
265 {
266 voicedir = argv[i+1];
267 if (flite_voice_list == NULL)
268 flite_set_voice_list(voicedir);
269 i++;
270 }
271 else if ((cst_streq(argv[i],"-add_lex")) && (i+1 < argc))
272 {
273 lex_addenda_file = argv[i+1];
274 i++;
275 }
276 else if (cst_streq(argv[i],"-f") && (i+1 < argc))
277 {
278 filename = argv[i+1];
279 explicit_filename = TRUE;
280 i++;
281 }
282 else if (cst_streq(argv[i],"-pw"))
283 {
284 feat_set_string(extra_feats,"print_info_relation","Word");
285 feat_set(extra_feats,"post_synth_hook_func",
286 uttfunc_val(&print_info));
287 }
288 else if (cst_streq(argv[i],"-ps"))
289 {
290 feat_set_string(extra_feats,"print_info_relation","Segment");
291 feat_set(extra_feats,"post_synth_hook_func",
292 uttfunc_val(&print_info));
293 }
294 else if (cst_streq(argv[i],"-psdur"))
295 {
296 // Added by AUP Mar 2013 for extracting durations (end-time) of segments
297 // (useful in talking heads, etc.)
298 feat_set_string(extra_feats,"print_info_relation","SegmentEndTime");
299 feat_set(extra_feats,"post_synth_hook_func",
300 uttfunc_val(&print_info));
301 }
302 else if (cst_streq(argv[i],"-psstress"))
303 {
304 feat_set_string(extra_feats,"print_info_relation","SegmentStress");
305 feat_set(extra_feats,"post_synth_hook_func",
306 uttfunc_val(&print_info));
307 }
308 else if (cst_streq(argv[i],"-ssml"))
309 {
310 ssml_mode = TRUE;
311 }
312 else if (cst_streq(argv[i],"-pr") && (i+1 < argc))
313 {
314 feat_set_string(extra_feats,"print_info_relation",argv[i+1]);
315 feat_set(extra_feats,"post_synth_hook_func",
316 uttfunc_val(&print_info));
317 i++;
318 }
319 else if (cst_streq(argv[i],"-voicedump") && (i+1 < argc))
320 {
321 voicedumpfile = argv[i+1];
322 i++;
323 }
324 else if ((cst_streq(argv[i],"-set") || cst_streq(argv[i],"-s"))
325 && (i+1 < argc))
326 {
327 ef_set(extra_feats,argv[i+1],0);
328 i++;
329 }
330 else if (cst_streq(argv[i],"--seti") && (i+1 < argc))
331 {
332 ef_set(extra_feats,argv[i+1],"int");
333 i++;
334 }
335 else if (cst_streq(argv[i],"--setf") && (i+1 < argc))
336 {
337 ef_set(extra_feats,argv[i+1],"float");
338 i++;
339 }
340 else if (cst_streq(argv[i],"--sets") && (i+1 < argc))
341 {
342 ef_set(extra_feats,argv[i+1],"string");
343 i++;
344 }
345 else if (cst_streq(argv[i],"-p") && (i+1 < argc))
346 {
347 filename = argv[i+1];
348 explicit_phones = TRUE;
349 i++;
350 }
351 else if (cst_streq(argv[i],"-t") && (i+1 < argc))
352 {
353 filename = argv[i+1];
354 explicit_text = TRUE;
355 i++;
356 }
357 else if (filename)
358 outtype = argv[i];
359 else
360 filename = argv[i];
361 }
362
363 if (filename == NULL) filename = "-"; /* stdin */
364 if (flite_voice_list == NULL)
365 flite_set_voice_list(voicedir);
366 if (desired_voice == 0)
367 desired_voice = flite_voice_select(NULL);
368
369 v = desired_voice;
370 feat_copy_into(extra_feats,v->features);
371 durs = 0.0;
372
373 if (voicedumpfile != NULL)
374 {
375 flite_voice_dump(v,voicedumpfile);
376 exit(0);
377 }
378
379 if (lex_addenda_file)
380 flite_voice_add_lex_addenda(v,lex_addenda_file);
381
382 if (cst_streq("stream",outtype))
383 {
384 asi = new_audio_streaming_info();
385 asi->asc = audio_stream_chunk;
386 feat_set(v->features,"streaming_info",audio_streaming_info_val(asi));
387 }
388
389 if (flite_bench)
390 {
391 outtype = "none";
392 filename = "A whole joy was reaping, but they've gone south, you should fetch azure mike.";
393 explicit_text = TRUE;
394 }
395
396 loop:
397 gettimeofday(&tv,NULL);
398 time_start = (double)(tv.tv_sec)+(((double)tv.tv_usec)/1000000.0);
399
400 if (explicit_phones)
401 durs = flite_phones_to_speech(filename,v,outtype);
402 else if ((strchr(filename,' ') && !explicit_filename) || explicit_text)
403 {
404 if (ssml_mode)
405 durs = flite_ssml_text_to_speech(filename,v,outtype);
406 else
407 durs = flite_text_to_speech(filename,v,outtype);
408 }
409 else
410 {
411 if (ssml_mode)
412 durs = flite_ssml_file_to_speech(filename,v,outtype);
413 else
414 durs = flite_file_to_speech(filename,v,outtype);
415 }
416
417 gettimeofday(&tv,NULL);
418 time_end = ((double)(tv.tv_sec))+((double)tv.tv_usec/1000000.0);
419
420 if (flite_verbose || (flite_bench && bench_iter == ITER_MAX))
421 printf("times faster than real-time: %f\n(%f seconds of speech synthesized in %f)\n",
422 durs/(float)(time_end-time_start),
423 durs,
424 (float)(time_end-time_start));
425
426 if (flite_loop || (flite_bench && bench_iter++ < ITER_MAX))
427 goto loop;
428
429 delete_features(extra_feats);
430 delete_val(flite_voice_list); flite_voice_list=0;
431 /* cst_alloc_debug_summary(); */
432
433 return 0;
434 }