shithub: opus

Download patch

ref: 54710acfe787aad78bcfa4d5150288b7fe027653
parent: fd7a03bdad2d118b0d63f1072c1e708708404231
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Sun Jun 24 22:10:31 EDT 2018

fix pitch

--- a/dnn/celt_lpc.c
+++ b/dnn/celt_lpc.c
@@ -34,8 +34,9 @@
 #include "common.h"
 #include "pitch.h"
 
-void _celt_lpc(
+float _celt_lpc(
       opus_val16       *_lpc, /* out: [0...p-1] LPC coefficients      */
+      opus_val16 *rc,
 const opus_val32 *ac,  /* in:  [0...p] autocorrelation values  */
 int          p
 )
@@ -50,6 +51,7 @@
 #endif
 
    RNN_CLEAR(lpc, p);
+   RNN_CLEAR(rc, p);
    if (ac[0] != 0)
    {
       for (i = 0; i < p; i++) {
@@ -59,6 +61,7 @@
             rr += MULT32_32_Q31(lpc[j],ac[i - j]);
          rr += SHR32(ac[i + 1],3);
          r = -SHL32(rr,3)/error;
+         rc[i] = r;
          /*  Update LPC coefficients and total error */
          lpc[i] = SHR32(r,3);
          for (j = 0; j < (i+1)>>1; j++)
@@ -85,6 +88,7 @@
    for (i=0;i<p;i++)
       _lpc[i] = ROUND16(lpc[i],16);
 #endif
+   return error;
 }
 
 
--- a/dnn/celt_lpc.h
+++ b/dnn/celt_lpc.h
@@ -37,7 +37,7 @@
 
 #define LPC_ORDER 16
 
-void _celt_lpc(opus_val16 *_lpc, const opus_val32 *ac, int p);
+float _celt_lpc(opus_val16 *_lpc, opus_val16 *rc, const opus_val32 *ac, int p);
 
 void celt_fir(
          const opus_val16 *x,
--- a/dnn/denoise.c
+++ b/dnn/denoise.c
@@ -308,28 +308,45 @@
 static void frame_analysis(DenoiseState *st, kiss_fft_cpx *X, float *Ex, const float *in) {
   int i;
   float x[WINDOW_SIZE];
+  float x0[WINDOW_SIZE];
   float ac[LPC_ORDER+1];
   float lpc[LPC_ORDER];
+  float rc[LPC_ORDER];
   RNN_COPY(x, st->analysis_mem, FRAME_SIZE);
   for (i=0;i<FRAME_SIZE;i++) x[FRAME_SIZE + i] = in[i];
   RNN_COPY(st->analysis_mem, in, FRAME_SIZE);
+  RNN_COPY(x0, x, WINDOW_SIZE);
   apply_window(x);
   {
+    float e;
+    float g_1;
     _celt_autocorr(x, ac, NULL, 0, LPC_ORDER, WINDOW_SIZE);
     /* -40 dB noise floor. */
     ac[0] += ac[0]*1e-4;
     /* Lag windowing. */
     for (i=1;i<LPC_ORDER+1;i++) ac[i] *= (1 - 6e-5*i*i);
-    _celt_lpc(lpc, ac, LPC_ORDER);
+    e = _celt_lpc(lpc, rc, ac, LPC_ORDER);
+    g_1 = sqrt(FRAME_SIZE/(1e-10+e));
 #if 0
     for(i=0;i<WINDOW_SIZE;i++) printf("%f ", x[i]);
     printf("\n");
 #endif
-#if 1
-    printf("1 ");
+#if 0
+    printf("%f 1 ", e);
     for(i=0;i<LPC_ORDER;i++) printf("%f ", lpc[i]);
     printf("\n");
 #endif
+#if 0
+    for (i=0;i<FRAME_SIZE;i++) {
+      int j;
+      float *z;
+      float tmp;
+      z = &x0[i]+FRAME_SIZE/2;
+      tmp = z[0];
+      for (j=0;j<LPC_ORDER;j++) tmp += lpc[j]*z[-1-j];
+      printf("%f\n", g_1*tmp);
+    }
+#endif
   }
   forward_transform(X, x);
 #if TRAINING
@@ -347,7 +364,7 @@
   float spec_variability = 0;
   float Ly[NB_BANDS];
   float p[WINDOW_SIZE];
-  float pitch_buf[PITCH_BUF_SIZE>>1];
+  float pitch_buf[PITCH_BUF_SIZE];
   int pitch_index;
   float gain;
   float *(pre[1]);
@@ -356,18 +373,21 @@
   frame_analysis(st, X, Ex, in);
   RNN_MOVE(st->pitch_buf, &st->pitch_buf[FRAME_SIZE], PITCH_BUF_SIZE-FRAME_SIZE);
   RNN_COPY(&st->pitch_buf[PITCH_BUF_SIZE-FRAME_SIZE], in, FRAME_SIZE);
-  pre[0] = &st->pitch_buf[0];
-  pitch_downsample(pre, pitch_buf, PITCH_BUF_SIZE, 1);
-  pitch_search(pitch_buf+(PITCH_MAX_PERIOD>>1), pitch_buf, PITCH_FRAME_SIZE,
-               PITCH_MAX_PERIOD-3*PITCH_MIN_PERIOD, &pitch_index);
-  pitch_index = PITCH_MAX_PERIOD-pitch_index;
-
-  gain = remove_doubling(pitch_buf, PITCH_MAX_PERIOD, PITCH_MIN_PERIOD,
-          PITCH_FRAME_SIZE, &pitch_index, st->last_period, st->last_gain);
+  //pre[0] = &st->pitch_buf[0];
+  RNN_COPY(pitch_buf, &st->pitch_buf[0], PITCH_BUF_SIZE);
+  pitch_downsample(pitch_buf, PITCH_BUF_SIZE);
+  pitch_search(pitch_buf+PITCH_MAX_PERIOD, pitch_buf, PITCH_FRAME_SIZE<<1,
+               (PITCH_MAX_PERIOD-3*PITCH_MIN_PERIOD)<<1, &pitch_index);
+  printf("%d ", pitch_index);
+  pitch_index = 2*PITCH_MAX_PERIOD-pitch_index;
+  printf("%d ", pitch_index);
+  gain = remove_doubling(pitch_buf, 2*PITCH_MAX_PERIOD, 2*PITCH_MIN_PERIOD,
+          2*PITCH_FRAME_SIZE, &pitch_index, st->last_period, st->last_gain);
   st->last_period = pitch_index;
   st->last_gain = gain;
+  printf("%d %f\n", pitch_index, gain);
   for (i=0;i<WINDOW_SIZE;i++)
-    p[i] = st->pitch_buf[PITCH_BUF_SIZE-WINDOW_SIZE-pitch_index+i];
+    p[i] = st->pitch_buf[PITCH_BUF_SIZE-WINDOW_SIZE-pitch_index/2+i];
   apply_window(p);
   forward_transform(P, p);
   compute_band_energy(Ep, P);
--- a/dnn/pitch.c
+++ b/dnn/pitch.c
@@ -145,43 +145,19 @@
 }
 
 
-void pitch_downsample(celt_sig *x[], opus_val16 *x_lp,
-      int len, int C)
+void pitch_downsample(opus_val16 *x_lp,
+      int len)
 {
    int i;
    opus_val32 ac[5];
    opus_val16 tmp=Q15ONE;
+   opus_val16 rc[4];
    opus_val16 lpc[4], mem[5]={0,0,0,0,0};
    opus_val16 lpc2[5];
    opus_val16 c1 = QCONST16(.8f,15);
-#ifdef FIXED_POINT
-   int shift;
-   opus_val32 maxabs = celt_maxabs32(x[0], len);
-   if (C==2)
-   {
-      opus_val32 maxabs_1 = celt_maxabs32(x[1], len);
-      maxabs = MAX32(maxabs, maxabs_1);
-   }
-   if (maxabs<1)
-      maxabs=1;
-   shift = celt_ilog2(maxabs)-10;
-   if (shift<0)
-      shift=0;
-   if (C==2)
-      shift++;
-#endif
-   for (i=1;i<len>>1;i++)
-      x_lp[i] = SHR32(HALF32(HALF32(x[0][(2*i-1)]+x[0][(2*i+1)])+x[0][2*i]), shift);
-   x_lp[0] = SHR32(HALF32(HALF32(x[0][1])+x[0][0]), shift);
-   if (C==2)
-   {
-      for (i=1;i<len>>1;i++)
-         x_lp[i] += SHR32(HALF32(HALF32(x[1][(2*i-1)]+x[1][(2*i+1)])+x[1][2*i]), shift);
-      x_lp[0] += SHR32(HALF32(HALF32(x[1][1])+x[1][0]), shift);
-   }
 
    _celt_autocorr(x_lp, ac, NULL, 0,
-                  4, len>>1);
+                  4, len);
 
    /* Noise floor -40 dB */
 #ifdef FIXED_POINT
@@ -200,7 +176,7 @@
 #endif
    }
 
-   _celt_lpc(lpc, ac, 4);
+   _celt_lpc(lpc, rc, ac, 4);
    for (i=0;i<4;i++)
    {
       tmp = MULT16_16_Q15(QCONST16(.9f,15), tmp);
@@ -212,7 +188,7 @@
    lpc2[2] = lpc[2] + MULT16_16_Q15(c1,lpc[1]);
    lpc2[3] = lpc[3] + MULT16_16_Q15(c1,lpc[2]);
    lpc2[4] = MULT16_16_Q15(c1,lpc[3]);
-   celt_fir5(x_lp, lpc2, x_lp, len>>1, mem);
+   celt_fir5(x_lp, lpc2, x_lp, len, mem);
 }
 
 void celt_pitch_xcorr(const opus_val16 *_x, const opus_val16 *_y,
--- a/dnn/pitch.h
+++ b/dnn/pitch.h
@@ -38,8 +38,8 @@
 //#include "cpu_support.h"
 #include "arch.h"
 
-void pitch_downsample(celt_sig *x[], opus_val16 *x_lp,
-      int len, int C);
+void pitch_downsample(opus_val16 *x_lp,
+      int len);
 
 void pitch_search(const opus_val16 *x_lp, opus_val16 *y,
                   int len, int max_pitch, int *pitch);
--