| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | #ifndef HTS_SSTREAM_C |
| 46 | #define HTS_SSTREAM_C |
| 47 | |
| 48 | #ifdef __cplusplus |
| 49 | #define HTS_SSTREAM_C_START extern "C" { |
| 50 | #define HTS_SSTREAM_C_END } |
| 51 | #else |
| 52 | #define HTS_SSTREAM_C_START |
| 53 | #define HTS_SSTREAM_C_END |
| 54 | #endif /* __CPLUSPLUS */ |
| 55 | |
| 56 | HTS_SSTREAM_C_START; |
| 57 | |
| 58 | |
| 59 | #include "HTS_hidden.h" |
| 60 | |
| 61 | static void HTS_set_duration(int *duration, double *mean, double *vari, |
| 62 | double *remain, int size, double frame_length) |
| 63 | { |
| 64 | int i; |
| 65 | double temp1, temp2; |
| 66 | double rho = 0.0; |
| 67 | |
| 68 | if (frame_length != 0.0) { |
| 69 | temp1 = 0.0; |
| 70 | temp2 = 0.0; |
| 71 | for (i = 0; i < size; i++) { |
| 72 | temp1 += mean[i]; |
| 73 | temp2 += vari[i]; |
| 74 | } |
| 75 | rho = (frame_length - temp1) / temp2; |
| 76 | } |
| 77 | for (i = 0; i < size; i++) { |
| 78 | temp1 = mean[i] + rho * vari[i] + *remain; |
| 79 | duration[i] = (int) (temp1 + 0.5); |
| 80 | if (duration[i] < 1) |
| 81 | duration[i] = 1; |
| 82 | *remain = temp1 - (double) duration[i]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | void HTS_SStreamSet_initialize(HTS_SStreamSet * sss) |
| 88 | { |
| 89 | sss->nstream = 0; |
| 90 | sss->nstate = 0; |
| 91 | sss->sstream = NULL((void*)0); |
| 92 | sss->duration = NULL((void*)0); |
| 93 | sss->total_state = 0; |
| 94 | sss->total_frame = 0; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | void HTS_SStreamSet_create(HTS_SStreamSet * sss, HTS_ModelSet * ms, |
| 99 | HTS_Label * label, double *duration_iw, |
| 100 | double **parameter_iw, double **gv_iw) |
| 101 | { |
| 102 | int i, j, k; |
| 103 | double temp1, temp2; |
| 104 | int state; |
| 105 | HTS_SStream *sst; |
| 106 | double *duration_mean, *duration_vari; |
| 107 | double duration_remain; |
| 108 | double frame_length; |
| 109 | int next_time; |
| 110 | int next_state; |
| 111 | |
| 112 | |
| 113 | sss->nstate = HTS_ModelSet_get_nstate(ms); |
| 114 | sss->nstream = HTS_ModelSet_get_nstream(ms); |
| 115 | sss->total_frame = 0; |
| 116 | sss->total_state = HTS_Label_get_size(label) * sss->nstate; |
| 117 | sss->duration = (int *) HTS_calloc(sss->total_state, sizeof(int)); |
| 118 | sss->sstream = (HTS_SStream *) HTS_calloc(sss->nstream, sizeof(HTS_SStream)); |
| 119 | for (i = 0; i < sss->nstream; i++) { |
| 120 | sst = &sss->sstream[i]; |
| 121 | sst->vector_length = HTS_ModelSet_get_vector_length(ms, i); |
| 122 | sst->mean = (double **) HTS_calloc(sss->total_state, sizeof(double *)); |
| 123 | sst->vari = (double **) HTS_calloc(sss->total_state, sizeof(double *)); |
| 124 | if (HTS_ModelSet_is_msd(ms, i)) |
| 125 | sst->msd = (double *) HTS_calloc(sss->total_state, sizeof(double)); |
| 126 | else |
| 127 | sst->msd = NULL((void*)0); |
| 128 | for (j = 0; j < sss->total_state; j++) { |
| 129 | sst->mean[j] = |
| 130 | (double *) HTS_calloc(sst->vector_length, sizeof(double)); |
| 131 | sst->vari[j] = |
| 132 | (double *) HTS_calloc(sst->vector_length, sizeof(double)); |
| 133 | } |
| 134 | sst->gv_switch = |
| 135 | (HTS_Boolean *) HTS_calloc(sss->total_state, sizeof(HTS_Boolean)); |
| 136 | for (j = 0; j < sss->total_state; j++) |
| 137 | sst->gv_switch[j] = TRUE1; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | for (i = 0, temp1 = 0.0; |
| 142 | i < HTS_ModelSet_get_duration_interpolation_size(ms); i++) |
| 143 | temp1 += duration_iw[i]; |
| 144 | for (i = 0; i < HTS_ModelSet_get_duration_interpolation_size(ms); i++) |
| 145 | if (duration_iw[i] != 0.0) |
| 146 | duration_iw[i] /= temp1; |
| 147 | for (i = 0; i < sss->nstream; i++) { |
| 148 | for (j = 0, temp1 = 0.0; |
| 149 | j < HTS_ModelSet_get_parameter_interpolation_size(ms, i); j++) |
| 150 | temp1 += parameter_iw[i][j]; |
| 151 | for (j = 0; j < HTS_ModelSet_get_parameter_interpolation_size(ms, i); j++) |
| 152 | if (parameter_iw[i][j] != 0.0) |
| 153 | parameter_iw[i][j] /= temp1; |
| 154 | if (HTS_ModelSet_use_gv(ms, i)) { |
| 155 | for (j = 0, temp1 = 0.0; |
| 156 | j < HTS_ModelSet_get_gv_interpolation_size(ms, i); j++) |
| 157 | temp1 += gv_iw[i][j]; |
| 158 | for (j = 0; j < HTS_ModelSet_get_gv_interpolation_size(ms, i); j++) |
| 159 | if (gv_iw[i][j] != 0.0) |
| 160 | gv_iw[i][j] /= temp1; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | |
| 165 | duration_mean = |
| 166 | (double *) HTS_calloc(sss->nstate * HTS_Label_get_size(label), |
| 167 | sizeof(double)); |
| 168 | duration_vari = |
| 169 | (double *) HTS_calloc(sss->nstate * HTS_Label_get_size(label), |
| 170 | sizeof(double)); |
| 171 | duration_remain = 0.0; |
| 172 | for (i = 0; i < HTS_Label_get_size(label); i++) |
| 173 | HTS_ModelSet_get_duration(ms, HTS_Label_get_string(label, i), |
| 174 | &duration_mean[i * sss->nstate], |
| 175 | &duration_vari[i * sss->nstate], duration_iw); |
| 176 | if (HTS_Label_get_frame_specified_flag(label)) { |
| 177 | |
| 178 | next_time = 0; |
| 179 | next_state = 0; |
| 180 | state = 0; |
| 181 | for (i = 0; i < HTS_Label_get_size(label); i++) { |
| 182 | temp1 = HTS_Label_get_start_frame(label, i); |
| Value stored to 'temp1' is never read |
| 183 | temp2 = HTS_Label_get_end_frame(label, i); |
| 184 | if (temp2 >= 0) { |
| 185 | HTS_set_duration(&sss->duration[next_state], |
| 186 | &duration_mean[next_state], |
| 187 | &duration_vari[next_state], &duration_remain, |
| 188 | state + sss->nstate - next_state, |
| 189 | temp2 - next_time); |
| 190 | for (j = next_state; j < state + sss->nstate; j++) |
| 191 | next_time += sss->duration[j]; |
| 192 | next_state = state + sss->nstate; |
| 193 | } else if (i + 1 == HTS_Label_get_size(label)) { |
| 194 | HTS_set_duration(&sss->duration[next_state], |
| 195 | &duration_mean[next_state], |
| 196 | &duration_vari[next_state], &duration_remain, |
| 197 | state + sss->nstate - next_state, 0.0); |
| 198 | } |
| 199 | state += sss->nstate; |
| 200 | } |
| 201 | } else { |
| 202 | |
| 203 | if (HTS_Label_get_speech_speed(label) != 1.0) { |
| 204 | temp1 = 0.0; |
| 205 | for (i = 0; i < HTS_Label_get_size(label) * sss->nstate; i++) { |
| 206 | temp1 += duration_mean[i]; |
| 207 | } |
| 208 | frame_length = temp1 / HTS_Label_get_speech_speed(label); |
| 209 | } else { |
| 210 | frame_length = 0.0; |
| 211 | } |
| 212 | |
| 213 | HTS_set_duration(sss->duration, duration_mean, duration_vari, |
| 214 | &duration_remain, |
| 215 | HTS_Label_get_size(label) * sss->nstate, frame_length); |
| 216 | } |
| 217 | HTS_free(duration_mean); |
| 218 | HTS_free(duration_vari); |
| 219 | |
| 220 | |
| 221 | for (i = 0, state = 0; i < HTS_Label_get_size(label); i++) { |
| 222 | for (j = 2; j <= sss->nstate + 1; j++) { |
| 223 | sss->total_frame += sss->duration[state]; |
| 224 | for (k = 0; k < sss->nstream; k++) { |
| 225 | sst = &sss->sstream[k]; |
| 226 | if (sst->msd) |
| 227 | HTS_ModelSet_get_parameter(ms, HTS_Label_get_string(label, i), |
| 228 | sst->mean[state], sst->vari[state], |
| 229 | &sst->msd[state], k, j, |
| 230 | parameter_iw[k]); |
| 231 | else |
| 232 | HTS_ModelSet_get_parameter(ms, HTS_Label_get_string(label, i), |
| 233 | sst->mean[state], sst->vari[state], |
| 234 | NULL((void*)0), k, j, parameter_iw[k]); |
| 235 | } |
| 236 | state++; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | for (i = 0; i < sss->nstream; i++) { |
| 242 | sst = &sss->sstream[i]; |
| 243 | sst->win_size = HTS_ModelSet_get_window_size(ms, i); |
| 244 | sst->win_max_width = HTS_ModelSet_get_window_max_width(ms, i); |
| 245 | sst->win_l_width = (int *) HTS_calloc(sst->win_size, sizeof(int)); |
| 246 | sst->win_r_width = (int *) HTS_calloc(sst->win_size, sizeof(int)); |
| 247 | sst->win_coefficient = |
| 248 | (double **) HTS_calloc(sst->win_size, sizeof(double *)); |
| 249 | for (j = 0; j < sst->win_size; j++) { |
| 250 | sst->win_l_width[j] = HTS_ModelSet_get_window_left_width(ms, i, j); |
| 251 | sst->win_r_width[j] = HTS_ModelSet_get_window_right_width(ms, i, j); |
| 252 | if (sst->win_l_width[j] + sst->win_r_width[j] == 0) |
| 253 | sst->win_coefficient[j] = |
| 254 | (double *) HTS_calloc(-2 * sst->win_l_width[j] + 1, |
| 255 | sizeof(double)); |
| 256 | else |
| 257 | sst->win_coefficient[j] = |
| 258 | (double *) HTS_calloc(-2 * sst->win_l_width[j], sizeof(double)); |
| 259 | sst->win_coefficient[j] -= sst->win_l_width[j]; |
| 260 | for (k = sst->win_l_width[j]; k <= sst->win_r_width[j]; k++) |
| 261 | sst->win_coefficient[j][k] = |
| 262 | HTS_ModelSet_get_window_coefficient(ms, i, j, k); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | |
| 267 | for (i = 0; i < sss->nstream; i++) { |
| 268 | sst = &sss->sstream[i]; |
| 269 | if (HTS_ModelSet_use_gv(ms, i)) { |
| 270 | sst->gv_mean = |
| 271 | (double *) HTS_calloc(sst->vector_length / sst->win_size, |
| 272 | sizeof(double)); |
| 273 | sst->gv_vari = |
| 274 | (double *) HTS_calloc(sst->vector_length / sst->win_size, |
| 275 | sizeof(double)); |
| 276 | HTS_ModelSet_get_gv(ms, HTS_Label_get_string(label, 0), sst->gv_mean, |
| 277 | sst->gv_vari, i, gv_iw[i]); |
| 278 | } else { |
| 279 | sst->gv_mean = NULL((void*)0); |
| 280 | sst->gv_vari = NULL((void*)0); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (HTS_ModelSet_have_gv_switch(ms) == TRUE1) |
| 285 | for (i = 0; i < HTS_Label_get_size(label); i++) |
| 286 | if (HTS_ModelSet_get_gv_switch(ms, HTS_Label_get_string(label, i)) == |
| 287 | FALSE0) |
| 288 | for (j = 0; j < sss->nstream; j++) |
| 289 | for (k = 0; k < sss->nstate; k++) |
| 290 | sss->sstream[j].gv_switch[i * sss->nstate + k] = FALSE0; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | int HTS_SStreamSet_get_nstream(HTS_SStreamSet * sss) |
| 295 | { |
| 296 | return sss->nstream; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | int HTS_SStreamSet_get_vector_length(HTS_SStreamSet * sss, int stream_index) |
| 301 | { |
| 302 | return sss->sstream[stream_index].vector_length; |
| 303 | } |
| 304 | |
| 305 | |
| 306 | HTS_Boolean HTS_SStreamSet_is_msd(HTS_SStreamSet * sss, int stream_index) |
| 307 | { |
| 308 | return sss->sstream[stream_index].msd ? TRUE1 : FALSE0; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | int HTS_SStreamSet_get_total_state(HTS_SStreamSet * sss) |
| 313 | { |
| 314 | return sss->total_state; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | int HTS_SStreamSet_get_total_frame(HTS_SStreamSet * sss) |
| 319 | { |
| 320 | return sss->total_frame; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | double HTS_SStreamSet_get_msd(HTS_SStreamSet * sss, int stream_index, |
| 325 | int state_index) |
| 326 | { |
| 327 | return sss->sstream[stream_index].msd[state_index]; |
| 328 | } |
| 329 | |
| 330 | |
| 331 | int HTS_SStreamSet_get_window_size(HTS_SStreamSet * sss, int stream_index) |
| 332 | { |
| 333 | return sss->sstream[stream_index].win_size; |
| 334 | } |
| 335 | |
| 336 | |
| 337 | int HTS_SStreamSet_get_window_left_width(HTS_SStreamSet * sss, |
| 338 | int stream_index, int window_index) |
| 339 | { |
| 340 | return sss->sstream[stream_index].win_l_width[window_index]; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | int HTS_SStreamSet_get_window_right_width(HTS_SStreamSet * sss, |
| 345 | int stream_index, int window_index) |
| 346 | { |
| 347 | return sss->sstream[stream_index].win_r_width[window_index]; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | double HTS_SStreamSet_get_window_coefficient(HTS_SStreamSet * sss, |
| 352 | int stream_index, |
| 353 | int window_index, |
| 354 | int coefficient_index) |
| 355 | { |
| 356 | return sss->sstream[stream_index]. |
| 357 | win_coefficient[window_index][coefficient_index]; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | int HTS_SStreamSet_get_window_max_width(HTS_SStreamSet * sss, int stream_index) |
| 362 | { |
| 363 | return sss->sstream[stream_index].win_max_width; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | HTS_Boolean HTS_SStreamSet_use_gv(HTS_SStreamSet * sss, int stream_index) |
| 368 | { |
| 369 | return sss->sstream[stream_index].gv_mean ? TRUE1 : FALSE0; |
| 370 | } |
| 371 | |
| 372 | |
| 373 | int HTS_SStreamSet_get_duration(HTS_SStreamSet * sss, int state_index) |
| 374 | { |
| 375 | return sss->duration[state_index]; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | double HTS_SStreamSet_get_mean(HTS_SStreamSet * sss, |
| 380 | int stream_index, |
| 381 | int state_index, int vector_index) |
| 382 | { |
| 383 | return sss->sstream[stream_index].mean[state_index][vector_index]; |
| 384 | } |
| 385 | |
| 386 | |
| 387 | void HTS_SStreamSet_set_mean(HTS_SStreamSet * sss, int stream_index, |
| 388 | int state_index, int vector_index, double f) |
| 389 | { |
| 390 | sss->sstream[stream_index].mean[state_index][vector_index] = f; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | double HTS_SStreamSet_get_vari(HTS_SStreamSet * sss, |
| 395 | int stream_index, |
| 396 | int state_index, int vector_index) |
| 397 | { |
| 398 | return sss->sstream[stream_index].vari[state_index][vector_index]; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | void HTS_SStreamSet_set_vari(HTS_SStreamSet * sss, int stream_index, |
| 403 | int state_index, int vector_index, double f) |
| 404 | { |
| 405 | sss->sstream[stream_index].vari[state_index][vector_index] = f; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | double HTS_SStreamSet_get_gv_mean(HTS_SStreamSet * sss, |
| 410 | int stream_index, int vector_index) |
| 411 | { |
| 412 | return sss->sstream[stream_index].gv_mean[vector_index]; |
| 413 | } |
| 414 | |
| 415 | |
| 416 | double HTS_SStreamSet_get_gv_vari(HTS_SStreamSet * sss, |
| 417 | int stream_index, int vector_index) |
| 418 | { |
| 419 | return sss->sstream[stream_index].gv_vari[vector_index]; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | void HTS_SStreamSet_set_gv_switch(HTS_SStreamSet * sss, int stream_index, |
| 424 | int state_index, HTS_Boolean i) |
| 425 | { |
| 426 | sss->sstream[stream_index].gv_switch[state_index] = i; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | HTS_Boolean HTS_SStreamSet_get_gv_switch(HTS_SStreamSet * sss, int stream_index, |
| 431 | int state_index) |
| 432 | { |
| 433 | return sss->sstream[stream_index].gv_switch[state_index]; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | void HTS_SStreamSet_clear(HTS_SStreamSet * sss) |
| 438 | { |
| 439 | int i, j; |
| 440 | HTS_SStream *sst; |
| 441 | |
| 442 | if (sss->sstream) { |
| 443 | for (i = 0; i < sss->nstream; i++) { |
| 444 | sst = &sss->sstream[i]; |
| 445 | for (j = 0; j < sss->total_state; j++) { |
| 446 | HTS_free(sst->mean[j]); |
| 447 | HTS_free(sst->vari[j]); |
| 448 | } |
| 449 | if (sst->msd) |
| 450 | HTS_free(sst->msd); |
| 451 | HTS_free(sst->mean); |
| 452 | HTS_free(sst->vari); |
| 453 | for (j = sst->win_size - 1; j >= 0; j--) { |
| 454 | sst->win_coefficient[j] += sst->win_l_width[j]; |
| 455 | HTS_free(sst->win_coefficient[j]); |
| 456 | } |
| 457 | HTS_free(sst->win_coefficient); |
| 458 | HTS_free(sst->win_l_width); |
| 459 | HTS_free(sst->win_r_width); |
| 460 | if (sst->gv_mean) |
| 461 | HTS_free(sst->gv_mean); |
| 462 | if (sst->gv_vari) |
| 463 | HTS_free(sst->gv_vari); |
| 464 | HTS_free(sst->gv_switch); |
| 465 | } |
| 466 | HTS_free(sss->sstream); |
| 467 | } |
| 468 | if (sss->duration) |
| 469 | HTS_free(sss->duration); |
| 470 | |
| 471 | HTS_SStreamSet_initialize(sss); |
| 472 | } |
| 473 | |
| 474 | HTS_SSTREAM_C_END; |
| 475 | |
| 476 | #endif /* !HTS_SSTREAM_C */ |