Cleaned up vocoder mode...
authorjmvalin <jmvalin@0101bb08-14d6-0310-b084-bc0e0c8e3800>
Wed, 14 Aug 2002 20:19:57 +0000 (20:19 +0000)
committerjmvalin <jmvalin@0101bb08-14d6-0310-b084-bc0e0c8e3800>
Wed, 14 Aug 2002 20:19:57 +0000 (20:19 +0000)
git-svn-id: http://svn.xiph.org/trunk/speex@3821 0101bb08-14d6-0310-b084-bc0e0c8e3800

libspeex/cb_search.c
libspeex/cb_search.h
libspeex/ltp.c
libspeex/ltp.h
libspeex/modes.c
libspeex/modes.h
libspeex/nb_celp.c

index 80412ff..300db3e 100644 (file)
@@ -508,8 +508,10 @@ float *stack
 
    /* Decode codewords and gains */
    for (i=0;i<nb_subvect;i++)
+   {
       ind[i] = speex_bits_unpack_unsigned(bits, params->shape_bits);
-
+      ind[i] = rand()%(1<<params->shape_bits);
+   }
    /* Compute decoded excitation */
    for (i=0;i<nb_subvect;i++)
       for (j=0;j<subvect_size;j++)
@@ -559,3 +561,46 @@ float *stack
    POP(stack);
    POP(stack);
 }
+
+void noise_codebook_quant(
+float target[],                        /* target vector */
+float ak[],                    /* LPCs for this subframe */
+float awk1[],                  /* Weighted LPCs for this subframe */
+float awk2[],                  /* Weighted LPCs for this subframe */
+void *par,                      /* Codebook/search parameters*/
+int   p,                        /* number of LPC coeffs */
+int   nsf,                      /* number of samples in subframe */
+float *exc,
+SpeexBits *bits,
+float *stack,
+int   complexity
+)
+{
+   int i;
+   float *tmp=PUSH(stack, nsf);
+   syn_filt_zero(target, awk1, tmp, nsf, p);
+   residue_zero(tmp, ak, tmp, nsf, p);
+   residue_zero(tmp, awk2, tmp, nsf, p);
+
+   for (i=0;i<nsf;i++)
+      exc[i]+=tmp[i];
+   for (i=0;i<nsf;i++)
+      target[i]=0;
+
+   POP(stack);
+}
+
+
+void noise_codebook_unquant(
+float *exc,
+void *par,                      /* non-overlapping codebook */
+int   nsf,                      /* number of samples in subframe */
+SpeexBits *bits,
+float *stack
+)
+{
+   int i;
+
+   for (i=0;i<nsf;i++)
+      exc[i]+=2.5*((((float)rand())/RAND_MAX)-.5);
+}
index 2515044..52f21eb 100644 (file)
@@ -74,4 +74,28 @@ SpeexBits *bits,
 float *stack
 );
 
+
+void noise_codebook_quant(
+float target[],                        /* target vector */
+float ak[],                    /* LPCs for this subframe */
+float awk1[],                  /* Weighted LPCs for this subframe */
+float awk2[],                  /* Weighted LPCs for this subframe */
+void *par,                      /* Codebook/search parameters*/
+int   p,                        /* number of LPC coeffs */
+int   nsf,                      /* number of samples in subframe */
+float *exc,
+SpeexBits *bits,
+float *stack,
+int   complexity
+);
+
+
+void noise_codebook_unquant(
+float *exc,
+void *par,                      /* non-overlapping codebook */
+int   nsf,                      /* number of samples in subframe */
+SpeexBits *bits,
+float *stack
+);
+
 #endif
index 497ea79..bbec13e 100644 (file)
@@ -18,9 +18,7 @@
 */
 
 #include <math.h>
-#ifdef DEBUG
 #include <stdio.h>
-#endif
 #include "ltp.h"
 #include "stack_alloc.h"
 #include "filters.h"
