Debian Codegraph @

src/synth/cst_ssml.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2001-2011 */
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: June 2008 */
35 /*************************************************************************/
36 /* */
37 /* SSML support for flite ( http://www.w3.org/TR/speech-synthesis/ ) */
38 /* */
39 /* We don't use a full XML parser here for space and availability */
40 /* reasons, but this is adequate for SSML */
41 /* This is based on some old SABLE support in flite that never got */
42 /* completed */
43 /* */
44 /* <ssml> </ssml> */
45 /* <voice ...> </voice> */
46 /* name or urls for voices */
47 /* <audio ...> </audio> */
48 /* <!-- ... --> */
49 /* <break .../> */
50 /* <prosody ...> </prosody> rate volume (no pitch yet) */
51 /* <emphasis ...> </emphasis> */
52 /* <sub alias="World Wide Web Consortium">W3C</sub> */
53 /* <phoneme ph="x x x"> </phoneme> */
54 /* */
55 /* <...> ignore all others */
56 /* */
57 /* Voice call backs (e.g. -pw and -ps) are not transfered when new */
58 /* voices are selected */
59 /* */
60 /*************************************************************************/
61
62 #include "flite.h"
63 #include "cst_tokenstream.h"
64
65 static const char * const ssml_singlecharsymbols_general = "<>&/\";";
66 static const char * const ssml_singlecharsymbols_inattr = "=>;/\"";
67
68 #define SSML_DEBUG 0
69
70 static const char *ts_get_quoted_remainder(cst_tokenstream *ts)
71 {
72 const char *q;
73
74 q = ts_get_quoted_token(ts,'"','\\');
75
76 return q;
77 }
78
79 static cst_features *ssml_get_attributes(cst_tokenstream *ts)
80 {
81 cst_features *a = new_features();
82 const char* name, *val;
83 const char *fnn,*vnn;
84 int i=0;
85
86 set_charclasses(ts,
87 ts->p_whitespacesymbols,
88 ssml_singlecharsymbols_inattr,
89 ts->p_prepunctuationsymbols,
90 ts->p_postpunctuationsymbols);
91
92 name = ts_get(ts);
93 while (!cst_streq(">",name))
94 {
95 /* I want names and values to be const */
96 if (i == 0)
97 {
98 fnn="_name0"; vnn="_val0";
99 }
100 else
101 {
102 fnn="_name1"; vnn="_val1";
103 }
104 if (cst_streq(name,"/"))
105 feat_set_string(a,"_type","startend");
106 else
107 {
108 feat_set_string(a,"_type","start");
109 feat_set_string(a,fnn,name);
110 if (cst_streq("=",ts_get(ts)))
111 {
112 val = ts_get_quoted_remainder(ts);
113 feat_set_string(a,vnn,val);
114 }
115 }
116 if (ts_eof(ts))
117 {
118 fprintf(stderr,"ssml: unexpected EOF\n");
119 delete_features(a);
120 return 0;
121 }
122 name = ts_get(ts);
123 i++;
124 }
125
126 set_charclasses(ts,
127 ts->p_whitespacesymbols,
128 ssml_singlecharsymbols_general,
129 ts->p_prepunctuationsymbols,
130 ts->p_postpunctuationsymbols);
131
132 return a;
133 }
134
135 static cst_utterance *ssml_apply_tag(const char *tag,
136 cst_features *attributes,
137 cst_utterance *u,
138 cst_features *word_feats,
139 cst_features *feats)
140 {
141 const char *wavefilename;
142 const char *vname;
143 cst_voice *nvoice;
144 cst_wave *wave;
145 cst_item *t;
146 cst_relation *r;
147 float break_size;
148
149 #if SSML_DEBUG
150 printf("SSML TAG %s\n",tag);
151 cst_feat_print(stdout,attributes);
152 printf("...\n");
153 #endif
154
155 if (cst_streq("AUDIO",tag))
156 {
157 if ((cst_streq("start",feat_string(attributes,"_type"))) ||
158 (cst_streq("startend",feat_string(attributes,"_type"))))
159 {
160 wavefilename = feat_string(attributes,"_val0");
161 wave = new_wave();
162 if (cst_wave_load_riff(wave,wavefilename) == CST_OK_FORMAT)
163 {
164 if (cst_streq("start",feat_string(attributes,"_type")))
165 {
166 feat_set_string(word_feats,"ssml_comment","1");
167 }
168 feat_set(word_feats,"ssml_play_audio",wave_val(wave));
169 }
170 else
171 delete_wave(wave);
172 return NULL; /* Cause eou */
173 }
174 else if (cst_streq("end",feat_string(attributes,"_type")))
175 {
176 feat_remove(word_feats,"ssml_comment");
177 return NULL; /* Cause eou */
178 }
179 }
180 else if (cst_streq("BREAK",tag))
181 {
182 if (u &&
183 ((r = utt_relation(u,"Token")) != NULL) &&
184 ((t = relation_tail(r)) != NULL))
185 {
186 item_set_string(t,"break","1");
187 /* cst_feat_print(stdout,attributes); */
188 if (cst_streq("size",get_param_string(attributes,"_name0","")))
189 {
190 break_size=feat_float(attributes,"_val0");
191 item_set_float(t,"break_size",break_size);
192 }
193 }
194 }
195 else if (cst_streq("PROSODY",tag))
196 {
197 if (cst_streq("start",feat_string(attributes,"_type")))
198 {
199 /* Note SSML doesn't do stretch it does reciprical of stretch */
200 if (cst_streq("rate",get_param_string(attributes,"_name0","")))
201 feat_set_float(word_feats,"local_duration_stretch",
202 1.0/feat_float(attributes,"_val0"));
203 if (cst_streq("rate",get_param_string(attributes,"_name1","")))
204 feat_set_float(word_feats,"local_duration_stretch",
205 1.0/feat_float(attributes,"_val1"));
206 if (cst_streq("volume",get_param_string(attributes,"_name0","")))
207 feat_set_float(word_feats,"local_gain",
208 feat_float(attributes,"_val0")/100.0);
209 if (cst_streq("volume",get_param_string(attributes,"_name1","")))
210 feat_set_float(word_feats,"local_gain",
211 feat_float(attributes,"_val1")/100.0);
212 }
213 else if (cst_streq("end",feat_string(attributes,"_type")))
214 {
215 feat_remove(word_feats,"local_duration_stretch");
216 feat_remove(word_feats,"local_gain");
217 }
218
219 }
220 else if (cst_streq("PHONEME",tag))
221 {
222 if (cst_streq("start",feat_string(attributes,"_type")))
223 {
224 if (cst_streq("ph",get_param_string(attributes,"_name0","")))
225 {
226 const char *ph;
227 ph = feat_string(attributes,"_val0");
228 feat_set_string(word_feats,"phones",ph);
229 }
230 }
231 else if (cst_streq("end",feat_string(attributes,"_type")))
232 {
233 feat_remove(word_feats,"phones");
234 }
235
236 }
237 else if (cst_streq("SUB",tag))
238 {
239 if (cst_streq("start",feat_string(attributes,"_type")))
240 {
241 if (cst_streq("alias",get_param_string(attributes,"_name0","")))
242 {
243 const char *alias;
244 alias = feat_string(attributes,"_val0");
245 feat_set_string(word_feats,"ssml_alias",alias);
246 }
247 }
248 else if (cst_streq("end",feat_string(attributes,"_type")))
249 {
250 feat_remove(word_feats,"ssml_alias");
251 }
252
253 }
254 else if (cst_streq("VOICE",tag))
255 {
256 if (cst_streq("start",feat_string(attributes,"_type")))
257 {
258 vname = get_param_string(attributes,"_val0","");
259 nvoice = flite_voice_select(vname);
260 feat_set(feats,"current_voice",userdata_val(nvoice));
261 return NULL; /* cause an utterance break */
262 }
263 else if (cst_streq("end",feat_string(attributes,"_type")))
264 {
265 /* Hmm we should really have a stack of these */
266 nvoice =
267 (cst_voice *)val_userdata(feat_val(feats,"default_voice"));
268 feat_set(feats,"current_voice",userdata_val(nvoice));
269 return NULL;
270 }
271 }
272
273 /* do stuff */
274 /* flag what to do mark or end */
275 /*
276 ph set attributes silence all contained tokens
277 break add to previous token a break marker
278 audio silence all following tokens (utt break)
279 insert waveform
280
281 */
282
283 return u;
284 }
285
286 static float flite_ssml_to_speech_ts(cst_tokenstream *ts,
287 cst_voice *voice,
288 const char *outtype)
289 {
290 /* This is a very ugly function, that might be better written with gotos */
291 /* This just doesn't seem to be properly functions -- perhaps a proper */
292 /* consumer/producer threaded model might be better here -- but its */
293 /* not clear. There is so much have-to-be-done-now vs note-for-later */
294 /* code, that the code is far from clear, and probably not right */
295 cst_features *ssml_feats, *ssml_word_feats;
296 cst_features *attributes;
297 const char *token = "";
298 char *tag=NULL;
299 cst_utterance *utt;
300 cst_relation *tokrel;
301 int num_tokens;
302 cst_breakfunc breakfunc = default_utt_break;
303 cst_uttfunc utt_user_callback = 0;
304 float durs = 0.0;
305 cst_item *t;
306 cst_voice *current_voice;
307 int ssml_eou = 0;
308 const cst_wave *wave;
309 cst_wave *w;
310
311 ssml_feats = new_features();
312 feat_set(ssml_feats,"current_voice",userdata_val(voice));
313 feat_set(ssml_feats,"default_voice",userdata_val(voice));
314 ssml_word_feats = new_features();
315 set_charclasses(ts,
316 " \t\n\r",
317 ssml_singlecharsymbols_general,
318 get_param_string(voice->features,"text_prepunctuation",""),
319 get_param_string(voice->features,"text_postpunctuation","")
320 );
321
322 if (feat_present(voice->features,"utt_break"))
323 breakfunc = val_breakfunc(feat_val(voice->features,"utt_break"));
324
325 if (feat_present(voice->features,"utt_user_callback"))
326 utt_user_callback = val_uttfunc(feat_val(voice->features,"utt_user_callback"));
327
328 /* If its a file to write to, create and save an empty wave file */
329 /* as we are going to incrementally append to it */
330 if (!cst_streq(outtype,"play") &&
331 !cst_streq(outtype,"none") &&
332 !cst_streq(outtype,"stream"))
333 {
334 w = new_wave();
335 cst_wave_resize(w,0,1);
336 cst_wave_set_sample_rate(w,16000);
337 cst_wave_save_riff(w,outtype); /* an empty wave */
338 delete_wave(w);
339 }
340
341 num_tokens = 0;
342 utt = new_utterance();
343
344 tokrel = utt_relation_create(utt, "Token");
345 while (!ts_eof(ts) || num_tokens > 0)
346 {
347 current_voice =
348 (cst_voice *)val_userdata(feat_val(ssml_feats,"current_voice"));
349 /* printf("awb_debug prewhile %d %s\n",ssml_eou,token); */
350 if (ssml_eou == 0)
351 token = ts_get(ts);
352 else
353 {
354 if (!cst_streq("<",token))
355 token = ts_get(ts);
356 ssml_eou = 0;
357 }
358 while ((cst_streq("<",token)) && (ssml_eou == 0))
359 { /* A tag -- look ahead and process it to find out how to advance */
360 tag = cst_upcase(ts_get(ts));
361 /* printf("awb_debug tag is %s\n",tag); */
362 if (cst_streq("/",tag)) /* an end tag */
363 {
364 cst_free(tag); tag=NULL;
365 tag = cst_upcase(ts_get(ts));
366 attributes = ssml_get_attributes(ts);
367 feat_set_string(attributes,"_type","end");
368 }
369 else
370 attributes = ssml_get_attributes(ts);
371 token = ts_get(ts); /* skip ">" */
372 if (ssml_apply_tag(tag,attributes,utt,ssml_word_feats,ssml_feats))
373 ssml_eou = 0;
374 else
375 ssml_eou = 1;
376
377 delete_features(attributes);
378 cst_free(tag); tag=NULL;
379 }
380
381 if ((cst_strlen(token) == 0) ||
382 (num_tokens > 500) || /* need an upper bound */
383 (ssml_eou == 1) || /* ssml tag was utterance break */
384 (relation_head(tokrel) &&
385 breakfunc(ts,token,tokrel)))
386 {
387 /* An end of utt, so synthesize it */
388 if (utt_user_callback)
389 utt = (utt_user_callback)(utt);
390
391 if (utt)
392 {
393 utt = flite_do_synth(utt,current_voice,utt_synth_tokens);
394 if (feat_present(utt->features,"Interrupted"))
395 {
396 delete_utterance(utt); utt = NULL;
397 break;
398 }
399 durs += flite_process_output(utt,outtype,TRUE);
400 delete_utterance(utt); utt = NULL;
401 }
402 else
403 break;
404
405 if (ts_eof(ts)) break;
406
407 utt = new_utterance();
408 tokrel = utt_relation_create(utt, "Token");
409 num_tokens = 0;
410 }
411
412 if (feat_present(ssml_word_feats,"ssml_play_audio"))
413 {
414 wave = val_wave(feat_val(ssml_word_feats,"ssml_play_audio"));
415 /* Should create an utterances with the waveform in it */
416 /* Have to stream it if there is streaming */
417 if (utt) delete_utterance(utt);
418 utt = utt_synth_wave(copy_wave(wave),current_voice);
419 if (utt_user_callback)
420 utt = (utt_user_callback)(utt);
421 durs += flite_process_output(utt,outtype,TRUE);
422 delete_utterance(utt); utt = NULL;
423
424 utt = new_utterance();
425 tokrel = utt_relation_create(utt, "Token");
426 num_tokens = 0;
427
428 feat_remove(ssml_word_feats,"ssml_play_audio");
429 }
430 else if (!cst_streq("<",token))
431 { /* wasn't an ssml tag */
432 num_tokens++;
433
434 t = relation_append(tokrel, NULL);
435 item_set_string(t,"name",token);
436 item_set_string(t,"whitespace",ts->whitespace);
437 item_set_string(t,"prepunctuation",ts->prepunctuation);
438 item_set_string(t,"punc",ts->postpunctuation);
439 /* Mark it at the beginning of the token */
440 item_set_int(t,"file_pos",
441 ts->file_pos-(1+ /* as we are already on the next char */
442 cst_strlen(token)+
443 cst_strlen(ts->prepunctuation)+
444 cst_strlen(ts->postpunctuation)));
445 item_set_int(t,"line_number",ts->line_number);
446 feat_copy_into(ssml_word_feats,item_feats(t));
447 }
448 }
449
450 delete_utterance(utt);
451 delete_features(ssml_feats);
452 delete_features(ssml_word_feats);
453 return durs;
454 }
455
456 float flite_ssml_file_to_speech(const char *filename,
457 cst_voice *voice,
458 const char *outtype)
459 {
460 cst_tokenstream *ts;
461 int fp;
462 cst_wave *w;
463 float d;
464
465 if ((ts = ts_open(filename,
466 get_param_string(voice->features,"text_whitespace",NULL),
467 get_param_string(voice->features,"text_singlecharsymbols",NULL),
468 get_param_string(voice->features,"text_prepunctuation",NULL),
469 get_param_string(voice->features,"text_postpunctuation",NULL)))
470 == NULL)
471 {
472 cst_errmsg("failed to open file \"%s\" for ssml reading\n",
473 filename);
474 return 1;
475 }
476 fp = get_param_int(voice->features,"file_start_position",0);
477 if (fp > 0)
478 ts_set_stream_pos(ts,fp);
479
480 /* If its a file to write to, create and save an empty wave file */
481 /* as we are going to incrementally append to it */
482 if (!cst_streq(outtype,"play") &&
483 !cst_streq(outtype,"none") &&
484 !cst_streq(outtype,"stream"))
485 {
486 w = new_wave();
487 cst_wave_resize(w,0,1);
488 cst_wave_set_sample_rate(w,16000);
489 cst_wave_save_riff(w,outtype); /* an empty wave */
490 delete_wave(w);
491 }
492
493 d = flite_ssml_to_speech_ts(ts,voice,outtype);
494
495 ts_close(ts);
496
497 return d;
498
499 }
500
501 float flite_ssml_text_to_speech(const char *text,
502 cst_voice *voice,
503 const char *outtype)
504 {
505 cst_tokenstream *ts;
506 int fp;
507 cst_wave *w;
508 float d;
509
510 if ((ts = ts_open_string(text,
511 get_param_string(voice->features,"text_whitespace",NULL),
512 get_param_string(voice->features,"text_singlecharsymbols",NULL),
513 get_param_string(voice->features,"text_prepunctuation",NULL),
514 get_param_string(voice->features,"text_postpunctuation",NULL)))
515 == NULL)
516 {
517 return 1;
518 }
519 fp = get_param_int(voice->features,"file_start_position",0);
520 if (fp > 0)
521 ts_set_stream_pos(ts,fp);
522
523 /* If its a file to write to, create and save an empty wave file */
524 /* as we are going to incrementally append to it */
525 if (!cst_streq(outtype,"play") &&
526 !cst_streq(outtype,"none") &&
527 !cst_streq(outtype,"stream"))
528 {
529 w = new_wave();
530 cst_wave_resize(w,0,1);
531 cst_wave_set_sample_rate(w,16000);
532 cst_wave_save_riff(w,outtype); /* an empty wave */
533 delete_wave(w);
534 }
535
536 d = flite_ssml_to_speech_ts(ts,voice,outtype);
537
538 ts_close(ts);
539
540 return d;
541
542 }
543
544