Debian Codegraph @

src/cg/cst_cg_map.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2010-2017 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alok Parlikar ([email protected]) */
34 /* Date: April 2010 */
35 /*************************************************************************/
36 /* */
37 /* A clustergen generic voice, that can load from a file */
38 /* */
39 /*************************************************************************/
40 #include "cst_string.h"
41 #include "cst_cg_map.h"
42
43 const char * const cg_voice_header_string = "CMU_FLITE_CG_VOXDATA-v2.0";
44
45 int cst_cg_read_header(cst_file fd)
46 {
47 char header[200];
48 unsigned int n;
49 int endianness;
50
51 n = cst_fread(fd,header,sizeof(char),cst_strlen(cg_voice_header_string)+1);
52
53 if (n < cst_strlen(cg_voice_header_string)+1)
54 return -1;
55
56 if (!cst_streq(header,cg_voice_header_string))
57 return -1;
58
59 cst_fread(fd,&endianness,sizeof(int),1); /* for byte order check */
60 if (endianness != cst_endian_loc)
61 return CST_CG_BYTESWAPPED_VOICE; /* dumped with other byte order */
62
63 return 0;
64 }
65
66 char *cst_read_string(cst_file fd, int bs)
67 {
68 int numbytes;
69
70 return (char *)cst_read_padded(fd,&numbytes,bs);
71 }
72
73 cst_cg_db *cst_cg_load_db(cst_voice *vox,cst_file fd, int bs /* byteswapped */)
74 {
75 cst_cg_db* db = cst_alloc(cst_cg_db,1);
76 int i;
77
78 db->freeable = 1; /* somebody can free this if they want */
79
80 db->name = cst_read_string(fd,bs);
81 db->types = (const char**)cst_read_db_types(fd,bs);
82
83 db->num_types = cst_read_int(fd,bs);
84 db->sample_rate = cst_read_int(fd,bs);
85 db->f0_mean = cst_read_float(fd,bs);
86 db->f0_stddev = cst_read_float(fd,bs);
87
88 db->num_f0_models = get_param_int(vox->features,"num_f0_models",1);
89 db->f0_trees = cst_alloc(const cst_cart **,db->num_f0_models);
90 for (i=0; i<db->num_f0_models; i++)
91 db->f0_trees[i] = (const cst_cart**) cst_read_tree_array(fd,bs);
92
93 db->model_shape = get_param_int(vox->features,"model_shape",
94 CST_CG_MODEL_SHAPE_BASE_MINRANGE);
95 db->num_param_models = get_param_int(vox->features,"num_param_models",1);
96 db->param_trees = cst_alloc(const cst_cart **,db->num_param_models);
97 for (i=0; i<db->num_param_models; i++)
98 db->param_trees[i] = (const cst_cart **) cst_read_tree_array(fd,bs);
99
100 db->spamf0 = cst_read_int(fd,bs);
101 if (db->spamf0)
102 {
103 db->spamf0_accent_tree = cst_read_tree(fd,bs);
104 db->spamf0_phrase_tree = cst_read_tree(fd,bs);
105 }
106
107 db->num_channels = cst_alloc(int,db->num_param_models);
108 db->num_frames = cst_alloc(int,db->num_param_models);
109 db->model_vectors = cst_alloc(const unsigned short **,db->num_param_models);
110 for (i=0; i<db->num_param_models; i++)
111 {
112 db->num_channels[i] = cst_read_int(fd,bs);
113 db->num_frames[i] = cst_read_int(fd,bs);
114 db->model_vectors[i] =
115 (const unsigned short **)cst_read_2d_ushort_array(fd,bs);
116 }
117 /* In voices that were built before, they might have NULLs as the */
118 /* the vectors rather than a real model, so adjust the num_param_models */
119 /* accordingly -- this wont cause a leak as there is no alloc'd memory */
120 /* in the later unset vectors */
121 for (i=0; i<db->num_param_models; i++)
122 {
123 if (db->model_vectors[i] == NULL)
124 break;
125 }
126 db->num_param_models = i;
127
128 if (db->spamf0)
129 {
130 db->num_channels_spamf0_accent = cst_read_int(fd,bs);
131 db->num_frames_spamf0_accent = cst_read_int(fd,bs);
132 db->spamf0_accent_vectors =
133 (const float * const *)cst_read_2d_float_array(fd,bs);
134 }
135
136 db->model_min = (const float *)cst_read_float_array(fd,bs);
137 db->model_range = (const float *)cst_read_float_array(fd,bs);
138
139 if (db->model_shape > CST_CG_MODEL_SHAPE_BASE_MINRANGE)
140 { /* there is a qtable if shape > 1 */
141 db->qtable = cst_alloc(const float **,db->num_param_models);
142 for (i=0; i<db->num_param_models; i++)
143 db->qtable[i] =
144 (const float **)cst_read_2d_float_array(fd,bs);
145 }
146
147 db->frame_advance = cst_read_float(fd,bs);
148
149 db->num_dur_models = get_param_int(vox->features,"num_dur_models",1);
150 db->dur_stats = cst_alloc(const dur_stat **,db->num_dur_models);
151 db->dur_cart = cst_alloc(const cst_cart *,db->num_dur_models);
152
153 for (i=0; i<db->num_dur_models; i++)
154 {
155 db->dur_stats[i] = (const dur_stat **)cst_read_dur_stats(fd,bs);
156 db->dur_cart[i] = (const cst_cart *)cst_read_tree(fd,bs);
157 }
158
159 db->phone_states =
160 (const char * const * const *)cst_read_phone_states(fd,bs);
161
162 db->do_mlpg = cst_read_int(fd,bs);
163 db->dynwin = cst_read_float_array(fd,bs);
164 db->dynwinsize = cst_read_int(fd,bs);
165
166 db->mlsa_alpha = cst_read_float(fd,bs);
167 db->mlsa_beta = cst_read_float(fd,bs);
168
169 db->multimodel = cst_read_int(fd,bs);
170 db->mixed_excitation = cst_read_int(fd,bs);
171
172 db->ME_num = cst_read_int(fd,bs);
173 db->ME_order = cst_read_int(fd,bs);
174 db->me_h = (const double * const *)cst_read_2d_double_array(fd,bs);
175
176 db->spamf0 = cst_read_int(fd,bs); /* yes, twice, its above too */
177 db->gain = cst_read_float(fd,bs);
178
179 /* If this is "grapheme" voice, we will have phoneset and char_map */
180
181 return db;
182
183 }
184
185 void cst_cg_free_db(cst_file fd, cst_cg_db *db)
186 {
187 /* Only gets called when this isn't populated : I think */
188 cst_free(db);
189 }
190
191 void *cst_read_padded(cst_file fd, int *numbytes, int bs)
192 {
193 void* ret;
194 int n;
195
196 *numbytes = cst_read_int(fd,bs);
197 ret = (void *)cst_alloc(char,*numbytes);
198 n = cst_fread(fd,ret,sizeof(char),*numbytes);
199 if (n != (*numbytes))
200 {
201 cst_free(ret);
202 return NULL;
203 }
204 return ret;
205 }
206
207 char **cst_read_db_types(cst_file fd, int bs)
208 {
209 char** types;
210 int numtypes;
211 int i;
212
213 numtypes = cst_read_int(fd,bs);
214 types = cst_alloc(char*,numtypes+1);
215
216 for(i=0;i<numtypes;i++)
217 {
218 types[i] = cst_read_string(fd,bs);
219 }
220 types[i] = 0;
221
222 return types;
223 }
224
225 cst_cart_node* cst_read_tree_nodes(cst_file fd,int bs)
226 {
227 cst_cart_node* nodes;
228 int i, num_nodes;
229 short vtype;
230 char *str;
231
232 num_nodes = cst_read_int(fd,bs);
233 nodes = cst_alloc(cst_cart_node,num_nodes+1);
234
235 for (i=0; i<num_nodes; i++)
236 {
237 cst_fread(fd,&nodes[i].feat,sizeof(char),1);
238 cst_fread(fd,&nodes[i].op,sizeof(char),1);
239 cst_fread(fd,&nodes[i].no_node,sizeof(short),1);
240 if (bs) nodes[i].no_node = SWAPSHORT(nodes[i].no_node);
241 cst_fread(fd,&vtype,sizeof(short),1);
242 if (bs) vtype = SWAPSHORT(vtype);
243 if (vtype == CST_VAL_TYPE_STRING)
244 {
245 str = cst_read_string(fd,bs);
246 nodes[i].val = string_val(str);
247 cst_free(str);
248 }
249 else if (vtype == CST_VAL_TYPE_INT)
250 nodes[i].val = int_val(cst_read_int(fd,bs));
251 else if (vtype == CST_VAL_TYPE_FLOAT)
252 nodes[i].val = float_val(cst_read_float(fd,bs));
253 else
254 nodes[i].val = int_val(cst_read_int(fd,bs));
255 }
256 nodes[i].val = NULL;
257
258 return nodes;
259 }
260
261 char** cst_read_tree_feats(cst_file fd,int bs)
262 {
263 char** feats;
264 int numfeats;
265 int i;
266
267 numfeats = cst_read_int(fd,bs);
268 feats = cst_alloc(char *,numfeats+1);
269
270 for(i=0;i<numfeats;i++)
271 feats[i] = cst_read_string(fd,bs);
272 feats[i] = 0;
273
274 return feats;
275 }
276
277 cst_cart* cst_read_tree(cst_file fd,int bs)
278 {
279 cst_cart* tree;
280
281 tree = cst_alloc(cst_cart,1);
282 tree->rule_table = cst_read_tree_nodes(fd,bs);
283 tree->feat_table = (const char * const *)cst_read_tree_feats(fd,bs);
284
285 return tree;
286 }
287
288 cst_cart** cst_read_tree_array(cst_file fd,int bs)
289 {
290 cst_cart** trees = NULL;
291 int numtrees;
292 int i;
293
294 numtrees = cst_read_int(fd,bs);
295 if (numtrees > 0)
296 {
297 trees = cst_alloc(cst_cart *,numtrees+1);
298
299 for(i=0;i<numtrees;i++)
300 trees[i] = cst_read_tree(fd,bs);
301 trees[i] = 0;
302 }
303
304 return trees;
305 }
306
307 #if 0
308 void* cst_read_array(cst_file fd,int bs)
309 {
310 int temp;
311 void* ret;
312 ret = cst_read_padded(fd,&temp,bs);
313 return ret;
314 }
315 #endif
316
317 float *cst_read_float_array(cst_file fd,int bs)
318 {
319 unsigned int i;
320 int bytecount;
321 float* ret;
322
323 ret = (float *)cst_read_padded(fd,&bytecount,bs);
324 if (bs)
325 for (i=0; i<bytecount/sizeof(float); i++)
326 swapfloat(&ret[i]);
327 return ret;
328 }
329
330 unsigned short *cst_read_ushort_array(cst_file fd,int bs)
331 {
332 unsigned int i;
333 int bytecount;
334 unsigned short* ret;
335
336 ret = (unsigned short *)cst_read_padded(fd,&bytecount,bs);
337 if (bs)
338 for (i=0; i<bytecount/sizeof(unsigned short); i++)
339 ret[i] = SWAPSHORT(ret[i]);
340 return ret;
341 }
342
343 double *cst_read_double_array(cst_file fd,int bs)
344 {
345 unsigned int i;
346 int bytecount;
347 double* ret;
348
349 ret = (double *)cst_read_padded(fd,&bytecount,bs);
350 if (bs)
351 for (i=0; i<bytecount/sizeof(double); i++)
352 swapdouble(&ret[i]);
353 return ret;
354 }
355
356 float** cst_read_2d_float_array(cst_file fd,int bs)
357 {
358 int numrows;
359 int i;
360 float** arrayrows = NULL;
361
362 numrows = cst_read_int(fd,bs);
363 if (numrows > 0)
364 {
365 arrayrows = cst_alloc(float *,numrows);
366 for(i=0;i<numrows;i++)
367 arrayrows[i] = cst_read_float_array(fd,bs);
368 }
369
370 return arrayrows;
371 }
372
373 unsigned short** cst_read_2d_ushort_array(cst_file fd,int bs)
374 {
375 int numrows;
376 int i;
377 unsigned short** arrayrows = NULL;
378
379 numrows = cst_read_int(fd,bs);
380 if (numrows > 0)
381 {
382 arrayrows = cst_alloc(unsigned short *,numrows);
383 for(i=0;i<numrows;i++)
384 arrayrows[i] = cst_read_ushort_array(fd,bs);
385 }
386
387 return arrayrows;
388 }
389
390 double** cst_read_2d_double_array(cst_file fd,int bs)
391 {
392 int numrows;
393 int i;
394 double** arrayrows = NULL;
395
396 numrows = cst_read_int(fd,bs);
397 if (numrows > 0)
398 {
399 arrayrows = cst_alloc(double *,numrows);
400 for(i=0;i<numrows;i++)
401 arrayrows[i] = cst_read_double_array(fd,bs);
402 }
403
404 return arrayrows;
405 }
406
407 dur_stat** cst_read_dur_stats(cst_file fd,int bs)
408 {
409 int numstats;
410 int i;
411 dur_stat** ds;
412
413 numstats = cst_read_int(fd,bs);
414 ds = cst_alloc(dur_stat *,(1+numstats));
415
416 /* load structuer values */
417 for(i=0;i<numstats;i++)
418 {
419 ds[i] = cst_alloc(dur_stat,1);
420 ds[i]->mean = cst_read_float(fd,bs);
421 ds[i]->stddev = cst_read_float(fd,bs);
422 ds[i]->phone = cst_read_string(fd,bs);
423 }
424 ds[i] = NULL;
425
426 return ds;
427 }
428
429 char*** cst_read_phone_states(cst_file fd,int bs)
430 {
431 int i,j,count1,count2;
432 char*** ps;
433
434 count1 = cst_read_int(fd,bs);
435 ps = cst_alloc(char **,count1+1);
436 for(i=0;i<count1;i++)
437 {
438 count2 = cst_read_int(fd,bs);
439 ps[i] = cst_alloc(char *,count2+1);
440 for(j=0;j<count2;j++)
441 {
442 ps[i][j]=cst_read_string(fd,bs);
443 }
444 ps[i][j] = 0;
445 }
446 ps[i] = 0;
447
448 return ps;
449 }
450
451 void cst_read_voice_feature(cst_file fd,char** fname, char** fval, int bs)
452 {
453 *fname = cst_read_string(fd,bs);
454 *fval = cst_read_string(fd,bs);
455 }
456
457 int cst_read_int(cst_file fd,int byteswapped)
458 {
459 int val;
460 int n;
461
462 n = cst_fread(fd,&val,sizeof(int),1);
463 if (n != 1) return 0;
464 if (byteswapped) val = SWAPINT(val);
465 return val;
466 }
467
468 float cst_read_float(cst_file fd, int byteswapped)
469 {
470 float val;
471 int n;
472
473 n = cst_fread(fd,&val,sizeof(float),1);
474 if (n != 1)
475 return 0;
476 if (byteswapped) swapfloat(&val);
477 return val;
478 }