@@ -48,7 +46,13 @@ void open_loop_nbest_pitch(float *sw, int start, int end, int len, int *pitch, f
       score=corr*corr/(energy+1);
       if (score>best_score[N-1])
       {
-         float g = sqrt(corr*corr/((energy+10)*(e0+10)));
+         float g1, g;
+         g1 = corr/(energy+10);
+         g = sqrt(corr/(e0+10));
+         if (g>g1)
+            g=g1;
+         if (g<0)
+            g=0;
          for (j=0;j<N;j++)
          {
             if (score > best_score[j])
@@ -269,6 +273,7 @@ float exc[],                    /* Excitation */
 void *par,
 int   start,                    /* Smallest pitch value allowed */
 int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
 int   p,                        /* Number of LPC coeffs */
 int   nsf,                      /* Number of samples in subframe */
 SpeexBits *bits,
@@ -337,6 +342,7 @@ void pitch_unquant_3tap(
 float exc[],                    /* Excitation */
 int   start,                    /* Smallest pitch value allowed */
 int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
 void *par,
 int   nsf,                      /* Number of samples in subframe */
 int *pitch_val,
@@ -415,3 +421,60 @@ int lost)
       POP(stack);
    }
 }
+
+
+/** Forced pitch delay and gain */
+int forced_pitch_quant(
+float target[],                 /* Target vector */
+float *sw,
+float ak[],                     /* LPCs for this subframe */
+float awk1[],                   /* Weighted LPCs #1 for this subframe */
+float awk2[],                   /* Weighted LPCs #2 for this subframe */
+float exc[],                    /* Excitation */
+void *par,
+int   start,                    /* Smallest pitch value allowed */
+int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
+int   p,                        /* Number of LPC coeffs */
+int   nsf,                      /* Number of samples in subframe */
+SpeexBits *bits,
+float *stack,
+float *exc2,
+int complexity
+)
+{
+   int i;
+   if (pitch_coef>.9)
+      pitch_coef=.9;
+   for (i=0;i<nsf;i++)
+   {
+      exc[i]=exc[i-start]*pitch_coef;
+   }
+   return start;
+}
+
+/** Unquantize forced pitch delay and gain */
+void forced_pitch_unquant(
+float exc[],                    /* Excitation */
+int   start,                    /* Smallest pitch value allowed */
+int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
+void *par,
+int   nsf,                      /* Number of samples in subframe */
+int *pitch_val,
+float *gain_val,
+SpeexBits *bits,
+float *stack,
+int lost)
+{
+   int i;
+   /*pitch_coef=.9;*/
+   if (pitch_coef>.9)
+      pitch_coef=.9;
+   for (i=0;i<nsf;i++)
+   {
+      exc[i]=exc[i-start]*pitch_coef;
+   }
+   *pitch_val = start;
+   *gain_val = pitch_coef;
+}
index ab51f16..59fc44a 100644 (file)
@@ -41,6 +41,7 @@ float exc[],                    /* Overlapping codebook */
 void *par,
 int   start,                    /* Smallest pitch value allowed */
 int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
 int   p,                        /* Number of LPC coeffs */
 int   nsf,                      /* Number of samples in subframe */
 SpeexBits *bits,
@@ -54,6 +55,7 @@ void pitch_unquant_3tap(
 float exc[],                    /* Excitation */
 int   start,                    /* Smallest pitch value allowed */
 int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
 void *par,
 int   nsf,                      /* Number of samples in subframe */
 int *pitch_val,
@@ -78,3 +80,38 @@ float *stack,
 float *exc2,
 int  *cdbk_index
 );
+
+
+/** Forced pitch delay and gain */
+int forced_pitch_quant(
+float target[],                 /* Target vector */
+float *sw,
+float ak[],                     /* LPCs for this subframe */
+float awk1[],                   /* Weighted LPCs #1 for this subframe */
+float awk2[],                   /* Weighted LPCs #2 for this subframe */
+float exc[],                    /* Excitation */
+void *par,
+int   start,                    /* Smallest pitch value allowed */
+int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
+int   p,                        /* Number of LPC coeffs */
+int   nsf,                      /* Number of samples in subframe */
+SpeexBits *bits,
+float *stack,
+float *exc2,
+int complexity
+);
+
+/** Unquantize forced pitch delay and gain */
+void forced_pitch_unquant(
+float exc[],                    /* Excitation */
+int   start,                    /* Smallest pitch value allowed */
+int   end,                      /* Largest pitch value allowed */
+float pitch_coef,               /* Voicing (pitch) coefficient */
+void *par,
+int   nsf,                      /* Number of samples in subframe */
+int *pitch_val,
+float *gain_val,
+SpeexBits *bits,
+float *stack,
+int lost);
index a7a961e..fc9bf73 100644 (file)
@@ -131,18 +131,18 @@ static split_cb_params split_cb_high_lbr = {
 static SpeexSubmode nb_submode1 = {
    0,
    1,
+   1,
    /* LSP quantization */
    lsp_quant_lbr,
    lsp_unquant_lbr,
    /* No pitch quantization */
-   NULL,
-   NULL,
+   forced_pitch_quant,
+   forced_pitch_unquant,
    NULL,
    /* No innovation quantization (noise only) */
+   noise_codebook_quant,
+   noise_codebook_unquant,
    NULL,
-   NULL,
-   NULL,
-
    0, 0, -1,
    43
 };
@@ -150,6 +150,7 @@ static SpeexSubmode nb_submode1 = {
 static SpeexSubmode nb_submode2 = {
    0,
    0,
+   0,
    /*LSP quantization*/
    lsp_quant_lbr,
    lsp_unquant_lbr,
@@ -169,6 +170,7 @@ static SpeexSubmode nb_submode2 = {
 
 static SpeexSubmode nb_submode3 = {
    -1,
+   0,
    1,
    /*LSP quantization*/
    lsp_quant_lbr,
@@ -188,6 +190,7 @@ static SpeexSubmode nb_submode3 = {
 
 static SpeexSubmode nb_submode4 = {
    -1,
+   0,
    1,
    /*LSP quantization*/
    lsp_quant_lbr,
@@ -207,6 +210,7 @@ static SpeexSubmode nb_submode4 = {
 
 static SpeexSubmode nb_submode5 = {
    -1,
+   0,
    3,
    /*LSP quantization*/
    lsp_quant_nb,
@@ -226,6 +230,7 @@ static SpeexSubmode nb_submode5 = {
 
 static SpeexSubmode nb_submode6 = {
    -1,
+   0,
    3,
    /*LSP quantization*/
    lsp_quant_nb,
@@ -283,6 +288,7 @@ SpeexMode speex_nb_mode = {
 
 static SpeexSubmode wb_submode1 = {
    0,
+   0,
    1,
    /*LSP quantization*/
    lsp_quant_high,
@@ -303,6 +309,7 @@ static SpeexSubmode wb_submode1 = {
 
 static SpeexSubmode wb_submode2 = {
    0,
+   0,
    1,
    /*LSP quantization*/
    lsp_quant_high,
@@ -323,6 +330,7 @@ static SpeexSubmode wb_submode2 = {
 
 static SpeexSubmode wb_submode3 = {
    0,
+   0,
    1,
    /*LSP quantization*/
    lsp_quant_high,
index fb7b707..d30476c 100644 (file)
@@ -42,11 +42,12 @@ typedef void (*lsp_unquant_func)(float *, int, SpeexBits *);
 
 /*Long-term predictor quantization*/
 typedef int (*ltp_quant_func)(float *, float *, float *, float *, 
-                                float *, float *, void *, int, int, 
-                                int, int, SpeexBits*, float *, float *, int);
+                              float *, float *, void *, int, int, float, 
+                              int, int, SpeexBits*, float *, float *, int);
 
 /*Long-term un-quantize*/
-typedef void (*ltp_unquant_func)(float *, int, int, void *, int, int *, float *, SpeexBits*, float*, int);
+typedef void (*ltp_unquant_func)(float *, int, int, float, void *, int, int *,
+                                 float *, SpeexBits*, float*, int);
 
 
 typedef void (*innovation_quant_func)(float *, float *, float *, float *, void *, int, int, 
@@ -56,6 +57,7 @@ typedef void (*innovation_unquant_func)(float *, void *, int, SpeexBits*, float
 
 typedef struct SpeexSubmode {
    int     lbr_pitch;
+   int     forced_pitch_gain;
    int     have_subframe_gain;
    /*LSP functions*/
    lsp_quant_func    lsp_quant;
index 7ff27b0..e2d7032 100644 (file)
@@ -353,13 +353,14 @@ void nb_encode(void *state, float *in, SpeexBits *bits)
 #endif
 
    /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
-   if (SUBMODE(lbr_pitch)!=-1 && SUBMODE(ltp_params))
+   if (SUBMODE(lbr_pitch)!=-1)
    {
       speex_bits_pack(bits, ol_pitch-st->min_pitch, 7);
-   } else if (SUBMODE(lbr_pitch)==0)
+   } 
+   
+   if (SUBMODE(forced_pitch_gain))
    {
       int quant;
-      speex_bits_pack(bits, ol_pitch-st->min_pitch, 7);
       quant = (int)floor(.5+15*ol_pitch_coef);
       if (quant>15)
          quant=0;
@@ -492,13 +493,13 @@ void nb_encode(void *state, float *in, SpeexBits *bits)
          exc[i]=exc2[i]=0;
 
       /* If we have a long-term predictor (not all sub-modes have one) */
-      if (SUBMODE(ltp_params))
+      if (SUBMODE(ltp_quant))
       {
+         int pit_min, pit_max;
          /* Long-term prediction */
          if (SUBMODE(lbr_pitch) != -1)
          {
             /* Low bit-rate pitch handling */
-            int pit_min, pit_max;
             int margin;
             margin = SUBMODE(lbr_pitch);
             if (margin)
@@ -512,24 +513,20 @@ void nb_encode(void *state, float *in, SpeexBits *bits)
             } else {
                pit_min=pit_max=ol_pitch;
             }
-            pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
-                                       exc, SUBMODE(ltp_params), pit_min, pit_max, 
-                                       st->lpcSize, st->subframeSize, bits, st->stack, 
-                                       exc2, st->complexity);
          } else {
-            /* Normal pitch handling */
-            pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
-                                       exc, SUBMODE(ltp_params), st->min_pitch, st->max_pitch, 
-                                       st->lpcSize, st->subframeSize, bits, st->stack, 
-                                       exc2, st->complexity);
+            pit_min = st->min_pitch;
+            pit_max = st->max_pitch;
          }
+
+         pitch = SUBMODE(ltp_quant)(target, sw, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,
+                                    exc, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
+                                    st->lpcSize, st->subframeSize, bits, st->stack, 
+                                    exc2, st->complexity);
+
          /*printf ("cl_pitch: %d\n", pitch);*/
          st->pitch[sub]=pitch;
-      } else if (SUBMODE(lbr_pitch==0)) {
-         for (i=0;i<st->subframeSize;i++)
-         {
-            exc[i]=exc[i-ol_pitch]*ol_pitch_coef;
-         }
+      } else {
+         fprintf (stderr, "No pitch prediction, what's wrong\n");
       }
 
       /* Update target for adaptive codebook contribution */
@@ -615,10 +612,7 @@ void nb_encode(void *state, float *in, SpeexBits *bits)
             for (i=0;i<st->subframeSize;i++)
                exc[i] += innov[i]*ener;
          } else {
-            /* This is the "real" (cheating) excitation in the encoder but the decoder will
-               use white noise */
-            for (i=0;i<st->subframeSize;i++)
-               exc[i] += st->buf2[i];
+            fprintf(stderr, "No fixed codebook\n");
          }
          POP(st->stack);
          for (i=0;i<st->subframeSize;i++)
@@ -807,15 +801,17 @@ void nb_decode(void *state, SpeexBits *bits, float *out, int lost)
    }
 
    /* Get open-loop pitch estimation for low bit-rate pitch coding */
-   if (SUBMODE(lbr_pitch)!=-1 && SUBMODE(ltp_params))
+   if (SUBMODE(lbr_pitch)!=-1)
    {
       ol_pitch = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);
-   } else if (SUBMODE(lbr_pitch)==0)
+   } 
+   
+   if (SUBMODE(forced_pitch_gain))
    {
       int quant;
-      ol_pitch = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);
       quant = speex_bits_unpack_unsigned(bits, 4);
       ol_pitch_coef=0.066667*quant;
+      /*fprintf (stderr, "unquant pitch coef: %f\n", ol_pitch_coef);*/
    }
    
    /* Get global excitation gain */
@@ -879,9 +875,9 @@ void nb_decode(void *state, SpeexBits *bits, float *out, int lost)
       /*Adaptive codebook contribution*/
       if (SUBMODE(ltp_unquant))
       {
+         int pit_min, pit_max;
          if (SUBMODE(lbr_pitch) != -1)
          {
-            int pit_min, pit_max;
             int margin;
             margin = SUBMODE(lbr_pitch);
             if (margin)
@@ -895,10 +891,14 @@ void nb_decode(void *state, SpeexBits *bits, float *out, int lost)
             } else {
                pit_min=pit_max=ol_pitch;
             }
-            SUBMODE(ltp_unquant)(exc, pit_min, pit_max, SUBMODE(ltp_params), st->subframeSize, &pitch, &pitch_gain[0], bits, st->stack, 0);
          } else {
-            SUBMODE(ltp_unquant)(exc, st->min_pitch, st->max_pitch, SUBMODE(ltp_params), st->subframeSize, &pitch, &pitch_gain[0], bits, st->stack, 0);
+            pit_min = st->min_pitch;
+            pit_max = st->max_pitch;
          }
+
+         SUBMODE(ltp_unquant)(exc, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params), 
+                              st->subframeSize, &pitch, &pitch_gain[0], bits, st->stack, 0);
+         
          
          if (!lost)
          {
@@ -924,11 +924,8 @@ void nb_decode(void *state, SpeexBits *bits, float *out, int lost)
             for (i=0;i<st->subframeSize;i++)
                exc[i]=st->last_pitch_gain*exc[i-st->last_pitch];
          }
-      } else if (SUBMODE(lbr_pitch==0)) {
-         for (i=0;i<st->subframeSize;i++)
-         {
-            exc[i]=exc[i-ol_pitch]*ol_pitch_coef;
-         }
+      } else {
+         fprintf (stderr, "No pitch prediction, what's wrong\n");
       }
       
       /* Unquantize the innovation */
@@ -960,28 +957,7 @@ void nb_decode(void *state, SpeexBits *bits, float *out, int lost)
             /*Fixed codebook contribution*/
             SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, st->stack);
          } else {
-#if 1
-            float scale;
-            scale = 3*sqrt(1.2-ol_pitch_coef);
-            for (i=0;i<st->subframeSize;i++)
-               innov[i] = scale*((((float)rand())/RAND_MAX)-.5);
-#else
-            static int tim=0;
-            float pitch_scale, noise_scale;
-            float voice=ol_pitch_coef;
-            if (voice>.9)
-               voice=.9;
-            noise_scale = sqrt(1.01-voice);
-            pitch_scale = sqrt(ol_pitch*voice); 
-            for (i=0;i<st->subframeSize;i++)
-               innov[i] = 3*noise_scale*((((float)rand())/RAND_MAX)-.5);
-            while (tim<st->subframeSize)
-            {
-               innov[tim] += pitch_scale;
-               tim+=ol_pitch;
-            }
-            tim-=st->subframeSize;
-#endif
+            fprintf(stderr, "No fixed codebook\n");
          }
 
          if (st->count_lost)