No.51373 作者:因果 邮件:chenshiyangyi@163.com ID:113570 登陆:7次 文章数:11篇 最后登陆IP:119.59.248.166 最后登陆:2011/10/3 17:36:40 注册:2008/6/11 14:23:51 财富:156 发帖时间:2008/6/26 11:14:29 发贴者IP:218.17.74.234 标题:因果:LPC10-15 2.4Kpbs语音压缩定点运算C语言源程序 摘要:No.51373LPC10-15 2.4Kpbs语音压缩定点运算C语言源程序 在2003年我接触ACCFast的mp3方案,其有Celp 4.8K的语音压缩录音,但是录音的效果一直非常差,在我看来其dsp的速度可以达到100M以上,内存空间也足够大,为什么效果非常差了。最近,由于工程的需要,我们急切需要一个压缩率低的,音质可以接受的压缩编码。这有两上原因,1.mcu的运算速度不高,内存变量空间很少;2.用于无线距离传输,可以使用传输速率低的RF传输,这样出错受干扰的可能性较少。于是我开始留意网上的2.4k的源程序。结果好多都是收费的,我试着在国外的网站找找,结果在网站 找到了源程序,还是定点运算。这个时候我才想起当初Accfast的录音音质差的原因,就是没有处理好定点与浮点运算。 此外,我把这一段时间收集的资料,包括lpc10-15, 对”LPC-10编码算法的分析与改进.pdf”, “简化的LPC-10语音编码算法研究与仿真.pdf”, HawkVoiceDI091src.zip, openlpc.fp.zip,lpc10-25s.wav(2.4k的压缩效果),一些文章压缩成一个rar,有需要的朋友请到: 下载 我过一段时间会把它的程序进行简化,到时再与大家分享. 其源程序: Openlpc.h: /* * LPC subroutine declarations */ #ifndef OPENLPC_H #define OPENLPC_H #ifdef __cplusplus extern "C" { #endif #define OPENLPC_FRAMESIZE_1_8 250 #define OPENLPC_FRAMESIZE_1_4 320 #define OPENLPC_ENCODED_FRAME_SIZE 7 typedef struct openlpc_e_state openlpc_encoder_state; typedef struct openlpc_d_state openlpc_decoder_state; openlpc_encoder_state *create_openlpc_encoder_state(void); void init_openlpc_encoder_state(openlpc_encoder_state *st, int framelen); int openlpc_encode(const short *in, unsigned char *out, openlpc_encoder_state *st); void destroy_openlpc_encoder_state(openlpc_encoder_state *st); openlpc_decoder_state *create_openlpc_decoder_state(void); void init_openlpc_decoder_state(openlpc_decoder_state *st, int framelen); int openlpc_decode(unsigned char *in, short *out, openlpc_decoder_state *st); void destroy_openlpc_decoder_state(openlpc_decoder_state *st); #ifdef __cplusplus } /* extern "C" */ #endif #endif /* OPENLPC_H */ Openlpc.fix.c: /* Fixed point OpenLPC codec Copyright (C) 2003-2005 Phil Frisbie, Jr. (phil@hawksoft.com) This is a major rewrite of the orginal floating point OpenLPC code from Future Dynamics. As such, a copywrite notice is not required to credit Future Dynamics. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. Or go to  */ /************************************************************************ Low bitrate LPC CODEC derived from the public domain implementation of Ron Frederick. The basic design is preserved, except for several bug fixes and the following modifications: 1. The pitch detector operates on the (downsampled) signal, not on the residue. This appears to yield better performances, and it lowers the processing load. 2. Each frame is elongated by 50% prefixing it with the last half of the previous frame. This design, inherited from the original code for windowing purposes, is exploited in order to provide two independent pitch analyses: on the first 2/3, and on the second 2/3 of the elongated frame (of course, they overlap by half): last half old frame new frame --------------------======================================== <--------- first pitch region ---------> <--------- second pitch region -------> Two voiced/unvoiced flags define the voicing status of each region; only one value for the period is retained (if both halves are voiced, the average is used). The two flags are used by the synthesizer for the halves of each frame to play back. Of course, this is non optimal but is good enough (a half-frame would be too short for measuring low pitches) 3. The parameters (one float for the period (pitch), one for the gain, and ten for the LPC-10 filter) are quantized according this procedure: - the period is logarithmically compressed, then quantized as 8-bit unsigned int (6 would actually suffice) - the gain is logarithmically compressed (using a different formula), then quantized at 6-bit unsigned int. The two remaining bits are used for the voicing flags. - the first two LPC parameters (k[1] and k[2]) are multiplied by PI/2, and the arcsine of the result is quantized as 6 and 5 bit signed integers. This has proved more effective than the log-area compression used by LPC-10. - the remaining eight LPC parameters (k[3]...k[10]) are quantized as, respectively, 5,4,4,3,3,3,3 and 2 bit signed integers. Finally, period and gain plus voicing flags are stored in the first two bytes of the 7-byte parameters block, and the quantized LPC parameters are packed into the remaining 5 bytes. Two bits remain unassigned, and can be used for error detection or other purposes. The frame lenght is actually variable, and is simply passed as initialization parameter to lpc_init(): this allows to experiment with various frame lengths. Long frames reduce the bitrate, but exceeding 320 samples (i.e. 40 ms, at 8000 samples/s) tend to deteriorate the speech, that sounds like spoken by a person affected by a stroke: the LPC parameters (modeling the vocal tract) can't change fast enough for a natural-sounding synthesis. 25 ms per frame already yields a quite tight compression, corresponding to 1000/40 * 7 * 8 = 1400 bps. The quality improves little with frames shorter than 250 samples (32 frames/s), so this is a recommended compromise. The bitrate is 32 * 7 * 8 = 1792 bps. The synthesizer has been modified as well. For voiced subframes it now uses a sawtooth excitation, instead of the original pulse train. This idea, copied from MELP, reduces the buzzing-noise artifacts. In order to compensate the non-white spectrum of the sawtooth, a pre-emphasis is applied to the signal before the Durbin calculation. The filter has (in s-space) two zeroes at (640, 0) Hz and two poles at (3200, 0) Hz. These filters have been handoded, and may not be optimal. Two other filters (anti-hum high-pass with corner at 100 Hz, and pre-downsampling lowpass with corner at 300 Hz) are Butterworth designs produced by the MkFilter package by A.J. Fisher ( ). \************************************************************************/ #ifdef _MSC_VER #pragma warning (disable:4711) /* to disable automatic inline warning */ #define M_PI (3.1415926535897932384626433832795) #endif #include <stdlib.h> #include <malloc.h> #include <string.h> #include <math.h> #include "openlpc.h" #define fixed32 long #if defined WIN32 || defined WIN64 || defined (_WIN32_WCE) #define fixed64 __int64 #else #define fixed64 long long #endif /* These are for development and debugging and should not be changed unless you REALLY know what you are doing ;) */ #define IGNORE_OVERFLOW #define FAST_FILTERS #define PRECISION 20 #define ftofix32(x) ((fixed32)((x) * (float)(1 < < PRECISION) + ((x) < 0 ? -0.5 : 0.5))) #define itofix32(x) ((x) < < PRECISION) #define fixtoi32(x) ((x) > > PRECISION) #define fixtof32(x) (float)((float)(x) / (float)(1 < < PRECISION)) static fixed32 fixmul32(fixed32 x, fixed32 y) { fixed64 temp; temp = x; temp *= y; temp > > = PRECISION; #ifndef IGNORE_OVERFLOW if(temp > 0x7fffffff) { return 0x7fffffff; } else if(temp < -0x7ffffffe) { return -0x7ffffffe; } #endif return (fixed32)temp; } static fixed32 fixdiv32(fixed32 x, fixed32 y) { fixed64 temp; if(x == 0) return 0; if(y == 0) return 0x7fffffff; temp = x; temp < <= PRECISION; return (fixed32)(temp / y); } static fixed32 fixsqrt32(fixed32 x) { unsigned long r = 0, s, v = (unsigned long)x; #define STEP(k) s = r + (1 < < k * 2); r > > = 1; if (s <= v) { v -= s; r |= (1 < < k * 2); } STEP(15); STEP(14); STEP(13); STEP(12); STEP(11); STEP(10); STEP(9); STEP(8); STEP(7); STEP(6); STEP(5); STEP(4); STEP(3); STEP(2); STEP(1); STEP(0); return (fixed32)(r < < (PRECISION / 2)); } __inline static fixed32 fixexp32(fixed32 x) { fixed64 result = ftofix32(1.f); fixed64 temp; int sign = 1; /* reduce range to 0.0 to 1.0 */ if(x < 0) { x = (fixed32)-x; sign = -1; } while(x > itofix32(1)) { x -= itofix32(1); result *= ftofix32(2.718282f); result > > = PRECISION; } /* reduce range to 0.0 to 0.5 */ if(x > ftofix32(0.5f)) { x -= ftofix32(0.5f); result *= ftofix32(1.648721f); result > > = PRECISION; } if(result > 0x7fffffff) { return 0x7fffffff; } temp = ftofix32(0.00138888f) * x; temp > > = PRECISION; temp = (temp + ftofix32(0.00833333f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(0.04166666f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(0.16666666f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(0.5f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(1.0f)) * x; temp > > = PRECISION; result *= (temp + ftofix32(1.f)); result > > = PRECISION; if(sign == -1) { temp = 1; result = (temp < < (PRECISION * 2)) / result; } if(result > 0x7fffffff) { return 0x7fffffff; } return (fixed32)result; } __inline static fixed32 fixlog32(fixed32 x) { fixed64 result = 0; fixed64 temp; if(x == 0) { return -0x7ffffffe; } else if(x < 0) { return 0; } while(x > itofix32(2)) { result += ftofix32(0.693147f); x /= 2; } while(x < itofix32(1)) { result -= ftofix32(0.693147f); x *= 2; } x -= itofix32(1); temp = ftofix32(-.0064535442f) * x; temp > > = PRECISION; temp = (temp + ftofix32(.0360884937f)) * x; temp > > = PRECISION; temp = (temp - ftofix32(.0953293897f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(.1676540711f)) * x; temp > > = PRECISION; temp = (temp - ftofix32(.2407338084f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(.3317990258f)) * x; temp > > = PRECISION; temp = (temp - ftofix32(.4998741238f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(.9999964239f)) * x; temp > > = PRECISION; result += temp; return (fixed32)result; } __inline fixed32 fixsin32(fixed32 x) { fixed64 x2, temp; int sign = 1; if(x < 0) { sign = -1; x = -x; } while(x > ftofix32(M_PI)) { x -= ftofix32(M_PI); sign = -sign; } if(x > ftofix32(M_PI/2)) { x = ftofix32(M_PI) - x; } x2 = (fixed64)x * x; x2 > > = PRECISION; if(sign != 1) { x = -x; } temp = ftofix32(-.0000000239f) * x2; temp > > = PRECISION; temp = (temp + ftofix32(.0000027526f)) * x2; temp > > = PRECISION; temp = (temp - ftofix32(.0001984090f)) * x2; temp > > = PRECISION; temp = (temp + ftofix32(.0083333315f)) * x2; temp > > = PRECISION; temp = (temp - ftofix32(.1666666664f)) * x2; temp > > = PRECISION; temp += itofix32(1); temp = temp * x; temp > > = PRECISION; return (fixed32)(temp); } __inline fixed32 fixasin32(fixed32 x) { fixed64 temp; int sign = 1; if(x > itofix32(1) || x < itofix32(-1)) { return 0; } if(x < 0) { sign = -1; x = -x; } temp = 0; temp = ftofix32(-.0012624911f) * (fixed64)x; temp > > = PRECISION; temp = (temp + ftofix32(.0066700901f)) * x; temp > > = PRECISION; temp = (temp - ftofix32(.0170881256f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(.0308918810f)) * x; temp > > = PRECISION; temp = (temp - ftofix32(.0501743046f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(.0889789874f)) * x; temp > > = PRECISION; temp = (temp - ftofix32(.2145988016f)) * x; temp > > = PRECISION; temp = (temp + ftofix32(1.570796305f)) * fixsqrt32(itofix32(1) - x); temp > > = PRECISION; return sign * (ftofix32(M_PI/2) - (fixed32)temp); } #define PREEMPH #define bcopy(a, b, n) memmove(b, a, n) #define LPC_FILTORDER 10 #define FS 8000.0 /* Sampling rate */ #define MAXWINDOW 1000 /* Max analysis window length */ typedef struct openlpc_e_state{ int framelen, buflen; fixed32 s[MAXWINDOW], y[MAXWINDOW], h[MAXWINDOW]; fixed32 xv1[3], yv1[3], xv2[2], yv2[2], xv3[1], yv3[3], xv4[2], yv4[2]; fixed32 w[MAXWINDOW], r[LPC_FILTORDER+1]; } openlpc_e_state_t; typedef struct openlpc_d_state{ fixed32 Oldper, OldG, Oldk[LPC_FILTORDER + 1]; fixed32 bp[LPC_FILTORDER+1]; fixed32 exc; fixed32 gainadj; int pitchctr, framelen, buflen; } openlpc_d_state_t; #define FC 200.0 /* Pitch analyzer filter cutoff */ #define DOWN 5 /* Decimation for pitch analyzer */ #define MINPIT 40.0 /* Minimum pitch (observed: 74) */ #define MAXPIT 320.0 /* Maximum pitch (observed: 250) */ #define MINPER (int)(FS / (DOWN * MAXPIT) + .5) /* Minimum period */ #define MAXPER (int)(FS / (DOWN * MINPIT) + .5) /* Maximum period */ #define REAL_MINPER (DOWN * MINPER) /* converted to samples units */ #define WSCALE 1.5863 /* Energy loss due to windowing */ #define BITS_FOR_LPC 38 #define ARCSIN_Q /* provides better quantization of first two k[] at low bitrates */ #if BITS_FOR_LPC == 38 /* (38 bit LPC-10, 2.7 Kbit/s @ 20ms, 2.4 Kbit/s @ 22.5 ms */ static int parambits[LPC_FILTORDER] = {6,5,5,4,4,3,3,3,3,2}; #elif BITS_FOR_LPC == 32 /* (32 bit LPC-10, 2.4 Kbit/s, not so good */ static int parambits[LPC_FILTORDER] = {5,5,5,4,3,3,2,2,2,1}; #else /* BITS_FOR_LPC == 80 */ /* 80-bit LPC10, 4.8 Kbit/s */ static int parambits[LPC_FILTORDER] = {8,8,8,8,8,8,8,8,8,8}; #endif static fixed32 logmaxminper; static int sizeofparm; /* computed by lpc_init */ static void auto_correl1(fixed32 *w, int n, fixed32 *r) { int i, k; fixed64 temp, temp2; for (k=0; k <= MAXPER; k++, n--) { temp = 0; for (i=0; i < n; i++) { temp2 = w[i]; temp += temp2 * w[i+k]; } r[k] = (fixed32)(temp > > PRECISION); } } static void auto_correl2(fixed32 *w, int n, fixed32 *r) { int i, k; fixed64 temp, temp2; for (k=0; k <= LPC_FILTORDER; k++, n--) { temp = 0; for (i=0; i < n; i++) { temp2 = w[i]; temp += temp2 * w[i+k]; } r[k] = (fixed32)(temp > > PRECISION); } } static void durbin(fixed32 r[], int p, fixed32 k[], fixed32 *g) { int i, j; fixed32 a[LPC_FILTORDER+1], at[LPC_FILTORDER+1], e; for (i=0; i <= p; i++) a[i] = at[i] = 0; e = r[0]; for (i=1; i <= p; i++) { k[i] = -r[i]; for (j=1; j < i; j++) { at[j] = a[j]; k[i] -= fixmul32(a[j], r[i-j]); } if (e == 0) { /* fix by John Walker */ *g = 0; return; } k[i] = fixdiv32(k[i], e); a[i] = k[i]; for (j=1; j < i; j++) a[j] = at[j] + fixmul32(k[i], at[i-j]); e = fixmul32(e, (itofix32(1) - fixmul32(k[i], k[i]))); } if (e < 0) { e = 0; /* fix by John Walker */ } *g = fixsqrt32(e); } static void calc_pitch(fixed32 w[], int len, fixed32 *per) { int i, j, rpos; fixed32 d[MAXWINDOW / DOWN], r[MAXPER + 1], rmax; fixed32 rval, rm, rp; fixed32 x, y; fixed32 vthresh; /* decimation */ for (i=0, j=0; i < len; i+=DOWN) d[j++] = w[i]; auto_correl1(d, len / DOWN, r); /* find peak between MINPER and MAXPER */ x = itofix32(1); rpos = 0; rmax = 0; for (i = 1; i <= MAXPER; i++) { rm = r[i-1]; rp = r[i+1]; y = rm+r[i]+rp; /* find max of integral from i-1 to i+1 */ if (y > rmax && r[i] > rm && r[i] > ......
>>返回讨论的主题
|