| 1 |
/*************************************************************************/ |
| 2 |
/* */ |
| 3 |
/* Language Technologies Institute */ |
| 4 |
/* Carnegie Mellon University */ |
| 5 |
/* Copyright (c) 2010 */ |
| 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: April 2010 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Resynthesize lpc with various techniques */ |
| 38 |
/*************************************************************************/ |
| 39 |
#include <stdio.h> |
| 40 |
#include "flite.h" |
| 41 |
#include "cst_track.h" |
| 42 |
#include "cst_wave.h" |
| 43 |
#include "cst_tokenstream.h" |
| 44 |
#include "cst_args.h" |
| 45 |
#include "cst_math.h" |
| 46 |
|
| 47 |
void lpc2ref(const float *lpc, float *rfc, int order); |
| 48 |
void ref2lpc(const float *rfc, float *lpc, int order); |
| 49 |
|
| 50 |
static double plus_or_minus_one() |
| 51 |
{ |
| 52 |
/* Randomly return 1 or -1 */ |
| 53 |
/* not sure rand() is portable */ |
| 54 |
if (rand() > RAND_MAX/2.0) |
| 55 |
return 1.0; |
| 56 |
else |
| 57 |
return -1.0; |
| 58 |
} |
| 59 |
|
| 60 |
static double rand_zero_to_one() |
| 61 |
{ |
| 62 |
/* Return number between 0.0 and 1.0 */ |
| 63 |
return rand()/(float)RAND_MAX; |
| 64 |
} |
| 65 |
|
| 66 |
int main(int argc, char **argv) |
| 67 |
{ |
| 68 |
cst_track *t1; |
| 69 |
cst_track *me_filters=NULL; |
| 70 |
cst_wave *w1, *w2, *res=NULL; |
| 71 |
cst_val *files; |
| 72 |
cst_features *args; |
| 73 |
int i, j; |
| 74 |
int order, o, s; |
| 75 |
int frame_length; |
| 76 |
float *lpcs, *residual; |
| 77 |
float m; |
| 78 |
const char *f1, *f2; |
| 79 |
const char *resfn = NULL; |
| 80 |
int last_peak = 0, next_peak; |
| 81 |
int period; |
| 82 |
float power; |
| 83 |
int rfc=0; |
| 84 |
int str=0; |
| 85 |
int fn, fo, ss; |
| 86 |
float xpulse, xnoise; |
| 87 |
float fxpulse, fxnoise; |
| 88 |
float x, me; |
| 89 |
float *hpulse=NULL, *hnoise=NULL; |
| 90 |
float *xpulsesig=NULL, *xnoisesig=NULL; |
| 91 |
int q=0; |
| 92 |
int position; |
| 93 |
int lpc_start = 0; |
| 94 |
|
| 95 |
args = new_features(); |
| 96 |
files = |
| 97 |
cst_args(argv,argc, |
| 98 |
"usage: lpc_resynth OPTIONS INTRACK OUTWAVE\n" |
| 99 |
"Resynth an lpc track\n" |
| 100 |
"-res <string> residual (as waveform)\n" |
| 101 |
"-save_res Save the generated residual\n" |
| 102 |
"-lpc_start <int> start of lpc params in lpc track {1}\n" |
| 103 |
"-order <int> LPC order {16}\n" |
| 104 |
"-str mixed excitation strengths\n" |
| 105 |
"-me_filters <string> mixed excitation filters\n" |
| 106 |
"-rfc Coefficents are reflection coefficients\n", |
| 107 |
args); |
| 108 |
|
| 109 |
f1 = val_string(val_car(files)); |
| 110 |
f2 = val_string(val_car(val_cdr(files))); |
| 111 |
t1 = new_track(); |
| 112 |
|
| 113 |
lpc_start = flite_get_param_int(args,"-lpc_start",1); |
| 114 |
if (feat_present(args,"-rfc")) |
| 115 |
rfc = 1; |
| 116 |
if (feat_present(args,"-str")) |
| 117 |
str = 1; |
| 118 |
if (feat_present(args,"-me_filters")) |
| 119 |
{ |
| 120 |
me_filters = new_track(); |
| 121 |
if (cst_track_load_est(me_filters,flite_get_param_string(args,"-me_filters","me_filters.track")) != CST_OK_FORMAT) |
| 122 |
{ |
| 123 |
fprintf(stderr, |
| 124 |
"lpc_resynth: can't read file or wrong format \"%s\"\n", |
| 125 |
f1); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
hpulse = cst_alloc(float,me_filters->num_channels); |
| 129 |
hnoise = cst_alloc(float,me_filters->num_channels); |
| 130 |
xpulsesig = cst_alloc(float,me_filters->num_channels); |
| 131 |
xnoisesig = cst_alloc(float,me_filters->num_channels); |
| 132 |
} |
| 133 |
|
| 134 |
if (cst_track_load_est(t1,f1) != CST_OK_FORMAT) |
| 135 |
{ |
| 136 |
fprintf(stderr, |
| 137 |
"lpc_resynth: can't read file or wrong format \"%s\"\n", |
| 138 |
f1); |
| 139 |
return -1; |
| 140 |
} |
| 141 |
|
| 142 |
w1 = new_wave(); |
| 143 |
w1->sample_rate = 16000; |
| 144 |
cst_wave_resize(w1,160+(int)(t1->times[t1->num_frames-1]*w1->sample_rate),1); |
| 145 |
|
| 146 |
if (feat_present(args,"-res")) |
| 147 |
{ |
| 148 |
res = new_wave(); |
| 149 |
resfn = flite_get_param_string(args,"-res","res.wav"); |
| 150 |
if (cst_wave_load_riff(res,resfn) != CST_OK_FORMAT) |
| 151 |
{ |
| 152 |
fprintf(stderr, |
| 153 |
"lpc_resynth: can't read file or wrong format \"%s\"\n", |
| 154 |
resfn); |
| 155 |
} |
| 156 |
} |
| 157 |
w2 = copy_wave(w1); |
| 158 |
|
| 159 |
order = flite_get_param_int(args,"-order",16); |
| 160 |
lpcs = cst_alloc(float,order); |
| 161 |
s = order; |
| 162 |
residual = NULL; |
| 163 |
|
| 164 |
for (i=1; i<t1->num_frames; i++) |
| 165 |
{ |
| 166 |
frame_length = 10+(int)(w1->sample_rate*t1->times[i])-s; |
| 167 |
cst_free(residual); |
| 168 |
residual = cst_alloc(float,frame_length); |
| 169 |
/* LPC coefficients */ |
| 170 |
if (rfc == 1) |
| 171 |
ref2lpc(&(t1->frames[i][lpc_start]),lpcs,order); |
| 172 |
else |
| 173 |
{ |
| 174 |
for (o=0; o<order; o++) |
| 175 |
lpcs[o] = t1->frames[i][lpc_start+o]; |
| 176 |
} |
| 177 |
|
| 178 |
/* Residual */ |
| 179 |
/* Mixed excitation residual */ |
| 180 |
if (str) |
| 181 |
{ |
| 182 |
for (fo=0; fo<me_filters->num_channels; fo++) |
| 183 |
{ |
| 184 |
hpulse[fo] = hnoise[fo] = 0.0; |
| 185 |
ss=order+2; |
| 186 |
for (fn=0; fn<me_filters->num_frames; fn++) |
| 187 |
{ |
| 188 |
me = me_filters->frames[fn][fo]; |
| 189 |
hpulse[fo] += t1->frames[i][ss+fn] * me; |
| 190 |
hnoise[fo] += (1 - t1->frames[i][ss+fn]) * me; |
| 191 |
} |
| 192 |
} |
| 193 |
/* For each sample in the frame */ |
| 194 |
for (j=0; j<frame_length; j++) |
| 195 |
{ |
| 196 |
xnoise = plus_or_minus_one(); |
| 197 |
if (t1->frames[i][0] == 0) |
| 198 |
{ |
| 199 |
xpulse = 0.0; /* unvoiced */ |
| 200 |
} |
| 201 |
else |
| 202 |
{ /* voiced */ |
| 203 |
period = w1->sample_rate/t1->frames[i][0]; |
| 204 |
next_peak = last_peak + period; |
| 205 |
#if 0 |
| 206 |
if (next_peak < (s + j)) |
| 207 |
{ /* voiced peak in this frame */ |
| 208 |
xpulse = sqrt(period); /* sqrt ??? */ |
| 209 |
last_peak = next_peak; |
| 210 |
} |
| 211 |
#endif |
| 212 |
#if 1 |
| 213 |
if (q == 3) |
| 214 |
{ |
| 215 |
xpulse = sqrt(period)/4.0; q--; |
| 216 |
} |
| 217 |
else if (q == 2) |
| 218 |
{ |
| 219 |
xpulse = sqrt(period)/2.0; q--; |
| 220 |
} |
| 221 |
else if (q == 1) |
| 222 |
{ |
| 223 |
xpulse = sqrt(period)/4.0; q--; |
| 224 |
} |
| 225 |
else if (next_peak < (s + j)) |
| 226 |
{ |
| 227 |
xpulse = sqrt(period)/4.0; |
| 228 |
q = 3; |
| 229 |
last_peak = next_peak; |
| 230 |
} |
| 231 |
#endif |
| 232 |
else |
| 233 |
xpulse = 0; |
| 234 |
} |
| 235 |
fxpulse = fxnoise = 0.0; |
| 236 |
for (fo=me_filters->num_channels-1; fo>0; fo--) |
| 237 |
{ |
| 238 |
fxpulse += hpulse[fo] * xpulsesig[fo]; |
| 239 |
fxnoise += hnoise[fo] * xnoisesig[fo]; |
| 240 |
|
| 241 |
xpulsesig[fo] = xpulsesig[fo-1]; |
| 242 |
xnoisesig[fo] = xnoisesig[fo-1]; |
| 243 |
} |
| 244 |
fxpulse += hpulse[0] * xpulse; |
| 245 |
fxnoise += hnoise[0] * xnoise; |
| 246 |
xpulsesig[0] = xpulse; |
| 247 |
xnoisesig[0] = xnoise; |
| 248 |
|
| 249 |
x = fxpulse + fxnoise; /* excitation is pulse plus noise */ |
| 250 |
|
| 251 |
residual[j] = x * sqrt(t1->frames[i][1]); |
| 252 |
} |
| 253 |
} |
| 254 |
/* predefined residual */ |
| 255 |
else if (resfn) |
| 256 |
{ |
| 257 |
for (j=0; j<frame_length; j++) |
| 258 |
residual[j] = res->samples[s-order+j]; |
| 259 |
} |
| 260 |
/* Spike plus white noise */ |
| 261 |
else if ((t1->frames[i][0] == 0) || |
| 262 |
(t1->frames[i][t1->num_channels-1] == 0)) |
| 263 |
{ /* unvoiced */ |
| 264 |
printf("awb_debug unvoiced %d\n",i); |
| 265 |
m = sqrt(t1->frames[i][1]); |
| 266 |
for (j=0; j<frame_length; j++) |
| 267 |
residual[j] = m*rand_zero_to_one()*plus_or_minus_one(); |
| 268 |
last_peak = s; /* identify where next pulse should be */ |
| 269 |
} |
| 270 |
else |
| 271 |
{ /* voiced */ |
| 272 |
printf("awb_debug voiced %d\n",i); |
| 273 |
period = w1->sample_rate/t1->frames[i][0]; |
| 274 |
next_peak = last_peak + period; |
| 275 |
memset(residual,0,sizeof(float)*frame_length); |
| 276 |
if (next_peak < (s + frame_length)) |
| 277 |
{ |
| 278 |
printf("awb_debug ping voiced %d\n",i); |
| 279 |
power = sqrt(t1->frames[i][1])*frame_length; |
| 280 |
if (next_peak-s < 2) |
| 281 |
position=2; |
| 282 |
else |
| 283 |
position=next_peak-s; |
| 284 |
|
| 285 |
residual[position-2] = power/4; |
| 286 |
residual[position-1] = power/2; |
| 287 |
residual[position] = power/4; |
| 288 |
last_peak = next_peak; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/* LPC resynthesis */ |
| 293 |
for (j=0; j< frame_length; j++) |
| 294 |
{ |
| 295 |
w1->samples[s] = residual[j]; |
| 296 |
w2->samples[s] = residual[j]; |
| 297 |
for (o=0; (o < order) && ((s-order) > 0); o++) |
| 298 |
w1->samples[s] += lpcs[o]*w1->samples[s-o-1]; |
| 299 |
s++; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
cst_wave_save_riff(w1,f2); |
| 304 |
if (feat_present(args,"-save_res")) |
| 305 |
cst_wave_save_riff(w2,"residual.wav"); |
| 306 |
|
| 307 |
cst_free(residual); |
| 308 |
cst_free(hpulse); |
| 309 |
cst_free(hnoise); |
| 310 |
cst_free(xpulsesig); |
| 311 |
cst_free(xnoisesig); |
| 312 |
delete_features(args); |
| 313 |
delete_wave(w1); |
| 314 |
delete_wave(w2); |
| 315 |
delete_wave(res); |
| 316 |
delete_track(t1); |
| 317 |
delete_track(me_filters); |
| 318 |
|
| 319 |
return 0; |
| 320 |
} |