| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2007-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 |
/* Authors: Alan W Black ([email protected]) */ |
| 34 |
/* Date: November 2007 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Implementation of Clustergen, Statistical Parameter Synthesizer in */ |
| 38 |
/* Flite */ |
| 39 |
/* */ |
| 40 |
/* A statistical corpus based synthesizer. */ |
| 41 |
/* See Black, A. (2006), CLUSTERGEN: A Statistical Parametric */ |
| 42 |
/* Synthesizer using Trajectory Modeling", Interspeech 2006 - ICSLP, */ |
| 43 |
/* Pittsburgh, PA. */ |
| 44 |
/* http://www.cs.cmu.edu/~awb/papers/is2006/IS061394.PDF */ |
| 45 |
/* */ |
| 46 |
/* Uses MLSA for resynthesis and MLPG for smoothing */ |
| 47 |
/* mlsa and mlpg come from Festvox's VC code (which came in turn */ |
| 48 |
/* came from NITECH's HTS */ |
| 49 |
/* */ |
| 50 |
/*************************************************************************/ |
| 51 |
|
| 52 |
#include "cst_cg.h" |
| 53 |
#include "cst_spamf0.h" |
| 54 |
#include "cst_hrg.h" |
| 55 |
#include "cst_utt_utils.h" |
| 56 |
#include "cst_audio.h" |
| 57 |
|
| 58 |
CST_VAL_REGISTER_TYPE(cg_db,cst_cg_db) |
| 59 |
|
| 60 |
static cst_utterance *cg_make_hmmstates(cst_utterance *utt); |
| 61 |
static cst_utterance *cg_make_params(cst_utterance *utt); |
| 62 |
static cst_utterance *cg_predict_params(cst_utterance *utt); |
| 63 |
static cst_utterance *cg_resynth(cst_utterance *utt); |
| 64 |
|
| 65 |
void delete_cg_db(cst_cg_db *db) |
| 66 |
{ |
| 67 |
int i,j; |
| 68 |
|
| 69 |
if (db->freeable == 0) |
| 70 |
return; /* its in the data segment, so not freeable */ |
| 71 |
|
| 72 |
/* Woo Hoo! We're gonna free this garbage with a big mallet */ |
| 73 |
/* In spite of what the const qualifiers say ... */ |
| 74 |
cst_free((void *)db->name); |
| 75 |
|
| 76 |
for (i=0; db->types && db->types[i]; i++) |
| 77 |
cst_free((void *)db->types[i]); |
| 78 |
cst_free((void *)db->types); |
| 79 |
|
| 80 |
for (j=0; j<db->num_f0_models; j++) |
| 81 |
{ |
| 82 |
for (i=0; db->f0_trees[j] && db->f0_trees[j][i]; i++) |
| 83 |
delete_cart((cst_cart *)(void *)db->f0_trees[j][i]); |
| 84 |
cst_free((void *)db->f0_trees[j]); |
| 85 |
} |
| 86 |
cst_free((void *)db->f0_trees); |
| 87 |
|
| 88 |
for (j=0; j<db->num_param_models; j++) |
| 89 |
{ |
| 90 |
for (i=0; db->param_trees[j] && db->param_trees[j][i]; i++) |
| 91 |
delete_cart((cst_cart *)(void *)db->param_trees[j][i]); |
| 92 |
cst_free((void *)db->param_trees[j]); |
| 93 |
} |
| 94 |
cst_free((void *)db->param_trees); |
| 95 |
|
| 96 |
if (db->spamf0) |
| 97 |
{ |
| 98 |
delete_cart((cst_cart *)(void *)db->spamf0_accent_tree); |
| 99 |
delete_cart((cst_cart *)(void *)db->spamf0_phrase_tree); |
| 100 |
for (i=0; i< db->num_frames_spamf0_accent; i++) |
| 101 |
cst_free((void *)db->spamf0_accent_vectors[i]); |
| 102 |
cst_free((void *)db->spamf0_accent_vectors); |
| 103 |
} |
| 104 |
|
| 105 |
for (j=0; j<db->num_param_models; j++) |
| 106 |
{ |
| 107 |
for (i=0; i<db->num_frames[j]; i++) |
| 108 |
cst_free((void *)db->model_vectors[j][i]); |
| 109 |
cst_free((void *)db->model_vectors[j]); |
| 110 |
} |
| 111 |
|
| 112 |
cst_free((void *)db->model_min); |
| 113 |
cst_free((void *)db->model_range); |
| 114 |
|
| 115 |
if (db->model_shape != CST_CG_MODEL_SHAPE_BASE_MINRANGE) |
| 116 |
{ |
| 117 |
for (j = 0; j<db->num_param_models; j++) |
| 118 |
{ |
| 119 |
for (i=0; i<db->num_channels[j]; i++) |
| 120 |
cst_free((void *)db->qtable[j][i]); |
| 121 |
cst_free((void *)db->qtable[j]); |
| 122 |
} |
| 123 |
} |
| 124 |
cst_free((void *)db->qtable); |
| 125 |
|
| 126 |
/* Moved to here so they can be used for the model_shape freeing */ |
| 127 |
cst_free(db->num_channels); |
| 128 |
cst_free(db->num_frames); |
| 129 |
cst_free((void *)db->model_vectors); |
| 130 |
|
| 131 |
for (j = 0; j<db->num_dur_models; j++) |
| 132 |
{ |
| 133 |
for (i=0; db->dur_stats[j] && db->dur_stats[j][i]; i++) |
| 134 |
{ |
| 135 |
cst_free((void *)db->dur_stats[j][i]->phone); |
| 136 |
cst_free((void *)db->dur_stats[j][i]); |
| 137 |
} |
| 138 |
cst_free((void *)db->dur_stats[j]); |
| 139 |
delete_cart((cst_cart *)(void *)db->dur_cart[j]); |
| 140 |
} |
| 141 |
cst_free((void *)db->dur_stats); |
| 142 |
cst_free((void *)db->dur_cart); |
| 143 |
|
| 144 |
for (i=0; db->phone_states && db->phone_states[i]; i++) |
| 145 |
{ |
| 146 |
for (j=0; db->phone_states[i][j]; j++) |
| 147 |
cst_free((void *)db->phone_states[i][j]); |
| 148 |
cst_free((void *)db->phone_states[i]); |
| 149 |
} |
| 150 |
cst_free((void *)db->phone_states); |
| 151 |
|
| 152 |
cst_free((void *)db->dynwin); |
| 153 |
|
| 154 |
for (i=0; i<db->ME_num; i++) |
| 155 |
cst_free((void *)db->me_h[i]); |
| 156 |
cst_free((void *)db->me_h); |
| 157 |
|
| 158 |
cst_free((void *)db); |
| 159 |
} |
| 160 |
|
| 161 |
/* */ |
| 162 |
cst_utterance *cg_synth(cst_utterance *utt) |
| 163 |
{ |
| 164 |
cst_cg_db *cg_db; |
| 165 |
cg_db = val_cg_db(utt_feat_val(utt,"cg_db")); |
| 166 |
|
| 167 |
cg_make_hmmstates(utt); |
| 168 |
cg_make_params(utt); |
| 169 |
cg_predict_params(utt); |
| 170 |
if (cg_db->spamf0) |
| 171 |
{ |
| 172 |
cst_spamf0(utt); |
| 173 |
} |
| 174 |
cg_resynth(utt); |
| 175 |
|
| 176 |
return utt; |
| 177 |
} |
| 178 |
|
| 179 |
static float cg_state_duration(cst_item *s, cst_cg_db *cg_db) |
| 180 |
{ |
| 181 |
float zdur, dur; |
| 182 |
const char *n; |
| 183 |
int i, x, dm; |
| 184 |
|
| 185 |
for (dm=0,zdur=0.0; dm < cg_db->num_dur_models; dm++) |
| 186 |
zdur += val_float(cart_interpret(s,cg_db->dur_cart[dm])); |
| 187 |
zdur /= dm; /* get average zdur prediction from all dur models */ |
| 188 |
n = item_feat_string(s,"name"); |
| 189 |
|
| 190 |
/* Note we only use the dur stats from the first model, that is */ |
| 191 |
/* correct, but wouldn't be if the dur tree was trained on different */ |
| 192 |
/* data */ |
| 193 |
for (x=i=0; cg_db->dur_stats[0][i]; i++) |
| 194 |
{ |
| 195 |
if (cst_streq(cg_db->dur_stats[0][i]->phone,n)) |
| 196 |
{ |
| 197 |
x=i; |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
if (!cg_db->dur_stats[0][i]) /* unknown type name */ |
| 202 |
x = 0; |
| 203 |
|
| 204 |
dur = (zdur*cg_db->dur_stats[0][x]->stddev)+cg_db->dur_stats[0][x]->mean; |
| 205 |
|
| 206 |
/* dur = 1.2 * (float)exp((float)dur); */ |
| 207 |
|
| 208 |
return dur; |
| 209 |
} |
| 210 |
|
| 211 |
static cst_utterance *cg_make_hmmstates(cst_utterance *utt) |
| 212 |
{ |
| 213 |
/* Build HMM state structure below the segment structure */ |
| 214 |
cst_cg_db *cg_db; |
| 215 |
cst_relation *hmmstate, *segstate; |
| 216 |
cst_item *seg, *s, *ss; |
| 217 |
const char *segname; |
| 218 |
int sp,p; |
| 219 |
|
| 220 |
cg_db = val_cg_db(utt_feat_val(utt,"cg_db")); |
| 221 |
hmmstate = utt_relation_create(utt,"HMMstate"); |
| 222 |
segstate = utt_relation_create(utt,"segstate"); |
| 223 |
|
| 224 |
for (seg = utt_rel_head(utt,"Segment"); seg; seg=item_next(seg)) |
| 225 |
{ |
| 226 |
ss = relation_append(segstate,seg); |
| 227 |
segname = item_feat_string(seg,"name"); |
| 228 |
for (p=0; cg_db->phone_states[p]; p++) |
| 229 |
if (cst_streq(segname,cg_db->phone_states[p][0])) |
| 230 |
break; |
| 231 |
if (cg_db->phone_states[p] == NULL) |
| 232 |
p = 0; /* unknown phoneme */ |
| 233 |
for (sp=1; cg_db->phone_states[p][sp]; sp++) |
| 234 |
{ |
| 235 |
s = relation_append(hmmstate,NULL); |
| 236 |
item_add_daughter(ss,s); |
| 237 |
item_set_string(s,"name",cg_db->phone_states[p][sp]); |
| 238 |
item_set_int(s,"statepos",sp); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return utt; |
| 243 |
} |
| 244 |
|
| 245 |
static cst_utterance *cg_make_params(cst_utterance *utt) |
| 246 |
{ |
| 247 |
/* puts in the frame items */ |
| 248 |
/* historically called "mcep" but can actually be any random vectors */ |
| 249 |
cst_cg_db *cg_db; |
| 250 |
cst_relation *mcep, *mcep_link; |
| 251 |
cst_item *s, *mcep_parent, *mcep_frame; |
| 252 |
int num_frames; |
| 253 |
float start, end; |
| 254 |
float dur_stretch, tok_stretch, rdur; |
| 255 |
|
| 256 |
cg_db = val_cg_db(utt_feat_val(utt,"cg_db")); |
| 257 |
mcep = utt_relation_create(utt,"mcep"); |
| 258 |
mcep_link = utt_relation_create(utt,"mcep_link"); |
| 259 |
end = 0.0; |
| 260 |
num_frames = 0; |
| 261 |
dur_stretch = get_param_float(utt->features,"duration_stretch", 1.0); |
| 262 |
|
| 263 |
for (s = utt_rel_head(utt,"HMMstate"); s; s=item_next(s)) |
| 264 |
{ |
| 265 |
start = end; |
| 266 |
tok_stretch = ffeature_float(s,"R:segstate.parent.R:SylStructure.parent.parent.R:Token.parent.local_duration_stretch"); |
| 267 |
if (tok_stretch == 0) |
| 268 |
tok_stretch = 1.0; |
| 269 |
rdur = tok_stretch*dur_stretch*cg_state_duration(s,cg_db); |
| 270 |
/* Guarantee duration to be alt least one frame */ |
| 271 |
if (rdur < cg_db->frame_advance) |
| 272 |
end = start + cg_db->frame_advance; |
| 273 |
else |
| 274 |
end = start + rdur; |
| 275 |
item_set_float(s,"end",end); |
| 276 |
mcep_parent = relation_append(mcep_link, s); |
| 277 |
for ( ; (num_frames * cg_db->frame_advance) <= end; num_frames++ ) |
| 278 |
{ |
| 279 |
mcep_frame = relation_append(mcep,NULL); |
| 280 |
item_add_daughter(mcep_parent,mcep_frame); |
| 281 |
item_set_int(mcep_frame,"frame_number",num_frames); |
| 282 |
item_set(mcep_frame,"name",item_feat(mcep_parent,"name")); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
/* Copy duration up onto Segment relation */ |
| 287 |
for (s = utt_rel_head(utt,"Segment"); s; s=item_next(s)) |
| 288 |
item_set(s,"end",ffeature(s,"R:segstate.daughtern.end")); |
| 289 |
|
| 290 |
utt_set_feat_int(utt,"param_track_num_frames",num_frames); |
| 291 |
|
| 292 |
return utt; |
| 293 |
} |
| 294 |
|
| 295 |
#if CG_OLD |
| 296 |
static int voiced_frame(cst_item *m) |
| 297 |
{ |
| 298 |
const char *ph_vc; |
| 299 |
const char *ph_cvox; |
| 300 |
|
| 301 |
ph_vc = ffeature_string(m,"R:mcep_link.parent.R:segstate.parent.ph_vc"); |
| 302 |
ph_cvox = ffeature_string(m,"R:mcep_link.parent.R:segstate.parent.ph_cvox"); |
| 303 |
|
| 304 |
if (cst_streq("-",ph_vc) && |
| 305 |
cst_streq("-",ph_cvox)) |
| 306 |
return 0; /* unvoiced */ |
| 307 |
else |
| 308 |
return 1; /* voiced */ |
| 309 |
} |
| 310 |
#endif |
| 311 |
|
| 312 |
static int voiced_frame(cst_item *m) |
| 313 |
{ |
| 314 |
const char *ph_vc; |
| 315 |
const char *ph_name; |
| 316 |
|
| 317 |
ph_vc = ffeature_string(m,"R:mcep_link.parent.R:segstate.parent.ph_vc"); |
| 318 |
ph_name = ffeature_string(m,"R:mcep_link.parent.R:segstate.parent.name"); |
| 319 |
|
| 320 |
if (cst_streq(ph_name,"pau")) |
| 321 |
return 0; /* unvoiced */ |
| 322 |
else if (cst_streq("+",ph_vc)) |
| 323 |
return 1; /* voiced */ |
| 324 |
else if (item_feat_float(m,"voicing") > 0.5) |
| 325 |
/* Even though the range is 0-10, I *do* mean 0.5 */ |
| 326 |
return 1; /* voiced */ |
| 327 |
else |
| 328 |
return 0; /* unvoiced */ |
| 329 |
} |
| 330 |
|
| 331 |
static float catmull_rom_spline(float p,float p0,float p1,float p2,float p3) |
| 332 |
/* http://www.mvps.org/directx/articles/ */ |
| 333 |
{ |
| 334 |
float q; |
| 335 |
|
| 336 |
q = ( 0.5 * |
| 337 |
( ( 2.0 * p1 ) + |
| 338 |
( p * (-p0 + p2) ) + |
| 339 |
( (p*p) * (((2.0 * p0) - (5.0 * p1)) + |
| 340 |
((4.0 * p2) - p3))) + |
| 341 |
( (p*p*p) * (-p0 + |
| 342 |
((3.0 * p1) - (3.0 * p2)) + |
| 343 |
p3)))); |
| 344 |
/* (set! q (* 0.5 (+ (* 2 p1) |
| 345 |
(* (+ (* -1 p0) p2) p) |
| 346 |
(* (+ (- (* 2 p0) (* 5 p1)) (- (* 4 p2) p3)) (* p p)) |
| 347 |
(* (+ (* -1 p0) (- (* 3 p1) (* 3 p2)) p3) (* p p p))))) |
| 348 |
*/ |
| 349 |
return q; |
| 350 |
} |
| 351 |
|
| 352 |
static void cg_F0_interpolate_spline(cst_utterance *utt, |
| 353 |
cst_track *param_track) |
| 354 |
{ |
| 355 |
float start_f0, mid_f0, end_f0; |
| 356 |
int start_index, end_index, mid_index; |
| 357 |
int nsi, nei, nmi; /* next syllable indices */ |
| 358 |
float nmid_f0, pmid_f0; |
| 359 |
cst_item *syl; |
| 360 |
int i; |
| 361 |
float m; |
| 362 |
|
| 363 |
start_f0 = mid_f0 = end_f0 = -1.0; |
| 364 |
|
| 365 |
for (syl=utt_rel_head(utt,"Syllable"); syl; syl=item_next(syl)) |
| 366 |
{ |
| 367 |
start_index = ffeature_int(syl,"R:SylStructure.daughter1.R:segstate.daughter1.R:mcep_link.daughter1.frame_number"); |
| 368 |
end_index = ffeature_int(syl,"R:SylStructure.daughtern.R:segstate.daughtern.R:mcep_link.daughtern.frame_number"); |
| 369 |
mid_index = (int)((start_index + end_index)/2.0); |
| 370 |
if (end_index <= start_index) |
| 371 |
continue; |
| 372 |
|
| 373 |
start_f0 = param_track->frames[start_index][0]; |
| 374 |
if (end_f0 > 0.0) |
| 375 |
start_f0 = end_f0; /* not first time through */ |
| 376 |
if (mid_f0 < 0.0) |
| 377 |
pmid_f0 = start_f0; /* first time through */ |
| 378 |
else |
| 379 |
pmid_f0 = mid_f0; |
| 380 |
mid_f0 = param_track->frames[mid_index][0]; |
| 381 |
if (item_next(syl)) /* not last syllable */ |
| 382 |
end_f0 = (param_track->frames[end_index-1][0]+ |
| 383 |
param_track->frames[end_index][0])/2.0; |
| 384 |
else |
| 385 |
end_f0 = param_track->frames[end_index-1][0]; |
| 386 |
nmid_f0=end_f0; /* in case there is no next syl */ |
| 387 |
|
| 388 |
if (item_next(syl)) |
| 389 |
{ |
| 390 |
nsi = ffeature_int(syl,"n.R:SylStructure.daughter1.R:segstate.daughter1.R:mcep_link.daughter1.frame_number"); |
| 391 |
nei = ffeature_int(syl,"n.R:SylStructure.daughtern.R:segstate.daughtern.R:mcep_link.daughtern.frame_number"); |
| 392 |
nmi = (int)((nsi + nei)/2.0); |
| 393 |
nmid_f0 = param_track->frames[nmi][0]; |
| 394 |
} |
| 395 |
/* start to mid syl */ |
| 396 |
m = 1.0 / (mid_index - start_index); |
| 397 |
for (i=0; ((start_index+i)<mid_index); i++) |
| 398 |
param_track->frames[start_index+i][0] = |
| 399 |
catmull_rom_spline(i*m,pmid_f0,start_f0,mid_f0,end_f0); |
| 400 |
|
| 401 |
/* mid syl to end */ |
| 402 |
m = 1.0 / (end_index - mid_index); |
| 403 |
for (i=0; ((mid_index+i)<end_index); i++) |
| 404 |
param_track->frames[mid_index+i][0] = |
| 405 |
catmull_rom_spline(i*m,start_f0,mid_f0,end_f0,nmid_f0); |
| 406 |
} |
| 407 |
|
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
#if 0 |
| 412 |
static void cg_smooth_F0_naive(cst_track *param_track) |
| 413 |
{ |
| 414 |
float l,s; |
| 415 |
int i,c; |
| 416 |
|
| 417 |
l = 0.0; |
| 418 |
for (i=0; i<param_track->num_frames-1; i++) |
| 419 |
{ |
| 420 |
c = 0; s = 0; |
| 421 |
if (l > 0.0) |
| 422 |
{ |
| 423 |
c++; s+=l; |
| 424 |
} |
| 425 |
if (param_track->frames[i+1][0] > 0.0) |
| 426 |
{ |
| 427 |
c++; s+=param_track->frames[i+1][0]; |
| 428 |
} |
| 429 |
l = param_track->frames[i][0]; |
| 430 |
if (param_track->frames[i][0] > 0.0) |
| 431 |
{ |
| 432 |
c++; s+=param_track->frames[i][0]; |
| 433 |
param_track->frames[i][0] = s/c; |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
return; |
| 438 |
} |
| 439 |
#endif |
| 440 |
|
| 441 |
static void cg_smooth_F0(cst_utterance *utt, |
| 442 |
cst_cg_db *cg_db, |
| 443 |
cst_track *param_track) |
| 444 |
{ |
| 445 |
/* Smooth F0 and mark unvoice frames as 0.0 */ |
| 446 |
cst_item *mcep; |
| 447 |
int i; |
| 448 |
float mean, stddev; |
| 449 |
|
| 450 |
/* cg_smooth_F0_naive(param_track); */ |
| 451 |
|
| 452 |
cg_F0_interpolate_spline(utt,param_track); |
| 453 |
|
| 454 |
mean = get_param_float(utt->features,"int_f0_target_mean", cg_db->f0_mean); |
| 455 |
mean *= get_param_float(utt->features,"f0_shift", 1.0); |
| 456 |
stddev = |
| 457 |
get_param_float(utt->features,"int_f0_target_stddev", cg_db->f0_stddev); |
| 458 |
#if 0 |
| 459 |
FILE *ftt; int ii; |
| 460 |
ftt = cst_fopen("awb.f0",CST_OPEN_WRITE); |
| 461 |
printf("awb_debug saving F0\n"); |
| 462 |
for (ii=0; ii<param_track->num_frames; ii++) |
| 463 |
cst_fprintf(ftt,"%f %f\n",param_track->frames[ii][0], |
| 464 |
param_track->frames[ii][param_track->num_channels-2]); |
| 465 |
cst_fclose(ftt); |
| 466 |
#endif |
| 467 |
|
| 468 |
for (i=0,mcep=utt_rel_head(utt,"mcep"); mcep; i++,mcep=item_next(mcep)) |
| 469 |
{ |
| 470 |
if (voiced_frame(mcep)) |
| 471 |
{ |
| 472 |
/* scale the F0 -- which normally wont change it at all */ |
| 473 |
param_track->frames[i][0] = |
| 474 |
(((param_track->frames[i][0]-cg_db->f0_mean)/cg_db->f0_stddev) |
| 475 |
*stddev)+mean; |
| 476 |
/* Some safety checks */ |
| 477 |
if (param_track->frames[i][0] < 50) |
| 478 |
param_track->frames[i][0] = 50; |
| 479 |
if (param_track->frames[i][0] > 700) |
| 480 |
param_track->frames[i][0] = 700; |
| 481 |
} |
| 482 |
else /* Unvoice it */ |
| 483 |
param_track->frames[i][0] = 0.0; |
| 484 |
} |
| 485 |
|
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
static int unpack_model_vector(cst_cg_db *cg_db,int pm,int f,float *v) |
| 490 |
{ |
| 491 |
/* This unpacked the potentially compressed/quantized data from the model */ |
| 492 |
int i,j; |
| 493 |
|
| 494 |
if (cg_db->model_shape == CST_CG_MODEL_SHAPE_QUANTIZED_PARAMS) |
| 495 |
{ |
| 496 |
for (i=0; i<cg_db->num_channels[pm]/2; i++) |
| 497 |
{ |
| 498 |
v[i*2] = cg_db->qtable[pm][i*2][cg_db->model_vectors[pm][f][i]/256]; |
| 499 |
v[(i*2)+1] = |
| 500 |
cg_db->qtable[pm][(i*2)+1][cg_db->model_vectors[pm][f][i]%256]; |
| 501 |
} |
| 502 |
#if 0 |
| 503 |
printf("awb_debug %d\n",f); |
| 504 |
for (i=0; i<cg_db->num_channels[pm]; i++) |
| 505 |
printf("%f ",v[i]); |
| 506 |
printf("\n"); |
| 507 |
for (i=0; i<cg_db->num_channels[pm]/2; i++) |
| 508 |
printf("%d %d ",cg_db->model_vectors[pm][f][i]/256, |
| 509 |
cg_db->model_vectors[pm][f][i]%256); |
| 510 |
printf("\n"); |
| 511 |
#endif |
| 512 |
return 0; |
| 513 |
} |
| 514 |
if (cg_db->model_shape == CST_CG_MODEL_SHAPE_QUANTIZED_PARAMS_41) |
| 515 |
{ |
| 516 |
j=1; /* skip F0 mean/stddev */ |
| 517 |
for (i=0; i<25; i++,j++) /* mcep static mean/stddev */ |
| 518 |
{ |
| 519 |
v[j*2] = cg_db->qtable[pm][j*2][cg_db->model_vectors[pm][f][i]/256]; |
| 520 |
v[(j*2)+1] = |
| 521 |
cg_db->qtable[pm][(j*2)+1][cg_db->model_vectors[pm][f][i]%256]; |
| 522 |
} |
| 523 |
for (i=25; i<25+12; i+=1,j+=2) /* mcep deltas no mean/stddev */ |
| 524 |
{ |
| 525 |
v[(j*2)+1] = cg_db->qtable[pm][(j*2)+1][cg_db->model_vectors[pm][f][i]/256]; |
| 526 |
v[(j*2)+3] = |
| 527 |
cg_db->qtable[pm][(j*2)+3][cg_db->model_vectors[pm][f][i]%256]; |
| 528 |
} |
| 529 |
/* one delta, one me */ |
| 530 |
v[(j*2)+1] = cg_db->qtable[pm][(j*2)+1][cg_db->model_vectors[pm][f][i]/256]; |
| 531 |
v[(j*2)+2] = cg_db->qtable[pm][(j*2)+2][cg_db->model_vectors[pm][f][i]%256]; |
| 532 |
i++; j+=2; |
| 533 |
/* one me, another me */ |
| 534 |
v[(j*2)] = cg_db->qtable[pm][j*2][cg_db->model_vectors[pm][f][i]/256]; |
| 535 |
v[(j*2)+2] = cg_db->qtable[pm][(j*2)+2][cg_db->model_vectors[pm][f][i]%256]; |
| 536 |
i++; j+=2; |
| 537 |
/* one me, another me */ |
| 538 |
v[(j*2)] = cg_db->qtable[pm][j*2][cg_db->model_vectors[pm][f][i]/256]; |
| 539 |
v[(j*2)+2] = cg_db->qtable[pm][(j*2)+2][cg_db->model_vectors[pm][f][i]%256]; |
| 540 |
i++; j+=2; |
| 541 |
/* one voicing and another v-stddef */ |
| 542 |
v[(j*2)] = cg_db->qtable[pm][j*2][cg_db->model_vectors[pm][f][i]/256]; |
| 543 |
v[(j*2)+1] = cg_db->qtable[pm][(j*2)+1][cg_db->model_vectors[pm][f][i]%256]; |
| 544 |
#if 0 |
| 545 |
printf("awb_debug pm %d frame %d\n",pm,f); |
| 546 |
for (i=0; i<cg_db->num_channels[pm]; i++) |
| 547 |
printf("%f ",v[i]); |
| 548 |
printf("\n"); |
| 549 |
#endif |
| 550 |
return 0; |
| 551 |
} |
| 552 |
/* if (cg_db->model_shape == CST_CG_MODEL_SHAPE_BASE_MINRANGE) */ |
| 553 |
else /* let's always do this second one in case model_shape isn't set */ |
| 554 |
{ |
| 555 |
for (i=0; i<cg_db->num_channels[pm]; i++) |
| 556 |
{ |
| 557 |
v[i] = cg_db->model_min[i]+ |
| 558 |
((float)((cg_db->model_vectors[pm][f][i])/ |
| 559 |
65535.0)*cg_db->model_range[i]); |
| 560 |
} |
| 561 |
return 0; |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
static cst_utterance *cg_predict_params(cst_utterance *utt) |
| 566 |
{ |
| 567 |
cst_cg_db *cg_db; |
| 568 |
cst_track *param_track; |
| 569 |
cst_track *str_track = NULL; |
| 570 |
cst_item *mcep; |
| 571 |
const cst_cart *mcep_tree, *f0_tree; |
| 572 |
int i,j,f,p,o,pm; |
| 573 |
const char *mname; |
| 574 |
float *unpacked_vector; |
| 575 |
float f0_val, f0_bit; |
| 576 |
float local_gain, voicing; |
| 577 |
int fff; |
| 578 |
int extra_feats = 0; |
| 579 |
|
| 580 |
cg_db = val_cg_db(utt_feat_val(utt,"cg_db")); |
| 581 |
param_track = new_track(); |
| 582 |
if (cg_db->do_mlpg) /* which should be the default */ |
| 583 |
fff = 1; /* copy details with stddevs */ |
| 584 |
else |
| 585 |
fff = 2; /* copy details without stddevs */ |
| 586 |
|
| 587 |
extra_feats = 1; /* voicing */ |
| 588 |
if (cg_db->mixed_excitation) |
| 589 |
{ |
| 590 |
extra_feats += 5; |
| 591 |
str_track = new_track(); |
| 592 |
cst_track_resize(str_track, |
| 593 |
utt_feat_int(utt,"param_track_num_frames"), |
| 594 |
5); |
| 595 |
} |
| 596 |
|
| 597 |
cst_track_resize(param_track, |
| 598 |
utt_feat_int(utt,"param_track_num_frames"), |
| 599 |
(cg_db->num_channels[0]/fff)- |
| 600 |
(2 * extra_feats));/* no voicing or str */ |
| 601 |
unpacked_vector = cst_alloc(float,cg_db->num_channels[0]); |
| 602 |
f = 0; |
| 603 |
for (i=0,mcep=utt_rel_head(utt,"mcep"); mcep; i++,mcep=item_next(mcep)) |
| 604 |
{ |
| 605 |
mname = item_feat_string(mcep,"name"); |
| 606 |
local_gain = ffeature_float(mcep,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Token.parent.local_gain"); |
| 607 |
if (local_gain == 0.0) local_gain = 1.0; |
| 608 |
for (p=0; cg_db->types[p]; p++) |
| 609 |
if (cst_streq(mname,cg_db->types[p])) |
| 610 |
break; |
| 611 |
if (cg_db->types[p] == NULL) |
| 612 |
p=0; /* if there isn't a matching tree, use the first one */ |
| 613 |
|
| 614 |
/* Predict F0 */ |
| 615 |
for (f0_val=pm=0; pm<cg_db->num_f0_models; pm++) |
| 616 |
{ |
| 617 |
f0_tree = cg_db->f0_trees[pm][p]; |
| 618 |
f0_bit = val_float(cart_interpret(mcep,f0_tree)); |
| 619 |
f0_val += f0_bit; |
| 620 |
} |
| 621 |
param_track->frames[i][0] = f0_val/cg_db->num_f0_models; |
| 622 |
if (param_track->frames[i][0] < 50.0) |
| 623 |
param_track->frames[i][0] = 0.0; |
| 624 |
/* what about stddev ? */ |
| 625 |
|
| 626 |
/* We only have multiple models now, but the default is one model */ |
| 627 |
/* Predict spectral coeffs */ |
| 628 |
voicing = 0.0; |
| 629 |
for (pm=0; pm<cg_db->num_param_models; pm++) |
| 630 |
{ |
| 631 |
mcep_tree = cg_db->param_trees[pm][p]; |
| 632 |
f = val_int(cart_interpret(mcep,mcep_tree)); |
| 633 |
/* If there is one model this will be fine, if there are */ |
| 634 |
/* multiple models this will be the nth model */ |
| 635 |
item_set_int(mcep,"clustergen_param_frame",f); |
| 636 |
|
| 637 |
/* Unpack the model[pm][f] vector */ |
| 638 |
unpack_model_vector(cg_db,pm,f,unpacked_vector); |
| 639 |
|
| 640 |
/* Old code used to average in param[0] with F0 too (???) */ |
| 641 |
|
| 642 |
for (j=2; j<param_track->num_channels; j++) |
| 643 |
{ |
| 644 |
if (pm == 0) param_track->frames[i][j] = 0.0; |
| 645 |
param_track->frames[i][j] += unpacked_vector[j*fff]/ |
| 646 |
|
| 647 |
(float)cg_db->num_param_models; |
| 648 |
} |
| 649 |
|
| 650 |
if (cg_db->mixed_excitation) |
| 651 |
{ |
| 652 |
o = j; |
| 653 |
for (j=0; j<5; j++) |
| 654 |
{ |
| 655 |
if (pm == 0) str_track->frames[i][j] = 0.0; |
| 656 |
str_track->frames[i][j] += |
| 657 |
unpacked_vector[(o+(2*j))*fff] / |
| 658 |
(float)cg_db->num_param_models; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
/* last coefficient is average voicing for cluster */ |
| 663 |
voicing /= (float)(pm+1); |
| 664 |
voicing += unpacked_vector[cg_db->num_channels[pm]-2] / |
| 665 |
(float)(pm+1); |
| 666 |
} |
| 667 |
item_set_float(mcep,"voicing",voicing); |
| 668 |
/* Apply local gain to c0 */ |
| 669 |
param_track->frames[i][2] *= local_gain; |
| 670 |
|
| 671 |
param_track->times[i] = i * cg_db->frame_advance; |
| 672 |
} |
| 673 |
|
| 674 |
cst_free(unpacked_vector); |
| 675 |
cg_smooth_F0(utt,cg_db,param_track); |
| 676 |
|
| 677 |
utt_set_feat(utt,"param_track",track_val(param_track)); |
| 678 |
if (cg_db->mixed_excitation) |
| 679 |
utt_set_feat(utt,"str_track",track_val(str_track)); |
| 680 |
|
| 681 |
return utt; |
| 682 |
} |
| 683 |
|
| 684 |
static cst_utterance *cg_resynth(cst_utterance *utt) |
| 685 |
{ |
| 686 |
cst_cg_db *cg_db; |
| 687 |
cst_wave *w; |
| 688 |
cst_track *param_track; |
| 689 |
cst_track *str_track = NULL; |
| 690 |
cst_track *smoothed_track; |
| 691 |
const cst_val *streaming_info_val; |
| 692 |
cst_audio_streaming_info *asi = NULL; |
| 693 |
int mlsa_speed_param = 0; |
| 694 |
|
| 695 |
streaming_info_val=get_param_val(utt->features,"streaming_info",NULL); |
| 696 |
if (streaming_info_val) |
| 697 |
{ |
| 698 |
asi = val_audio_streaming_info(streaming_info_val); |
| 699 |
asi->utt = utt; |
| 700 |
} |
| 701 |
/* Values 5-15 might be reasonably to speed things up. This number */ |
| 702 |
/* is used to reduce the number of parameters used in the mceps */ |
| 703 |
/* e.g. value 10 will speed up from 21.0 faster than real time */ |
| 704 |
/* to 26.4 times faster than real time (for builtin rms) */ |
| 705 |
mlsa_speed_param = get_param_int(utt->features,"mlsa_speed_param",0); |
| 706 |
|
| 707 |
cg_db = val_cg_db(utt_feat_val(utt,"cg_db")); |
| 708 |
param_track = val_track(utt_feat_val(utt,"param_track")); |
| 709 |
if (cg_db->mixed_excitation) |
| 710 |
str_track = val_track(utt_feat_val(utt,"str_track")); |
| 711 |
|
| 712 |
if (cg_db->do_mlpg) |
| 713 |
{ |
| 714 |
smoothed_track = mlpg(param_track, cg_db); |
| 715 |
w = mlsa_resynthesis(smoothed_track,str_track,cg_db, |
| 716 |
asi,mlsa_speed_param); |
| 717 |
delete_track(smoothed_track); |
| 718 |
} |
| 719 |
else |
| 720 |
w=mlsa_resynthesis(param_track,str_track,cg_db, |
| 721 |
asi,mlsa_speed_param); |
| 722 |
|
| 723 |
if (w == NULL) |
| 724 |
{ |
| 725 |
/* Synthesis Failed, probably because it was interrupted */ |
| 726 |
utt_set_feat_int(utt,"Interrupted",1); |
| 727 |
w = new_wave(); |
| 728 |
} |
| 729 |
|
| 730 |
#if 0 |
| 731 |
/* Apply local gain */ |
| 732 |
for (i=0,tok=utt_rel_head(utt,"Token"); tok; i++,tok=item_next(tok)) |
| 733 |
{ |
| 734 |
if (item_feat_present(tok,"local_gain")) |
| 735 |
local_gain = item_feat_float(tokget_param_fffeature_float(tok,"R:mcep_link.parent.R:segstate.parent.R:SylStructure.parent.parent.R:Token.parent.local_gain"); |
| 736 |
|
| 737 |
} |
| 738 |
#endif |
| 739 |
|
| 740 |
utt_set_wave(utt,w); |
| 741 |
|
| 742 |
return utt; |
| 743 |
} |
| 744 |
|
| 745 |
|
| 746 |
|