| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2010-2011 */ |
| 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: March 2010 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Utility for dumping a clustergen voice as a loadable file */ |
| 38 |
/* Should be safe over different address architectures, but cannot */ |
| 39 |
/* yet load from files dumps with different endianness */ |
| 40 |
/*************************************************************************/ |
| 41 |
|
| 42 |
#include "cst_file.h" |
| 43 |
#include "cst_cg.h" |
| 44 |
#include "cst_cart.h" |
| 45 |
#include "cst_cg_map.h" |
| 46 |
|
| 47 |
/* Write magic string */ |
| 48 |
static void cst_cg_write_header(cst_file fd) |
| 49 |
{ |
| 50 |
const char *header = cg_voice_header_string; |
| 51 |
|
| 52 |
cst_fwrite(fd,header,1,cst_strlen(header)+1); |
| 53 |
cst_fwrite(fd,&cst_endian_loc,sizeof(int),1); /* for byte order check */ |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
static void cst_cg_write_padded(cst_file fd, const void* data, int numbytes) |
| 58 |
{ |
| 59 |
/* We used to do 4-byte alignments. but that's not necessary now */ |
| 60 |
cst_fwrite(fd, &numbytes, sizeof(int), 1); |
| 61 |
cst_fwrite(fd, data, 1, numbytes); |
| 62 |
} |
| 63 |
|
| 64 |
/* Write the CG types */ |
| 65 |
static void cst_cg_write_db_types(cst_file fd, const cst_cg_db* db) |
| 66 |
{ |
| 67 |
int i,n; |
| 68 |
|
| 69 |
n=0; |
| 70 |
while(db->types[n]) |
| 71 |
n++; |
| 72 |
|
| 73 |
cst_fwrite(fd, &n,sizeof(int),1); |
| 74 |
|
| 75 |
for (i=0; i<n; i++) |
| 76 |
cst_cg_write_padded(fd, (void*)(db->types[i]), strlen(db->types[i])+1); |
| 77 |
} |
| 78 |
|
| 79 |
/* Write the nodes of a cart tree */ |
| 80 |
static void cst_cg_write_tree_nodes(cst_file fd, const cst_cart_node* nodes) |
| 81 |
{ |
| 82 |
int num_nodes, i; |
| 83 |
int an_int; |
| 84 |
float a_float; |
| 85 |
|
| 86 |
num_nodes=0; |
| 87 |
while(nodes[num_nodes].val != 0) |
| 88 |
num_nodes++; |
| 89 |
|
| 90 |
cst_fwrite(fd, &num_nodes,sizeof(int),1); |
| 91 |
for (i=0; i<num_nodes; i++) |
| 92 |
{ |
| 93 |
cst_fwrite(fd, &nodes[i].feat,sizeof(char),1); |
| 94 |
cst_fwrite(fd, &nodes[i].op,sizeof(char),1); |
| 95 |
cst_fwrite(fd, &nodes[i].no_node,sizeof(short),1); |
| 96 |
cst_fwrite(fd, &nodes[i].val->c.a.type,sizeof(short),1); |
| 97 |
if (nodes[i].val->c.a.type == CST_VAL_TYPE_STRING) |
| 98 |
{ |
| 99 |
cst_cg_write_padded(fd, nodes[i].val->c.a.v.vval, |
| 100 |
cst_strlen(nodes[i].val->c.a.v.vval)+1); |
| 101 |
} |
| 102 |
else if (nodes[i].val->c.a.type == CST_VAL_TYPE_INT) |
| 103 |
{ |
| 104 |
an_int=nodes[i].val->c.a.v.ival; |
| 105 |
cst_fwrite(fd, &an_int,sizeof(int),1); |
| 106 |
} |
| 107 |
else if (nodes[i].val->c.a.type == CST_VAL_TYPE_FLOAT) |
| 108 |
{ |
| 109 |
a_float=nodes[i].val->c.a.v.fval; |
| 110 |
cst_fwrite(fd, &a_float,sizeof(float),1); |
| 111 |
} |
| 112 |
else |
| 113 |
{ /* its not going to work without more code ... */ |
| 114 |
cst_fwrite(fd, &nodes[i].val->c.a.v.ival,sizeof(int),1); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* Write the feats table of a cart tree */ |
| 120 |
static void cst_cg_write_tree_feats(cst_file fd, const char* const* feats) |
| 121 |
{ |
| 122 |
int i; |
| 123 |
|
| 124 |
i=0; |
| 125 |
while(feats[i]) i++; |
| 126 |
cst_fwrite(fd, &i,sizeof(int),1); |
| 127 |
|
| 128 |
for (i=0; feats[i]; i++) |
| 129 |
cst_cg_write_padded(fd, (void*)(feats[i]), strlen(feats[i])+1); |
| 130 |
} |
| 131 |
|
| 132 |
/* Write a cart tree */ |
| 133 |
static void cst_cg_write_tree(cst_file fd, const cst_cart* tree) |
| 134 |
{ |
| 135 |
cst_cg_write_tree_nodes(fd, tree->rule_table); |
| 136 |
cst_cg_write_tree_feats(fd, tree->feat_table); |
| 137 |
} |
| 138 |
|
| 139 |
/* Write an array of cart trees */ |
| 140 |
static void cst_cg_write_tree_array(cst_file fd, const cst_cart* const * trees) |
| 141 |
{ |
| 142 |
int i=0; |
| 143 |
|
| 144 |
while(trees && trees[i]) i++; |
| 145 |
cst_fwrite(fd, &i,sizeof(int),1); |
| 146 |
|
| 147 |
for (i=0; trees && trees[i]; i++) |
| 148 |
cst_cg_write_tree(fd, trees[i]); |
| 149 |
} |
| 150 |
|
| 151 |
/* Write a single dimensional array whose total size is "bytesize" */ |
| 152 |
static void cst_cg_write_array(cst_file fd, const void* data, int bytesize) |
| 153 |
{ |
| 154 |
cst_cg_write_padded(fd, data, bytesize); |
| 155 |
} |
| 156 |
|
| 157 |
/* Write a two dimensional array, use different functions for different |
| 158 |
item sizes -- to be safe (I made errors before on this) */ |
| 159 |
static void cst_cg_write_2d_array_short(cst_file fd, |
| 160 |
const unsigned short ** data, |
| 161 |
int rows, int cols) |
| 162 |
{ |
| 163 |
int i; |
| 164 |
int columnsize = cols*sizeof(unsigned short); |
| 165 |
|
| 166 |
cst_fwrite(fd, &rows, sizeof(int),1); |
| 167 |
|
| 168 |
for(i=0;i<rows;i++) |
| 169 |
cst_cg_write_array(fd, data[i], columnsize); |
| 170 |
} |
| 171 |
|
| 172 |
static void cst_cg_write_2d_array_float(cst_file fd, |
| 173 |
const float * const *data, |
| 174 |
int rows, int cols) |
| 175 |
{ |
| 176 |
int i; |
| 177 |
int columnsize = cols*sizeof(float); |
| 178 |
|
| 179 |
cst_fwrite(fd, &rows, sizeof(int),1); |
| 180 |
|
| 181 |
for(i=0;i<rows;i++) |
| 182 |
cst_cg_write_array(fd, data[i], columnsize); |
| 183 |
} |
| 184 |
|
| 185 |
static void cst_cg_write_2d_array_double(cst_file fd, |
| 186 |
const double *const *data, |
| 187 |
int rows, int cols) |
| 188 |
{ |
| 189 |
int i; |
| 190 |
int columnsize = cols*sizeof(double); |
| 191 |
|
| 192 |
cst_fwrite(fd, &rows, sizeof(int),1); |
| 193 |
|
| 194 |
for(i=0;i<rows;i++) |
| 195 |
cst_cg_write_array(fd, data[i], columnsize); |
| 196 |
} |
| 197 |
|
| 198 |
/* Write duration stats */ |
| 199 |
static void cst_cg_write_dur_stats(cst_file fd, const dur_stat* const * ds) |
| 200 |
{ |
| 201 |
int i; |
| 202 |
int numstats; |
| 203 |
const char* s; |
| 204 |
numstats = 0; |
| 205 |
while(ds[numstats]) |
| 206 |
numstats++; |
| 207 |
|
| 208 |
cst_fwrite(fd, &numstats, sizeof(int), 1); |
| 209 |
|
| 210 |
/* Write the string resources */ |
| 211 |
for(i=0;i<numstats;i++) |
| 212 |
{ |
| 213 |
cst_fwrite(fd, &ds[i]->mean, sizeof(float), 1); |
| 214 |
cst_fwrite(fd, &ds[i]->stddev, sizeof(float), 1); |
| 215 |
s = ds[i]->phone; |
| 216 |
cst_cg_write_padded(fd, s, strlen(s)+1); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/* Write phone-states mapping */ |
| 221 |
static void cst_cg_write_phone_states(cst_file fd, const char* const * const * ps) |
| 222 |
{ |
| 223 |
int i,j; |
| 224 |
int count; |
| 225 |
int count2; |
| 226 |
|
| 227 |
count=0; |
| 228 |
while(ps[count]) count++; |
| 229 |
cst_fwrite(fd, &count, sizeof(int),1); |
| 230 |
|
| 231 |
for(i=0;i<count;i++) |
| 232 |
{ |
| 233 |
count2=0; |
| 234 |
while(ps[i][count2]) count2++; |
| 235 |
cst_fwrite(fd, &count2, sizeof(int),1); |
| 236 |
|
| 237 |
for(j=0;j<count2;j++) |
| 238 |
cst_cg_write_padded(fd, ps[i][j],strlen(ps[i][j])+1); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
/* Write a feature that will get loaded into the voice */ |
| 243 |
static void cst_cg_write_voice_feature(cst_file fd, |
| 244 |
const char* fname, const char* fval) |
| 245 |
{ |
| 246 |
cst_cg_write_padded(fd, fname, cst_strlen(fname)+1); |
| 247 |
cst_cg_write_padded(fd, fval, cst_strlen(fval)+1); |
| 248 |
} |
| 249 |
|
| 250 |
static int cst_actual_num_channels(const cst_cg_db *db, int pm) |
| 251 |
{ |
| 252 |
/* This function should really be part of the commpression/uncompresssion |
| 253 |
code */ |
| 254 |
if (db->model_shape == CST_CG_MODEL_SHAPE_QUANTIZED_PARAMS) |
| 255 |
return db->num_channels[pm]/2; |
| 256 |
else if (db->model_shape == CST_CG_MODEL_SHAPE_QUANTIZED_PARAMS_41) |
| 257 |
return 41; |
| 258 |
else |
| 259 |
return db->num_channels[pm]; |
| 260 |
} |
| 261 |
|
| 262 |
int cst_cg_dump_voice(const cst_voice *v,const cst_string *filename) |
| 263 |
{ |
| 264 |
cst_file fd; |
| 265 |
const cst_cg_db* db; |
| 266 |
int dm, pm; |
| 267 |
|
| 268 |
if ((fd = cst_fopen(filename,CST_OPEN_WRITE|CST_OPEN_BINARY)) == NULL) |
| 269 |
return 0; |
| 270 |
|
| 271 |
if (!feat_present(v->features,"cg_db")) |
| 272 |
return 0; /* not a CG voice */ |
| 273 |
|
| 274 |
db = val_cg_db(feat_val(v->features,"cg_db")); |
| 275 |
|
| 276 |
cst_cg_write_header(fd); |
| 277 |
|
| 278 |
cst_cg_write_voice_feature(fd, "language", |
| 279 |
get_param_string(v->features,"language","eng")); |
| 280 |
if (get_param_string(v->features,"secondary_languages",NULL)) |
| 281 |
cst_cg_write_voice_feature(fd, "secondary_languages", |
| 282 |
get_param_string(v->features,"secondary_languages","")); |
| 283 |
cst_cg_write_voice_feature(fd, "country", |
| 284 |
get_param_string(v->features,"country","USA")); |
| 285 |
cst_cg_write_voice_feature(fd, "variant", |
| 286 |
get_param_string(v->features,"variant","none")); |
| 287 |
cst_cg_write_voice_feature(fd, "age", |
| 288 |
get_param_string(v->features,"age","30")); |
| 289 |
cst_cg_write_voice_feature(fd, "gender", |
| 290 |
get_param_string(v->features,"gender","unknown")); |
| 291 |
cst_cg_write_voice_feature(fd, "build_date", |
| 292 |
get_param_string(v->features,"build_date","unknown")); |
| 293 |
cst_cg_write_voice_feature(fd, "description", |
| 294 |
get_param_string(v->features,"description","unknown")); |
| 295 |
if (get_param_string(v->features,"prompt_dur",NULL)) |
| 296 |
cst_cg_write_voice_feature(fd, "prompt_dur", |
| 297 |
get_param_string(v->features,"prompt_dur","unknown")); |
| 298 |
if (get_param_string(v->features,"english_data",NULL)) |
| 299 |
cst_cg_write_voice_feature(fd, "english_data", |
| 300 |
get_param_string(v->features,"english_data","unknown")); |
| 301 |
/* Sai Krishna 07 July 2017 */ |
| 302 |
/* Flag to use shared vs mapped eng phoneset, for bilingual voices */ |
| 303 |
if (get_param_string(v->features,"eng_shared",NULL)) |
| 304 |
cst_cg_write_voice_feature(fd, "eng_shared", |
| 305 |
get_param_string(v->features,"eng_shared","0")); |
| 306 |
|
| 307 |
/* These three must be saved as string features |
| 308 |
(though are interpreted as floats) */ |
| 309 |
if (get_param_string(v->features,"int_f0_target_mean",NULL)) |
| 310 |
cst_cg_write_voice_feature(fd, "int_f0_target_mean", |
| 311 |
get_param_string(v->features,"int_f0_target_mean",NULL)); |
| 312 |
if (get_param_string(v->features,"int_f0_target_stddev",NULL)) |
| 313 |
cst_cg_write_voice_feature(fd, "int_f0_target_stddev", |
| 314 |
get_param_string(v->features,"int_f0_target_stddev",NULL)); |
| 315 |
if (get_param_string(v->features,"duration_stretch",NULL)) |
| 316 |
cst_cg_write_voice_feature(fd, "duration_stretch", |
| 317 |
get_param_string(v->features,"duration_stretch",NULL)); |
| 318 |
|
| 319 |
cst_cg_write_voice_feature(fd, "copyright", |
| 320 |
get_param_string(v->features,"copyright","unknown")); |
| 321 |
|
| 322 |
/* These features define the number of models to be read */ |
| 323 |
cst_cg_write_voice_feature(fd,"num_dur_models", |
| 324 |
val_string(val_string_n(db->num_dur_models))); |
| 325 |
cst_cg_write_voice_feature(fd,"num_param_models", |
| 326 |
val_string(val_string_n(db->num_param_models))); |
| 327 |
cst_cg_write_voice_feature(fd,"model_shape", |
| 328 |
val_string(val_string_n(db->model_shape))); |
| 329 |
cst_cg_write_voice_feature(fd,"num_f0_models", |
| 330 |
val_string(val_string_n(db->num_f0_models))); |
| 331 |
|
| 332 |
cst_cg_write_voice_feature(fd, "end_of_features","end_of_features"); |
| 333 |
|
| 334 |
/* db name */ |
| 335 |
cst_cg_write_padded(fd, (void*)db->name, cst_strlen(db->name)+1); |
| 336 |
cst_cg_write_db_types(fd, db); |
| 337 |
|
| 338 |
cst_fwrite(fd,&db->num_types,sizeof(int),1); |
| 339 |
cst_fwrite(fd,&db->sample_rate,sizeof(int),1); |
| 340 |
cst_fwrite(fd,&db->f0_mean,sizeof(float),1); |
| 341 |
cst_fwrite(fd,&db->f0_stddev,sizeof(float),1); |
| 342 |
|
| 343 |
for (pm=0; pm<db->num_f0_models; pm++) |
| 344 |
cst_cg_write_tree_array(fd,db->f0_trees[pm]); |
| 345 |
|
| 346 |
for (pm=0; pm<db->num_param_models; pm++) |
| 347 |
cst_cg_write_tree_array(fd,db->param_trees[pm]); |
| 348 |
|
| 349 |
cst_fwrite(fd,&db->spamf0,sizeof(int),1); |
| 350 |
if (db->spamf0) |
| 351 |
{ |
| 352 |
cst_cg_write_tree(fd,db->spamf0_accent_tree); |
| 353 |
cst_cg_write_tree(fd,db->spamf0_phrase_tree); |
| 354 |
} |
| 355 |
|
| 356 |
for (pm=0; pm<db->num_param_models; pm++) |
| 357 |
{ |
| 358 |
cst_fwrite(fd,&db->num_channels[pm],sizeof(int),1); |
| 359 |
cst_fwrite(fd,&db->num_frames[pm],sizeof(int),1); |
| 360 |
cst_cg_write_2d_array_short(fd, db->model_vectors[pm], |
| 361 |
db->num_frames[pm], |
| 362 |
cst_actual_num_channels(db,pm)); |
| 363 |
} |
| 364 |
|
| 365 |
if (db->spamf0) |
| 366 |
{ |
| 367 |
cst_fwrite(fd,&db->num_channels_spamf0_accent,sizeof(int),1); |
| 368 |
cst_fwrite(fd,&db->num_frames_spamf0_accent,sizeof(int),1); |
| 369 |
cst_cg_write_2d_array_float(fd, db->spamf0_accent_vectors, |
| 370 |
db->num_frames_spamf0_accent, |
| 371 |
db->num_channels_spamf0_accent); |
| 372 |
} |
| 373 |
|
| 374 |
cst_cg_write_array(fd, db->model_min, sizeof(float)*db->num_channels[0]); |
| 375 |
cst_cg_write_array(fd, db->model_range, sizeof(float)*db->num_channels[0]); |
| 376 |
|
| 377 |
if (db->model_shape > CST_CG_MODEL_SHAPE_BASE_MINRANGE) |
| 378 |
{ /* dump the qtable if shape > 1 */ |
| 379 |
for (pm=0; pm<db->num_param_models; pm++) |
| 380 |
{ |
| 381 |
cst_cg_write_2d_array_float(fd, db->qtable[pm], |
| 382 |
db->num_channels[pm], /* "frames" */ |
| 383 |
256); |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
cst_fwrite(fd,&db->frame_advance,sizeof(float),1); |
| 388 |
|
| 389 |
for (dm=0; dm<db->num_dur_models; dm++) |
| 390 |
{ |
| 391 |
cst_cg_write_dur_stats(fd, db->dur_stats[dm]); |
| 392 |
cst_cg_write_tree(fd, db->dur_cart[dm]); |
| 393 |
} |
| 394 |
|
| 395 |
cst_cg_write_phone_states(fd, db->phone_states); |
| 396 |
|
| 397 |
cst_fwrite(fd,&db->do_mlpg,sizeof(int),1); |
| 398 |
cst_cg_write_array(fd, db->dynwin, db->dynwinsize*sizeof(float)); |
| 399 |
cst_fwrite(fd,&db->dynwinsize,sizeof(int),1); |
| 400 |
|
| 401 |
cst_fwrite(fd,&db->mlsa_alpha,sizeof(float),1); |
| 402 |
cst_fwrite(fd,&db->mlsa_beta,sizeof(float),1); |
| 403 |
|
| 404 |
cst_fwrite(fd,&db->multimodel,sizeof(int),1); |
| 405 |
cst_fwrite(fd,&db->mixed_excitation,sizeof(int),1); |
| 406 |
|
| 407 |
cst_fwrite(fd,&db->ME_num,sizeof(int),1); |
| 408 |
cst_fwrite(fd,&db->ME_order,sizeof(int),1); |
| 409 |
cst_cg_write_2d_array_double(fd, db->me_h, |
| 410 |
db->ME_num, db->ME_order); |
| 411 |
|
| 412 |
cst_fwrite(fd,&db->spamf0,sizeof(int),1); |
| 413 |
cst_fwrite(fd,&db->gain,sizeof(float),1); |
| 414 |
|
| 415 |
cst_fclose(fd); |
| 416 |
return 1; |
| 417 |
} |