Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / hts_engine / HTS_engine.c
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 /* ----------------------------------------------------------------- */
6 /*                                                                   */
7 /*  Copyright (c) 2001-2010  Nagoya Institute of Technology          */
8 /*                           Department of Computer Science          */
9 /*                                                                   */
10 /*                2001-2008  Tokyo Institute of Technology           */
11 /*                           Interdisciplinary Graduate School of    */
12 /*                           Science and Engineering                 */
13 /*                                                                   */
14 /* All rights reserved.                                              */
15 /*                                                                   */
16 /* Redistribution and use in source and binary forms, with or        */
17 /* without modification, are permitted provided that the following   */
18 /* conditions are met:                                               */
19 /*                                                                   */
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.   */
29 /*                                                                   */
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 /* ----------------------------------------------------------------- */
44
45 #ifndef HTS_ENGINE_C
46 #define HTS_ENGINE_C
47
48 #ifdef __cplusplus
49 #define HTS_ENGINE_C_START extern "C" {
50 #define HTS_ENGINE_C_END   }
51 #else
52 #define HTS_ENGINE_C_START
53 #define HTS_ENGINE_C_END
54 #endif                          /* __CPLUSPLUS */
55
56 HTS_ENGINE_C_START;
57
58 #include <string.h>             /* for strcpy() */
59
60 /* hts_engine libraries */
61 #include "HTS_hidden.h"
62
63 /* HTS_Engine_initialize: initialize engine */
64 void HTS_Engine_initialize(HTS_Engine * engine, int nstream)
65 {
66    int i;
67
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;
80
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;
90
91    /* GV weight */
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;
95
96    /* stop flag */
97    engine->global.stop = FALSE;
98    /* volume */
99    engine->global.volume = 1.0;
100
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);
111 }
112
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)
116 {
117    int i;
118    FILE **pdf_fp, **tree_fp;
119
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");
125    }
126    HTS_Engine_load_duration_from_fp(engine, pdf_fp, tree_fp,
127                                     interpolation_size);
128    for (i = 0; i < interpolation_size; i++) {
129       fclose(pdf_fp[i]);
130       fclose(tree_fp[i]);
131    }
132    HTS_free(pdf_fp);
133    HTS_free(tree_fp);
134 }
135
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)
139 {
140    int i;
141
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;
147 }
148
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)
154 {
155    int i;
156    FILE **pdf_fp, **tree_fp, **win_fp;
157
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");
164    }
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++) {
171       fclose(pdf_fp[i]);
172       fclose(tree_fp[i]);
173    }
174    for (i = 0; i < window_size; i++)
175       fclose(win_fp[i]);
176    HTS_free(pdf_fp);
177    HTS_free(tree_fp);
178    HTS_free(win_fp);
179 }
180
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)
186 {
187    int i;
188
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;
196 }
197
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)
202 {
203    int i;
204    FILE **pdf_fp, **tree_fp;
205
206    pdf_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
207    if (tree_fn)
208       tree_fp = (FILE **) HTS_calloc(interpolation_size, sizeof(FILE *));
209    else
210       tree_fp = NULL;
211    for (i = 0; i < interpolation_size; i++) {
212       pdf_fp[i] = HTS_get_fp(pdf_fn[i], "rb");
213       if (tree_fn) {
214          if (tree_fn[i])
215             tree_fp[i] = HTS_get_fp(tree_fn[i], "r");
216          else
217             tree_fp[i] = NULL;
218       }
219    }
220    HTS_Engine_load_gv_from_fp(engine, pdf_fp, tree_fp, stream_index,
221                               interpolation_size);
222    for (i = 0; i < interpolation_size; i++) {
223       fclose(pdf_fp[i]);
224       if (tree_fp && tree_fp[i])
225          fclose(tree_fp[i]);
226    }
227    HTS_free(pdf_fp);
228    if (tree_fp)
229       HTS_free(tree_fp);
230 }
231
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)
236 {
237    int i;
238
239    HTS_ModelSet_load_gv(&engine->ms, pdf_fp, tree_fp, stream_index,
240                         interpolation_size);
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;
245 }
246
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)
249 {
250    FILE *fp = HTS_get_fp(fn, "r");
251
252    HTS_Engine_load_gv_switch_from_fp(engine, fp);
253    fclose(fp);
254 }
255
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)
258 {
259    HTS_ModelSet_load_gv_switch(&engine->ms, fp);
260 }
261
262 /* HTS_Engine_set_sampling_rate: set sampling rate */
263 void HTS_Engine_set_sampling_rate(HTS_Engine * engine, int i)
264 {
265    if (i < 1)
266       i = 1;
267    if (i > 48000)
268       i = 48000;
269    engine->global.sampling_rate = i;
270 }
271
272 /* HTS_Engine_get_sampling_rate: get sampling rate */
273 int HTS_Engine_get_sampling_rate(HTS_Engine * engine)
274 {
275    return engine->global.sampling_rate;
276 }
277
278 /* HTS_Engine_set_fperiod: set frame shift */
279 void HTS_Engine_set_fperiod(HTS_Engine * engine, int i)
280 {
281    if (i < 1)
282       i = 1;
283    if (i > 48000)
284       i = 48000;
285    engine->global.fperiod = i;
286 }
287
288 /* HTS_Engine_get_fperiod: get frame shift */
289 int HTS_Engine_get_fperiod(HTS_Engine * engine)
290 {
291    return engine->global.fperiod;
292 }
293
294 /* HTS_Engine_set_alpha: set alpha */
295 void HTS_Engine_set_alpha(HTS_Engine * engine, double f)
296 {
297    if (f < 0.0)
298       f = 0.0;
299    if (f > 1.0)
300       f = 1.0;
301    engine->global.alpha = f;
302 }
303
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)
306 {
307    if (i < 0)
308       i = 0;
309    engine->global.stage = i;
310 }
311
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)
314 {
315    engine->global.use_log_gain = i;
316 }
317
318 /* HTS_Engine_set_beta: set beta */
319 void HTS_Engine_set_beta(HTS_Engine * engine, double f)
320 {
321    if (f < -0.8)
322       f = -0.8;
323    if (f > 0.8)
324       f = 0.8;
325    engine->global.beta = f;
326 }
327
328 /* HTS_Engine_set_audio_buff_size: set audio buffer size */
329 void HTS_Engine_set_audio_buff_size(HTS_Engine * engine, int i)
330 {
331    if (i < 0)
332       i = 0;
333    if (i > 48000)
334       i = 48000;
335    engine->global.audio_buff_size = i;
336 }
337
338 /* HTS_Engine_get_audio_buff_size: get audio buffer size */
339 int HTS_Engine_get_audio_buff_size(HTS_Engine * engine)
340 {
341    return engine->global.audio_buff_size;
342 }
343
344 /* HTS_Egnine_set_msd_threshold: set MSD threshold */
345 void HTS_Engine_set_msd_threshold(HTS_Engine * engine, int stream_index,
346                                   double f)
347 {
348    if (f < 0.0)
349       f = 0.0;
350    if (f > 1.0)
351       f = 1.0;
352    engine->global.msd_threshold[stream_index] = f;
353 }
354
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,
358                                                   double f)
359 {
360    engine->global.duration_iw[interpolation_index] = f;
361 }
362
363 /* HTS_Engine_set_parameter_interpolation_weight: set interpolation weight for parameter */
364 void HTS_Engine_set_parameter_interpolation_weight(HTS_Engine * engine,
365                                                    int stream_index,
366                                                    int interpolation_index,
367                                                    double f)
368 {
369    engine->global.parameter_iw[stream_index][interpolation_index] = f;
370 }
371
372 /* HTS_Engine_set_gv_interpolation_weight: set interpolation weight for GV */
373 void HTS_Engine_set_gv_interpolation_weight(HTS_Engine * engine,
374                                             int stream_index,
375                                             int interpolation_index, double f)
376 {
377    engine->global.gv_iw[stream_index][interpolation_index] = f;
378 }
379
380 /* HTS_Engine_set_gv_weight: set GV weight */
381 void HTS_Engine_set_gv_weight(HTS_Engine * engine, int stream_index, double f)
382 {
383    if (f < 0.0)
384       f = 0.0;
385    if (f > 2.0)
386       f = 2.0;
387    engine->global.gv_weight[stream_index] = f;
388 }
389
390 /* HTS_Engine_set_stop_flag: set stop flag */
391 void HTS_Engine_set_stop_flag(HTS_Engine * engine, HTS_Boolean b)
392 {
393    engine->global.stop = b;
394 }
395
396 /* HTS_Engine_set_volume: set volume */
397 void HTS_Engine_set_volume(HTS_Engine * engine, double f)
398 {
399    if (f < 0.0)
400       f = 0.0;
401    engine->global.volume = f;
402 }
403
404 /* HTS_Engine_get_total_state: get total number of state */
405 int HTS_Engine_get_total_state(HTS_Engine * engine)
406 {
407    return HTS_SStreamSet_get_total_state(&engine->sss);
408 }
409
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)
413 {
414    HTS_SStreamSet_set_mean(&engine->sss, stream_index, state_index,
415                            vector_index, f);
416 }
417
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)
421 {
422    return HTS_SStreamSet_get_mean(&engine->sss, stream_index, state_index,
423                                   vector_index);
424 }
425
426 /* HTS_Engine_get_state_duration: get state duration */
427 int HTS_Engine_get_state_duration(HTS_Engine * engine, int state_index)
428 {
429    return HTS_SStreamSet_get_duration(&engine->sss, state_index);
430 }
431
432 /* HTS_Engine_get_nstate: get number of state */
433 int HTS_Engine_get_nstate(HTS_Engine * engine)
434 {
435    return HTS_ModelSet_get_nstate(&engine->ms);
436 }
437
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)
440 {
441    HTS_Label_load_from_fn(&engine->label, engine->global.sampling_rate,
442                           engine->global.fperiod, fn);
443 }
444
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)
447 {
448    HTS_Label_load_from_fp(&engine->label, engine->global.sampling_rate,
449                           engine->global.fperiod, fp);
450 }
451
452 /* HTS_Engine_load_label_from_string: load label from string */
453 void HTS_Engine_load_label_from_string(HTS_Engine * engine, char *data)
454 {
455    HTS_Label_load_from_string(&engine->label, engine->global.sampling_rate,
456                               engine->global.fperiod, data);
457 }
458
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,
461                                             int size)
462 {
463    HTS_Label_load_from_string_list(&engine->label, engine->global.sampling_rate,
464                                    engine->global.fperiod, data, size);
465 }
466
467 /* HTS_Engine_create_sstream: parse label and determine state duration */
468 void HTS_Engine_create_sstream(HTS_Engine * engine)
469 {
470    HTS_SStreamSet_create(&engine->sss, &engine->ms, &engine->label,
471                          engine->global.duration_iw,
472                          engine->global.parameter_iw, engine->global.gv_iw);
473 }
474
475 /* HTS_Engine_create_pstream: generate speech parameter vector sequence */
476 void HTS_Engine_create_pstream(HTS_Engine * engine)
477 {
478    HTS_PStreamSet_create(&engine->pss, &engine->sss,
479                          engine->global.msd_threshold,
480                          engine->global.gv_weight);
481 }
482
483 /* HTS_Engine_create_gstream: synthesis speech */
484 void HTS_Engine_create_gstream(HTS_Engine * engine)
485 {
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);
492 }
493
494 /* HTS_Engine_save_information: output trace information */
495 void HTS_Engine_save_information(HTS_Engine * engine, FILE * fp)
496 {
497    int i, j, k, l, m, n;
498    double temp;
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;
504
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",
510            global->fperiod);
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);
524    fprintf(fp, "\n");
525
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++)
540       fprintf(fp,
541               "         Interpolation weight[%2d]      -> %8.0f(%%)\n", i,
542               (float) (100 * global->duration_iw[i]));
543    fprintf(fp, "\n");
544
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));
552       /* interpolation */
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++)
562          fprintf(fp,
563                  "           Interpolation weight[%2d]    -> %8.0f(%%)\n", j,
564                  (float) (100 * global->parameter_iw[i][j]));
565       /* MSD */
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");
572       }
573       /* GV */
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)) {
578                fprintf(fp,
579                        "           GV type                     ->     CDGV\n");
580                fprintf(fp,
581                        "                                       ->  +SWITCH\n");
582             } else
583                fprintf(fp,
584                        "           GV type                     ->   SWITCH\n");
585          } else {
586             if (HTS_ModelSet_have_gv_tree(ms, i))
587                fprintf(fp,
588                        "           GV type                     ->     CDGV\n");
589             else
590                fprintf(fp,
591                        "           GV type                     ->   NORMAL\n");
592          }
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));
597          /* interpolation */
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++)
605             fprintf(fp,
606                     "           GV interpolation weight[%2d] -> %8.0f(%%)\n", j,
607                     (float) (100 * global->gv_iw[i][j]));
608       } else {
609          fprintf(fp, "           GV flag                     ->    FALSE\n");
610       }
611    }
612    fprintf(fp, "\n");
613
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);
625
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,
634                                          &l, j);
635          fprintf(fp, "      Tree index                       -> %8d\n", k);
636          fprintf(fp, "      PDF index                        -> %8d\n", l);
637       }
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) +
643                                              j));
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
648                    (sss, k,
649                     i * HTS_ModelSet_get_nstate(ms) + j) >
650                    global->msd_threshold[k])
651                   fprintf(fp,
652                           "      MSD flag                         ->     TRUE\n");
653                else
654                   fprintf(fp,
655                           "      MSD flag                         ->    FALSE\n");
656             }
657             for (l = 0;
658                  l < HTS_ModelSet_get_parameter_interpolation_size(ms, k);
659                  l++) {
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",
665                        m);
666                fprintf(fp, "        PDF index                      -> %8d\n",
667                        n);
668             }
669          }
670       }
671    }
672 }
673
674 /* HTS_Engine_save_label: output label with time */
675 void HTS_Engine_save_label(HTS_Engine * engine, FILE * fp)
676 {
677    int i, j;
678    int frame, state, duration;
679
680    HTS_Label *label = &engine->label;
681    HTS_SStreamSet *sss = &engine->sss;
682    const int nstate = HTS_ModelSet_get_nstate(&engine->ms);
683    const double rate =
684        engine->global.fperiod * 1e+7 / engine->global.sampling_rate;
685
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));
693       frame += duration;
694    }
695 }
696
697 #ifndef HTS_EMBEDDED
698 /* HTS_Engine_save_generated_parameter: output generated parameter */
699 void HTS_Engine_save_generated_parameter(HTS_Engine * engine, FILE * fp,
700                                          int stream_index)
701 {
702    int i, j;
703    float temp;
704    HTS_GStreamSet *gss = &engine->gss;
705
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);
710       }
711 }
712 #endif                          /* !HTS_EMBEDDED */
713
714 /* HTS_Engine_save_generated_speech: output generated speech */
715 void HTS_Engine_save_generated_speech(HTS_Engine * engine, FILE * fp)
716 {
717    int i;
718    short temp;
719    HTS_GStreamSet *gss = &engine->gss;
720
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);
724    }
725 }
726
727 /* HTS_Engine_save_riff: output RIFF format file */
728 void HTS_Engine_save_riff(HTS_Engine * engine, FILE * fp)
729 {
730    int i;
731    short temp;
732
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', ' ' };
738    int data_17_20 = 16;
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);
747
748    /* write header */
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);
762    /* write data */
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);
766    }
767 }
768
769 /* HTS_Engine_refresh: free model per one time synthesis */
770 void HTS_Engine_refresh(HTS_Engine * engine)
771 {
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);
780    /* stop flag */
781    engine->global.stop = FALSE;
782 }
783
784 /* HTS_Engine_clear: free engine */
785 void HTS_Engine_clear(HTS_Engine * engine)
786 {
787    int i;
788
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]);
795    }
796    HTS_free(engine->global.parameter_iw);
797    HTS_free(engine->global.gv_iw);
798    HTS_free(engine->global.gv_weight);
799
800    HTS_ModelSet_clear(&engine->ms);
801 }
802
803 /* HTS_get_copyright: write copyright to string */
804 void HTS_get_copyright(char *str)
805 {
806    int i, nCopyright = HTS_NCOPYRIGHT;
807    char url[] = HTS_URL, version[] = HTS_VERSION;
808    char *copyright[] = { HTS_COPYRIGHT };
809
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++) {
813       if (i == 0)
814          sprintf(str, "%sCopyright (C) %s\n", str, copyright[i]);
815       else
816          sprintf(str, "%s              %s\n", str, copyright[i]);
817    }
818    sprintf(str, "%sAll rights reserved.\n", str);
819
820    return;
821 }
822
823 /* HTS_show_copyright: write copyright to file pointer */
824 void HTS_show_copyright(FILE * fp)
825 {
826    char buf[HTS_MAXBUFLEN];
827
828    HTS_get_copyright(buf);
829    fprintf(fp, "%s", buf);
830
831    return;
832 }
833
834 HTS_ENGINE_C_END;
835
836 #endif                          /* !HTS_ENGINE_C */