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_LABEL_C_START extern "C" {
50 #define HTS_LABEL_C_END }
52 #define HTS_LABEL_C_START
53 #define HTS_LABEL_C_END
54 #endif /* __CPLUSPLUS */
58 #include <stdlib.h> /* for atof() */
59 #include <ctype.h> /* for isgraph(),isdigit() */
61 /* hts_engine libraries */
62 #include "HTS_hidden.h"
64 static HTS_Boolean isdigit_string(char *str)
68 if (sscanf(str, "%d", &i) == 1)
74 /* HTS_Label_initialize: initialize label */
75 void HTS_Label_initialize(HTS_Label * label)
79 label->frame_flag = FALSE;
80 label->speech_speed = 1.0;
83 /* HTS_Label_check_time: check label */
84 static void HTS_Label_check_time(HTS_Label * label)
86 HTS_LabelString *lstring = label->head;
87 HTS_LabelString *next = NULL;
95 if (lstring->end < 0.0 && next->start >= 0.0)
96 lstring->end = next->start;
97 else if (lstring->end >= 0.0 && next->start < 0.0)
98 next->start = lstring->end;
99 if (lstring->start < 0.0)
100 lstring->start = -1.0;
101 if (lstring->end < 0.0)
107 /* HTS_Label_load_from_fn: load label from file name */
108 void HTS_Label_load_from_fn(HTS_Label * label, int sampling_rate, int fperiod,
111 FILE *fp = HTS_get_fp(fn, "r");
112 HTS_Label_load_from_fp(label, sampling_rate, fperiod, fp);
116 /* HTS_Label_load_from_fp: load label from file pointer */
117 void HTS_Label_load_from_fp(HTS_Label * label, int sampling_rate, int fperiod,
120 char buff[HTS_MAXBUFLEN];
121 HTS_LabelString *lstring = NULL;
123 const double rate = (double) sampling_rate / ((double) fperiod * 1e+7);
125 if (label->head || label->size != 0)
126 HTS_error(1, "HTS_Label_load_from_fp: label is not initialized.\n");
127 /* parse label file */
128 while (HTS_get_token(fp, buff)) {
129 if (!isgraph((int) buff[0]))
135 (HTS_LabelString *) HTS_calloc(1, sizeof(HTS_LabelString));
136 lstring = lstring->next;
137 } else { /* first time */
138 lstring = (HTS_LabelString *) HTS_calloc(1, sizeof(HTS_LabelString));
139 label->head = lstring;
141 if (isdigit_string(buff)) { /* has frame infomation */
143 HTS_get_token(fp, buff);
145 HTS_get_token(fp, buff);
146 lstring->start = rate * start;
147 lstring->end = rate * end;
149 lstring->start = -1.0;
152 lstring->next = NULL;
153 lstring->name = HTS_strdup(buff);
155 HTS_Label_check_time(label);
158 /* HTS_Label_load_from_string: load label from string */
159 void HTS_Label_load_from_string(HTS_Label * label, int sampling_rate,
160 int fperiod, char *data)
162 char buff[HTS_MAXBUFLEN];
163 HTS_LabelString *lstring = NULL;
164 int data_index = 0; /* data index */
166 const double rate = (double) sampling_rate / ((double) fperiod * 1e+7);
168 if (label->head || label->size != 0)
169 HTS_error(1, "HTS_Label_load_from_fp: label list is not initialized.\n");
171 while (HTS_get_token_from_string(data, &data_index, buff)) {
172 if (!isgraph((int) buff[0]))
178 (HTS_LabelString *) HTS_calloc(1, sizeof(HTS_LabelString));
179 lstring = lstring->next;
180 } else { /* first time */
181 lstring = (HTS_LabelString *) HTS_calloc(1, sizeof(HTS_LabelString));
182 label->head = lstring;
184 if (isdigit_string(buff)) { /* has frame infomation */
186 HTS_get_token_from_string(data, &data_index, buff);
188 HTS_get_token_from_string(data, &data_index, buff);
189 lstring->start = rate * start;
190 lstring->end = rate * end;
192 lstring->start = -1.0;
195 lstring->next = NULL;
196 lstring->name = HTS_strdup(buff);
198 HTS_Label_check_time(label);
201 /* HTS_Label_load_from_string_list: load label from string list */
202 void HTS_Label_load_from_string_list(HTS_Label * label, int sampling_rate,
203 int fperiod, char **data, int size)
205 char buff[HTS_MAXBUFLEN];
206 HTS_LabelString *lstring = NULL;
210 const double rate = (double) sampling_rate / ((double) fperiod * 1e+7);
212 if (label->head || label->size != 0)
213 HTS_error(1, "HTS_Label_load_from_fp: label list is not initialized.\n");
215 for (i = 0; i < size; i++) {
216 if (!isgraph((int) data[i][0]))
222 (HTS_LabelString *) HTS_calloc(1, sizeof(HTS_LabelString));
223 lstring = lstring->next;
224 } else { /* first time */
225 lstring = (HTS_LabelString *) HTS_calloc(1, sizeof(HTS_LabelString));
226 label->head = lstring;
229 if (isdigit_string(data[i])) { /* has frame infomation */
230 HTS_get_token_from_string(data[i], &data_index, buff);
232 HTS_get_token_from_string(data[i], &data_index, buff);
234 HTS_get_token_from_string(data[i], &data_index, buff);
235 lstring->name = HTS_strdup(&buff[data_index]);
236 lstring->start = rate * start;
237 lstring->end = rate * end;
239 lstring->start = -1.0;
241 lstring->name = HTS_strdup(data[i]);
243 lstring->next = NULL;
245 HTS_Label_check_time(label);
248 /* HTS_Label_set_frame_specified_flag: set frame specified flag */
249 void HTS_Label_set_frame_specified_flag(HTS_Label * label, HTS_Boolean i)
251 label->frame_flag = i;
254 /* HTS_Label_set_speech_speed: set speech speed rate */
255 void HTS_Label_set_speech_speed(HTS_Label * label, double f)
257 if (f > 0.0 && f <= 10.0)
258 label->speech_speed = f;
261 /* HTS_Label_get_size: get number of label string */
262 int HTS_Label_get_size(HTS_Label * label)
267 /* HTS_Label_get_string: get label string */
268 char *HTS_Label_get_string(HTS_Label * label, int string_index)
270 HTS_LabelString *lstring = label->head;
272 while (string_index-- && lstring)
273 lstring = lstring->next;
276 return lstring->name;
279 /* HTS_Label_get_frame_specified_flag: get frame specified flag */
280 HTS_Boolean HTS_Label_get_frame_specified_flag(HTS_Label * label)
282 return label->frame_flag;
285 /* HTS_Label_get_start_frame: get start frame */
286 double HTS_Label_get_start_frame(HTS_Label * label, int string_index)
288 HTS_LabelString *lstring = label->head;
290 while (string_index-- && lstring)
291 lstring = lstring->next;
294 return lstring->start;
297 /* HTS_Label_get_end_frame: get end frame */
298 double HTS_Label_get_end_frame(HTS_Label * label, int string_index)
300 HTS_LabelString *lstring = label->head;
302 while (string_index-- && lstring)
303 lstring = lstring->next;
309 /* HTS_Label_get_speech_speed: get speech speed rate */
310 double HTS_Label_get_speech_speed(HTS_Label * label)
312 return label->speech_speed;
315 /* HTS_Label_clear: free label */
316 void HTS_Label_clear(HTS_Label * label)
318 HTS_LabelString *lstring, *next_lstring;
320 for (lstring = label->head; lstring; lstring = next_lstring) {
321 next_lstring = lstring->next;
322 HTS_free(lstring->name);
325 HTS_Label_initialize(label);
330 #endif /* !HTS_LABEL_C */