| 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 |
#include "cst_tokenstream.h" |
| 86 |
#include "flite.h" |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 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; |
| 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 |
|
| 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 |
| 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 |
|
| 233 |
|
| 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); |
| 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) || |
| 253 |
(relation_head(tokrel) && |
| 254 |
breakfunc(ts,token,tokrel))) |
| 255 |
{ |
| 256 |
|
| 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 |
|
| 283 |
item_set_int(t,"file_pos", |
| 284 |
ts->file_pos-(1+ |
| 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 |
|
| 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 |
|
| 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 |
| 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 |
} |