Initial import to Gerrit.
[profile/ivi/festival.git] / src / modules / hts_engine / HTS_label.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_LABEL_C
46 #define HTS_LABEL_C
47
48 #ifdef __cplusplus
49 #define HTS_LABEL_C_START extern "C" {
50 #define HTS_LABEL_C_END   }
51 #else
52 #define HTS_LABEL_C_START
53 #define HTS_LABEL_C_END
54 #endif                          /* __CPLUSPLUS */
55
56 HTS_LABEL_C_START;
57
58 #include <stdlib.h>             /* for atof() */
59 #include <ctype.h>              /* for isgraph(),isdigit() */
60
61 /* hts_engine libraries */
62 #include "HTS_hidden.h"
63
64 static HTS_Boolean isdigit_string(char *str)
65 {
66    int i;
67
68    if (sscanf(str, "%d", &i) == 1)
69       return TRUE;
70    else
71       return FALSE;
72 }
73
74 /* HTS_Label_initialize: initialize label */
75 void HTS_Label_initialize(HTS_Label * label)
76 {
77    label->head = NULL;
78    label->size = 0;
79    label->frame_flag = FALSE;
80    label->speech_speed = 1.0;
81 }
82
83 /* HTS_Label_check_time: check label */
84 static void HTS_Label_check_time(HTS_Label * label)
85 {
86    HTS_LabelString *lstring = label->head;
87    HTS_LabelString *next = NULL;
88
89    if (lstring)
90       lstring->start = 0.0;
91    while (lstring) {
92       next = lstring->next;
93       if (!next)
94          break;
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)
102          lstring->end = -1.0;
103       lstring = next;
104    }
105 }
106
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,
109                             char *fn)
110 {
111    FILE *fp = HTS_get_fp(fn, "r");
112    HTS_Label_load_from_fp(label, sampling_rate, fperiod, fp);
113    fclose(fp);
114 }
115
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,
118                             FILE * fp)
119 {
120    char buff[HTS_MAXBUFLEN];
121    HTS_LabelString *lstring = NULL;
122    double start, end;
123    const double rate = (double) sampling_rate / ((double) fperiod * 1e+7);
124
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]))
130          break;
131       label->size++;
132
133       if (lstring) {
134          lstring->next =
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;
140       }
141       if (isdigit_string(buff)) {       /* has frame infomation */
142          start = atof(buff);
143          HTS_get_token(fp, buff);
144          end = atof(buff);
145          HTS_get_token(fp, buff);
146          lstring->start = rate * start;
147          lstring->end = rate * end;
148       } else {
149          lstring->start = -1.0;
150          lstring->end = -1.0;
151       }
152       lstring->next = NULL;
153       lstring->name = HTS_strdup(buff);
154    }
155    HTS_Label_check_time(label);
156 }
157
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)
161 {
162    char buff[HTS_MAXBUFLEN];
163    HTS_LabelString *lstring = NULL;
164    int data_index = 0;          /* data index */
165    double start, end;
166    const double rate = (double) sampling_rate / ((double) fperiod * 1e+7);
167
168    if (label->head || label->size != 0)
169       HTS_error(1, "HTS_Label_load_from_fp: label list is not initialized.\n");
170    /* copy label */
171    while (HTS_get_token_from_string(data, &data_index, buff)) {
172       if (!isgraph((int) buff[0]))
173          break;
174       label->size++;
175
176       if (lstring) {
177          lstring->next =
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;
183       }
184       if (isdigit_string(buff)) {       /* has frame infomation */
185          start = atof(buff);
186          HTS_get_token_from_string(data, &data_index, buff);
187          end = atof(buff);
188          HTS_get_token_from_string(data, &data_index, buff);
189          lstring->start = rate * start;
190          lstring->end = rate * end;
191       } else {
192          lstring->start = -1.0;
193          lstring->end = -1.0;
194       }
195       lstring->next = NULL;
196       lstring->name = HTS_strdup(buff);
197    }
198    HTS_Label_check_time(label);
199 }
200
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)
204 {
205    char buff[HTS_MAXBUFLEN];
206    HTS_LabelString *lstring = NULL;
207    int i;
208    int data_index;
209    double start, end;
210    const double rate = (double) sampling_rate / ((double) fperiod * 1e+7);
211
212    if (label->head || label->size != 0)
213       HTS_error(1, "HTS_Label_load_from_fp: label list is not initialized.\n");
214    /* copy label */
215    for (i = 0; i < size; i++) {
216       if (!isgraph((int) data[i][0]))
217          break;
218       label->size++;
219
220       if (lstring) {
221          lstring->next =
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;
227       }
228       data_index = 0;
229       if (isdigit_string(data[i])) {    /* has frame infomation */
230          HTS_get_token_from_string(data[i], &data_index, buff);
231          start = atof(buff);
232          HTS_get_token_from_string(data[i], &data_index, buff);
233          end = atof(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;
238       } else {
239          lstring->start = -1.0;
240          lstring->end = -1.0;
241          lstring->name = HTS_strdup(data[i]);
242       }
243       lstring->next = NULL;
244    }
245    HTS_Label_check_time(label);
246 }
247
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)
250 {
251    label->frame_flag = i;
252 }
253
254 /* HTS_Label_set_speech_speed: set speech speed rate */
255 void HTS_Label_set_speech_speed(HTS_Label * label, double f)
256 {
257    if (f > 0.0 && f <= 10.0)
258       label->speech_speed = f;
259 }
260
261 /* HTS_Label_get_size: get number of label string */
262 int HTS_Label_get_size(HTS_Label * label)
263 {
264    return label->size;
265 }
266
267 /* HTS_Label_get_string: get label string */
268 char *HTS_Label_get_string(HTS_Label * label, int string_index)
269 {
270    HTS_LabelString *lstring = label->head;
271
272    while (string_index-- && lstring)
273       lstring = lstring->next;
274    if (!lstring)
275       return NULL;
276    return lstring->name;
277 }
278
279 /* HTS_Label_get_frame_specified_flag: get frame specified flag */
280 HTS_Boolean HTS_Label_get_frame_specified_flag(HTS_Label * label)
281 {
282    return label->frame_flag;
283 }
284
285 /* HTS_Label_get_start_frame: get start frame */
286 double HTS_Label_get_start_frame(HTS_Label * label, int string_index)
287 {
288    HTS_LabelString *lstring = label->head;
289
290    while (string_index-- && lstring)
291       lstring = lstring->next;
292    if (!lstring)
293       return -1.0;
294    return lstring->start;
295 }
296
297 /* HTS_Label_get_end_frame: get end frame */
298 double HTS_Label_get_end_frame(HTS_Label * label, int string_index)
299 {
300    HTS_LabelString *lstring = label->head;
301
302    while (string_index-- && lstring)
303       lstring = lstring->next;
304    if (!lstring)
305       return -1.0;
306    return lstring->end;
307 }
308
309 /* HTS_Label_get_speech_speed: get speech speed rate */
310 double HTS_Label_get_speech_speed(HTS_Label * label)
311 {
312    return label->speech_speed;
313 }
314
315 /* HTS_Label_clear: free label */
316 void HTS_Label_clear(HTS_Label * label)
317 {
318    HTS_LabelString *lstring, *next_lstring;
319
320    for (lstring = label->head; lstring; lstring = next_lstring) {
321       next_lstring = lstring->next;
322       HTS_free(lstring->name);
323       HTS_free(lstring);
324    }
325    HTS_Label_initialize(label);
326 }
327
328 HTS_LABEL_C_END;
329
330 #endif                          /* !HTS_LABEL_C */