From 2a520207a09b6f8f92baa94c8593da728e813b58 Mon Sep 17 00:00:00 2001 From: drowe67 Date: Tue, 31 Aug 2010 02:46:26 +0000 Subject: [PATCH] Removed statics from nlp (thanks Mathieu), and cleaned up some warnings I found when compiling with -02 git-svn-id: https://svn.code.sf.net/p/freetel/code@189 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2/README.txt | 3 +- codec2/src/c2dec.c | 7 +- codec2/src/c2enc.c | 7 +- codec2/src/c2sim.c | 8 +- codec2/src/codec2.c | 21 ++++- codec2/src/nlp.c | 191 ++++++++++++++++++++++++++--------------- codec2/src/nlp.h | 9 +- codec2/src/quantise.c | 1 - codec2/src/sine.c | 1 + codec2/unittest/tnlp.c | 8 +- 10 files changed, 170 insertions(+), 86 deletions(-) diff --git a/codec2/README.txt b/codec2/README.txt index e3b14143..7cf3a395 100644 --- a/codec2/README.txt +++ b/codec2/README.txt @@ -19,7 +19,8 @@ Programs -------- 1/ c2enc encodes a file of speech sample to a file of encoded bits. -One bit is stored in the LSB or each byte. +One bit is stored in the LSB of each byte. Note this is unpacked, +i.e. 51 bits/frame consumes 51 bytes/frame on disk. 2/ c2dec decodes a file of bits to a file of speech samples. diff --git a/codec2/src/c2dec.c b/codec2/src/c2dec.c index 1d2cf994..e22757a1 100644 --- a/codec2/src/c2dec.c +++ b/codec2/src/c2dec.c @@ -7,6 +7,10 @@ Decodes a file of bits to a file of raw speech samples using codec2. Demo program for codec2. + NOTE: the bit file is not packed, 51 bits/frame actually consumes 51 + bytes/frame on disk. If you are using this for a real world + application you may want to pack the 51 bytes into 7 bytes. + \*---------------------------------------------------------------------------*/ /* @@ -39,7 +43,6 @@ int main(int argc, char *argv[]) FILE *fout; short buf[CODEC2_SAMPLES_PER_FRAME]; char bits[CODEC2_BITS_PER_FRAME]; - int i; if (argc != 3) { printf("usage: %s InputBitFile OutputRawSpeechFile\n", argv[0]); @@ -62,8 +65,6 @@ int main(int argc, char *argv[]) while(fread(bits, sizeof(char), CODEC2_BITS_PER_FRAME, fin) == CODEC2_BITS_PER_FRAME) { - //for(i=0; iSn[i] = 1.0; @@ -103,6 +110,12 @@ void *codec2_create() c2->prev_model.A[l] = 0.0; c2->prev_model.Wo = TWO_PI/P_MAX; + c2->nlp = nlp_create(); + if (c2->nlp == NULL) { + free (c2); + return NULL; + } + return (void*)c2; } @@ -118,7 +131,11 @@ void *codec2_create() void codec2_destroy(void *codec2_state) { + CODEC2 *c2; + assert(codec2_state != NULL); + c2 = (CODEC2*)codec2_state; + nlp_destroy(c2->nlp); free(codec2_state); } @@ -306,7 +323,7 @@ void analyse_one_frame(CODEC2 *c2, MODEL *model, short speech[]) /* Estimate pitch */ - nlp(c2->Sn,N,M,P_MIN,P_MAX,&pitch,Sw,&c2->prev_Wo); + nlp(c2->nlp,c2->Sn,N,M,P_MIN,P_MAX,&pitch,Sw,&c2->prev_Wo); c2->prev_Wo = TWO_PI/pitch; model->Wo = TWO_PI/pitch; model->L = PI/model->Wo; diff --git a/codec2/src/nlp.c b/codec2/src/nlp.c index b4c96189..193ca921 100644 --- a/codec2/src/nlp.c +++ b/codec2/src/nlp.c @@ -27,10 +27,13 @@ */ #include "defines.h" -#include "dump.h" #include "nlp.h" +#include "dump.h" +#include "four1.h" + #include #include +#include /*---------------------------------------------------------------------------*\ @@ -47,6 +50,7 @@ #define T 0.1 /* threshold for local minima candidate */ #define F0_MAX 500 #define CNLP 0.3 /* post processor constant */ +#define NLP_NTAP 48 /* Decimation LPF order */ /*---------------------------------------------------------------------------*\ @@ -107,11 +111,57 @@ float nlp_fir[] = { -1.0818124e-03 }; +typedef struct { + float sq[PMAX_M]; /* squared speech samples */ + float mem_x,mem_y; /* memory for notch filter */ + float mem_fir[NLP_NTAP]; /* decimation FIR filter memory */ +} NLP; + float post_process_mbe(COMP Fw[], int pmin, int pmax, float gmax); float post_process_sub_multiples(COMP Fw[], int pmin, int pmax, float gmax, int gmax_bin, float *prev_Wo); -extern int frames; + +/*---------------------------------------------------------------------------*\ + + nlp_create() + + Initialisation function for NLP pitch estimator. + +\*---------------------------------------------------------------------------*/ + +void *nlp_create() +{ + NLP *nlp; + int i; + + nlp = (NLP*)malloc(sizeof(NLP)); + if (nlp == NULL) + return NULL; + + for(i=0; isq[i] = 0.0; + nlp->mem_x = 0.0; + nlp->mem_y = 0.0; + for(i=0; imem_fir[i] = 0.0; + + return (void*)nlp; +} + +/*---------------------------------------------------------------------------*\ + + nlp_destory() + + Initialisation function for NLP pitch estimator. + +\*---------------------------------------------------------------------------*/ + +void nlp_destroy(void *nlp_state) +{ + assert(nlp_state != NULL); + free(nlp_state); +} /*---------------------------------------------------------------------------*\ @@ -144,6 +194,7 @@ extern int frames; \*---------------------------------------------------------------------------*/ float nlp( + void *nlp_state, float Sn[], /* input speech vector */ int n, /* frames shift (no. new samples in Sn[]) */ int m, /* analysis window size */ @@ -154,78 +205,81 @@ float nlp( float *prev_Wo ) { - static float sq[PMAX_M]; /* squared speech samples */ - float notch; /* current notch filter output */ - static float mem_x,mem_y; /* memory for notch filter */ - static float mem_fir[NLP_NTAP];/* decimation FIR filter memory */ - COMP Fw[PE_FFT_SIZE]; /* DFT of squared signal */ - float gmax; - int gmax_bin; - int i,j; - float best_f0; - - /* Square, notch filter at DC, and LP filter vector */ - - for(i=m-n; i gmax) { - gmax = Fw[i].real; - gmax_bin = i; + NLP *nlp; + float notch; /* current notch filter output */ + COMP Fw[PE_FFT_SIZE]; /* DFT of squared signal */ + float gmax; + int gmax_bin; + int i,j; + float best_f0; + + assert(nlp_state != NULL); + nlp = (NLP*)nlp_state; + + /* Square, notch filter at DC, and LP filter vector */ + + for(i=m-n; isq[i] = Sn[i]*Sn[i]; + + for(i=m-n; isq[i] - nlp->mem_x; + notch += COEFF*nlp->mem_y; + nlp->mem_x = nlp->sq[i]; + nlp->mem_y = notch; + nlp->sq[i] = notch; + } + + for(i=m-n; imem_fir[j] = nlp->mem_fir[j+1]; + nlp->mem_fir[NLP_NTAP-1] = nlp->sq[i]; + + nlp->sq[i] = 0.0; + for(j=0; jsq[i] += nlp->mem_fir[j]*nlp_fir[j]; + } + + /* Decimate and DFT */ + + for(i=0; isq[i*DEC]*(0.5 - 0.5*cos(2*PI*i/(m/DEC-1))); + } + dump_dec(Fw); + four1(&Fw[-1].imag,PE_FFT_SIZE,1); + for(i=0; isq); + dump_Fw(Fw); + + /* find global peak */ + + gmax = 0.0; + gmax_bin = PE_FFT_SIZE*DEC/pmax; + for(i=PE_FFT_SIZE*DEC/pmax; i<=PE_FFT_SIZE*DEC/pmin; i++) { + if (Fw[i].real > gmax) { + gmax = Fw[i].real; + gmax_bin = i; + } } - } - best_f0 = post_process_sub_multiples(Fw, pmin, pmax, gmax, gmax_bin, prev_Wo); + best_f0 = post_process_sub_multiples(Fw, pmin, pmax, gmax, gmax_bin, + prev_Wo); - /* Shift samples in buffer to make room for new samples */ + /* Shift samples in buffer to make room for new samples */ - for(i=0; isq[i] = nlp->sq[i+n]; - /* return pitch and F0 estimate */ + /* return pitch and F0 estimate */ - *pitch = (float)SAMPLE_RATE/best_f0; - return(best_f0); + *pitch = (float)SAMPLE_RATE/best_f0; + return(best_f0); } /*---------------------------------------------------------------------------*\ @@ -284,6 +338,7 @@ float post_process_sub_multiples(COMP Fw[], thresh = CNLP*gmax; lmax = 0; + lmax_bin = bmin; for (b=bmin; b<=bmax; b++) /* look for maximum in interval */ if (Fw[b].real > lmax) { lmax = Fw[b].real; diff --git a/codec2/src/nlp.h b/codec2/src/nlp.h index 8bc7a755..eaaae970 100644 --- a/codec2/src/nlp.h +++ b/codec2/src/nlp.h @@ -29,11 +29,10 @@ #ifndef __NLP__ #define __NLP__ -#include "sine.h" - -#define NLP_NTAP 48 /* Decimation LPF order */ - -float nlp(float Sn[], int n, int m, int pmin, int pmax, float *pitch, COMP Sw[], float *prev_Wo); +void *nlp_create(); +void nlp_destroy(void *nlp_state); +float nlp(void *nlp_state, float Sn[], int n, int m, int pmin, int pmax, + float *pitch, COMP Sw[], float *prev_Wo); float test_candidate_mbe(COMP Sw[], float f0, COMP Sw_[]); #endif diff --git a/codec2/src/quantise.c b/codec2/src/quantise.c index 25fba89e..4cc3815f 100644 --- a/codec2/src/quantise.c +++ b/codec2/src/quantise.c @@ -834,7 +834,6 @@ void encode_amplitudes(int lsp_indexes[], float lsps[LPC_ORD]; float ak[LPC_ORD+1]; float e; - int i; e = speech_to_uq_lsps(lsps, ak, Sn, w, LPC_ORD); encode_lsps(lsp_indexes, lsps, LPC_ORD); diff --git a/codec2/src/sine.c b/codec2/src/sine.c index 5f876340..9263151b 100644 --- a/codec2/src/sine.c +++ b/codec2/src/sine.c @@ -272,6 +272,7 @@ void hs_pitch_refinement(MODEL *model, COMP Sw[], float pmin, float pmax, float /* Initialisation */ model->L = PI/model->Wo; /* use initial pitch est. for L */ + Wom = model->Wo; Em = 0.0; r = TWO_PI/FFT_ENC; diff --git a/codec2/unittest/tnlp.c b/codec2/unittest/tnlp.c index 0f4020c8..4abf69c4 100644 --- a/codec2/unittest/tnlp.c +++ b/codec2/unittest/tnlp.c @@ -39,6 +39,7 @@ #include "defines.h" #include "dump.h" +#include "sine.h" #include "nlp.h" int frames; @@ -87,7 +88,8 @@ char *argv[]; int i; int dump; float prev_Wo; - + void *nlp_states; + if (argc < 3) { printf("\nusage: tnlp InputRawSpeechFile OutputPitchTextFile " "[--dump DumpFile]\n"); @@ -112,6 +114,7 @@ char *argv[]; if (dump) dump_on(argv[dump+1]); + nlp_states = nlp_create(); make_analysis_window(w,W); frames = 0; @@ -128,7 +131,7 @@ char *argv[]; dft_speech(Sw, Sn, w); dump_Sn(Sn); dump_Sw(Sw); - nlp(Sn,N,M,PITCH_MIN,PITCH_MAX,&pitch,Sw,&prev_Wo); + nlp(nlp_states,Sn,N,M,PITCH_MIN,PITCH_MAX,&pitch,Sw,&prev_Wo); prev_Wo = TWO_PI/pitch; fprintf(fout,"%f\n",pitch); @@ -137,6 +140,7 @@ char *argv[]; fclose(fin); fclose(fout); if (dump) dump_off(); + nlp_destroy(nlp_states); return 0; } -- 2.25.1