1 /* ----------------------------------------------------------------- */
2 /* The HMM-Based Speech Synthesis Engine "hts_engine API" */
3 /* developed by HTS Working Group */
4 /* http://hts-engine.sourceforge.net/ */
5 /* ----------------------------------------------------------------- */
7 /* Copyright (c) 2001-2015 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_MISC_C_START extern "C" {
50 #define HTS_MISC_C_END }
52 #define HTS_MISC_C_START
53 #define HTS_MISC_C_END
54 #endif /* __CPLUSPLUS */
58 #include <stdlib.h> /* for exit(),calloc(),free() */
59 #include <stdarg.h> /* for va_list */
60 #include <string.h> /* for strcpy(),strlen() */
62 /* hts_engine libraries */
63 #include "HTS_hidden.h"
66 #include "EST_walloc.h"
72 typedef struct _HTS_Data {
78 /* HTS_fopen_from_fn: wrapper for fopen */
79 HTS_File *HTS_fopen_from_fn(const char *name, const char *opt)
81 HTS_File *fp = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
84 fp->pointer = (void *) fopen(name, opt);
86 if (fp->pointer == NULL) {
87 HTS_error(0, "HTS_fopen: Cannot open %s.\n", name);
95 /* HTS_fopen_from_fp: wrapper for fopen */
96 HTS_File *HTS_fopen_from_fp(HTS_File * fp, size_t size)
98 if (fp == NULL || size == 0)
100 else if (fp->type == HTS_FILE) {
103 d = (HTS_Data *) HTS_calloc(1, sizeof(HTS_Data));
104 d->data = (unsigned char *) HTS_calloc(size, sizeof(unsigned char));
107 if (fread(d->data, sizeof(unsigned char), size, (FILE *) fp->pointer) != size) {
112 f = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
114 f->pointer = (void *) d;
116 } else if (fp->type == HTS_DATA) {
118 HTS_Data *tmp1, *tmp2;
119 tmp1 = (HTS_Data *) fp->pointer;
120 if (tmp1->index + size > tmp1->size)
122 tmp2 = (HTS_Data *) HTS_calloc(1, sizeof(HTS_Data));
123 tmp2->data = (unsigned char *) HTS_calloc(size, sizeof(unsigned char));
126 memcpy(tmp2->data, &tmp1->data[tmp1->index], size);
128 f = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
130 f->pointer = (void *) tmp2;
134 HTS_error(0, "HTS_fopen_from_fp: Unknown file type.\n");
138 /* HTS_fopen_from_data: wrapper for fopen */
139 HTS_File *HTS_fopen_from_data(void *data, size_t size)
144 if (data == NULL || size == 0)
147 d = (HTS_Data *) HTS_calloc(1, sizeof(HTS_Data));
148 d->data = (unsigned char *) HTS_calloc(size, sizeof(unsigned char));
152 memcpy(d->data, data, size);
154 f = (HTS_File *) HTS_calloc(1, sizeof(HTS_File));
156 f->pointer = (void *) d;
161 /* HTS_fclose: wrapper for fclose */
162 void HTS_fclose(HTS_File * fp)
166 } else if (fp->type == HTS_FILE) {
167 if (fp->pointer != NULL)
168 fclose((FILE *) fp->pointer);
171 } else if (fp->type == HTS_DATA) {
172 if (fp->pointer != NULL) {
173 HTS_Data *d = (HTS_Data *) fp->pointer;
181 HTS_error(0, "HTS_fclose: Unknown file type.\n");
184 /* HTS_fgetc: wrapper for fgetc */
185 int HTS_fgetc(HTS_File * fp)
189 } else if (fp->type == HTS_FILE) {
190 return fgetc((FILE *) fp->pointer);
191 } else if (fp->type == HTS_DATA) {
192 HTS_Data *d = (HTS_Data *) fp->pointer;
193 if (d->size <= d->index)
195 return (int) d->data[d->index++];
197 HTS_error(0, "HTS_fgetc: Unknown file type.\n");
201 /* HTS_feof: wrapper for feof */
202 int HTS_feof(HTS_File * fp)
206 } else if (fp->type == HTS_FILE) {
207 return feof((FILE *) fp->pointer);
208 } else if (fp->type == HTS_DATA) {
209 HTS_Data *d = (HTS_Data *) fp->pointer;
210 return d->size <= d->index ? 1 : 0;
212 HTS_error(0, "HTS_feof: Unknown file type.\n");
216 /* HTS_fseek: wrapper for fseek */
217 int HTS_fseek(HTS_File * fp, long offset, int origin)
221 } else if (fp->type == HTS_FILE) {
222 return fseek((FILE *) fp->pointer, offset, origin);
223 } else if (fp->type == HTS_DATA) {
224 HTS_Data *d = (HTS_Data *) fp->pointer;
225 if (origin == SEEK_SET) {
226 d->index = (size_t) offset;
227 } else if (origin == SEEK_CUR) {
229 } else if (origin == SEEK_END) {
230 d->index = d->size + offset;
236 HTS_error(0, "HTS_fseek: Unknown file type.\n");
240 /* HTS_ftell: rapper for ftell */
241 size_t HTS_ftell(HTS_File * fp)
245 } else if (fp->type == HTS_FILE) {
247 fgetpos((FILE *) fp->pointer, &pos);
248 #if defined(_WIN32) || defined(__CYGWIN__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__ANDROID__)
251 return (size_t) pos.__pos;
252 #endif /* _WIN32 || __CYGWIN__ || __APPLE__ || __ANDROID__ */
253 } else if (fp->type == HTS_DATA) {
254 HTS_Data *d = (HTS_Data *) fp->pointer;
257 HTS_error(0, "HTS_ftell: Unknown file type.\n");
261 /* HTS_fread: wrapper for fread */
262 static size_t HTS_fread(void *buf, size_t size, size_t n, HTS_File * fp)
264 if (fp == NULL || size == 0 || n == 0) {
267 if (fp->type == HTS_FILE) {
268 return fread(buf, size, n, (FILE *) fp->pointer);
269 } else if (fp->type == HTS_DATA) {
270 HTS_Data *d = (HTS_Data *) fp->pointer;
271 size_t i, length = size * n;
272 unsigned char *c = (unsigned char *) buf;
273 for (i = 0; i < length; i++) {
274 if (d->index < d->size)
275 c[i] = d->data[d->index++];
284 HTS_error(0, "HTS_fread: Unknown file type.\n");
288 /* HTS_byte_swap: byte swap */
289 static void HTS_byte_swap(void *p, size_t size, size_t block)
296 for (i = 0; i < block; i++) {
297 for (j = 0; j < (size / 2); j++) {
299 *(q + j) = *(q + (size - 1 - j));
300 *(q + (size - 1 - j)) = tmp;
306 /* HTS_fread_big_endian: fread with byteswap */
307 size_t HTS_fread_big_endian(void *buf, size_t size, size_t n, HTS_File * fp)
309 size_t block = HTS_fread(buf, size, n, fp);
311 #ifdef WORDS_LITTLEENDIAN
312 HTS_byte_swap(buf, size, block);
313 #endif /* WORDS_LITTLEENDIAN */
318 /* HTS_fread_little_endian: fread with byteswap */
319 size_t HTS_fread_little_endian(void *buf, size_t size, size_t n, HTS_File * fp)
321 size_t block = HTS_fread(buf, size, n, fp);
323 #ifdef WORDS_BIGENDIAN
324 HTS_byte_swap(buf, size, block);
325 #endif /* WORDS_BIGENDIAN */
330 /* HTS_fwrite_little_endian: fwrite with byteswap */
331 size_t HTS_fwrite_little_endian(const void *buf, size_t size, size_t n, FILE * fp)
333 #ifdef WORDS_BIGENDIAN
334 HTS_byte_swap(buf, size, n * size);
335 #endif /* WORDS_BIGENDIAN */
336 return fwrite(buf, size, n, fp);
339 /* HTS_get_pattern_token: get pattern token (single/double quote can be used) */
340 HTS_Boolean HTS_get_pattern_token(HTS_File * fp, char *buff)
344 HTS_Boolean squote = FALSE, dquote = FALSE;
346 if (fp == NULL || HTS_feof(fp))
350 while (c == ' ' || c == '\n') {
356 if (c == '\'') { /* single quote case */
363 if (c == '\"') { /*double quote case */
370 if (c == ',') { /*special character ',' */
379 if (squote && c == '\'')
381 if (dquote && c == '\"')
383 if (!squote && !dquote) {
397 /* HTS_get_token: get token from file pointer (separators are space, tab, and line break) */
398 HTS_Boolean HTS_get_token_from_fp(HTS_File * fp, char *buff)
403 if (fp == NULL || HTS_feof(fp))
406 while (c == ' ' || c == '\n' || c == '\t') {
414 for (i = 0; c != ' ' && c != '\n' && c != '\t';) {
427 /* HTS_get_token_with_separator: get token from file pointer with specified separator */
428 HTS_Boolean HTS_get_token_from_fp_with_separator(HTS_File * fp, char *buff, char separator)
433 if (fp == NULL || HTS_feof(fp))
436 while (c == separator) {
444 for (i = 0; c != separator;) {
457 /* HTS_get_token_from_string: get token from string (separators are space, tab, and line break) */
458 HTS_Boolean HTS_get_token_from_string(const char *string, size_t * index, char *buff)
463 c = string[(*index)];
466 c = string[(*index)++];
469 while (c == ' ' || c == '\n' || c == '\t') {
472 c = string[(*index)++];
474 for (i = 0; c != ' ' && c != '\n' && c != '\t' && c != '\0'; i++) {
476 c = string[(*index)++];
483 /* HTS_get_token_from_string_with_separator: get token from string with specified separator */
484 HTS_Boolean HTS_get_token_from_string_with_separator(const char *str, size_t * index, char *buff, char separator)
495 while (c == separator) {
501 while (c != separator && c != '\0') {
517 /* HTS_calloc: wrapper for calloc */
518 void *HTS_calloc(const size_t num, const size_t size)
520 size_t n = num * size;
527 mem = (void *) safe_wcalloc(n);
529 mem = (void *) malloc(n);
530 #endif /* FESTIVAL */
535 HTS_error(1, "HTS_calloc: Cannot allocate memory.\n");
540 /* HTS_Free: wrapper for free */
541 void HTS_free(void *ptr)
547 #endif /* FESTIVAL */
550 /* HTS_strdup: wrapper for strdup */
551 char *HTS_strdup(const char *string)
554 return (wstrdup(string));
556 char *buff = (char *) HTS_calloc(strlen(string) + 1, sizeof(char));
557 strcpy(buff, string);
559 #endif /* FESTIVAL */
562 /* HTS_alloc_matrix: allocate double matrix */
563 double **HTS_alloc_matrix(size_t x, size_t y)
568 if (x == 0 || y == 0)
571 p = (double **) HTS_calloc(x, sizeof(double *));
573 for (i = 0; i < x; i++)
574 p[i] = (double *) HTS_calloc(y, sizeof(double));
578 /* HTS_free_matrix: free double matrix */
579 void HTS_free_matrix(double **p, size_t x)
583 for (i = 0; i < x; i++)
588 /* HTS_error: output error message */
589 void HTS_error(int error, const char *message, ...)
597 fprintf(stderr, "\nError: ");
599 fprintf(stderr, "\nWarning: ");
601 va_start(arg, message);
602 vfprintf(stderr, message, arg);
613 #endif /* !HTS_MISC_C */