Debian Codegraph @

src/synth/flite.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2000-2008 */
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: September 2000 */
35 /*************************************************************************/
36 /* */
37 /* Basic user level functions */
38 /* */
39 /*************************************************************************/
40
41 #include "cst_tokenstream.h"
42 #include "flite.h"
43 #include "cst_alloc.h"
44 #include "cst_clunits.h"
45 #include "cst_cg.h"
46
47 #ifdef WIN32
48 /* For Visual Studio 2012 global variable definitions */
49 #define GLOBALVARDEF __declspec(dllexport)
50 #else
51 #define GLOBALVARDEF
52 #endif
53
54 /* This is a global, which isn't ideal, this may change */
55 /* It is set when flite_set_voice_list() is called which happens in */
56 /* flite_main(), but it is now also possible to leave this unset if */
57 /* all voice selection names are pathnames to (cg) ./flitevox files */
58 /* then this gets populated as the voices get loaded */
59 /* Note these voices remain loaded, there is currently no automatic */
60 /* garbage collection, that would be necessary in the long run */
61 /* delete_voice will work, but you'd need to know when to call it */
62 GLOBALVARDEF cst_val *flite_voice_list = NULL;
63
64 /* Another global with hold pointers to the language and lexicon */
65 /* initalization functions, we limiting to 20 but it could be bigger */
66 /* if we really did support over 20 different languages */
67 #define FLITE_MAX_LANGS 20
68 GLOBALVARDEF cst_lang flite_lang_list[FLITE_MAX_LANGS];
69 GLOBALVARDEF int flite_lang_list_length = 0;
70
71 int flite_init()
72 {
73 cst_regex_init();
74
75 return 0;
76 }
77
78 int flite_voice_dump(cst_voice *voice, const char *filename)
79 {
80 return cst_cg_dump_voice(voice,filename);
81 }
82
83 cst_voice *flite_voice_load(const char *filename)
84 {
85 /* Currently only supported for CG voices */
86 /* filename make be a local pathname or a url (http:/file:) */
87 cst_voice *v = NULL;
88
89 v = cst_cg_load_voice(filename,flite_lang_list);
90
91 return v;
92 }
93
94 int flite_add_voice(cst_voice *voice)
95 {
96 const cst_val *x;
97 if (voice)
98 {
99 /* add to second place -- first is default voice */
100 /* This is thread unsafe */
101 if (flite_voice_list)
102 { /* Other voices -- first is default, add this second */
103 x = cons_val(voice_val(voice),
104 val_cdr(flite_voice_list));
105 set_cdr((cst_val *)(void *)flite_voice_list,x);
106 }
107 else
108 { /* Only voice so goes on front */
109 flite_voice_list = cons_val(voice_val(voice),flite_voice_list);
110 }
111
112 return TRUE;
113 }
114 else
115 return FALSE;
116
117 }
118
119 int flite_add_lang(const char *langname,
120 void (*lang_init)(cst_voice *vox),
121 cst_lexicon *(*lex_init)())
122 {
123 if (flite_lang_list_length < (FLITE_MAX_LANGS-1))
124 {
125 flite_lang_list[flite_lang_list_length].lang = langname;
126 flite_lang_list[flite_lang_list_length].lang_init = lang_init;
127 flite_lang_list[flite_lang_list_length].lex_init = lex_init;
128 flite_lang_list_length++;
129 flite_lang_list[flite_lang_list_length].lang = NULL;
130 }
131
132 return TRUE;
133 }
134
135
136 cst_voice *flite_voice_select(const char *name)
137 {
138 const cst_val *v;
139 cst_voice *voice;
140
141 if (name == NULL)
142 {
143 if (flite_voice_list == NULL)
144 return NULL; /* oops, not good */
145 return val_voice(val_car(flite_voice_list));
146 }
147
148 for (v=flite_voice_list; v; v=val_cdr(v))
149 {
150 voice = val_voice(val_car(v));
151 if (cst_streq(name,voice->name)) /* short name */
152 return voice;
153 if (cst_streq(name,get_param_string(voice->features,"name","")))
154 /* longer name */
155 return voice;
156 if (cst_streq(name,get_param_string(voice->features,"pathname","")))
157 /* even longer name (url) */
158 return voice;
159 }
160
161 if (cst_urlp(name) || /* naive check if its a url */
162 cst_strchr(name,'/') ||
163 cst_strchr(name,'\\') ||
164 cst_strstr(name,".flitevox"))
165 {
166 voice = flite_voice_load(name);
167 if (!voice)
168 cst_errmsg("Error load voice: failed to load voice from %s\n",name);
169 else
170 flite_add_voice(voice);
171 return voice;
172 }
173
174 return flite_voice_select(NULL);
175
176 }
177
178 int flite_voice_add_lex_addenda(cst_voice *v, const cst_string *lexfile)
179 {
180 /* Add addenda in lexfile to current voice */
181 cst_lexicon *lex;
182 const cst_val *lex_addenda = NULL;
183 cst_val *new_addenda;
184
185 lex = val_lexicon(feat_val(v->features,"lexicon"));
186 if (feat_present(v->features, "lex_addenda"))
187 lex_addenda = feat_val(v->features, "lex_addenda");
188
189 new_addenda = cst_lex_load_addenda(lex,lexfile);
190 #if 0
191 printf("\naddenda: ");
192 val_print(stdout,new_addenda);
193 printf("\n");
194 #endif
195
196 new_addenda = val_append(new_addenda,(cst_val *)lex_addenda);
197 if (lex->lex_addenda)
198 delete_val(lex->lex_addenda);
199 lex->lex_addenda = new_addenda;
200
201 return 0;
202 }
203
204 cst_utterance *flite_do_synth(cst_utterance *u,
205 cst_voice *voice,
206 cst_uttfunc synth)
207 {
208 utt_init(u, voice);
209 if ((*synth)(u) == NULL)
210 {
211 delete_utterance(u);
212 return NULL;
213 }
214 else
215 return u;
216 }
217
218 cst_utterance *flite_synth_text(const char *text, cst_voice *voice)
219 {
220 cst_utterance *u;
221
222 u = new_utterance();
223 utt_set_input_text(u,text);
224 return flite_do_synth(u, voice, utt_synth);
225 }
226
227 cst_utterance *flite_synth_phones(const char *text, cst_voice *voice)
228 {
229 cst_utterance *u;
230
231 u = new_utterance();
232 utt_set_input_text(u,text);
233 return flite_do_synth(u, voice, utt_synth_phones);
234 }
235
236 cst_wave *flite_text_to_wave(const char *text, cst_voice *voice)
237 {
238 cst_utterance *u;
239 cst_wave *w;
240
241 if ((u = flite_synth_text(text,voice)) == NULL)
242 return NULL;
243
244 w = copy_wave(utt_wave(u));
245 delete_utterance(u);
246 return w;
247 }
248
249 float flite_file_to_speech(const char *filename,
250 cst_voice *voice,
251 const char *outtype)
252 {
253 cst_tokenstream *ts;
254
255 if ((ts = ts_open(filename,
256 get_param_string(voice->features,"text_whitespace",NULL),
257 get_param_string(voice->features,"text_singlecharsymbols",NULL),
258 get_param_string(voice->features,"text_prepunctuation",NULL),
259 get_param_string(voice->features,"text_postpunctuation",NULL)))
260 == NULL)
261 {
262 cst_errmsg("failed to open file \"%s\" for reading\n",
263 filename);
264 return 1;
265 }
266 return flite_ts_to_speech(ts,voice,outtype);
267 }
268
269
270 float flite_ts_to_speech(cst_tokenstream *ts,
271 cst_voice *voice,
272 const char *outtype)
273 {
274 cst_utterance *utt;
275 const char *token;
276 cst_item *t;
277 cst_relation *tokrel;
278 float durs = 0;
279 int num_tokens;
280 cst_wave *w;
281 cst_breakfunc breakfunc = default_utt_break;
282 cst_uttfunc utt_user_callback = 0;
283 int fp;
284
285 fp = get_param_int(voice->features,"file_start_position",0);
286 if (fp > 0)
287 ts_set_stream_pos(ts,fp);
288 if (feat_present(voice->features,"utt_break"))
289 breakfunc = val_breakfunc(feat_val(voice->features,"utt_break"));
290
291 if (feat_present(voice->features,"utt_user_callback"))
292 utt_user_callback = val_uttfunc(feat_val(voice->features,"utt_user_callback"));
293
294 /* If its a file to write to, create and save an empty wave file */
295 /* as we are going to incrementally append to it */
296 if (!cst_streq(outtype,"play") &&
297 !cst_streq(outtype,"none") &&
298 !cst_streq(outtype,"stream"))
299 {
300 w = new_wave();
301 cst_wave_resize(w,0,1);
302 cst_wave_set_sample_rate(w,16000);
303 cst_wave_save_riff(w,outtype); /* an empty wave */
304 delete_wave(w);
305 }
306
307 num_tokens = 0;
308 utt = new_utterance();
309 tokrel = utt_relation_create(utt, "Token");
310 while (!ts_eof(ts) || num_tokens > 0)
311 {
312 token = ts_get(ts);
313 if ((cst_strlen(token) == 0) ||
314 (num_tokens > 500) || /* need an upper bound */
315 (relation_head(tokrel) &&
316 breakfunc(ts,token,tokrel)))
317 {
318 /* An end of utt, so synthesize it */
319 if (utt_user_callback)
320 utt = (utt_user_callback)(utt);
321
322 if (utt)
323 {
324 utt = flite_do_synth(utt,voice,utt_synth_tokens);
325 if (feat_present(utt->features,"Interrupted"))
326 {
327 delete_utterance(utt); utt = NULL;
328 break;
329 }
330 durs += flite_process_output(utt,outtype,TRUE);
331 delete_utterance(utt); utt = NULL;
332 }
333 else
334 break;
335
336 if (ts_eof(ts)) break;
337
338 utt = new_utterance();
339 tokrel = utt_relation_create(utt, "Token");
340 num_tokens = 0;
341 }
342 num_tokens++;
343
344 t = relation_append(tokrel, NULL);
345 item_set_string(t,"name",token);
346 item_set_string(t,"whitespace",ts->whitespace);
347 item_set_string(t,"prepunctuation",ts->prepunctuation);
348 item_set_string(t,"punc",ts->postpunctuation);
349 /* Mark it at the beginning of the token */
350 item_set_int(t,"file_pos",
351 ts->file_pos-(1+ /* as we are already on the next char */
352 cst_strlen(token)+
353 cst_strlen(ts->prepunctuation)+
354 cst_strlen(ts->postpunctuation)));
355 item_set_int(t,"line_number",ts->line_number);
356 }
357 if (utt) delete_utterance(utt);
358 ts_close(ts);
359 return durs;
360 }
361
362 float flite_text_to_speech(const char *text,
363 cst_voice *voice,
364 const char *outtype)
365 {
366 cst_utterance *u;
367 float dur;
368
369 u = flite_synth_text(text,voice);
370 dur = flite_process_output(u,outtype,FALSE);
371 delete_utterance(u);
372
373 return dur;
374 }
375
376 float flite_phones_to_speech(const char *text,
377 cst_voice *voice,
378 const char *outtype)
379 {
380 cst_utterance *u;
381 float dur;
382
383 u = flite_synth_phones(text,voice);
384 dur = flite_process_output(u,outtype,FALSE);
385 delete_utterance(u);
386
387 return dur;
388 }
389
390 float flite_process_output(cst_utterance *u, const char *outtype,
391 int append)
392 {
393 /* Play or save (append) output to output file */
394 cst_wave *w;
395 float dur;
396
397 if (!u) return 0.0;
398
399 w = utt_wave(u);
400
401 dur = (float)w->num_samples/(float)w->sample_rate;
402
403 if (cst_streq(outtype,"play"))
404 play_wave(w);
405 else if (cst_streq(outtype,"stream"))
406 {
407 /* It's already been played so do nothing */
408
409 }
410 else if (!cst_streq(outtype,"none"))
411 {
412 if (append)
413 cst_wave_append_riff(w,outtype);
414 else
415 cst_wave_save_riff(w,outtype);
416 }
417
418 return dur;
419 }
420
421 int flite_get_param_int(const cst_features *f, const char *name,int def)
422 {
423 return get_param_int(f,name,def);
424 }
425 float flite_get_param_float(const cst_features *f, const char *name, float def)
426 {
427 return get_param_float(f,name,def);
428 }
429 const char *flite_get_param_string(const cst_features *f, const char *name, const char *def)
430 {
431 return get_param_string(f,name,def);
432 }
433 const cst_val *flite_get_param_val(const cst_features *f, const char *name, cst_val *def)
434 {
435 return get_param_val(f,name,def);
436 }
437
438 void flite_feat_set_int(cst_features *f, const char *name, int v)
439 {
440 feat_set_int(f,name,v);
441 }
442 void flite_feat_set_float(cst_features *f, const char *name, float v)
443 {
444 feat_set_float(f,name,v);
445 }
446 void flite_feat_set_string(cst_features *f, const char *name, const char *v)
447 {
448 feat_set_string(f,name,v);
449 }
450 void flite_feat_set(cst_features *f, const char *name,const cst_val *v)
451 {
452 feat_set(f,name,v);
453 }
454 int flite_feat_remove(cst_features *f, const char *name)
455 {
456 return feat_remove(f,name);
457 }
458
459 const char *flite_ffeature_string(const cst_item *item,const char *featpath)
460 {
461 return ffeature_string(item,featpath);
462 }
463 int flite_ffeature_int(const cst_item *item,const char *featpath)
464 {
465 return ffeature_int(item,featpath);
466 }
467 float flite_ffeature_float(const cst_item *item,const char *featpath)
468 {
469 return ffeature_float(item,featpath);
470 }
471 const cst_val *flite_ffeature(const cst_item *item,const char *featpath)
472 {
473 return ffeature(item,featpath);
474 }
475
476 cst_item* flite_path_to_item(const cst_item *item,const char *featpath)
477 {
478 return path_to_item(item,featpath);
479 }
480
481 int flite_mmap_clunit_voxdata(const char *voxdir, cst_voice *voice)
482 {
483 /* Map clunit_db in voice data for giveb voice */
484 char *path;
485 const char *name;
486 const char *x;
487 int *indexes;
488 cst_filemap *vd;
489 cst_clunit_db *clunit_db;
490 int i;
491
492 name = get_param_string(voice->features,"name","voice");
493 path = cst_alloc(char,cst_strlen(voxdir)+1+cst_strlen(name)+1+cst_strlen("voxdata")+1);
494 cst_sprintf(path,"%s/%s.voxdata",voxdir,name);
495
496 vd = cst_mmap_file(path);
497
498 flite_feat_set_string(voice->features,"voxdir",path);
499 cst_free(path);
500
501 if (vd == NULL)
502 return -1;
503
504 x = (const char *)vd->mem;
505 if (!cst_streq("CMUFLITE",x))
506 { /* Not a Flite voice data file */
507 cst_munmap_file(vd);
508 return -1;
509 }
510
511 for (i=9; x[i] &&i<64; i++)
512 if (x[i] != ' ')
513 break;
514
515 if (!cst_streq(name,&x[i]))
516 { /* Not a voice data file for this voice */
517 cst_munmap_file(vd);
518 return -1;
519 }
520
521 /* This uses a hack to put in a void pointer to the cst_filemap */
522 flite_feat_set(voice->features,"voxdata",userdata_val(vd));
523 indexes = (int *)&x[64];
524
525 clunit_db = val_clunit_db(feat_val(voice->features,"clunit_db"));
526
527 clunit_db->sts->resoffs =
528 (const unsigned int *)&x[64+20];
529 clunit_db->sts->frames =
530 (const unsigned short *)&x[64+20+indexes[0]];
531 clunit_db->mcep->frames =
532 (const unsigned short *)&x[64+20+indexes[0]+indexes[1]];
533 clunit_db->sts->residuals =
534 (const unsigned char *)&x[64+20+indexes[0]+indexes[1]+indexes[2]];
535 clunit_db->sts->ressizes =
536 (const unsigned char *)&x[64+20+indexes[0]+indexes[1]+indexes[2]+indexes[3]];
537
538 return 0;
539 }
540
541 int flite_munmap_clunit_voxdata(cst_voice *voice)
542 {
543
544 cst_filemap *vd;
545 const cst_val *val_vd;
546 const cst_val *val_clunit_database;
547 cst_clunit_db *clunit_db;
548
549 val_vd = flite_get_param_val(voice->features,"voxdata",NULL);
550 val_clunit_database = flite_get_param_val(voice->features,"clunit_db",NULL);
551
552 if (val_vd && val_clunit_database)
553 {
554 clunit_db = val_clunit_db(val_clunit_database);
555 clunit_db->sts->resoffs = NULL;
556 clunit_db->sts->frames = NULL;
557 clunit_db->mcep->frames = NULL;
558 clunit_db->sts->residuals = NULL;
559 clunit_db->sts->ressizes = NULL;
560 vd = (cst_filemap *)val_userdata(val_vd);
561 cst_munmap_file(vd);
562 }
563
564 return 0;
565 }
566