Debian Codegraph @

3rdparty/flite_hts_engine/flite/src/synth/flite.c

raw symbols SCIP
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 /* ----------------------------------------------------------------- */
42 /* The English TTS System "Flite+hts_engine" */
43 /* developed by HTS Working Group */
44 /* http://hts-engine.sourceforge.net/ */
45 /* ----------------------------------------------------------------- */
46 /* */
47 /* Copyright (c) 2005-2013 Nagoya Institute of Technology */
48 /* Department of Computer Science */
49 /* */
50 /* 2005-2008 Tokyo Institute of Technology */
51 /* Interdisciplinary Graduate School of */
52 /* Science and Engineering */
53 /* */
54 /* All rights reserved. */
55 /* */
56 /* Redistribution and use in source and binary forms, with or */
57 /* without modification, are permitted provided that the following */
58 /* conditions are met: */
59 /* */
60 /* - Redistributions of source code must retain the above copyright */
61 /* notice, this list of conditions and the following disclaimer. */
62 /* - Redistributions in binary form must reproduce the above */
63 /* copyright notice, this list of conditions and the following */
64 /* disclaimer in the documentation and/or other materials provided */
65 /* with the distribution. */
66 /* - Neither the name of the HTS working group nor the names of its */
67 /* contributors may be used to endorse or promote products derived */
68 /* from this software without specific prior written permission. */
69 /* */
70 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
71 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
72 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
73 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
74 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
75 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
76 /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
77 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
78 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
79 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
80 /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
81 /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
82 /* POSSIBILITY OF SUCH DAMAGE. */
83 /* ----------------------------------------------------------------- */
84
85 #include "cst_tokenstream.h"
86 #include "flite.h"
87
88 /* This is a global, which isn't ideal, this may change */
89 /* It is set when flite_set_voice_list() is called which happens in */
90 /* flite_main() */
91 cst_val *flite_voice_list = 0;
92
93 #ifndef FLITE_PLUS_HTS_ENGINE
94 int flite_init()
95 {
96 cst_regex_init();
97
98 return 0;
99 }
100
101 cst_voice *flite_voice_select(const char *name)
102 {
103 const cst_val *v;
104 cst_voice *voice;
105
106 if (flite_voice_list == NULL)
107 return NULL; /* oops, not good */
108 if (name == NULL)
109 return val_voice(val_car(flite_voice_list));
110
111 for (v=flite_voice_list; v; v=val_cdr(v))
112 {
113 voice = val_voice(val_car(v));
114 if (cst_streq(name,voice->name))
115 return voice;
116 }
117
118 return flite_voice_select(NULL);
119
120 }
121
122 int flite_voice_add_lex_addenda(cst_voice *v, const cst_string *lexfile)
123 {
124 /* Add addenda in lexfile to current voice */
125 cst_lexicon *lex;
126 const cst_val *lex_addenda = NULL;
127 cst_val *new_addenda;
128
129 lex = val_lexicon(feat_val(v->features,"lexicon"));
130 if (feat_present(v->features, "lex_addenda"))
131 lex_addenda = feat_val(v->features, "lex_addenda");
132
133 new_addenda = cst_lex_load_addenda(lex,lexfile);
134 #if 0
135 printf("\naddenda: ");
136 val_print(stdout,new_addenda);
137 printf("\n");
138 #endif
139
140 new_addenda = val_append(new_addenda,(cst_val *)lex_addenda);
141 if (lex->lex_addenda)
142 delete_val(lex->lex_addenda);
143 lex->lex_addenda = new_addenda;
144
145 return 0;
146 }
147 #endif /* !FLITE_PLUS_HTS_ENGINE */
148
149 cst_utterance *flite_do_synth(cst_utterance *u,
150 cst_voice *voice,
151 cst_uttfunc synth)
152 {
153 utt_init(u, voice);
154 if ((*synth)(u) == NULL)
155 {
156 delete_utterance(u);
157 return NULL;
158 }
159 else
160 return u;
161 }
162
163 cst_utterance *flite_synth_text(const char *text, cst_voice *voice)
164 {
165 cst_utterance *u;
166
167 u = new_utterance();
168 utt_set_input_text(u,text);
169 return flite_do_synth(u, voice, utt_synth);
170 }
171
172 #ifndef FLITE_PLUS_HTS_ENGINE
173 cst_utterance *flite_synth_phones(const char *text, cst_voice *voice)
174 {
175 cst_utterance *u;
176
177 u = new_utterance();
178 utt_set_input_text(u,text);
179 return flite_do_synth(u, voice, utt_synth_phones);
180 }
181
182 cst_wave *flite_text_to_wave(const char *text, cst_voice *voice)
183 {
184 cst_utterance *u;
185 cst_wave *w;
186
187 if ((u = flite_synth_text(text,voice)) == NULL)
188 return NULL;
189
190 w = copy_wave(utt_wave(u));
191 delete_utterance(u);
192 return w;
193 }
194
195 float flite_file_to_speech(const char *filename,
196 cst_voice *voice,
197 const char *outtype)
198 {
199 cst_utterance *utt;
200 cst_tokenstream *ts;
201 const char *token;
202 cst_item *t;
203 cst_relation *tokrel;
204 float durs = 0;
205 int num_tokens;
206 cst_wave *w;
207 cst_breakfunc breakfunc = default_utt_break;
208 cst_uttfunc utt_user_callback = 0;
209 int fp;
210
211 if ((ts = ts_open(filename,
212 get_param_string(voice->features,"text_whitespace",NULL),
213 get_param_string(voice->features,"text_singlecharsymbols",NULL),
214 get_param_string(voice->features,"text_prepunctuation",NULL),
215 get_param_string(voice->features,"text_postpunctuation",NULL)))
216 == NULL)
217 {
218 cst_errmsg("failed to open file \"%s\" for reading\n",
219 filename);
220 return 1;
221 }
222 fp = get_param_int(voice->features,"file_start_position",0);
223 if (fp > 0)
224 ts_set_stream_pos(ts,fp);
225
226 if (feat_present(voice->features,"utt_break"))
227 breakfunc = val_breakfunc(feat_val(voice->features,"utt_break"));
228
229 if (feat_present(voice->features,"utt_user_callback"))
230 utt_user_callback = val_uttfunc(feat_val(voice->features,"utt_user_callback"));
231
232 /* If its a file to write to, create and save an empty wave file */
233 /* as we are going to incrementally append to it */
234 if (!cst_streq(outtype,"play") &&
235 !cst_streq(outtype,"none") &&
236 !cst_streq(outtype,"stream"))
237 {
238 w = new_wave();
239 cst_wave_resize(w,0,1);
240 cst_wave_set_sample_rate(w,16000);
241 cst_wave_save_riff(w,outtype); /* an empty wave */
242 delete_wave(w);
243 }
244
245 num_tokens = 0;
246 utt = new_utterance();
247 tokrel = utt_relation_create(utt, "Token");
248 while (!ts_eof(ts) || num_tokens > 0)
249 {
250 token = ts_get(ts);
251 if ((cst_strlen(token) == 0) ||
252 (num_tokens > 500) || /* need an upper bound */
253 (relation_head(tokrel) &&
254 breakfunc(ts,token,tokrel)))
255 {
256 /* An end of utt, so synthesize it */
257 if (utt_user_callback)
258 utt = (utt_user_callback)(utt);
259
260 if (utt)
261 {
262 utt = flite_do_synth(utt,voice,utt_synth_tokens);
263 durs += flite_process_output(utt,outtype,TRUE);
264 delete_utterance(utt); utt = NULL;
265 }
266 else
267 break;
268
269 if (ts_eof(ts)) break;
270
271 utt = new_utterance();
272 tokrel = utt_relation_create(utt, "Token");
273 num_tokens = 0;
274 }
275 num_tokens++;
276
277 t = relation_append(tokrel, NULL);
278 item_set_string(t,"name",token);
279 item_set_string(t,"whitespace",ts->whitespace);
280 item_set_string(t,"prepunctuation",ts->prepunctuation);
281 item_set_string(t,"punc",ts->postpunctuation);
282 /* Mark it at the beginning of the token */
283 item_set_int(t,"file_pos",
284 ts->file_pos-(1+ /* as we are already on the next char */
285 cst_strlen(token)+
286 cst_strlen(ts->prepunctuation)+
287 cst_strlen(ts->postpunctuation)));
288 item_set_int(t,"line_number",ts->line_number);
289 }
290
291 delete_utterance(utt);
292 ts_close(ts);
293 return durs;
294 }
295
296 float flite_text_to_speech(const char *text,
297 cst_voice *voice,
298 const char *outtype)
299 {
300 cst_utterance *u;
301 float dur;
302
303 u = flite_synth_text(text,voice);
304 dur = flite_process_output(u,outtype,FALSE);
305 delete_utterance(u);
306
307 return dur;
308 }
309
310 float flite_phones_to_speech(const char *text,
311 cst_voice *voice,
312 const char *outtype)
313 {
314 cst_utterance *u;
315 float dur;
316
317 u = flite_synth_phones(text,voice);
318 dur = flite_process_output(u,outtype,FALSE);
319 delete_utterance(u);
320
321 return dur;
322 }
323
324 float flite_process_output(cst_utterance *u, const char *outtype,
325 int append)
326 {
327 /* Play or save (append) output to output file */
328 cst_wave *w;
329 float dur;
330
331 if (!u) return 0.0;
332
333 w = utt_wave(u);
334
335 dur = (float)w->num_samples/(float)w->sample_rate;
336
337 if (cst_streq(outtype,"play"))
338 play_wave(w);
339 else if (cst_streq(outtype,"stream"))
340 {
341 /* It's already been played so do nothing */
342
343 }
344 else if (!cst_streq(outtype,"none"))
345 {
346 if (append)
347 cst_wave_append_riff(w,outtype);
348 else
349 cst_wave_save_riff(w,outtype);
350 }
351
352 return dur;
353 }
354 #endif /* !FLITE_PLUS_HTS_ENGINE */
355
356 int flite_get_param_int(const cst_features *f, const char *name,int def)
357 {
358 return get_param_int(f,name,def);
359 }
360 float flite_get_param_float(const cst_features *f, const char *name, float def)
361 {
362 return get_param_float(f,name,def);
363 }
364 const char *flite_get_param_string(const cst_features *f, const char *name, const char *def)
365 {
366 return get_param_string(f,name,def);
367 }
368 const cst_val *flite_get_param_val(const cst_features *f, const char *name, cst_val *def)
369 {
370 return get_param_val(f,name,def);
371 }
372
373 void flite_feat_set_int(cst_features *f, const char *name, int v)
374 {
375 feat_set_int(f,name,v);
376 }
377 void flite_feat_set_float(cst_features *f, const char *name, float v)
378 {
379 feat_set_float(f,name,v);
380 }
381 void flite_feat_set_string(cst_features *f, const char *name, const char *v)
382 {
383 feat_set_string(f,name,v);
384 }
385 void flite_feat_set(cst_features *f, const char *name,const cst_val *v)
386 {
387 feat_set(f,name,v);
388 }
389
390 int flite_feat_remove(cst_features *f, const char *name)
391 {
392 return feat_remove(f,name);
393 }
394
395 const char *flite_ffeature_string(const cst_item *item,const char *featpath)
396 {
397 return ffeature_string(item,featpath);
398 }
399
400 int flite_ffeature_int(const cst_item *item,const char *featpath)
401 {
402 return ffeature_int(item,featpath);
403 }
404 float flite_ffeature_float(const cst_item *item,const char *featpath)
405 {
406 return ffeature_float(item,featpath);
407 }
408 const cst_val *flite_ffeature(const cst_item *item,const char *featpath)
409 {
410 return ffeature(item,featpath);
411 }
412 cst_item* flite_path_to_item(const cst_item *item,const char *featpath)
413 {
414 return path_to_item(item,featpath);
415 }