1 /* ----------------------------------------------------------------- */
2 /* The HMM-Based Speech Synthesis System (HTS) */
3 /* hts_engine API developed by HTS Working Group */
4 /* http://hts-engine.sourceforge.net/ */
5 /* ----------------------------------------------------------------- */
7 /* Copyright (c) 2001-2010 Nagoya Institute of Technology */
8 /* Department of Computer Science */
10 /* 2001-2008 Tokyo Institute of Technology */
11 /* Interdisciplinary Graduate School of */
12 /* Science and Engineering */
14 /* All rights reserved. */
16 /* Redistribution and use in source and binary forms, with or */
17 /* without modification, are permitted provided that the following */
18 /* conditions are met: */
20 /* - Redistributions of source code must retain the above copyright */
21 /* notice, this list of conditions and the following disclaimer. */
22 /* - Redistributions in binary form must reproduce the above */
23 /* copyright notice, this list of conditions and the following */
24 /* disclaimer in the documentation and/or other materials provided */
25 /* with the distribution. */
26 /* - Neither the name of the HTS working group nor the names of its */
27 /* contributors may be used to endorse or promote products derived */
28 /* from this software without specific prior written permission. */
30 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND */
31 /* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, */
32 /* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
33 /* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
34 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS */
35 /* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, */
36 /* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
37 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, */
38 /* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
39 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, */
40 /* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY */
41 /* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE */
42 /* POSSIBILITY OF SUCH DAMAGE. */
43 /* ----------------------------------------------------------------- */
49 #define HTS_ENGINE_C_START extern "C" {
50 #define HTS_ENGINE_C_END }
52 #define HTS_ENGINE_C_START
53 #define HTS_ENGINE_C_END
54 #endif /* __CPLUSPLUS */
58 #include <string.h> /* for strcpy() */
60 /* hts_engine libraries */
61 #include "HTS_hidden.h"
63 /* HTS_Engine_initialize: initialize engine */
64 void HTS_Engine_initialize(HTS_Engine * engine, int nstream)
68 /* default value for control parameter */
69 engine->global.stage = 0;
70 engine->global.use_log_gain = FALSE;
71 engine->global.sampling_rate = 16000;
72 engine->global.fperiod = 80;
73 engine->global.alpha = 0.42;
74 engine->global.beta = 0.0;
75 engine->global.audio_buff_size = 0;
76 engine->global.msd_threshold =
77 (double *) HTS_calloc(nstream, sizeof(double));
78 for (i = 0; i < nstream; i++)
79 engine->global.msd_threshold[i] = 0.5;
81 /* interpolation weight */
82 engine->global.parameter_iw =
83 (double **) HTS_calloc(nstream, sizeof(double *));
84 engine->global.gv_iw = (double **) HTS_calloc(nstream, sizeof(double *));
85 engine->global.duration_iw = NULL;
86 for (i = 0; i < nstream; i++)
87 engine->global.parameter_iw[i] = NULL;
88 for (i = 0; i < nstream; i++)
89 engine->global.gv_iw[i] = NULL;
92 engine->global.gv_weight = (double *) HTS_calloc(nstream, sizeof(double));
93 for (i = 0; i < nstream; i++)
94 engine->global.gv_weight[i] = 1.0;
97 engine->global.stop = FALSE;
99 engine->global.volume = 1.0;
101 /* initialize model set */
102 HTS_ModelSet_initialize(&engine->ms, nstream);
103 /* initialize label list */
104 HTS_Label_initialize(&engine->label);
105 /* initialize state sequence set */
106 HTS_SStreamSet_initialize(&engine->sss);
107 /* initialize pstream set */
108 HTS_PStreamSet_initialize(&engine->pss);
109 /* initialize gstream set */
110 HTS_GStreamSet_initialize(&engine->gss);
113 /* HTS_Engine_load_duratin_from_fn: load duration pdfs, trees and number of state from file names */
114 void HTS_Engine_load_duration_from_fn(HTS_Engine * engine, char **pdf_fn,
115 char **tree_fn, int interpolation_size)
118 FILE **pdf_fp, **tree_fp;
120 pdf_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
121 tree_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
122 for (i = 0; i < interpolation_size; i++) {
123 pdf_fp[i] = HTS_get_fp(pdf_fn[i], "rb");
124 tree_fp[i] = HTS_get_fp(tree_fn[i], "r");
126 HTS_Engine_load_duration_from_fp(engine, pdf_fp, tree_fp,
128 for (i = 0; i < interpolation_size; i++) {
136 /* HTS_Engine_load_duration_from_fp: load duration pdfs, trees and number of state from file pointers */
137 void HTS_Engine_load_duration_from_fp(HTS_Engine * engine, FILE ** pdf_fp,
138 FILE ** tree_fp, int interpolation_size)
142 HTS_ModelSet_load_duration(&engine->ms, pdf_fp, tree_fp, interpolation_size);
143 engine->global.duration_iw =
144 (double *) HTS_calloc(interpolation_size, sizeof(double));
145 for (i = 0; i < interpolation_size; i++)
146 engine->global.duration_iw[i] = 1.0 / interpolation_size;
149 /* HTS_Engine_load_parameter_from_fn: load parameter pdfs, trees and windows from file names */
150 void HTS_Engine_load_parameter_from_fn(HTS_Engine * engine, char **pdf_fn,
151 char **tree_fn, char **win_fn,
152 int stream_index, HTS_Boolean msd_flag,
153 int window_size, int interpolation_size)
156 FILE **pdf_fp, **tree_fp, **win_fp;
158 pdf_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
159 tree_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
160 win_fp = (FILE **) HTS_calloc(window_size, sizeof(FILE *));
161 for (i = 0; i < interpolation_size; i++) {
162 pdf_fp[i] = HTS_get_fp(pdf_fn[i], "rb");
163 tree_fp[i] = HTS_get_fp(tree_fn[i], "r");
165 for (i = 0; i < window_size; i++)
166 win_fp[i] = HTS_get_fp(win_fn[i], "r");
167 HTS_Engine_load_parameter_from_fp(engine, pdf_fp, tree_fp, win_fp,
168 stream_index, msd_flag,
169 window_size, interpolation_size);
170 for (i = 0; i < interpolation_size; i++) {
174 for (i = 0; i < window_size; i++)
181 /* HTS_Engine_load_parameter_from_fp: load parameter pdfs, trees and windows from file pointers */
182 void HTS_Engine_load_parameter_from_fp(HTS_Engine * engine, FILE ** pdf_fp,
183 FILE ** tree_fp, FILE ** win_fp,
184 int stream_index, HTS_Boolean msd_flag,
185 int window_size, int interpolation_size)
189 HTS_ModelSet_load_parameter(&engine->ms, pdf_fp, tree_fp, win_fp,
190 stream_index, msd_flag,
191 window_size, interpolation_size);
192 engine->global.parameter_iw[stream_index] =
193 (double *) HTS_calloc(interpolation_size, sizeof(double));
194 for (i = 0; i < interpolation_size; i++)
195 engine->global.parameter_iw[stream_index][i] = 1.0 / interpolation_size;
198 /* HTS_Engine_load_gv_from_fn: load GV pdfs and trees from file names */
199 void HTS_Engine_load_gv_from_fn(HTS_Engine * engine, char **pdf_fn,
200 char **tree_fn, int stream_index,
201 int interpolation_size)
204 FILE **pdf_fp, **tree_fp;
206 pdf_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
208 tree_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
211 for (i = 0; i < interpolation_size; i++) {
212 pdf_fp[i] = HTS_get_fp(pdf_fn[i], "rb");
215 tree_fp[i] = HTS_get_fp(tree_fn[i], "r");
220 HTS_Engine_load_gv_from_fp(engine, pdf_fp, tree_fp, stream_index,
222 for (i = 0; i < interpolation_size; i++) {
224 if (tree_fp && tree_fp[i])
232 /* HTS_Engine_load_gv_from_fp: load GV pdfs and trees from file pointers */
233 void HTS_Engine_load_gv_from_fp(HTS_Engine * engine, FILE ** pdf_fp,
234 FILE ** tree_fp, int stream_index,
235 int interpolation_size)
239 HTS_ModelSet_load_gv(&engine->ms, pdf_fp, tree_fp, stream_index,
241 engine->global.gv_iw[stream_index] =
242 (double *) HTS_calloc(interpolation_size, sizeof(double));
243 for (i = 0; i < interpolation_size; i++)
244 engine->global.gv_iw[stream_index][i] = 1.0 / interpolation_size;
247 /* HTS_Engine_load_gv_switch_from_fn: load GV switch from file name */
248 void HTS_Engine_load_gv_switch_from_fn(HTS_Engine * engine, char *fn)
250 FILE *fp = HTS_get_fp(fn, "r");
252 HTS_Engine_load_gv_switch_from_fp(engine, fp);
256 /* HTS_Engine_load_gv_switch_from_fp: load GV switch from file pointer */
257 void HTS_Engine_load_gv_switch_from_fp(HTS_Engine * engine, FILE * fp)
259 HTS_ModelSet_load_gv_switch(&engine->ms, fp);
262 /* HTS_Engine_set_sampling_rate: set sampling rate */
263 void HTS_Engine_set_sampling_rate(HTS_Engine * engine, int i)
269 engine->global.sampling_rate = i;
272 /* HTS_Engine_get_sampling_rate: get sampling rate */
273 int HTS_Engine_get_sampling_rate(HTS_Engine * engine)
275 return engine->global.sampling_rate;
278 /* HTS_Engine_set_fperiod: set frame shift */
279 void HTS_Engine_set_fperiod(HTS_Engine * engine, int i)
285 engine->global.fperiod = i;
288 /* HTS_Engine_get_fperiod: get frame shift */
289 int HTS_Engine_get_fperiod(HTS_Engine * engine)
291 return engine->global.fperiod;
294 /* HTS_Engine_set_alpha: set alpha */
295 void HTS_Engine_set_alpha(HTS_Engine * engine, double f)
301 engine->global.alpha = f;
304 /* HTS_Engine_set_gamma: set gamma (Gamma = -1/i: if i=0 then Gamma=0) */
305 void HTS_Engine_set_gamma(HTS_Engine * engine, int i)
309 engine->global.stage = i;
312 /* HTS_Engine_set_log_gain: set log gain flag (for LSP) */
313 void HTS_Engine_set_log_gain(HTS_Engine * engine, HTS_Boolean i)
315 engine->global.use_log_gain = i;
318 /* HTS_Engine_set_beta: set beta */
319 void HTS_Engine_set_beta(HTS_Engine * engine, double f)
325 engine->global.beta = f;
328 /* HTS_Engine_set_audio_buff_size: set audio buffer size */
329 void HTS_Engine_set_audio_buff_size(HTS_Engine * engine, int i)
335 engine->global.audio_buff_size = i;
338 /* HTS_Engine_get_audio_buff_size: get audio buffer size */
339 int HTS_Engine_get_audio_buff_size(HTS_Engine * engine)
341 return engine->global.audio_buff_size;
344 /* HTS_Egnine_set_msd_threshold: set MSD threshold */
345 void HTS_Engine_set_msd_threshold(HTS_Engine * engine, int stream_index,
352 engine->global.msd_threshold[stream_index] = f;
355 /* HTS_Engine_set_duration_interpolation_weight: set interpolation weight for duration */
356 void HTS_Engine_set_duration_interpolation_weight(HTS_Engine * engine,
357 int interpolation_index,
360 engine->global.duration_iw[interpolation_index] = f;
363 /* HTS_Engine_set_parameter_interpolation_weight: set interpolation weight for parameter */
364 void HTS_Engine_set_parameter_interpolation_weight(HTS_Engine * engine,
366 int interpolation_index,
369 engine->global.parameter_iw[stream_index][interpolation_index] = f;
372 /* HTS_Engine_set_gv_interpolation_weight: set interpolation weight for GV */
373 void HTS_Engine_set_gv_interpolation_weight(HTS_Engine * engine,
375 int interpolation_index, double f)
377 engine->global.gv_iw[stream_index][interpolation_index] = f;
380 /* HTS_Engine_set_gv_weight: set GV weight */
381 void HTS_Engine_set_gv_weight(HTS_Engine * engine, int stream_index, double f)
387 engine->global.gv_weight[stream_index] = f;
390 /* HTS_Engine_set_stop_flag: set stop flag */
391 void HTS_Engine_set_stop_flag(HTS_Engine * engine, HTS_Boolean b)
393 engine->global.stop = b;
396 /* HTS_Engine_set_volume: set volume */
397 void HTS_Engine_set_volume(HTS_Engine * engine, double f)
401 engine->global.volume = f;
404 /* HTS_Engine_get_total_state: get total number of state */
405 int HTS_Engine_get_total_state(HTS_Engine * engine)
407 return HTS_SStreamSet_get_total_state(&engine->sss);
410 /* HTS_Engine_set_state_mean: set mean value of state */
411 void HTS_Engine_set_state_mean(HTS_Engine * engine, int stream_index,
412 int state_index, int vector_index, double f)
414 HTS_SStreamSet_set_mean(&engine->sss, stream_index, state_index,
418 /* HTS_Engine_get_state_mean: get mean value of state */
419 double HTS_Engine_get_state_mean(HTS_Engine * engine, int stream_index,
420 int state_index, int vector_index)
422 return HTS_SStreamSet_get_mean(&engine->sss, stream_index, state_index,
426 /* HTS_Engine_get_state_duration: get state duration */
427 int HTS_Engine_get_state_duration(HTS_Engine * engine, int state_index)
429 return HTS_SStreamSet_get_duration(&engine->sss, state_index);
432 /* HTS_Engine_get_nstate: get number of state */
433 int HTS_Engine_get_nstate(HTS_Engine * engine)
435 return HTS_ModelSet_get_nstate(&engine->ms);
438 /* HTS_Engine_load_label_from_fn: load label from file name */
439 void HTS_Engine_load_label_from_fn(HTS_Engine * engine, char *fn)
441 HTS_Label_load_from_fn(&engine->label, engine->global.sampling_rate,
442 engine->global.fperiod, fn);
445 /* HTS_Engine_load_label_from_fp: load label from file pointer */
446 void HTS_Engine_load_label_from_fp(HTS_Engine * engine, FILE * fp)
448 HTS_Label_load_from_fp(&engine->label, engine->global.sampling_rate,
449 engine->global.fperiod, fp);
452 /* HTS_Engine_load_label_from_string: load label from string */
453 void HTS_Engine_load_label_from_string(HTS_Engine * engine, char *data)
455 HTS_Label_load_from_string(&engine->label, engine->global.sampling_rate,
456 engine->global.fperiod, data);
459 /* HTS_Engine_load_label_from_string_list: load label from string list */
460 void HTS_Engine_load_label_from_string_list(HTS_Engine * engine, char **data,
463 HTS_Label_load_from_string_list(&engine->label, engine->global.sampling_rate,
464 engine->global.fperiod, data, size);
467 /* HTS_Engine_create_sstream: parse label and determine state duration */
468 void HTS_Engine_create_sstream(HTS_Engine * engine)
470 HTS_SStreamSet_create(&engine->sss, &engine->ms, &engine->label,
471 engine->global.duration_iw,
472 engine->global.parameter_iw, engine->global.gv_iw);
475 /* HTS_Engine_create_pstream: generate speech parameter vector sequence */
476 void HTS_Engine_create_pstream(HTS_Engine * engine)
478 HTS_PStreamSet_create(&engine->pss, &engine->sss,
479 engine->global.msd_threshold,
480 engine->global.gv_weight);
483 /* HTS_Engine_create_gstream: synthesis speech */
484 void HTS_Engine_create_gstream(HTS_Engine * engine)
486 HTS_GStreamSet_create(&engine->gss, &engine->pss, engine->global.stage,
487 engine->global.use_log_gain,
488 engine->global.sampling_rate, engine->global.fperiod,
489 engine->global.alpha, engine->global.beta,
490 &engine->global.stop, engine->global.volume,
491 engine->global.audio_buff_size);
494 /* HTS_Engine_save_information: output trace information */
495 void HTS_Engine_save_information(HTS_Engine * engine, FILE * fp)
497 int i, j, k, l, m, n;
499 HTS_Global *global = &engine->global;
500 HTS_ModelSet *ms = &engine->ms;
501 HTS_Label *label = &engine->label;
502 HTS_SStreamSet *sss = &engine->sss;
503 HTS_PStreamSet *pss = &engine->pss;
505 /* global parameter */
506 fprintf(fp, "[Global parameter]\n");
507 fprintf(fp, "Sampring frequency -> %8d(Hz)\n",
508 global->sampling_rate);
509 fprintf(fp, "Frame period -> %8d(point)\n",
511 fprintf(fp, " %8.5f(msec)\n",
512 1e+3 * global->fperiod / global->sampling_rate);
513 fprintf(fp, "All-pass constant -> %8.5f\n",
514 (float) global->alpha);
515 fprintf(fp, "Gamma -> %8.5f\n",
516 (float) (global->stage == 0 ? 0.0 : -1.0 / global->stage));
517 if (global->stage != 0)
518 fprintf(fp, "Log gain flag -> %s\n",
519 global->use_log_gain ? "TRUE" : "FALSE");
520 fprintf(fp, "Postfiltering coefficient -> %8.5f\n",
521 (float) global->beta);
522 fprintf(fp, "Audio buffer size -> %8d(sample)\n",
523 global->audio_buff_size);
526 /* duration parameter */
527 fprintf(fp, "[Duration parameter]\n");
528 fprintf(fp, "Number of states -> %8d\n",
529 HTS_ModelSet_get_nstate(ms));
530 fprintf(fp, " Interpolation -> %8d\n",
531 HTS_ModelSet_get_duration_interpolation_size(ms));
532 /* check interpolation */
533 for (i = 0, temp = 0.0;
534 i < HTS_ModelSet_get_duration_interpolation_size(ms); i++)
535 temp += global->duration_iw[i];
536 for (i = 0; i < HTS_ModelSet_get_duration_interpolation_size(ms); i++)
537 if (global->duration_iw[i] != 0.0)
538 global->duration_iw[i] /= temp;
539 for (i = 0; i < HTS_ModelSet_get_duration_interpolation_size(ms); i++)
541 " Interpolation weight[%2d] -> %8.0f(%%)\n", i,
542 (float) (100 * global->duration_iw[i]));
545 fprintf(fp, "[Stream parameter]\n");
546 for (i = 0; i < HTS_ModelSet_get_nstream(ms); i++) {
547 /* stream parameter */
548 fprintf(fp, "Stream[%2d] vector length -> %8d\n", i,
549 HTS_ModelSet_get_vector_length(ms, i));
550 fprintf(fp, " Dynamic window size -> %8d\n",
551 HTS_ModelSet_get_window_size(ms, i));
553 fprintf(fp, " Interpolation -> %8d\n",
554 HTS_ModelSet_get_parameter_interpolation_size(ms, i));
555 for (j = 0, temp = 0.0;
556 j < HTS_ModelSet_get_parameter_interpolation_size(ms, i); j++)
557 temp += global->parameter_iw[i][j];
558 for (j = 0; j < HTS_ModelSet_get_parameter_interpolation_size(ms, i); j++)
559 if (global->parameter_iw[i][j] != 0.0)
560 global->parameter_iw[i][j] /= temp;
561 for (j = 0; j < HTS_ModelSet_get_parameter_interpolation_size(ms, i); j++)
563 " Interpolation weight[%2d] -> %8.0f(%%)\n", j,
564 (float) (100 * global->parameter_iw[i][j]));
566 if (HTS_ModelSet_is_msd(ms, i)) { /* for MSD */
567 fprintf(fp, " MSD flag -> TRUE\n");
568 fprintf(fp, " MSD threshold -> %8.5f\n",
569 global->msd_threshold[i]);
570 } else { /* for non MSD */
571 fprintf(fp, " MSD flag -> FALSE\n");
574 if (HTS_ModelSet_use_gv(ms, i)) {
575 fprintf(fp, " GV flag -> TRUE\n");
576 if (HTS_ModelSet_have_gv_switch(ms)) {
577 if (HTS_ModelSet_have_gv_tree(ms, i)) {
579 " GV type -> CDGV\n");
584 " GV type -> SWITCH\n");
586 if (HTS_ModelSet_have_gv_tree(ms, i))
588 " GV type -> CDGV\n");
591 " GV type -> NORMAL\n");
593 fprintf(fp, " GV weight -> %8.0f(%%)\n",
594 (float) (100 * global->gv_weight[i]));
595 fprintf(fp, " GV interpolation size -> %8d\n",
596 HTS_ModelSet_get_gv_interpolation_size(ms, i));
598 for (j = 0, temp = 0.0;
599 j < HTS_ModelSet_get_gv_interpolation_size(ms, i); j++)
600 temp += global->gv_iw[i][j];
601 for (j = 0; j < HTS_ModelSet_get_gv_interpolation_size(ms, i); j++)
602 if (global->gv_iw[i][j] != 0.0)
603 global->gv_iw[i][j] /= temp;
604 for (j = 0; j < HTS_ModelSet_get_gv_interpolation_size(ms, i); j++)
606 " GV interpolation weight[%2d] -> %8.0f(%%)\n", j,
607 (float) (100 * global->gv_iw[i][j]));
609 fprintf(fp, " GV flag -> FALSE\n");
614 /* generated sequence */
615 fprintf(fp, "[Generated sequence]\n");
616 fprintf(fp, "Number of HMMs -> %8d\n",
617 HTS_Label_get_size(label));
618 fprintf(fp, "Number of stats -> %8d\n",
619 HTS_Label_get_size(label) * HTS_ModelSet_get_nstate(ms));
620 fprintf(fp, "Length of this speech -> %8.3f(sec)\n",
621 (float) ((double) HTS_PStreamSet_get_total_frame(pss) *
622 global->fperiod / global->sampling_rate));
623 fprintf(fp, " -> %8.3d(frames)\n",
624 HTS_PStreamSet_get_total_frame(pss) * global->fperiod);
626 for (i = 0; i < HTS_Label_get_size(label); i++) {
627 fprintf(fp, "HMM[%2d]\n", i);
628 fprintf(fp, " Name -> %s\n",
629 HTS_Label_get_string(label, i));
630 fprintf(fp, " Duration\n");
631 for (j = 0; j < HTS_ModelSet_get_duration_interpolation_size(ms); j++) {
632 fprintf(fp, " Interpolation[%2d]\n", j);
633 HTS_ModelSet_get_duration_index(ms, HTS_Label_get_string(label, i), &k,
635 fprintf(fp, " Tree index -> %8d\n", k);
636 fprintf(fp, " PDF index -> %8d\n", l);
638 for (j = 0; j < HTS_ModelSet_get_nstate(ms); j++) {
639 fprintf(fp, " State[%2d]\n", j + 2);
640 fprintf(fp, " Length -> %8d(frames)\n",
641 HTS_SStreamSet_get_duration(sss,
642 i * HTS_ModelSet_get_nstate(ms) +
644 for (k = 0; k < HTS_ModelSet_get_nstream(ms); k++) {
645 fprintf(fp, " Stream[%2d]\n", k);
646 if (HTS_ModelSet_is_msd(ms, k)) {
647 if (HTS_SStreamSet_get_msd
649 i * HTS_ModelSet_get_nstate(ms) + j) >
650 global->msd_threshold[k])
652 " MSD flag -> TRUE\n");
655 " MSD flag -> FALSE\n");
658 l < HTS_ModelSet_get_parameter_interpolation_size(ms, k);
660 fprintf(fp, " Interpolation[%2d]\n", l);
661 HTS_ModelSet_get_parameter_index(ms,
662 HTS_Label_get_string(label, i),
663 &m, &n, k, j + 2, l);
664 fprintf(fp, " Tree index -> %8d\n",
666 fprintf(fp, " PDF index -> %8d\n",
674 /* HTS_Engine_save_label: output label with time */
675 void HTS_Engine_save_label(HTS_Engine * engine, FILE * fp)
678 int frame, state, duration;
680 HTS_Label *label = &engine->label;
681 HTS_SStreamSet *sss = &engine->sss;
682 const int nstate = HTS_ModelSet_get_nstate(&engine->ms);
684 engine->global.fperiod * 1e+7 / engine->global.sampling_rate;
686 for (i = 0, state = 0, frame = 0; i < HTS_Label_get_size(label); i++) {
687 for (j = 0, duration = 0; j < nstate; j++)
688 duration += HTS_SStreamSet_get_duration(sss, state++);
689 /* in HTK & HTS format */
690 fprintf(fp, "%d %d %s\n", (int) (frame * rate),
691 (int) ((frame + duration) * rate),
692 HTS_Label_get_string(label, i));
698 /* HTS_Engine_save_generated_parameter: output generated parameter */
699 void HTS_Engine_save_generated_parameter(HTS_Engine * engine, FILE * fp,
704 HTS_GStreamSet *gss = &engine->gss;
706 for (i = 0; i < HTS_GStreamSet_get_total_frame(gss); i++)
707 for (j = 0; j < HTS_GStreamSet_get_static_length(gss, stream_index); j++) {
708 temp = (float) HTS_GStreamSet_get_parameter(gss, stream_index, i, j);
709 fwrite(&temp, sizeof(float), 1, fp);
712 #endif /* !HTS_EMBEDDED */
714 /* HTS_Engine_save_generated_speech: output generated speech */
715 void HTS_Engine_save_generated_speech(HTS_Engine * engine, FILE * fp)
719 HTS_GStreamSet *gss = &engine->gss;
721 for (i = 0; i < HTS_GStreamSet_get_total_nsample(gss); i++) {
722 temp = HTS_GStreamSet_get_speech(gss, i);
723 fwrite(&temp, sizeof(short), 1, fp);
727 /* HTS_Engine_save_riff: output RIFF format file */
728 void HTS_Engine_save_riff(HTS_Engine * engine, FILE * fp)
733 HTS_GStreamSet *gss = &engine->gss;
734 char data_01_04[] = { 'R', 'I', 'F', 'F' };
735 int data_05_08 = HTS_GStreamSet_get_total_nsample(gss) * sizeof(short) + 36;
736 char data_09_12[] = { 'W', 'A', 'V', 'E' };
737 char data_13_16[] = { 'f', 'm', 't', ' ' };
739 short data_21_22 = 1; /* PCM */
740 short data_23_24 = 1; /* monoral */
741 int data_25_28 = engine->global.sampling_rate;
742 int data_29_32 = engine->global.sampling_rate * sizeof(short);
743 short data_33_34 = sizeof(short);
744 short data_35_36 = (short) (sizeof(short) * 8);
745 char data_37_40[] = { 'd', 'a', 't', 'a' };
746 int data_41_44 = HTS_GStreamSet_get_total_nsample(gss) * sizeof(short);
749 HTS_fwrite_little_endian(data_01_04, sizeof(char), 4, fp);
750 HTS_fwrite_little_endian(&data_05_08, sizeof(int), 1, fp);
751 HTS_fwrite_little_endian(data_09_12, sizeof(char), 4, fp);
752 HTS_fwrite_little_endian(data_13_16, sizeof(char), 4, fp);
753 HTS_fwrite_little_endian(&data_17_20, sizeof(int), 1, fp);
754 HTS_fwrite_little_endian(&data_21_22, sizeof(short), 1, fp);
755 HTS_fwrite_little_endian(&data_23_24, sizeof(short), 1, fp);
756 HTS_fwrite_little_endian(&data_25_28, sizeof(int), 1, fp);
757 HTS_fwrite_little_endian(&data_29_32, sizeof(int), 1, fp);
758 HTS_fwrite_little_endian(&data_33_34, sizeof(short), 1, fp);
759 HTS_fwrite_little_endian(&data_35_36, sizeof(short), 1, fp);
760 HTS_fwrite_little_endian(data_37_40, sizeof(char), 4, fp);
761 HTS_fwrite_little_endian(&data_41_44, sizeof(int), 1, fp);
763 for (i = 0; i < HTS_GStreamSet_get_total_nsample(gss); i++) {
764 temp = HTS_GStreamSet_get_speech(gss, i);
765 HTS_fwrite_little_endian(&temp, sizeof(short), 1, fp);
769 /* HTS_Engine_refresh: free model per one time synthesis */
770 void HTS_Engine_refresh(HTS_Engine * engine)
772 /* free generated parameter stream set */
773 HTS_GStreamSet_clear(&engine->gss);
774 /* free parameter stream set */
775 HTS_PStreamSet_clear(&engine->pss);
776 /* free state stream set */
777 HTS_SStreamSet_clear(&engine->sss);
778 /* free label list */
779 HTS_Label_clear(&engine->label);
781 engine->global.stop = FALSE;
784 /* HTS_Engine_clear: free engine */
785 void HTS_Engine_clear(HTS_Engine * engine)
789 HTS_free(engine->global.msd_threshold);
790 HTS_free(engine->global.duration_iw);
791 for (i = 0; i < HTS_ModelSet_get_nstream(&engine->ms); i++) {
792 HTS_free(engine->global.parameter_iw[i]);
793 if (engine->global.gv_iw[i])
794 HTS_free(engine->global.gv_iw[i]);
796 HTS_free(engine->global.parameter_iw);
797 HTS_free(engine->global.gv_iw);
798 HTS_free(engine->global.gv_weight);
800 HTS_ModelSet_clear(&engine->ms);
803 /* HTS_get_copyright: write copyright to string */
804 void HTS_get_copyright(char *str)
806 int i, nCopyright = HTS_NCOPYRIGHT;
807 char url[] = HTS_URL, version[] = HTS_VERSION;
808 char *copyright[] = { HTS_COPYRIGHT };
810 sprintf(str, "\nThe HMM-based speech synthesis system (HTS)\n");
811 sprintf(str, "%shts_engine API version %s (%s)\n", str, version, url);
812 for (i = 0; i < nCopyright; i++) {
814 sprintf(str, "%sCopyright (C) %s\n", str, copyright[i]);
816 sprintf(str, "%s %s\n", str, copyright[i]);
818 sprintf(str, "%sAll rights reserved.\n", str);
823 /* HTS_show_copyright: write copyright to file pointer */
824 void HTS_show_copyright(FILE * fp)
826 char buf[HTS_MAXBUFLEN];
828 HTS_get_copyright(buf);
829 fprintf(fp, "%s", buf);
836 #endif /* !HTS_ENGINE_C */