📄 lpc10dec.c
字号:
lpi0 = temp = sscale * kexc[i];
temp = lpi0 * .125f + lpi1 * .75f + lpi2 * .125f;
lpi2 = lpi1;
lpi1 = lpi0;
}
else{
lpi0 = temp = 0.f;
temp = lpi1 * .75f + lpi2 * .125f;
lpi2 = lpi1;
lpi1 = lpi0;
}
hpi0 = (float)(random16() >> 6);
exc[10 + i] = temp + hpi0 * -.125f + hpi1 * .25f + hpi2 * -.125f;
hpi2 = hpi1;
hpi1 = hpi0;
}
}
/* Synthesis filters: */
/* Modify the excitation with all-zero filter 1 + G*SUM */
xssq = 0.f;
for (i = 0; i < ip; ++i) {
k = 10 + i;
sum = 0.f;
for (j = 1; j <= 10; ++j) {
sum += coef[j] * exc[k - j];
}
sum *= *g2pass;
exc2[k] = sum + exc[k];
}
/* Synthesize using the all pole filter 1 / (1 - SUM) */
for (i = 0; i < ip; ++i) {
k = 10 + i;
sum = 0.f;
for (j = 1; j <= 10; ++j) {
sum += coef[j] * exc2[k - j];
}
exc2[k] += sum;
xssq += exc2[k] * exc2[k];
}
/* Save filter history for next epoch */
for (i = 0; i < 10; ++i) {
exc[i] = exc[ip + i];
exc2[i] = exc2[ip + i];
}
/* Apply gain to match RMS */
ssq = *rms * *rms * ip;
gain = (float)sqrt(ssq / xssq);
for (i = 0; i < ip; ++i) {
sout[i] = gain * exc2[10 + i];
}
st->lpi1 = lpi1;
st->lpi2 = lpi2;
st->hpi1 = hpi1;
st->hpi2 = hpi2;
} /* bsynz_ */
static void irc2pc(float *rc, float *pc, float gprime, float *g2pass)
{
/* System generated locals */
long i2;
/* Local variables */
float temp[10];
long i, j;
/* Parameter adjustments */
--pc;
--rc;
/* Function Body */
*g2pass = 1.f;
for (i = 1; i <= 10; ++i) {
*g2pass *= 1.f - rc[i] * rc[i];
}
*g2pass = gprime * (float)sqrt(*g2pass);
pc[1] = rc[1];
for (i = 2; i <= 10; ++i) {
i2 = i - 1;
for (j = 1; j <= i2; ++j) {
temp[j - 1] = pc[j] - rc[i] * pc[i - j];
}
i2 = i - 1;
for (j = 1; j <= i2; ++j) {
pc[j] = temp[j - 1];
}
pc[i] = rc[i];
}
} /* irc2pc_ */
static void deemp(float *x, long n, lpc10_decoder_state *st)
{
/* Initialized data */
float dei1;
float dei2;
float deo1;
float deo2;
float deo3;
/* Local variables */
long k;
float dei0;
/* Function Body */
dei1 = st->dei1;
dei2 = st->dei2;
deo1 = st->deo1;
deo2 = st->deo2;
deo3 = st->deo3;
for (k = 0; k < n; ++k) {
dei0 = x[k];
x[k] = dei0 - dei1 * 1.9998f + dei2 + deo1 * 2.5f - deo2 * 2.0925f + deo3 * .585f;
dei2 = dei1;
dei1 = dei0;
deo3 = deo2;
deo2 = deo1;
deo1 = x[k];
}
st->dei1 = dei1;
st->dei2 = dei2;
st->deo1 = deo1;
st->deo2 = deo2;
st->deo3 = deo3;
} /* deemp_ */
static void synths(long *voice, long pitch, float rms, float *rc, short *speech, lpc10_decoder_state *st)
{
/* Initialized data */
float *buf;
long *buflen;
/* Local variables */
float rmsi[16];
long nout, ivuv[16], i, j;
float ratio;
long ipiti[16];
float g2pass;
float pc[10];
float rci[160] /* was [10][16] */;
/* Function Body */
buf = &(st->buf[0]);
buflen = &(st->buflen);
pitch = max(min(pitch,156), 20);
for (i = 0; i < 10; ++i) {
rc[i] = max(min(rc[i],.99f), -.99f);
}
pitsyn(voice, &pitch, &rms, rc, ivuv, ipiti, rmsi, rci, &nout, &ratio, st);
if (nout > 0) {
for (j = 0; j < nout; ++j) {
irc2pc(&rci[j * 10], pc, 0.7f, &g2pass);
bsynz(pc, ipiti[j], &ivuv[j], &buf[*buflen], &rmsi[j], &ratio, &g2pass, st);
deemp(&buf[*buflen], ipiti[j], st);
*buflen += ipiti[j];
}
for (i = 0; i < LPC10_SAMPLES_PER_FRAME; ++i) {
speech[i] = (short) max(-32768, min(lrintf(8.0f * buf[i]), 32767));
}
*buflen -= LPC10_SAMPLES_PER_FRAME;
for (i = 0; i < *buflen; ++i) {
buf[i] = buf[i + LPC10_SAMPLES_PER_FRAME];
}
}
} /* synths_ */
static void decode(long ipitv, long irms, long *irc, long *voice, long *pitch,
float *rms, float *rc, lpc10_decoder_state *st)
{
/* Local variables */
long i, i1, i2, i4;
long ishift;
/* Function Body */
i4 = detau[ipitv];
voice[0] = 1;
voice[1] = 1;
if (ipitv <= 1) {
voice[0] = 0;
}
if (ipitv == 0 || ipitv == 2) {
voice[1] = 0;
}
*pitch = i4;
if (*pitch <= 4) {
*pitch = st->iptold;
}
if (voice[0] == 1 && voice[1] == 1) {
st->iptold = *pitch;
}
if (voice[0] != voice[1]) {
*pitch = st->iptold;
}
/* Decode RMS */
irms = rmst[(31 - irms) * 2];
/* Decode RC(1) and RC(2) from log-area-ratios */
/* Protect from illegal coded value (-16) caused by bit errors */
for (i = 0; i < 2; ++i) {
i2 = irc[i];
i1 = 0;
if (i2 < 0) {
i1 = 1;
i2 = -i2;
if (i2 > 15) {
i2 = 0;
}
}
i2 = detab7[i2 * 2];
if (i1 == 1) {
i2 = -i2;
}
ishift = 15 - nbit[i];
irc[i] = i2 * (2 << (ishift-1));
}
/* Decode RC(3)-RC(10) to sign plus 14 bits */
for (i = 2; i < 10; ++i) {
i2 = irc[i];
ishift = 15 - nbit[i];
i2 *= (2 << (ishift-1));
i2 += qb[i - 2];
irc[i] = (int)(i2 * descl[i - 2] + deadd[i - 2]);
}
/* Scale RMS and RC's to floats */
*rms = (float) (irms);
for (i = 0; i < 10; ++i) {
rc[i] = irc[i] / 16384.f;
}
} /* decode_ */
static void unpack(long *array, long bits, long *value, long *pointer)
{
int i;
for (i = 0, *value = 0; i < bits; i++, (*pointer)++)
*value |= array[*pointer] << i;
}
static void chanrd(long *ipitv, long *irms, long *irc, long *ibits, int *p, int vbr)
{
static long bit[] = {0, 1, 2, 4, 8, 16, 32, 64, 128};
long pointer, i;
pointer = 0;
unpack(ibits, 7, ipitv, &pointer);
if(vbr == TRUE)
{
/* check for silence packet */
if (*ipitv == 127) {
*ipitv = 0;
*irms = 0;
*p = 1;
return;
}
}
unpack(ibits, 5, irms, &pointer);
*p = 4;
for (i = 0; i < 4; ++i) {
unpack(ibits, lpcbits[i], &irc[i], &pointer);
}
if ((*ipitv != 0 && *ipitv != 126) || vbr == FALSE) {
*p = 7;
for (i = 4; i < 10; ++i) {
unpack(ibits, lpcbits[i], &irc[i], &pointer);
}
}
/* Sign extend RC's */
for (i = 0; i < 10; ++i) {
if ((irc[i] & bit[lpcbits[i]]) != 0) {
irc[i] -= bit[lpcbits[i]] << 1;
}
}
}
static int lpc10_decode_int(unsigned char *in, short *speech,
lpc10_decoder_state *st, int *p, int vbr)
{
long bits[LPC10_BITS_IN_COMPRESSED_FRAME];
int i;
long irms, voice[2], pitch, ipitv;
float rc[10];
long irc[10];
float rms;
/* unpack bits into array */
for (i = 0; i < LPC10_BITS_IN_COMPRESSED_FRAME; i++)
{
bits[i] = (in[i >> 3] & (1 << (i & 7))) != 0 ? 1 : 0;
}
/* decode speech */
memset(irc, 0, sizeof(irc));
chanrd(&ipitv, &irms, irc, bits, p, vbr);
decode(ipitv, irms, irc, voice, &pitch, &rms, rc, st);
synths(voice, pitch, rms, rc, speech, st);
return LPC10_SAMPLES_PER_FRAME;
}
int lpc10_decode(unsigned char *in, short *speech, lpc10_decoder_state *st)
{
int p = 0;
return lpc10_decode_int(in, speech, st, &p, FALSE);
}
int vbr_lpc10_decode(unsigned char *in, short *speech, lpc10_decoder_state *st, int *p)
{
return lpc10_decode_int(in, speech, st, p, TRUE);
}
/* Allocate memory for, and initialize, the state that needs to be
kept from decoding one frame to the next for a single
LPC-10-compressed audio stream. Return 0 if malloc fails,
otherwise return pointer to new structure. */
lpc10_decoder_state *create_lpc10_decoder_state(void)
{
lpc10_decoder_state *st;
st = (lpc10_decoder_state *)malloc((unsigned) sizeof (lpc10_decoder_state));
return (st);
}
void init_lpc10_decoder_state(lpc10_decoder_state *st)
{
int i;
/* State used by function decode */
st->iptold = 60;
/* State used by function synths */
for (i = 0; i < 360; i++) {
st->buf[i] = 0.0f;
}
st->buflen = 180;
/* State used by function pitsyn */
st->rmso = 1.0f;
st->first_pitsyn = TRUE;
/* State used by function bsynz */
st->ipo = 0;
for (i = 0; i < 166; i++) {
st->exc[i] = 0.0f;
st->exc2[i] = 0.0f;
}
st->lpi1 = 0.0f;
st->lpi2 = 0.0f;
st->lpi3 = 0.0f;
st->hpi1 = 0.0f;
st->hpi2 = 0.0f;
st->hpi3 = 0.0f;
st->rmso_bsynz = 0.0f;
/* State used by function deemp */
st->dei1 = 0.0f;
st->dei2 = 0.0f;
st->deo1 = 0.0f;
st->deo2 = 0.0f;
st->deo3 = 0.0f;
}
void destroy_lpc10_decoder_state (lpc10_decoder_state *st)
{
if(st != NULL)
{
free(st);
st = NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -