1 /************************************************************************\
2
3 Low bitrate LPC CODEC derived from the public domain implementation
4 of Ron Frederick.
5
6 The basic design is preserved, except for several bug fixes and
7 the following modifications:
8
9 1. The pitch detector operates on the (downsampled) signal, not on
10 the residue. This appears to yield better performances, and it
11 lowers the processing load.
12 2. Each frame is elongated by 50% prefixing it with the last half
13 of the previous frame. This design, inherited from the original
14 code for windowing purposes, is exploited in order to provide
15 two independent pitch analyses: on the first 2/3, and on the
16 second 2/3 of the elongated frame (of course, they overlap by
17 half):
18
19 last half old frame new frame
20 --------------------========================================
21 <--------- first pitch region --------->
22 <--------- second pitch region ------->
23
24 Two voiced/unvoiced flags define the voicing status of each
25 region; only one value for the period is retained (if both
26 halves are voiced, the average is used).
27 The two flags are used by the synthesizer for the halves of
28 each frame to play back. Of course, this is non optimal but
29 is good enough (a half-frame would be too short for measuring
30 low pitches)
31 3. The parameters (one float for the period (pitch), one for the
32 gain, and ten for the LPC-10 filter) are quantized according
33 this procedure:
34 - the period is logarithmically compressed, then quantized
35 as 8-bit unsigned int (6 would actually suffice)
36 - the gain is logarithmically compressed (using a different
37 formula), then quantized at 6-bit unsigned int. The two
38 remaining bits are used for the voicing flags.
39 - the first two LPC parameters (k[1] and k[2]) are multiplied
40 by PI/2, and the arcsine of the result is quantized as
41 6 and 5 bit signed integers. This has proved more effective
42 than the log-area compression used by LPC-10.
43 - the remaining eight LPC parameters (k[3]...k[10]) are
44 quantized as, respectively, 5,4,4,3,3,3,3 and 2 bit signed
45 integers.
46 Finally, period and gain plus voicing flags are stored in the
47 first two bytes of the 7-byte parameters block, and the quantized
48 LPC parameters are packed into the remaining 5 bytes. Two bits
49 remain unassigned, and can be used for error detection or other
50 purposes.
51
52 The frame lenght is actually variable, and is simply passed as
53 initialization parameter to lpc_init(): this allows to experiment
54 with various frame lengths. Long frames reduce the bitrate, but
55 exceeding 320 samples (i.e. 40 ms, at 8000 samples/s) tend to
56 deteriorate the speech, that sounds like spoken by a person
57 affected by a stroke: the LPC parameters (modeling the vocal
58 tract) can't change fast enough for a natural-sounding synthesis.
59 25 ms per frame already yields a quite tight compression, corresponding
60 to 1000/40 * 7 * 8 = 1400 bps. The quality improves little with
61 frames shorter than 250 samples (32 frames/s), so this is a recommended
62 compromise. The bitrate is 32 * 7 * 8 = 1792 bps.
63
64 The synthesizer has been modified as well. For voiced subframes it
65 now uses a sawtooth excitation, instead of the original pulse train.
66 This idea, copied from MELP, reduces the buzzing-noise artifacts.
67 In order to compensate the non-white spectrum of the sawtooth, a
68 pre-emphasis is applied to the signal before the Durbin calculation.
69 The filter has (in s-space) two zeroes at (640, 0) Hz and two poles
70 at (3200, 0) Hz. These filters have been handoded, and may not be
71 optimal. Two other filters (anti-hum high-pass with corner at 100 Hz,
72 and pre-downsampling lowpass with corner at 300 Hz) are Butterworth
73 designs produced by the MkFilter package by A.J. Fisher
74 (http://www.cs.york.ac.uk/~fisher/mkfilter/).
75
76 The C style has been ANSI-fied.
77
78 Complexity: As any LPC CODEC, also this one is not very demanding:
79 for real-time use analysis and synthesis takes each about 6 - 8%
80 of the CPU cycles on a Cy686/166, when the code is compiled with
81 MSVC++ 4.2 with /Ox or gcc with -O3.
82 However, a floating point unit is absolutely required.
83
84
85 \************************************************************************/
86
87 #include <stdio.h>
88 #include <math.h>
89 #include <string.h>
90 #include <stdlib.h>
91
92 #include "soundpipe.h"
93 #include "openlpc.h"
94 #include "ftol.h"
95
96 #define PREEMPH
97
98 #define bcopy(a, b, n) memmove(b, a, n)
99
100 #ifndef M_PI
101 #define M_PI (3.1415926535897932384626433832795)
102 #endif
103
104 static float my_fs = 11025.0;
105 #define LPC_FILTORDER 10
106 #define FS my_fs /* Sampling rate */
107 #define MAXWINDOW 1000 /* Max analysis window length */
108
109 typedef struct openlpc_e_state{
110 float s[MAXWINDOW], y[MAXWINDOW], h[MAXWINDOW];
111 int framelen, buflen;
112 float xv1[3], yv1[3],
113 xv2[2], yv2[2],
114 xv3[1], yv3[3],
115 xv4[2], yv4[2];
116 float w[MAXWINDOW], r[LPC_FILTORDER+1];
117 } openlpc_e_state_t;
118
119 typedef struct openlpc_d_state{
120 float Oldper, OldG, Oldk[LPC_FILTORDER + 1];
121 float bp[LPC_FILTORDER+1];
122 float exc;
123 int pitchctr, framelen, buflen;
124 } openlpc_d_state_t;
125
126 #define FC 200.0 /* Pitch analyzer filter cutoff */
127 #define DOWN 5 /* Decimation for pitch analyzer */
128 #define MINPIT 40.0 /* Minimum pitch (observed: 74) */
129 #define MAXPIT 320.0 /* Maximum pitch (observed: 250) */
130
131 #define MINPER (int)(FS/(DOWN*MAXPIT)+.5) /* Minimum period */
132 #define MAXPER (int)(FS/(DOWN*MINPIT)+.5) /* Maximum period */
133
134 #define REAL_MINPER (DOWN*MINPER) /* converted to samples units */
135
136 #define WSCALE 1.5863 /* Energy loss due to windowing */
137
138 #define BITS_FOR_LPC 38
139
140 #define ARCSIN_Q /* provides better quantization of first two k[] at low bitrates */
141
142 #if BITS_FOR_LPC == 38
143 /* (38 bit LPC-10, 2.7 Kbit/s @ 20ms, 2.4 Kbit/s @ 22.5 ms */
144 static int parambits[LPC_FILTORDER] = {6,5,5,4,4,3,3,3,3,2};
145 #elif BITS_FOR_LPC == 32
146 /* (32 bit LPC-10, 2.4 Kbit/s, not so good */
147 static int parambits[LPC_FILTORDER] = {5,5,5,4,3,3,2,2,2,1};
148 #else /* BITS_FOR_LPC == 80 */
149 /* 80-bit LPC10, 4.8 Kbit/s */
150 static int parambits[LPC_FILTORDER] = {8,8,8,8,8,8,8,8,8,8};
151 #endif
152
153 static float logmaxminper;
154 static int sizeofparm; /* computed by lpc_init */
155
auto_correl1(float * w,int n,float * r)156 static void auto_correl1(float *w, int n, float *r)
157 {
158 int i, k;
159
160 for (k=0; k <= MAXPER; k++, n--) {
161 r[k] = 0.0;
162 for (i=0; i < n; i++) {
163 r[k] += (w[i] * w[i+k]);
164 }
165 }
166 }
167
auto_correl2(float * w,int n,float * r)168 static void auto_correl2(float *w, int n, float *r)
169 {
170 int i, k;
171
172 for (k=0; k <= LPC_FILTORDER; k++, n--) {
173 r[k] = 0.0;
174 for (i=0; i < n; i++) {
175 r[k] += (w[i] * w[i+k]);
176 }
177 }
178 }
179
durbin(float r[],int p,float k[],float * g)180 static void durbin(float r[], int p, float k[], float *g)
181 {
182 int i, j;
183 float a[LPC_FILTORDER+1], at[LPC_FILTORDER+1], e;
184
185 for (i=0; i <= p; i++) a[i] = at[i] = 0.0;
186
187 e = r[0];
188 for (i=1; i <= p; i++) {
189 k[i] = -r[i];
190 for (j=1; j < i; j++) {
191 at[j] = a[j];
192 k[i] -= a[j] * r[i-j];
193 }
194 if (e == 0) { /* fix by John Walker */
195 *g = 0;
196 return;
197 }
198 k[i] /= e;
199 a[i] = k[i];
200 for (j=1; j < i; j++) a[j] = at[j] + k[i] * at[i-j];
201 e *= 1.0f - k[i]*k[i];
202 }
203 if (e < 0) {
204 e = 0; /* fix by John Walker */
205 }
206 *g = (float)sqrt(e);
207 }
208
209 /* Enzo's streamlined pitch extractor - on the signal, not the residue */
210
calc_pitch(float * w,int len,float * per)211 static void calc_pitch(float *w, int len, float *per)
212 {
213 int i, j, rpos;
214 float d[MAXWINDOW/DOWN], r[MAXPER+1], rmax;
215 float rval, rm, rp;
216 float x, y;
217 float vthresh;
218
219 /* decimation */
220 for (i=0, j=0; i < len; i+=DOWN)
221 d[j++] = w[i];
222
223 auto_correl1(d, len/DOWN, r);
224
225 /* find peak between MINPER and MAXPER */
226 x = 1;
227 rpos = 0;
228 rmax = 0.0;
229 y = 0;
230 rm = 0;
231 rp = 0;
232
233 vthresh = 0.;
234
235 for (i = 1; i < MAXPER; i++) {
236 rm = r[i-1];
237 rp = r[i+1];
238 y = rm+r[i]+rp; /* find max of integral from i-1 to i+1 */
239
240 if ((y > rmax) && (r[i] > rm) && (r[i] > rp) && (i > MINPER)) {
241 rmax = y;
242 rpos = i;
243 }
244 }
245
246 /* consider adjacent values */
247 rm = r[rpos-1];
248 rp = r[rpos+1];
249
250 #if 0
251 {
252 float a, b, c, x, y;
253 /* parabolic interpolation */
254 a = 0.5f * rm - rmax + 0.5f * rp;
255 b = -0.5f*rm*(2.0f*rpos+1.0f) + 2.0f*rpos*rmax + 0.5f*rp*(1.0f-2.0f*rpos);
256 c = 0.5f*rm*(rpos*rpos+rpos) + rmax*(1.0f-rpos*rpos) + 0.5f*rp*(rpos*rpos-rpos);
257
258 /* find max of interpolating parabole */
259 x = -b / (2.0f * a);
260 y = a*x*x + b*x + c;
261
262 rmax = y;
263 /* normalize, so that 0. < rval < 1. */
264 rval = (r[0] == 0 ? 1.0f : rmax / r[0]);
265 }
266 #else
267 if(rpos > 0) {
268 x = ((rpos-1)*rm + rpos*r[rpos] + (rpos+1)*rp)/(rm+r[rpos]+rp);
269 }
270 /* normalize, so that 0. < rval < 1. */
271 rval = (r[0] == 0 ? 0 : r[rpos] / r[0]);
272 #endif
273
274 /* periods near the low boundary and at low volumes
275 are usually spurious and
276 manifest themselves as annoying mosquito buzzes */
277
278 *per = 0; /* default: unvoiced */
279 if ( x > MINPER && /* x could be < MINPER or even < 0 if pos == MINPER */
280 x < (MAXPER+1) /* same story */
281 ) {
282
283 vthresh = 0.6f;
284 if(r[0] > 0.002) /* at low volumes (< 0.002), prefer unvoiced */
285 vthresh = 0.25; /* drop threshold at high volumes */
286
287 if(rval > vthresh)
288 *per = x * DOWN;
289 }
290 }
291
292 /* Initialization of various parameters */
293
create_openlpc_encoder_state(void)294 openlpc_encoder_state *create_openlpc_encoder_state(void)
295 {
296 openlpc_encoder_state *state;
297
298 state = (openlpc_encoder_state *)calloc(1, sizeof(openlpc_encoder_state));
299
300 return state;
301 }
302
303
init_openlpc_encoder_state(openlpc_encoder_state * st,int framelen)304 void init_openlpc_encoder_state(openlpc_encoder_state *st, int framelen)
305 {
306 int i, j;
307
308 st->framelen = framelen;
309
310 st->buflen = framelen*3/2;
311 /* (st->buflen > MAXWINDOW) return -1;*/
312
313 for(i=0, j=0; i<sizeof(parambits)/sizeof(parambits[0]); i++) {
314 j += parambits[i];
315 }
316 sizeofparm = (j+7)/8 + 2;
317 for (i = 0; i < st->buflen; i++) {
318 st->s[i] = 0.0;
319 st->h[i] = (float)(WSCALE*(0.54 - 0.46 * cos(2 * M_PI * i / (st->buflen-1.0))));
320 }
321 /* init the filters */
322 st->xv1[0] = st->xv1[1] = st->xv1[2] = st->yv1[0] = st->yv1[1] = st->yv1[2] = 0.0f;
323 st->xv2[0] = st->xv2[1] = st->yv2[0] = st->yv2[1] = 0.0f;
324 st->xv3[0] = st->yv3[0] = st->yv3[1] = st->yv3[2] = 0.0f;
325 st->xv4[0] = st->xv4[1] = st->yv4[0] = st->yv4[1] = 0.0f;
326
327 logmaxminper = (float)log((float)MAXPER/MINPER);
328
329 }
330
destroy_openlpc_encoder_state(openlpc_encoder_state * st)331 void destroy_openlpc_encoder_state(openlpc_encoder_state *st)
332 {
333 if(st != NULL)
334 {
335 free(st);
336 st = NULL;
337 }
338 }
339
340 /* LPC Analysis (compression) */
341
openlpc_encode(const short * buf,unsigned char * parm,openlpc_encoder_state * st)342 int openlpc_encode(const short *buf, unsigned char *parm, openlpc_encoder_state *st)
343 {
344 int i, j;
345 float per, gain, k[LPC_FILTORDER+1];
346 float per1, per2;
347 float xv10, xv11, xv12, yv10, yv11, yv12,
348 xv20, xv21, yv20, yv21,
349 xv30, yv30, yv31, yv32,
350 xv40, xv41, yv40, yv41;
351
352 xv10 = st->xv1[0];
353 xv11 = st->xv1[1];
354 xv12 = st->xv1[2];
355 yv10 = st->yv1[0];
356 yv11 = st->yv1[1];
357 yv12 = st->yv1[2];
358 xv30 = st->xv3[0];
359 yv30 = st->yv3[0];
360 yv31 = st->yv3[1];
361 yv32 = st->yv3[2];
362 for(i = 0; i < LPC_FILTORDER + 1; i++) k[i] = 0;
363 /* convert short data in buf[] to signed lin. data in s[] and prefilter */
364 for (i=0, j=st->buflen - st->framelen; i < st->framelen; i++, j++) {
365
366 float u = (float)(buf[i]/32768.0f);
367
368 /* Anti-hum 2nd order Butterworth high-pass, 100 Hz corner frequency */
369 /* Digital filter designed by mkfilter/mkshape/gencode A.J. Fisher
370 mkfilter -Bu -Hp -o 2 -a 0.0125 -l -z */
371
372 xv10 = xv11;
373 xv11 = xv12;
374 xv12 = (float)(u * 0.94597831f); /* /GAIN */
375
376 yv10 = yv11;
377 yv11 = yv12;
378 yv12 = (float)((xv10 + xv12) - 2 * xv11
379 + ( -0.8948742499f * yv10) + ( 1.8890389823f * yv11));
380
381 u = st->s[j] = yv12; /* also affects input of next stage, to the LPC filter synth */
382
383 /* low-pass filter s[] -> y[] before computing pitch */
384 /* second-order Butterworth low-pass filter, corner at 300 Hz */
385 /* Digital filter designed by mkfilter/mkshape/gencode A.J. Fisher
386 MKFILTER.EXE -Bu -Lp -o 2 -a 0.0375 -l -z */
387
388 /*st->xv3[0] = (float)(u / 2.127814584e+001);*/ /* GAIN */
389 xv30 = (float)(u * 0.04699658f); /* GAIN */
390 yv30 = yv31;
391 yv31 = yv32;
392 yv32 = xv30 + (float)(( -0.7166152306f * yv30) + (1.6696186545f * yv31));
393 st->y[j] = yv32;
394 }
395 st->xv1[0] = xv10;
396 st->xv1[1] = xv11;
397 st->xv1[2] = xv12;
398 st->yv1[0] = yv10;
399 st->yv1[1] = yv11;
400 st->yv1[2] = yv12;
401 st->xv3[0] = xv30;
402 st->yv3[0] = yv30;
403 st->yv3[1] = yv31;
404 st->yv3[2] = yv32;
405 #ifdef PREEMPH
406 /* operate optional preemphasis s[] -> s[] on the newly arrived frame */
407 xv20 = st->xv2[0];
408 xv21 = st->xv2[1];
409 yv20 = st->yv2[0];
410 yv21 = st->yv2[1];
411 xv40 = st->xv4[0];
412 xv41 = st->xv4[1];
413 yv40 = st->yv4[0];
414 yv41 = st->yv4[1];
415 for (j=st->buflen - st->framelen; j < st->buflen; j++) {
416 float u = st->s[j];
417
418 /* handcoded filter: 1 zero at 640 Hz, 1 pole at 3200 */
419 #define TAU (FS/3200.f)
420 #define RHO (0.1f)
421 xv20 = xv21; /* e(n-1) */
422 xv21 = (float)(u * 1.584f); /* e(n) , add 4 dB to compensate attenuation */
423 yv20 = yv21;
424 yv21 = (float)(TAU/(1.f+RHO+TAU) * yv20 /* u(n) */
425 + (RHO+TAU)/(1.f+RHO+TAU) * xv21
426 - TAU/(1.f+RHO+TAU) * xv20);
427 u = yv21;
428
429 /* cascaded copy of handcoded filter: 1 zero at 640 Hz, 1 pole at 3200 */
430 xv40 = xv41;
431 xv41 = (float)(u * 1.584f);
432 yv40 = yv41;
433 yv41 = (float)(TAU/(1.f+RHO+TAU) * yv40
434 + (RHO+TAU)/(1.f+RHO+TAU) * xv41
435 - TAU/(1.f+RHO+TAU) * xv40);
436 u = yv41;
437
438 st->s[j] = u;
439 }
440 st->xv2[0] = xv20;
441 st->xv2[1] = xv21;
442 st->yv2[0] = yv20;
443 st->yv2[1] = yv21;
444 st->xv4[0] = xv40;
445 st->xv4[1] = xv41;
446 st->yv4[0] = yv40;
447 st->yv4[1] = yv41;
448 #endif
449
450 /* operate windowing s[] -> w[] */
451
452 for (i=0; i < st->buflen; i++)
453 st->w[i] = st->s[i] * st->h[i];
454
455 /* compute LPC coeff. from autocorrelation (first 11 values) of windowed data */
456 auto_correl2(st->w, st->buflen, st->r);
457 durbin(st->r, LPC_FILTORDER, k, &gain);
458
459 /* calculate pitch */
460 calc_pitch(st->y, st->framelen, &per1); /* first 2/3 of buffer */
461 calc_pitch(st->y + st->buflen - st->framelen, st->framelen, &per2); /* last 2/3 of buffer */
462 if(per1 > 0 && per2 >0)
463 per = (per1+per2)/2;
464 else if(per1 > 0)
465 per = per1;
466 else if(per2 > 0)
467 per = per2;
468 else
469 per = 0;
470
471 /* logarithmic q.: 0 = MINPER, 256 = MAXPER */
472 parm[0] = (unsigned char)(per == 0? 0 : (unsigned char)(log(per/(REAL_MINPER)) / logmaxminper * (1<<8)));
473
474 #ifdef LINEAR_G_Q
475 i = gain * (1<<7);
476 if(i > 255) /* bug fix by EM */
477 i = 255;
478 #else
479 i = (int)(float)(256.0f * log(1 + (2.718-1.f)/10.f*gain)); /* deriv = 5.82 allowing to reserve 2 bits */
480 if(i > 255) i = 255; /* reached when gain = 10 */
481 i = (i+2) & 0xfc;
482 #endif
483
484 parm[1] = (unsigned char)i;
485
486 if(per1 > 0)
487 parm[1] |= 1;
488 if(per2 > 0)
489 parm[1] |= 2;
490
491 for(j=2; j < sizeofparm; j++)
492 parm[j] = 0;
493
494 for (i=0; i < LPC_FILTORDER; i++) {
495 int bitamount = parambits[i];
496 int bitc8 = 8-bitamount;
497 int q = (1 << bitc8); /* quantum: 1, 2, 4... */
498 float u = k[i+1];
499 int iu;
500 #ifdef ARCSIN_Q
501 if(i < 2) u = (float)(asin(u)*2.f/M_PI);
502 #endif
503 u *= 127;
504 if(u < 0)
505 u += (0.6f * q);
506 else
507 u += (0.4f * q); /* highly empirical! */
508
509 iu = lrintf(u);
510 iu = iu & 0xff; /* keep only 8 bits */
511
512 /* make room at the left of parm array shifting left */
513 for(j=sizeofparm-1; j >= 3; j--) {
514 parm[j] = (unsigned char)((parm[j] << bitamount) | (parm[j-1] >> bitc8));
515 }
516 parm[2] = (unsigned char)((parm[2] << bitamount) | (iu >> bitc8)); /* parm[2] */
517 }
518
519 bcopy(st->s + st->framelen, st->s, (st->buflen - st->framelen)*sizeof(st->s[0]));
520 bcopy(st->y + st->framelen, st->y, (st->buflen - st->framelen)*sizeof(st->y[0]));
521
522 return sizeofparm;
523 }
524
create_openlpc_decoder_state(void)525 openlpc_decoder_state *create_openlpc_decoder_state(void)
526 {
527 openlpc_decoder_state *state;
528
529 state = (openlpc_decoder_state *)calloc(1, sizeof(openlpc_decoder_state));
530
531 return state;
532 }
533
init_openlpc_decoder_state(openlpc_decoder_state * st,int framelen)534 void init_openlpc_decoder_state(openlpc_decoder_state *st, int framelen)
535 {
536 int i, j;
537
538 st->Oldper = 0.0f;
539 st->OldG = 0.0f;
540 for (i = 0; i <= LPC_FILTORDER; i++) {
541 st->Oldk[i] = 0.0f;
542 st->bp[i] = 0.0f;
543 }
544 st->pitchctr = 0;
545 st->exc = 0.0f;
546 logmaxminper = (float)log((float)MAXPER/MINPER);
547
548 for(i=0, j=0; i<sizeof(parambits)/sizeof(parambits[0]); i++) {
549 j += parambits[i];
550 }
551 sizeofparm = (j+7)/8 + 2;
552
553 /* test for a valid frame len? */
554 st->framelen = framelen;
555 st->buflen = framelen*3/2;
556 }
557
558 /* LPC Synthesis (decoding) */
559
openlpc_decode(sp_data * sp,unsigned char * parm,short * buf,openlpc_decoder_state * st)560 int openlpc_decode(sp_data *sp, unsigned char *parm, short *buf, openlpc_decoder_state *st)
561 {
562 int i, j, flen=st->framelen;
563 float per, gain, k[LPC_FILTORDER+1];
564 float f, u, newgain, Ginc, Newper, perinc;
565 float Newk[LPC_FILTORDER+1], kinc[LPC_FILTORDER+1];
566 float gainadj;
567 int hframe;
568 float hper[2];
569 int ii;
570 float bp0, bp1, bp2, bp3, bp4, bp5, bp6, bp7, bp8, bp9, bp10;
571 float kj;
572
573 bp0 = st->bp[0];
574 bp1 = st->bp[1];
575 bp2 = st->bp[2];
576 bp3 = st->bp[3];
577 bp4 = st->bp[4];
578 bp5 = st->bp[5];
579 bp6 = st->bp[6];
580 bp7 = st->bp[7];
581 bp8 = st->bp[8];
582 bp9 = st->bp[9];
583 bp10 = st->bp[10];
584
585 per = (float)(parm[0]);
586
587 per = (float)(per == 0? 0: REAL_MINPER * exp(per/(1<<8) * logmaxminper));
588
589 hper[0] = hper[1] = per;
590
591 if((parm[1] & 0x1) == 0) hper[0] = 0;
592 if((parm[1] & 0x2) == 0) hper[1] = 0;
593
594 #ifdef LINEAR_G_Q
595 gain = (float)parm[1] / (1<<7);
596 #else
597 gain = (float)parm[1] / 256.f;
598 gain = (float)((exp(gain) - 1)/((2.718-1.f)/10));
599 #endif
600
601 k[0] = 0.0;
602
603 for (i=LPC_FILTORDER-1; i >= 0; i--) {
604 int bitamount = parambits[i];
605 int bitc8 = 8-bitamount;
606 /* casting to char should set the sign properly */
607 char c = (char)(parm[2] << bitc8);
608
609 for(j=2; j<sizeofparm; j++)
610 parm[j] = (unsigned char)((parm[j] >> bitamount) | (parm[j+1] << bitc8));
611
612 k[i+1] = ((float)c / (1<<7));
613 #ifdef ARCSIN_Q
614 if(i<2) k[i+1] = (float)sin(M_PI/2*k[i+1]);
615 #endif
616 }
617
618 /* k[] are the same in the two subframes */
619 for (i=1; i <= LPC_FILTORDER; i++) {
620 Newk[i] = st->Oldk[i];
621 kinc[i] = (k[i] - st->Oldk[i]) / flen;
622 }
623
624 /* Loop on two half frames */
625
626 for(hframe=0, ii=0; hframe<2; hframe++) {
627
628 Newper = st->Oldper;
629 newgain = st->OldG;
630
631 Ginc = (gain - st->OldG) / (flen/2);
632 per = hper[hframe];
633
634 if (per == 0.0) { /* if unvoiced */
635 gainadj = /* 1.5874 * */ (float)sqrt(3.0f/st->buflen);
636 } else {
637 gainadj = (float)sqrt(per/st->buflen);
638 }
639
640 /* Interpolate period ONLY if both old and new subframes are voiced, gain and K always */
641
642 if (st->Oldper != 0 && per != 0) {
643 perinc = (per - st->Oldper) / (flen/2);
644 } else {
645 perinc = 0.0f;
646 Newper = per;
647 }
648
649 if (Newper == 0.f) st->pitchctr = 0;
650
651 for (i=0; i < flen/2; i++, ii++) {
652 if (Newper == 0.f) {
653 u = (float)(((sp_rand(sp)*(1/(1.0f+RAND_MAX))) - 0.5f ) * newgain * gainadj);
654 } else { /* voiced: send a delta every per samples */
655 /* triangular excitation */
656 if (st->pitchctr == 0) {
657 st->exc = newgain * 0.25f * gainadj;
658 st->pitchctr = (int) Newper;
659 } else {
660 st->exc -= newgain/Newper * 0.5f * gainadj;
661 st->pitchctr--;
662 }
663 u = st->exc;
664 }
665 f = u;
666 /* excitation */
667 kj = Newk[10];
668 f -= kj * bp9;
669 bp10 = bp9 + kj * f;
670
671 kj = Newk[9];
672 f -= kj * bp8;
673 bp9 = bp8 + kj * f;
674
675 kj = Newk[8];
676 f -= kj * bp7;
677 bp8 = bp7 + kj * f;
678
679 kj = Newk[7];
680 f -= kj * bp6;
681 bp7 = bp6 + kj * f;
682
683 kj = Newk[6];
684 f -= kj * bp5;
685 bp6 = bp5 + kj * f;
686
687 kj = Newk[5];
688 f -= kj * bp4;
689 bp5 = bp4 + kj * f;
690
691 kj = Newk[4];
692 f -= kj * bp3;
693 bp4 = bp3 + kj * f;
694
695 kj = Newk[3];
696 f -= kj * bp2;
697 bp3 = bp2 + kj * f;
698
699 kj = Newk[2];
700 f -= kj * bp1;
701 bp2 = bp1 + kj * f;
702
703 kj = Newk[1];
704 f -= kj * bp0;
705 bp1 = bp0 + kj * f;
706
707 bp0 = f;
708 u = f;
709
710 if (u < -0.9999f) {
711 u = -0.9999f;
712 } else if (u > 0.9999f) {
713 u = 0.9999f;
714 }
715
716 buf[ii] = (short)lrintf(u * 32767.0f);
717
718 Newper += perinc;
719 newgain += Ginc;
720 for (j=1; j <= LPC_FILTORDER; j++) Newk[j] += kinc[j];
721
722 }
723
724 st->Oldper = per;
725 st->OldG = gain;
726 }
727 st->bp[0] = bp0;
728 st->bp[1] = bp1;
729 st->bp[2] = bp2;
730 st->bp[3] = bp3;
731 st->bp[4] = bp4;
732 st->bp[5] = bp5;
733 st->bp[6] = bp6;
734 st->bp[7] = bp7;
735 st->bp[8] = bp8;
736 st->bp[9] = bp9;
737 st->bp[10] = bp10;
738
739 for (j=1; j <= LPC_FILTORDER; j++) st->Oldk[j] = k[j];
740
741 return flen;
742 }
743
destroy_openlpc_decoder_state(openlpc_decoder_state * st)744 void destroy_openlpc_decoder_state(openlpc_decoder_state *st)
745 {
746 if(st != NULL)
747 {
748 free(st);
749 st = NULL;
750 }
751 }
752
openlpc_sr(float sr)753 void openlpc_sr(float sr)
754 {
755 my_fs = sr;
756 }
757
openlpc_get_encoder_state_size(void)758 size_t openlpc_get_encoder_state_size(void)
759 {
760 return sizeof(openlpc_encoder_state);
761 }
762
openlpc_get_decoder_state_size(void)763 size_t openlpc_get_decoder_state_size(void)
764 {
765 return sizeof(openlpc_decoder_state);
766 }
767