From c8906ffdfca29494b72fad47eb2d29054d66aacf Mon Sep 17 00:00:00 2001 From: drowe67 Date: Sun, 15 Aug 2010 01:29:08 +0000 Subject: [PATCH] included lsp files, simpler lsp.c, fixed build issues found by Andreas, plaraw modified for modern play utility arguments, menu.sh is still broken git-svn-id: https://svn.code.sf.net/p/freetel/code@168 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2/script/menu.sh | 9 +- codec2/script/playraw.sh | 4 +- codec2/src/Makefile | 7 +- codec2/src/lsp.c | 277 ++++++++++++++++++++++++++++++++++++++ codec2/src/lsp.h | 20 +++ codec2/src/quantise.c | 3 - codec2/unittest/lsp1.txt | 16 +++ codec2/unittest/lsp10.txt | 5 + codec2/unittest/lsp2.txt | 17 +++ codec2/unittest/lsp3.txt | 17 +++ codec2/unittest/lsp4.txt | 17 +++ codec2/unittest/lsp5.txt | 18 +++ codec2/unittest/lsp6.txt | 18 +++ codec2/unittest/lsp7.txt | 18 +++ codec2/unittest/lsp8.txt | 10 ++ codec2/unittest/lsp9.txt | 10 ++ 16 files changed, 453 insertions(+), 13 deletions(-) create mode 100644 codec2/src/lsp.c create mode 100644 codec2/src/lsp.h create mode 100644 codec2/unittest/lsp1.txt create mode 100644 codec2/unittest/lsp10.txt create mode 100644 codec2/unittest/lsp2.txt create mode 100644 codec2/unittest/lsp3.txt create mode 100644 codec2/unittest/lsp4.txt create mode 100644 codec2/unittest/lsp5.txt create mode 100644 codec2/unittest/lsp6.txt create mode 100644 codec2/unittest/lsp7.txt create mode 100644 codec2/unittest/lsp8.txt create mode 100644 codec2/unittest/lsp9.txt diff --git a/codec2/script/menu.sh b/codec2/script/menu.sh index 75072841..e9ab7f02 100755 --- a/codec2/script/menu.sh +++ b/codec2/script/menu.sh @@ -59,12 +59,13 @@ do echo -n -e "\r -" stty cbreak # or stty raw readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null` + echo $readchar stty -cbreak - if [ $readchar == 'q' ] ; then - readchar=0 - fi +# if [ $readchar == 'q' ] ; then +# readchar=0 +# fi if [ $readchar -ne 0 ] ; then - play -f s -r 8000 -s w ${file[$readchar]} $dsp > /dev/null + echo "play -r 8000 -s -2 ${file[$readchar]} $dsp > /dev/null" fi done echo diff --git a/codec2/script/playraw.sh b/codec2/script/playraw.sh index f6033e20..683cbaa1 100755 --- a/codec2/script/playraw.sh +++ b/codec2/script/playraw.sh @@ -2,5 +2,5 @@ # Plays a raw file # usage: # playraw file.raw -# playraw file.raw -d /deve/dsp1 -play -f s -r 8000 -s w $1 $2 $3 +# playraw file.raw -d /dev/dsp1 (e.g. for USB headphones) +play -r 8000 -s -2 $1 $2 $3 diff --git a/codec2/src/Makefile b/codec2/src/Makefile index 03036bc0..21643058 100644 --- a/codec2/src/Makefile +++ b/codec2/src/Makefile @@ -1,11 +1,10 @@ CC=gcc -CFLAGS=-g -Wall -I. -I../src -I../speex -Wall -g -DFLOATING_POINT -DVAR_ARRAYS +CFLAGS=-g -Wall -I. -I../src -Wall -g -DFLOATING_POINT -DVAR_ARRAYS SINENC_OBJ = sinenc.o globals.o initenc.o four1.o refine.o spec.o dump.o SINEDEC_OBJ = sinedec.o globals.o initenc.o initdec.o four1.o synth.o \ - quantise.o lpc.o dump.o refine.o ../speex/lsp.o \ - ../speex/quant_lsp.o ../speex/bits.o ../speex/lsp_tables_nb.o \ - ../speex/high_lsp_tables.o phase.o postfilter.o interp.o + quantise.o lpc.o dump.o refine.o lsp.o phase.o postfilter.o \ + interp.o all: sinenc sinedec diff --git a/codec2/src/lsp.c b/codec2/src/lsp.c new file mode 100644 index 00000000..3f223e0e --- /dev/null +++ b/codec2/src/lsp.c @@ -0,0 +1,277 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: lsp.c + AUTHOR......: David Rowe + DATE CREATED: 24/2/93 + + + This file contains functions for LPC to LSP conversion and LSP to + LPC conversion. Note that the LSP coefficients are not in radians + format but in the x domain of the unit circle. + +\*---------------------------------------------------------------------------*/ + +#include "lsp.h" +#include +#include +#include + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: cheb_poly_eva() + AUTHOR......: David Rowe + DATE CREATED: 24/2/93 + + This function evalutes a series of chebyshev polynomials + + FIXME: performing memory allocation at run time is very inefficient, + replace with stack variables of MAX_P size. + +\*---------------------------------------------------------------------------*/ + + +float cheb_poly_eva(float *coef,float x,int m) +/* float coef[] coefficients of the polynomial to be evaluated */ +/* float x the point where polynomial is to be evaluated */ +/* int m order of the polynomial */ +{ + int i; + float *T,*t,*u,*v,sum; + + /* Allocate memory for chebyshev series formulation */ + + if((T = (float *)malloc((m/2+1)*sizeof(float))) == NULL){ + printf("not enough memory to allocate buffer\n"); + exit(1); + } + + /* Initialise pointers */ + + t = T; /* T[i-2] */ + *t++ = 1.0; + u = t--; /* T[i-1] */ + *u++ = x; + v = u--; /* T[i] */ + + /* Evaluate chebyshev series formulation using iterative approach */ + + for(i=2;i<=m/2;i++) + *v++ = (2*x)*(*u++) - *t++; /* T[i] = 2*x*T[i-1] - T[i-2] */ + + sum=0.0; /* initialise sum to zero */ + t = T; /* reset pointer */ + + /* Evaluate polynomial and return value also free memory space */ + + for(i=0;i<=m/2;i++) + sum+=coef[(m/2)-i]**t++; + + free(T); + return sum; +} + + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: lpc_to_lsp() + AUTHOR......: David Rowe + DATE CREATED: 24/2/93 + + This function converts LPC coefficients to LSP coefficients. + +\*---------------------------------------------------------------------------*/ + +int lpc_to_lsp (float *a,int lpcrdr,float *freq,int nb,float delta) +/* float *a lpc coefficients */ +/* int lpcrdr order of LPC coefficients (10) */ +/* float *freq LSP frequencies in the x domain */ +/* int nb number of sub-intervals (4) */ +/* float delta grid spacing interval (0.02) */ +{ + float psuml,psumr,psumm,temp_xr,xl,xr,xm; + float temp_psumr; + int i,j,m,flag,k; + float *Q; /* ptrs for memory allocation */ + float *P; + float *px; /* ptrs of respective P'(z) & Q'(z) */ + float *qx; + float *p; + float *q; + float *pt; /* ptr used for cheb_poly_eval() + whether P' or Q' */ + int roots=0; /* number of roots found */ + flag = 1; + m = lpcrdr/2; /* order of P'(z) & Q'(z) polynimials */ + + /* Allocate memory space for polynomials */ + + if((Q = (float *) malloc((m+1)*sizeof(float))) == NULL){ + printf("not enough memory to allocate buffer\n"); + exit(1); + } + + if((P = (float *) malloc((m+1)*sizeof(float))) == NULL){ + printf("not enough memory to allocate buffer\n"); + exit(1); + } + + /* determine P'(z)'s and Q'(z)'s coefficients where + P'(z) = P(z)/(1 + z^(-1)) and Q'(z) = Q(z)/(1-z^(-1)) */ + + px = P; /* initilaise ptrs */ + qx = Q; + p = px; + q = qx; + *px++ = 1.0; + *qx++ = 1.0; + for(i=1;i<=m;i++){ + *px++ = a[i]+a[lpcrdr+1-i]-*p++; + *qx++ = a[i]-a[lpcrdr+1-i]+*q++; + } + px = P; + qx = Q; + for(i=0;i= -1.0)){ + xr = xl - delta ; /* interval spacing */ + psumr = cheb_poly_eva(pt,xr,lpcrdr);/* poly(xl-delta_x) */ + temp_psumr = psumr; + temp_xr = xr; + + /* if no sign change increment xr and re-evaluate + poly(xr). Repeat til sign change. if a sign change has + occurred the interval is bisected and then checked again + for a sign change which determines in which interval the + zero lies in. If there is no sign change between poly(xm) + and poly(xl) set interval between xm and xr else set + interval between xl and xr and repeat till root is located + within the specified limits */ + + if((psumr*psuml)<0.0){ + roots++; + + psumm=psuml; + for(k=0;k<=nb;k++){ + xm = (xl+xr)/2; /* bisect the interval */ + psumm=cheb_poly_eva(pt,xm,lpcrdr); + if(psumm*psuml>0.){ + psuml=psumm; + xl=xm; + } + else{ + psumr=psumm; + xr=xm; + } + } + + /* once zero is found, reset initial interval to xr */ + freq[j] = (xm); + xl = xm; + flag = 0; /* reset flag for next search */ + } + else{ + psuml=temp_psumr; + xl=temp_xr; + } + } + } + free(P); /* free memory space */ + free(Q); + + return(roots); +} + +/*---------------------------------------------------------------------------*\ + + FUNCTION....: lsp_to_lpc() + AUTHOR......: David Rowe + DATE CREATED: 24/2/93 + + This function converts LSP coefficients to LPC coefficients. In the + Speex code we worked out a wayto simplify this significantly. + +\*---------------------------------------------------------------------------*/ + +void lsp_to_lpc(float *freq,float *ak,int lpcrdr) +/* float *freq array of LSP frequencies in the x domain */ +/* float *ak array of LPC coefficients */ +/* int lpcrdr order of LPC coefficients */ + + +{ + int i,j; + float xout1,xout2,xin1,xin2; + float *Wp; + float *pw,*n1,*n2,*n3,*n4; + int m = lpcrdr/2; + + if((Wp = (float *) malloc((4*m+2)*sizeof(float))) == NULL){ + printf("not enough memory to allocate buffer\n"); + exit(1); + } + pw = Wp; + + /* initialise contents of array */ + + for(i=0;i<=4*m+1;i++){ /* set contents of buffer to 0 */ + *pw++ = 0.0; + } + + /* Set pointers up */ + + pw = Wp; + xin1 = 1.0; + xin2 = 1.0; + + /* reconstruct P(z) and Q(z) by cascading second order polynomials + in form 1 - 2xz(-1) +z(-2), where x is the LSP coefficient */ + + for(j=0;j<=lpcrdr;j++){ + for(i=0;i -#include -#include #define MAX_ORDER 20 #define LSP_DELTA1 0.01 /* grid spacing for LSP root searches */ diff --git a/codec2/unittest/lsp1.txt b/codec2/unittest/lsp1.txt new file mode 100644 index 00000000..88995dea --- /dev/null +++ b/codec2/unittest/lsp1.txt @@ -0,0 +1,16 @@ +225 +250 +275 +300 +325 +350 +375 +400 +425 +450 +475 +500 +525 +550 +575 +600 diff --git a/codec2/unittest/lsp10.txt b/codec2/unittest/lsp10.txt new file mode 100644 index 00000000..1f1129b8 --- /dev/null +++ b/codec2/unittest/lsp10.txt @@ -0,0 +1,5 @@ +2900 +3100 +3300 +3500 + diff --git a/codec2/unittest/lsp2.txt b/codec2/unittest/lsp2.txt new file mode 100644 index 00000000..35bdc62f --- /dev/null +++ b/codec2/unittest/lsp2.txt @@ -0,0 +1,17 @@ +325 +350 +375 +400 +425 +450 +475 +500 +525 +550 +575 +600 +625 +650 +675 +700 + diff --git a/codec2/unittest/lsp3.txt b/codec2/unittest/lsp3.txt new file mode 100644 index 00000000..200e11bf --- /dev/null +++ b/codec2/unittest/lsp3.txt @@ -0,0 +1,17 @@ +500 +550 +600 +650 +700 +750 +800 +850 +900 +950 +1000 +1050 +1100 +1150 +1200 +1250 + diff --git a/codec2/unittest/lsp4.txt b/codec2/unittest/lsp4.txt new file mode 100644 index 00000000..bce8ffc8 --- /dev/null +++ b/codec2/unittest/lsp4.txt @@ -0,0 +1,17 @@ +700 +800 +900 +1000 +1100 +1200 +1300 +1400 +1500 +1600 +1700 +1800 +1900 +2000 +2100 + + diff --git a/codec2/unittest/lsp5.txt b/codec2/unittest/lsp5.txt new file mode 100644 index 00000000..05d72210 --- /dev/null +++ b/codec2/unittest/lsp5.txt @@ -0,0 +1,18 @@ + 950 +1050 +1150 +1250 +1350 +1450 +1550 +1650 +1750 +1850 +1950 +2050 +2150 +2250 +2350 +2450 + + diff --git a/codec2/unittest/lsp6.txt b/codec2/unittest/lsp6.txt new file mode 100644 index 00000000..d1207ffe --- /dev/null +++ b/codec2/unittest/lsp6.txt @@ -0,0 +1,18 @@ +1100 +1200 +1300 +1400 +1500 +1600 +1700 +1800 +1900 +2000 +2100 +2200 +2300 +2400 +2500 +2600 + + diff --git a/codec2/unittest/lsp7.txt b/codec2/unittest/lsp7.txt new file mode 100644 index 00000000..1f6eaa66 --- /dev/null +++ b/codec2/unittest/lsp7.txt @@ -0,0 +1,18 @@ +1500 +1600 +1700 +1800 +1900 +2000 +2100 +2200 +2300 +2400 +2500 +2600 +2700 +2800 +2900 +3000 + + diff --git a/codec2/unittest/lsp8.txt b/codec2/unittest/lsp8.txt new file mode 100644 index 00000000..89607c8c --- /dev/null +++ b/codec2/unittest/lsp8.txt @@ -0,0 +1,10 @@ +2300 +2400 +2500 +2600 +2700 +2800 +2900 +3000 + + diff --git a/codec2/unittest/lsp9.txt b/codec2/unittest/lsp9.txt new file mode 100644 index 00000000..82be58c0 --- /dev/null +++ b/codec2/unittest/lsp9.txt @@ -0,0 +1,10 @@ +2500 +2600 +2700 +2800 +2900 +3000 +3100 +3200 + + -- 2.25.1