| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2001 */ |
| 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: Alan W Black ([email protected]) */ |
| 34 |
/* Date: June 2001 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* residuals etc */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
#include <stdio.h> |
| 41 |
#include <math.h> |
| 42 |
#include <string.h> |
| 43 |
|
| 44 |
/* To allow some normally const fields to manipulated during building */ |
| 45 |
#define const |
| 46 |
|
| 47 |
#include "cst_args.h" |
| 48 |
#include "cst_wave.h" |
| 49 |
#include "cst_track.h" |
| 50 |
#include "cst_sigpr.h" |
| 51 |
|
| 52 |
float lpc_min; |
| 53 |
float lpc_range; |
| 54 |
const char *residual_codec = "ulaw"; |
| 55 |
cst_track *vuv; |
| 56 |
|
| 57 |
void inv_lpc_filterd(short *sig, float *a, int order, double *res, int size) |
| 58 |
{ |
| 59 |
/* Note this address bytes before sig, up to order shorts back */ |
| 60 |
int i, j; |
| 61 |
double r; |
| 62 |
|
| 63 |
for (i = 0; i < size; i++) |
| 64 |
{ |
| 65 |
r = sig[i]; |
| 66 |
for (j = 1; j < order; j++) |
| 67 |
r -= a[j] * (double)sig[i - j]; |
| 68 |
res[i] = r; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
static int voiced_frame(cst_track *lpc,int i,cst_track *vuv) |
| 73 |
{ |
| 74 |
int p; |
| 75 |
|
| 76 |
p = lpc->times[i]/(vuv->times[1]-vuv->times[0]); |
| 77 |
|
| 78 |
if (p < 2) /* some reasonable edge conditions */ |
| 79 |
return 0; /* unvoiced */ |
| 80 |
else if (p+2 >= vuv->num_frames) |
| 81 |
return 0; /* unvoiced */ |
| 82 |
else if (vuv->frames[p][0] == 1) |
| 83 |
return 1; /* voiced */ |
| 84 |
else if ((vuv->frames[p-2][0] == 0) && |
| 85 |
(vuv->frames[p-1][0] == 0) && |
| 86 |
(vuv->frames[p+1][0] == 0) && |
| 87 |
(vuv->frames[p+2][0] == 0) |
| 88 |
) |
| 89 |
return 0; /* unvoiced */ |
| 90 |
else |
| 91 |
return 1; |
| 92 |
} |
| 93 |
|
| 94 |
cst_sts *find_sts(cst_wave *sig, cst_track *lpc) |
| 95 |
{ |
| 96 |
cst_sts *sts; |
| 97 |
int i,j; |
| 98 |
double *resd; |
| 99 |
int size,start,end; |
| 100 |
short *sigplus; |
| 101 |
unsigned char *ulaw_res, *power; |
| 102 |
int ipower; |
| 103 |
float sump; |
| 104 |
int packed_size; |
| 105 |
#if 0 |
| 106 |
unsigned char *residual_unpacked; |
| 107 |
int frame_size; |
| 108 |
#endif |
| 109 |
|
| 110 |
sts = cst_alloc(cst_sts,lpc->num_frames); |
| 111 |
sigplus = cst_alloc(short,sig->num_samples+lpc->num_channels); |
| 112 |
memset(sigplus,0,sizeof(short)*lpc->num_channels); |
| 113 |
memmove(&sigplus[lpc->num_channels], |
| 114 |
sig->samples, |
| 115 |
sizeof(short)*sig->num_samples); |
| 116 |
/* EST LPC Windows are centered around the point */ |
| 117 |
/* so offset things by a half period */ |
| 118 |
start = (int)((float)sig->sample_rate * lpc->times[0]/2); |
| 119 |
for (i=0; i<lpc->num_frames; i++) |
| 120 |
{ |
| 121 |
if (i+1 == lpc->num_frames) |
| 122 |
end = (int)((float)sig->sample_rate * lpc->times[i]); |
| 123 |
else |
| 124 |
end = (int)((float)sig->sample_rate * |
| 125 |
(lpc->times[i]+lpc->times[i+1]))/2; |
| 126 |
size = end - start; |
| 127 |
if (size == 0) |
| 128 |
printf("frame size at %f is 0\n",lpc->times[i]); |
| 129 |
resd = cst_alloc(double,size); |
| 130 |
|
| 131 |
inv_lpc_filterd(&sigplus[start+lpc->num_channels], |
| 132 |
lpc->frames[i],lpc->num_channels, |
| 133 |
resd, |
| 134 |
size); |
| 135 |
sts[i].size = size; |
| 136 |
sts[i].frame = cst_alloc(unsigned short,lpc->num_channels-1); |
| 137 |
for (j=1; j < lpc->num_channels; j++) |
| 138 |
sts[i].frame[j-1] = (unsigned short) |
| 139 |
(((lpc->frames[i][j]-lpc_min)/lpc_range)*65535); |
| 140 |
if (cst_streq(residual_codec,"ulaw")) |
| 141 |
{ |
| 142 |
sts[i].residual = cst_alloc(unsigned char,size); |
| 143 |
for (j=0; j < size; j++) |
| 144 |
sts[i].residual[j] = cst_short_to_ulaw((short)resd[j]); |
| 145 |
} |
| 146 |
else if (cst_streq(residual_codec,"g721")) |
| 147 |
{ |
| 148 |
ulaw_res = cst_alloc(unsigned char,size+CST_G721_LEADIN); |
| 149 |
for (j=0; j < size+CST_G721_LEADIN; j++) |
| 150 |
ulaw_res[j] = cst_short_to_ulaw((short)resd[j-CST_G721_LEADIN]); |
| 151 |
sts[i].residual = cst_g721_encode(&packed_size,size+CST_G721_LEADIN,ulaw_res); |
| 152 |
cst_free(ulaw_res); |
| 153 |
} |
| 154 |
else if (cst_streq(residual_codec,"g721vuv")) |
| 155 |
{ |
| 156 |
sump = 0.0; |
| 157 |
for (j=0; j < size; j++) |
| 158 |
sump+=resd[j]*resd[j]; |
| 159 |
ipower = (int)sqrt(sump/size); |
| 160 |
if (voiced_frame(lpc,i,vuv) || (ipower > 250)) |
| 161 |
{ |
| 162 |
ulaw_res = cst_alloc(unsigned char,size+CST_G721_LEADIN); |
| 163 |
for (j=0; j < size+CST_G721_LEADIN; j++) |
| 164 |
ulaw_res[j] = cst_short_to_ulaw((short)resd[j-CST_G721_LEADIN]); |
| 165 |
sts[i].residual = cst_g721_encode(&packed_size,size+CST_G721_LEADIN,ulaw_res); |
| 166 |
cst_free(ulaw_res); |
| 167 |
} |
| 168 |
else /* Unvoiced frame */ |
| 169 |
{ |
| 170 |
power = cst_alloc(unsigned char,6); |
| 171 |
power[0] = 0; |
| 172 |
power[1] = (unsigned char)ipower%256; |
| 173 |
ipower = ipower >> 8; |
| 174 |
power[2] = (unsigned char)ipower%256; |
| 175 |
ipower = ipower >> 8; |
| 176 |
power[3] = (unsigned char)ipower%256; |
| 177 |
ipower = ipower >> 8; |
| 178 |
power[4] = (unsigned char)ipower%256; |
| 179 |
sts[i].residual = power; |
| 180 |
} |
| 181 |
} |
| 182 |
else if (cst_streq(residual_codec,"vuv")) |
| 183 |
{ |
| 184 |
sump = 0.0; |
| 185 |
for (j=0; j < size; j++) |
| 186 |
sump+=resd[j]*resd[j]; |
| 187 |
ipower = (int)sqrt(sump/size); |
| 188 |
if (voiced_frame(lpc,i,vuv) || ipower > 250) |
| 189 |
{ |
| 190 |
sts[i].residual = cst_alloc(unsigned char,size); |
| 191 |
for (j=0; j < size; j++) |
| 192 |
sts[i].residual[j] = cst_short_to_ulaw((short)resd[j]); |
| 193 |
} |
| 194 |
else /* Unvoiced frame */ |
| 195 |
{ |
| 196 |
sump = 0.0; |
| 197 |
for (j=0; j < size; j++) |
| 198 |
sump+=resd[j]*resd[j]; |
| 199 |
power = cst_alloc(unsigned char,6); |
| 200 |
power[0] = 0; |
| 201 |
power[1] = (unsigned char)ipower%256; |
| 202 |
ipower = ipower >> 8; |
| 203 |
power[2] = (unsigned char)ipower%256; |
| 204 |
ipower = ipower >> 8; |
| 205 |
power[3] = (unsigned char)ipower%256; |
| 206 |
ipower = ipower >> 8; |
| 207 |
power[4] = (unsigned char)ipower%256; |
| 208 |
sts[i].residual = power; |
| 209 |
} |
| 210 |
} |
| 211 |
else |
| 212 |
{ |
| 213 |
fprintf(stderr,"unknown codec \'%s\"\n",residual_codec); |
| 214 |
exit(-1); |
| 215 |
} |
| 216 |
start = end; |
| 217 |
} |
| 218 |
|
| 219 |
cst_free(sigplus); |
| 220 |
return sts; |
| 221 |
} |
| 222 |
|
| 223 |
static double plus_or_minus_one() |
| 224 |
{ |
| 225 |
/* Randomly return 1 or -1 */ |
| 226 |
/* not sure rand() is portable */ |
| 227 |
if (rand() > RAND_MAX/2.0) |
| 228 |
return 1.0; |
| 229 |
else |
| 230 |
return -1.0; |
| 231 |
} |
| 232 |
|
| 233 |
static double rand_zero_to_one() |
| 234 |
{ |
| 235 |
/* Return number between 0.0 and 1.0 */ |
| 236 |
return rand()/(float)RAND_MAX; |
| 237 |
} |
| 238 |
|
| 239 |
cst_wave *reconstruct_wave(cst_wave *sig, cst_sts *sts, cst_track *lpc) |
| 240 |
{ |
| 241 |
cst_lpcres *lpcres; |
| 242 |
int i,j,r; |
| 243 |
int start; |
| 244 |
int num_samples; |
| 245 |
unsigned char *residual_unpacked; |
| 246 |
int frame_size; |
| 247 |
int p; |
| 248 |
float m, q; |
| 249 |
|
| 250 |
FILE *ofd; int s; |
| 251 |
|
| 252 |
for (num_samples = 0, i=0; i < lpc->num_frames; i++) |
| 253 |
num_samples += sts[i].size; |
| 254 |
|
| 255 |
lpcres = new_lpcres(); |
| 256 |
lpcres_resize_frames(lpcres,lpc->num_frames); |
| 257 |
lpcres->num_channels = lpc->num_channels-1; |
| 258 |
start = (int)((float)sig->sample_rate * lpc->times[0]/2); |
| 259 |
num_samples += start; |
| 260 |
for (i=0; i<lpc->num_frames; i++) |
| 261 |
{ |
| 262 |
lpcres->frames[i] = sts[i].frame; |
| 263 |
lpcres->sizes[i] = sts[i].size; |
| 264 |
} |
| 265 |
lpcres_resize_samples(lpcres,num_samples); |
| 266 |
lpcres->lpc_min = lpc_min; |
| 267 |
lpcres->lpc_range = lpc_range; |
| 268 |
lpcres->sample_rate = sig->sample_rate; |
| 269 |
for (r=start,i=0; i<lpc->num_frames; i++) |
| 270 |
{ |
| 271 |
if (cst_streq(residual_codec,"g721")) |
| 272 |
{ |
| 273 |
residual_unpacked = |
| 274 |
cst_g721_decode(&frame_size, (sts[i].size+CST_G721_LEADIN+1)/2, sts[i].residual); |
| 275 |
for (j=0; j<sts[i].size; j++,r++) |
| 276 |
lpcres->residual[r] = residual_unpacked[j+CST_G721_LEADIN]; |
| 277 |
cst_free(residual_unpacked); |
| 278 |
} |
| 279 |
else if (cst_streq(residual_codec,"g721vuv")) |
| 280 |
{ |
| 281 |
if (sts[i].residual[0] == 0) |
| 282 |
{ |
| 283 |
p = sts[i].residual[4]; p = p << 8; |
| 284 |
p += sts[i].residual[3]; p = p << 8; |
| 285 |
p += sts[i].residual[2]; p = p << 8; |
| 286 |
p += sts[i].residual[1]; |
| 287 |
m = ((float)p); |
| 288 |
for (j=0; j<sts[i].size; j++,r++) |
| 289 |
{ |
| 290 |
q = m*2*rand_zero_to_one()*plus_or_minus_one(); |
| 291 |
lpcres->residual[r] = cst_short_to_ulaw((short)q); |
| 292 |
} |
| 293 |
} |
| 294 |
else |
| 295 |
{ |
| 296 |
residual_unpacked = |
| 297 |
cst_g721_decode(&frame_size, (sts[i].size+CST_G721_LEADIN+1)/2, sts[i].residual); |
| 298 |
for (j=0; j<sts[i].size; j++,r++) |
| 299 |
lpcres->residual[r] = residual_unpacked[j+CST_G721_LEADIN]; |
| 300 |
cst_free(residual_unpacked); |
| 301 |
} |
| 302 |
} |
| 303 |
else if (cst_streq(residual_codec,"vuv")) |
| 304 |
{ |
| 305 |
if (sts[i].residual[0] == 0) |
| 306 |
{ |
| 307 |
p = sts[i].residual[4]; p = p << 8; |
| 308 |
p += sts[i].residual[3]; p = p << 8; |
| 309 |
p += sts[i].residual[2]; p = p << 8; |
| 310 |
p += sts[i].residual[1]; |
| 311 |
m = ((float)p); |
| 312 |
for (j=0; j<sts[i].size; j++,r++) |
| 313 |
{ |
| 314 |
q = m*2*rand_zero_to_one()*plus_or_minus_one(); |
| 315 |
lpcres->residual[r] = cst_short_to_ulaw((short)q); |
| 316 |
} |
| 317 |
} |
| 318 |
else |
| 319 |
{ |
| 320 |
for (j=0; j<sts[i].size; j++,r++) |
| 321 |
lpcres->residual[r] = sts[i].residual[j]; |
| 322 |
} |
| 323 |
} |
| 324 |
else /* ulaw */ |
| 325 |
{ |
| 326 |
for (j=0; j<sts[i].size; j++,r++) |
| 327 |
lpcres->residual[r] = sts[i].residual[j]; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
#if 1 |
| 332 |
/* Debug dump */ |
| 333 |
ofd = fopen("lpc_resid.lpc","w"); |
| 334 |
for (s=0,i=0; i<lpcres->num_frames; i++) |
| 335 |
{ |
| 336 |
fprintf(ofd,"%d %d %d\n",i,0,lpcres->sizes[i]); |
| 337 |
for (j=0; j < lpcres->num_channels; j++) |
| 338 |
fprintf(ofd,"%d ",lpcres->frames[i][j]); |
| 339 |
fprintf(ofd,"\n"); |
| 340 |
fprintf(ofd,"%d ",32000); s++; |
| 341 |
for (j=1; j < lpcres->sizes[i]; j++,s++) |
| 342 |
fprintf(ofd,"%d ",cst_ulaw_to_short(lpcres->residual[s])); |
| 343 |
fprintf(ofd,"\n"); |
| 344 |
} |
| 345 |
fclose(ofd); |
| 346 |
ofd = fopen("lpc_resid.res","w"); |
| 347 |
for (i=0; i < r; i++) |
| 348 |
fprintf(ofd,"%d\n",cst_ulaw_to_short(lpcres->residual[i])); |
| 349 |
fclose(ofd); |
| 350 |
#endif |
| 351 |
|
| 352 |
return lpc_resynth_fixedpoint(lpcres); |
| 353 |
} |
| 354 |
|
| 355 |
void compare_waves(cst_wave *a, cst_wave *b) |
| 356 |
{ |
| 357 |
int i; |
| 358 |
double r; |
| 359 |
|
| 360 |
if (a->num_samples != b->num_samples) |
| 361 |
{ |
| 362 |
if (a->num_samples > b->num_samples) |
| 363 |
{ |
| 364 |
compare_waves(b,a); |
| 365 |
return; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
for (r=0.0,i=0; i<a->num_samples; i++) |
| 370 |
r += ((float)a->samples[i]-(float)b->samples[i]) * |
| 371 |
((float)a->samples[i]-(float)b->samples[i]); |
| 372 |
r /= a->num_samples; |
| 373 |
printf("a/b diff %f\n",sqrt(r)); |
| 374 |
} |
| 375 |
|
| 376 |
void save_sts(cst_sts *sts, cst_track *lpc, cst_wave *sig, const char *fn) |
| 377 |
{ |
| 378 |
FILE *fd; |
| 379 |
int i,j; |
| 380 |
|
| 381 |
if ((fd=fopen(fn,"w"))== NULL) |
| 382 |
{ |
| 383 |
fprintf(stderr,"can't open for writing file: %s\n", fn); |
| 384 |
exit(-1); |
| 385 |
} |
| 386 |
|
| 387 |
fprintf(fd,"( %d %d %d %f %f)\n", lpc->num_frames, |
| 388 |
lpc->num_channels-1, sig->sample_rate, |
| 389 |
lpc_min, lpc_range); |
| 390 |
for (i=0; i<lpc->num_frames; i++) |
| 391 |
{ |
| 392 |
fprintf(fd,"( %f (",lpc->times[i]); |
| 393 |
for (j=1; j < lpc->num_channels; j++) |
| 394 |
fprintf(fd," %d",sts[i].frame[j-1]); |
| 395 |
fprintf(fd," ) %d ( ", sts[i].size); |
| 396 |
if (cst_streq(residual_codec,"ulaw")) |
| 397 |
{ |
| 398 |
for (j=0; j < sts[i].size; j++) |
| 399 |
fprintf(fd," %d",sts[i].residual[j]); |
| 400 |
} |
| 401 |
else if (cst_streq(residual_codec,"g721")) |
| 402 |
{ |
| 403 |
for (j=0; j < (sts[i].size+CST_G721_LEADIN+1)/2; j++) |
| 404 |
fprintf(fd," %d",sts[i].residual[j]); |
| 405 |
} |
| 406 |
else if (cst_streq(residual_codec,"g721vuv")) |
| 407 |
{ |
| 408 |
if (sts[i].residual[0] == 0) |
| 409 |
for (j=0; j < 5; j++) |
| 410 |
fprintf(fd," %d",sts[i].residual[j]); |
| 411 |
else |
| 412 |
for (j=0; j < (sts[i].size+CST_G721_LEADIN+1)/2; j++) |
| 413 |
fprintf(fd," %d",sts[i].residual[j]); |
| 414 |
} |
| 415 |
else if (cst_streq(residual_codec,"vuv")) |
| 416 |
{ |
| 417 |
if (sts[i].residual[0] == 0) |
| 418 |
for (j=0; j < 5; j++) |
| 419 |
fprintf(fd," %d",sts[i].residual[j]); |
| 420 |
else |
| 421 |
for (j=0; j < sts[i].size; j++) |
| 422 |
fprintf(fd," %d",sts[i].residual[j]); |
| 423 |
} |
| 424 |
fprintf(fd," ))\n"); |
| 425 |
} |
| 426 |
|
| 427 |
fclose(fd); |
| 428 |
} |
| 429 |
|
| 430 |
int main(int argc, char **argv) |
| 431 |
{ |
| 432 |
cst_track *lpc; |
| 433 |
cst_wave *sig, *sig2; |
| 434 |
cst_sts *sts; |
| 435 |
cst_features *args; |
| 436 |
|
| 437 |
args = new_features(); |
| 438 |
cst_args(argv,argc, |
| 439 |
"usage: find_sts OPTIONS\n" |
| 440 |
"Find sts from lpc and waveform\n" |
| 441 |
"-codec <string> ulaw (default), g721, g721vuv, vuv\n" |
| 442 |
"-vuv <string> v/uv track (for some codecs)\n" |
| 443 |
"-lpcmin <float> minimum LPC value\n" |
| 444 |
"-lpcrange <float> range of LPC values\n" |
| 445 |
"-lpc <string> LPC track file\n" |
| 446 |
"-wave <string> WAVE file\n" |
| 447 |
"-o <string> STS output file\n", |
| 448 |
args); |
| 449 |
|
| 450 |
lpc_min = get_param_float(args,"-lpcmin",-10.0); |
| 451 |
lpc_range = get_param_float(args,"-lpcrange",20.0); |
| 452 |
residual_codec = get_param_string(args,"-codec","ulaw"); |
| 453 |
|
| 454 |
if (feat_present(args,"-vuv")) |
| 455 |
{ |
| 456 |
/* Fixed frame track of voiced (1) unvoived (0) information */ |
| 457 |
vuv = new_track(); |
| 458 |
cst_track_load_est(vuv,get_param_string(args,"-vuv","file.vuv")); |
| 459 |
} |
| 460 |
|
| 461 |
lpc = new_track(); |
| 462 |
cst_track_load_est(lpc,get_param_string(args,"-lpc","file.lpc")); |
| 463 |
sig = new_wave(); |
| 464 |
if (cst_wave_load_riff(sig,get_param_string(args,"-wave","file.wav")) |
| 465 |
== CST_WRONG_FORMAT) |
| 466 |
{ |
| 467 |
fprintf(stderr, |
| 468 |
"cannot load waveform, format unrecognized, from \"%s\"\n", |
| 469 |
get_param_string(args,"-wave","file.wav")); |
| 470 |
exit(-1); |
| 471 |
} |
| 472 |
|
| 473 |
sts = find_sts(sig,lpc); |
| 474 |
|
| 475 |
/* See if it worked */ |
| 476 |
sig2 = reconstruct_wave(sig,sts,lpc); |
| 477 |
|
| 478 |
compare_waves(sig,sig2); |
| 479 |
cst_wave_save_riff(sig2,"sig2.wav"); |
| 480 |
|
| 481 |
save_sts(sts,lpc,sig,get_param_string(args,"-o","file.sts")); |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |