1 /* ----------------------------------------------------------------- */
2 /* The HMM-Based Speech Synthesis Engine "hts_engine API" */
3 /* developed by HTS Working Group */
4 /* http://hts-engine.sourceforge.net/ */
5 /* ----------------------------------------------------------------- */
6 /* */
7 /* Copyright (c) 2001-2011 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 HTS106_MISC_C
46 #define HTS106_MISC_C
47
48 #ifdef __cplusplus
49 #define HTS106_MISC_C_START extern "C" {
50 #define HTS106_MISC_C_END }
51 #else
52 #define HTS106_MISC_C_START
53 #define HTS106_MISC_C_END
54 #endif /* __CPLUSPLUS */
55
56 HTS106_MISC_C_START;
57
58 #include <stdlib.h> /* for exit(),calloc(),free() */
59 #include <stdarg.h> /* for va_list */
60 #include <string.h> /* for strcpy(),strlen() */
61
62 /* hts_engine libraries */
63 #include "HTS106_hidden.h"
64
65 #ifdef FESTIVAL
66 #include "EST_walloc.h"
67 #endif /* FESTIVAL */
68
69 #include "utils.h"
70
71 /* HTS106_byte_swap: byte swap */
HTS106_byte_swap(void * p,const int size,const int block)72 static int HTS106_byte_swap(void *p, const int size, const int block)
73 {
74 char *q, tmp;
75 int i, j;
76
77 q = (char *) p;
78
79 for (i = 0; i < block; i++) {
80 for (j = 0; j < (size / 2); j++) {
81 tmp = *(q + j);
82 *(q + j) = *(q + (size - 1 - j));
83 *(q + (size - 1 - j)) = tmp;
84 }
85 q += size;
86 }
87
88 return i;
89 }
90
91 /* HTS106_error: output error message */
HTS106_error(const int error,char * message,...)92 void HTS106_error(const int error, char *message, ...)
93 {
94 va_list arg;
95
96 fflush(stdout);
97 fflush(stderr);
98
99 if (error > 0)
100 fprintf(stderr, "\nError: ");
101 else
102 fprintf(stderr, "\nWarning: ");
103
104 va_start(arg, message);
105 vfprintf(stderr, message, arg);
106 va_end(arg);
107
108 fflush(stderr);
109
110 if (error > 0)
111 exit(error);
112 }
113
114 /* HTS106_fopen: wrapper for fopen */
HTS106_fopen(const char * name,const char * opt)115 HTS106_File *HTS106_fopen(const char *name, const char *opt)
116 {
117 HTS106_File *fp = utf8_fopen(name, opt);
118
119 if (fp == NULL) {
120 HTS106_error(1, "HTS106_fopen: Cannot open %s.\n", name);
121 return NULL;
122 }
123
124 return fp;
125 }
126
127 /* HTS106_fgetc: wrapper for fgetc */
HTS106_fgetc(HTS106_File * fp)128 int HTS106_fgetc(HTS106_File * fp)
129 {
130 return fgetc(fp);
131 }
132
133 /* HTS106_feof: wrapper for feof */
HTS106_feof(HTS106_File * fp)134 int HTS106_feof(HTS106_File * fp)
135 {
136 return feof(fp);
137 }
138
139 /* HTS106_fread: wrapper for fread */
HTS106_fread(void * buf,size_t size,size_t n,HTS106_File * fp)140 size_t HTS106_fread(void *buf, size_t size, size_t n, HTS106_File * fp)
141 {
142 return fread(buf, size, n, fp);
143 }
144
145 /* HTS106_fwrite: wrapper for fwrite */
HTS106_fwrite(const void * buf,size_t size,size_t n,HTS106_File * fp)146 size_t HTS106_fwrite(const void *buf, size_t size, size_t n, HTS106_File * fp)
147 {
148 return fwrite(buf, size, n, fp);
149 }
150
151 /* HTS106_fclose: wrapper for fclose */
HTS106_fclose(HTS106_File * fp)152 void HTS106_fclose(HTS106_File * fp)
153 {
154 fclose(fp);
155 }
156
157 /* HTS106_get_pattern_token: get pattern token */
HTS106_get_pattern_token(HTS106_File * fp,char * buff)158 HTS106_Boolean HTS106_get_pattern_token(HTS106_File * fp, char *buff)
159 {
160 char c;
161 int i;
162 HTS106_Boolean squote = FALSE, dquote = FALSE;
163
164 if (fp == NULL || HTS106_feof(fp))
165 return FALSE;
166 c = HTS106_fgetc(fp);
167
168 while (c == ' ' || c == '\n') {
169 if (HTS106_feof(fp))
170 return FALSE;
171 c = HTS106_fgetc(fp);
172 }
173
174 if (c == '\'') { /* single quote case */
175 if (HTS106_feof(fp))
176 return FALSE;
177 c = HTS106_fgetc(fp);
178 squote = TRUE;
179 }
180
181 if (c == '\"') { /*double quote case */
182 if (HTS106_feof(fp))
183 return FALSE;
184 c = HTS106_fgetc(fp);
185 dquote = TRUE;
186 }
187
188 if (c == ',') { /*special character ',' */
189 strcpy(buff, ",");
190 return TRUE;
191 }
192
193 i = 0;
194 while (1) {
195 buff[i++] = c;
196 c = HTS106_fgetc(fp);
197 if (squote && c == '\'')
198 break;
199 if (dquote && c == '\"')
200 break;
201 if (!squote && !dquote) {
202 if (c == ' ')
203 break;
204 if (c == '\n')
205 break;
206 if (HTS106_feof(fp))
207 break;
208 }
209 }
210
211 buff[i] = '\0';
212 return TRUE;
213 }
214
215 /* HTS106_get_token: get token (separators are space, tab, and line break) */
HTS106_get_token(HTS106_File * fp,char * buff)216 HTS106_Boolean HTS106_get_token(HTS106_File * fp, char *buff)
217 {
218 char c;
219 int i;
220
221 if (fp == NULL || HTS106_feof(fp))
222 return FALSE;
223 c = HTS106_fgetc(fp);
224 while (c == ' ' || c == '\n' || c == '\t') {
225 if (HTS106_feof(fp))
226 return FALSE;
227 c = HTS106_fgetc(fp);
228 }
229
230 for (i = 0; c != ' ' && c != '\n' && c != '\t';) {
231 buff[i++] = c;
232 if (HTS106_feof(fp))
233 break;
234 c = HTS106_fgetc(fp);
235 }
236
237 buff[i] = '\0';
238 return TRUE;
239 }
240
241 /* HTS106_get_token_from_string: get token from string (separators are space, tab, and line break) */
HTS106_get_token_from_string(char * string,int * index,char * buff)242 HTS106_Boolean HTS106_get_token_from_string(char *string, int *index, char *buff)
243 {
244 char c;
245 int i;
246
247 c = string[(*index)];
248 if (c == '\0')
249 return FALSE;
250 c = string[(*index)++];
251 if (c == '\0')
252 return FALSE;
253 while (c == ' ' || c == '\n' || c == '\t') {
254 if (c == '\0')
255 return FALSE;
256 c = string[(*index)++];
257 }
258 for (i = 0; c != ' ' && c != '\n' && c != '\t' && c != '\0'; i++) {
259 buff[i] = c;
260 c = string[(*index)++];
261 }
262
263 buff[i] = '\0';
264 return TRUE;
265 }
266
267 /* HTS106_fread_big_endian: fread with byteswap */
HTS106_fread_big_endian(void * p,const int size,const int num,HTS106_File * fp)268 int HTS106_fread_big_endian(void *p, const int size, const int num, HTS106_File * fp)
269 {
270 const int block = HTS106_fread(p, size, num, fp);
271
272 if(is_machine_little_endian())
273 HTS106_byte_swap(p, size, block);
274
275 return block;
276 }
277
278 /* HTS106_fwrite_little_endian: fwrite with byteswap */
HTS106_fwrite_little_endian(void * p,const int size,const int num,HTS106_File * fp)279 int HTS106_fwrite_little_endian(void *p, const int size, const int num, HTS106_File * fp)
280 {
281 const int block = num * size;
282
283 if(is_machine_big_endian())
284 HTS106_byte_swap(p, size, block);
285 HTS106_fwrite(p, size, num, fp);
286
287 return block;
288 }
289
290 /* HTS106_calloc: wrapper for calloc */
HTS106_calloc(const size_t num,const size_t size)291 char *HTS106_calloc(const size_t num, const size_t size)
292 {
293 #ifdef FESTIVAL
294 char *mem = (char *) safe_wcalloc(num * size);
295 #else
296 char *mem = (char *) calloc(num, size);
297 #endif /* FESTIVAL */
298
299 if (mem == NULL)
300 HTS106_error(1, "HTS106_calloc: Cannot allocate memory.\n");
301
302 return mem;
303 }
304
305 /* HTS106_Free: wrapper for free */
HTS106_free(void * ptr)306 void HTS106_free(void *ptr)
307 {
308 #ifdef FESTIVAL
309 wfree(ptr);
310 #else
311 free(ptr);
312 #endif /* FESTIVAL */
313 }
314
315 /* HTS106_strdup: wrapper for strdup */
HTS106_strdup(const char * string)316 char *HTS106_strdup(const char *string)
317 {
318 #ifdef FESTIVAL
319 return (wstrdup(string));
320 #else
321 char *buff = (char *) HTS106_calloc(strlen(string) + 1, sizeof(char));
322 strcpy(buff, string);
323 return buff;
324 #endif /* FESTIVAL */
325 }
326
327 /* HTS106_alloc_matrix: allocate double matrix */
HTS106_alloc_matrix(const int x,const int y)328 double **HTS106_alloc_matrix(const int x, const int y)
329 {
330 int i;
331 double **p = (double **) HTS106_calloc(x, sizeof(double *));
332
333 for (i = 0; i < x; i++)
334 p[i] = (double *) HTS106_calloc(y, sizeof(double));
335 return p;
336 }
337
338 /* HTS106_free_matrix: free double matrix */
HTS106_free_matrix(double ** p,const int x)339 void HTS106_free_matrix(double **p, const int x)
340 {
341 int i;
342
343 for (i = x - 1; i >= 0; i--)
344 HTS106_free(p[i]);
345 HTS106_free(p);
346 }
347
348 HTS106_MISC_C_END;
349
350 #endif /* !HTS106_MISC_C */
351