| 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: January 2001 */ |
| 35 |
/*************************************************************************/ |
| 36 |
/* */ |
| 37 |
/* Signal processing functions */ |
| 38 |
/* */ |
| 39 |
/*************************************************************************/ |
| 40 |
|
| 41 |
#include "cst_math.h" |
| 42 |
#include "cst_hrg.h" |
| 43 |
#include "cst_wave.h" |
| 44 |
#include "cst_sigpr.h" |
| 45 |
#include "cst_sts.h" |
| 46 |
|
| 47 |
cst_wave *lpc_resynth(cst_lpcres *lpcres) |
| 48 |
{ |
| 49 |
cst_wave *w; |
| 50 |
int i,j,r,o,k; |
| 51 |
int ci,cr; |
| 52 |
float *outbuf, *lpccoefs; |
| 53 |
int pm_size_samps; |
| 54 |
|
| 55 |
/* Get a new wave to build the signal into */ |
| 56 |
w = new_wave(); |
| 57 |
cst_wave_resize(w,lpcres->num_samples,1); |
| 58 |
w->sample_rate = lpcres->sample_rate; |
| 59 |
/* outbuf is a circular buffer with past relevant samples in it */ |
| 60 |
outbuf = cst_alloc(float,1+lpcres->num_channels); |
| 61 |
/* unpacked lpc coefficients */ |
| 62 |
lpccoefs = cst_alloc(float,lpcres->num_channels); |
| 63 |
|
| 64 |
for (r=0,o=lpcres->num_channels,i=0; i < lpcres->num_frames; i++) |
| 65 |
{ |
| 66 |
pm_size_samps = lpcres->sizes[i]; |
| 67 |
|
| 68 |
/* Unpack the LPC coefficients */ |
| 69 |
for (k=0; k<lpcres->num_channels; k++) |
| 70 |
{ |
| 71 |
lpccoefs[k] = (float)((((double)lpcres->frames[i][k])/65535.0)* |
| 72 |
lpcres->lpc_range) + lpcres->lpc_min; |
| 73 |
} |
| 74 |
/* Note we don't zero the lead in from the previous part */ |
| 75 |
/* seems like you should but it makes it worse if you do */ |
| 76 |
/* memset(outbuf,0,sizeof(float)*(1+lpcres->num_channels)); */ |
| 77 |
|
| 78 |
/* resynthesis the signal */ |
| 79 |
for (j=0; j < pm_size_samps; j++,r++) |
| 80 |
{ |
| 81 |
outbuf[o] = (float)cst_ulaw_to_short(lpcres->residual[r]); |
| 82 |
cr = (o == 0 ? lpcres->num_channels : o-1); |
| 83 |
for (ci=0; ci < lpcres->num_channels; ci++) |
| 84 |
{ |
| 85 |
outbuf[o] += lpccoefs[ci] * outbuf[cr]; |
| 86 |
cr = (cr == 0 ? lpcres->num_channels : cr-1); |
| 87 |
} |
| 88 |
w->samples[r] = (short)(outbuf[o]); |
| 89 |
o = (o == lpcres->num_channels ? 0 : o+1); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
cst_free(outbuf); |
| 94 |
cst_free(lpccoefs); |
| 95 |
|
| 96 |
return w; |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
cst_wave *lpc_resynth_windows(cst_lpcres *lpcres) |
| 101 |
{ |
| 102 |
cst_wave *w; |
| 103 |
int i,j,r,o,k; |
| 104 |
int ci,cr; |
| 105 |
float *outbuf, *lpccoefs; |
| 106 |
int pm_size_samps; |
| 107 |
|
| 108 |
/* Get a new wave to build the signal into */ |
| 109 |
w = new_wave(); |
| 110 |
cst_wave_resize(w,lpcres->num_samples,1); |
| 111 |
w->sample_rate = lpcres->sample_rate; |
| 112 |
/* outbuf is a circular buffer with past relevant samples in it */ |
| 113 |
outbuf = cst_alloc(float,1+lpcres->num_channels); |
| 114 |
/* unpacked lpc coefficients */ |
| 115 |
lpccoefs = cst_alloc(float,lpcres->num_channels); |
| 116 |
|
| 117 |
for (r=0,o=lpcres->num_channels,i=0; i < lpcres->num_frames; i++) |
| 118 |
{ |
| 119 |
pm_size_samps = lpcres->sizes[i]; |
| 120 |
|
| 121 |
/* Unpack the LPC coefficients */ |
| 122 |
for (k=0; k<lpcres->num_channels; k++) |
| 123 |
{ |
| 124 |
lpccoefs[k] = ((float)(((double)lpcres->frames[i][k])/65535.0)* |
| 125 |
lpcres->lpc_range) + lpcres->lpc_min; |
| 126 |
} |
| 127 |
memset(outbuf,0,sizeof(float)*(1+lpcres->num_channels)); |
| 128 |
|
| 129 |
/* resynthesis the signal */ |
| 130 |
for (j=0; j < pm_size_samps; j++,r++) |
| 131 |
{ |
| 132 |
outbuf[o] = (float)cst_ulaw_to_short(lpcres->residual[r]); |
| 133 |
cr = (o == 0 ? lpcres->num_channels : o-1); |
| 134 |
for (ci=0; ci < lpcres->num_channels; ci++) |
| 135 |
{ |
| 136 |
outbuf[o] += lpccoefs[ci] * outbuf[cr]; |
| 137 |
cr = (cr == 0 ? lpcres->num_channels : cr-1); |
| 138 |
} |
| 139 |
w->samples[r] = (short)(outbuf[o]); |
| 140 |
o = (o == lpcres->num_channels ? 0 : o+1); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
cst_free(outbuf); |
| 145 |
cst_free(lpccoefs); |
| 146 |
|
| 147 |
return w; |
| 148 |
|
| 149 |
} |
| 150 |
|
| 151 |
const static short ulaw_to_short_table[] = |
| 152 |
{ |
| 153 |
-32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, |
| 154 |
-23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, |
| 155 |
-15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, |
| 156 |
-11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, |
| 157 |
-7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, |
| 158 |
-5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, |
| 159 |
-3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, |
| 160 |
-2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, |
| 161 |
-1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, |
| 162 |
-1372, -1308, -1244, -1180, -1116, -1052, -988, -924, |
| 163 |
-876, -844, -812, -780, -748, -716, -684, -652, |
| 164 |
-620, -588, -556, -524, -492, -460, -428, -396, |
| 165 |
-372, -356, -340, -324, -308, -292, -276, -260, |
| 166 |
-244, -228, -212, -196, -180, -164, -148, -132, |
| 167 |
-120, -112, -104, -96, -88, -80, -72, -64, |
| 168 |
-56, -48, -40, -32, -24, -16, -8, 0, |
| 169 |
32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, |
| 170 |
23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, |
| 171 |
15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, |
| 172 |
11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, |
| 173 |
7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, |
| 174 |
5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, |
| 175 |
3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, |
| 176 |
2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, |
| 177 |
1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, |
| 178 |
1372, 1308, 1244, 1180, 1116, 1052, 988, 924, |
| 179 |
876, 844, 812, 780, 748, 716, 684, 652, |
| 180 |
620, 588, 556, 524, 492, 460, 428, 396, |
| 181 |
372, 356, 340, 324, 308, 292, 276, 260, |
| 182 |
244, 228, 212, 196, 180, 164, 148, 132, |
| 183 |
120, 112, 104, 96, 88, 80, 72, 64, |
| 184 |
56, 48, 40, 32, 24, 16, 8, 0 }; |
| 185 |
|
| 186 |
cst_wave *lpc_resynth_fixedpoint(cst_lpcres *lpcres) |
| 187 |
{ |
| 188 |
/* The fixed point version, without floats */ |
| 189 |
cst_wave *w; |
| 190 |
int i,j,r,o,k; |
| 191 |
int stream_mark; |
| 192 |
int ci,cr; |
| 193 |
int *outbuf, *lpccoefs; |
| 194 |
int pm_size_samps, ilpc_min, ilpc_range; |
| 195 |
int rc = CST_AUDIO_STREAM_CONT; |
| 196 |
|
| 197 |
/* Get a new wave to build the signal into */ |
| 198 |
w = new_wave(); |
| 199 |
cst_wave_resize(w,lpcres->num_samples,1); |
| 200 |
w->sample_rate = lpcres->sample_rate; |
| 201 |
/* outbuf is a circular buffer with past relevant samples in it */ |
| 202 |
outbuf = cst_alloc(int,1+lpcres->num_channels); |
| 203 |
/* unpacked lpc coefficients */ |
| 204 |
lpccoefs = cst_alloc(int,lpcres->num_channels); |
| 205 |
ilpc_min = (int)(lpcres->lpc_min*32768.0); |
| 206 |
/* assume range is never > abs(16) */ |
| 207 |
ilpc_range = (int)(lpcres->lpc_range*2048.0); |
| 208 |
|
| 209 |
stream_mark = 0; |
| 210 |
for (r=0,o=lpcres->num_channels,i=0; |
| 211 |
(rc == CST_AUDIO_STREAM_CONT) && (i < lpcres->num_frames); |
| 212 |
i++) |
| 213 |
{ |
| 214 |
pm_size_samps = lpcres->sizes[i]; |
| 215 |
|
| 216 |
if (lpcres->delayed_decoding) |
| 217 |
{ |
| 218 |
/* do decoding for this frame */ |
| 219 |
add_residual_g721vuv(lpcres->sizes[i], |
| 220 |
&lpcres->residual[r], |
| 221 |
lpcres->sizes[i], |
| 222 |
lpcres->packed_residuals[i]); |
| 223 |
} |
| 224 |
|
| 225 |
/* Unpack the LPC coefficients */ |
| 226 |
for (k=0; k<lpcres->num_channels; k++) |
| 227 |
lpccoefs[k]=((lpcres->frames[i][k]/2*ilpc_range)/2048+ilpc_min)/2; |
| 228 |
|
| 229 |
/* resynthesis the signal */ |
| 230 |
for (j=0; j < pm_size_samps; j++,r++) |
| 231 |
{ |
| 232 |
outbuf[o] = (int)ulaw_to_short_table[lpcres->residual[r]]; |
| 233 |
outbuf[o] *= 16384; |
| 234 |
cr = (o == 0 ? lpcres->num_channels : o-1); |
| 235 |
for (ci=0; ci < lpcres->num_channels; ci++) |
| 236 |
{ |
| 237 |
outbuf[o] += lpccoefs[ci]*outbuf[cr]; |
| 238 |
cr = (cr == 0 ? lpcres->num_channels : cr-1); |
| 239 |
} |
| 240 |
outbuf[o] /= 16384; |
| 241 |
w->samples[r] = (short)outbuf[o]; |
| 242 |
o = (o == lpcres->num_channels ? 0 : o+1); |
| 243 |
} |
| 244 |
if (lpcres->asi && (r-stream_mark > lpcres->asi->min_buffsize)) |
| 245 |
{ |
| 246 |
rc = (*lpcres->asi->asc)(w,stream_mark,r-stream_mark,0, |
| 247 |
lpcres->asi); |
| 248 |
stream_mark = r; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
if ((lpcres->asi) && (rc == CST_AUDIO_STREAM_CONT)) |
| 253 |
(*lpcres->asi->asc)(w,stream_mark,r-stream_mark,1,lpcres->asi); |
| 254 |
|
| 255 |
cst_free(outbuf); |
| 256 |
cst_free(lpccoefs); |
| 257 |
w->num_samples = r; /* just to be safe */ |
| 258 |
|
| 259 |
if (rc == CST_AUDIO_STREAM_STOP) |
| 260 |
{ |
| 261 |
delete_wave(w); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
else |
| 265 |
return w; |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
cst_wave *lpc_resynth_sfp(cst_lpcres *lpcres) |
| 270 |
{ |
| 271 |
/* The fixed point spike excited, without floats */ |
| 272 |
cst_wave *w; |
| 273 |
int i,j,r,o,k; |
| 274 |
int ci,cr; |
| 275 |
int *outbuf, *lpccoefs; |
| 276 |
int pm_size_samps, ilpc_min, ilpc_range; |
| 277 |
|
| 278 |
/* Get a new wave to build the signal into */ |
| 279 |
w = new_wave(); |
| 280 |
cst_wave_resize(w,lpcres->num_samples,1); |
| 281 |
w->sample_rate = lpcres->sample_rate; |
| 282 |
/* outbuf is a circular buffer with past relevant samples in it */ |
| 283 |
outbuf = cst_alloc(int,1+lpcres->num_channels); |
| 284 |
/* unpacked lpc coefficients */ |
| 285 |
lpccoefs = cst_alloc(int,lpcres->num_channels); |
| 286 |
ilpc_min = (int)(lpcres->lpc_min*32768.0); |
| 287 |
/* assume range is never > abs(16) */ |
| 288 |
ilpc_range = (int)(lpcres->lpc_range*2048.0); |
| 289 |
|
| 290 |
for (r=0,o=lpcres->num_channels,i=0; i < lpcres->num_frames; i++) |
| 291 |
{ |
| 292 |
pm_size_samps = lpcres->sizes[i]; |
| 293 |
|
| 294 |
/* Unpack the LPC coefficients */ |
| 295 |
for (k=0; k<lpcres->num_channels; k++) |
| 296 |
lpccoefs[k]=((lpcres->frames[i][k]/2*ilpc_range)/2048+ilpc_min)/2; |
| 297 |
|
| 298 |
/* resynthesis the signal */ |
| 299 |
for (j=0; j < pm_size_samps; j++,r++) |
| 300 |
{ |
| 301 |
outbuf[o] = (int)cst_ulaw_to_short(lpcres->residual[r]); |
| 302 |
cr = (o == 0 ? lpcres->num_channels : o-1); |
| 303 |
for (ci=0; ci < lpcres->num_channels; ci++) |
| 304 |
{ |
| 305 |
outbuf[o] += (lpccoefs[ci]*outbuf[cr])/16384; |
| 306 |
cr = (cr == 0 ? lpcres->num_channels : cr-1); |
| 307 |
} |
| 308 |
w->samples[r] = (short)outbuf[o]; |
| 309 |
o = (o == lpcres->num_channels ? 0 : o+1); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
cst_free(outbuf); |
| 314 |
cst_free(lpccoefs); |
| 315 |
|
| 316 |
return w; |
| 317 |
|
| 318 |
} |