Home | History | Annotate | Download | only in source
      1 /*
      2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
      3  *
      4  *  Use of this source code is governed by a BSD-style license
      5  *  that can be found in the LICENSE file in the root of the source
      6  *  tree. An additional intellectual property rights grant can be found
      7  *  in the file PATENTS.  All contributing project authors may
      8  *  be found in the AUTHORS file in the root of the source tree.
      9  */
     10 
     11 /* encode.c  - Encoding function for the iSAC coder */
     12 
     13 #include "structs.h"
     14 #include "codec.h"
     15 #include "pitch_estimator.h"
     16 
     17 #include <math.h>
     18 
     19 void WebRtcIsac_InitMasking(MaskFiltstr *maskdata) {
     20 
     21   int k;
     22 
     23   for (k = 0; k < WINLEN; k++) {
     24     maskdata->DataBufferLo[k] = 0.0;
     25     maskdata->DataBufferHi[k] = 0.0;
     26   }
     27   for (k = 0; k < ORDERLO+1; k++) {
     28     maskdata->CorrBufLo[k] = 0.0;
     29     maskdata->PreStateLoF[k] = 0.0;
     30     maskdata->PreStateLoG[k] = 0.0;
     31     maskdata->PostStateLoF[k] = 0.0;
     32     maskdata->PostStateLoG[k] = 0.0;
     33   }
     34   for (k = 0; k < ORDERHI+1; k++) {
     35     maskdata->CorrBufHi[k] = 0.0;
     36     maskdata->PreStateHiF[k] = 0.0;
     37     maskdata->PreStateHiG[k] = 0.0;
     38     maskdata->PostStateHiF[k] = 0.0;
     39     maskdata->PostStateHiG[k] = 0.0;
     40   }
     41 
     42   maskdata->OldEnergy = 10.0;
     43   return;
     44 }
     45 
     46 void WebRtcIsac_InitPreFilterbank(PreFiltBankstr *prefiltdata)
     47 {
     48   int k;
     49 
     50   for (k = 0; k < QLOOKAHEAD; k++) {
     51     prefiltdata->INLABUF1[k] = 0;
     52     prefiltdata->INLABUF2[k] = 0;
     53 
     54     prefiltdata->INLABUF1_float[k] = 0;
     55     prefiltdata->INLABUF2_float[k] = 0;
     56   }
     57   for (k = 0; k < 2*(QORDER-1); k++) {
     58     prefiltdata->INSTAT1[k] = 0;
     59     prefiltdata->INSTAT2[k] = 0;
     60     prefiltdata->INSTATLA1[k] = 0;
     61     prefiltdata->INSTATLA2[k] = 0;
     62 
     63     prefiltdata->INSTAT1_float[k] = 0;
     64     prefiltdata->INSTAT2_float[k] = 0;
     65     prefiltdata->INSTATLA1_float[k] = 0;
     66     prefiltdata->INSTATLA2_float[k] = 0;
     67   }
     68 
     69   /* High pass filter states */
     70   prefiltdata->HPstates[0] = 0.0;
     71   prefiltdata->HPstates[1] = 0.0;
     72 
     73   prefiltdata->HPstates_float[0] = 0.0f;
     74   prefiltdata->HPstates_float[1] = 0.0f;
     75 
     76   return;
     77 }
     78 
     79 void WebRtcIsac_InitPostFilterbank(PostFiltBankstr *postfiltdata)
     80 {
     81   int k;
     82 
     83   for (k = 0; k < 2*POSTQORDER; k++) {
     84     postfiltdata->STATE_0_LOWER[k] = 0;
     85     postfiltdata->STATE_0_UPPER[k] = 0;
     86 
     87     postfiltdata->STATE_0_LOWER_float[k] = 0;
     88     postfiltdata->STATE_0_UPPER_float[k] = 0;
     89   }
     90 
     91   /* High pass filter states */
     92   postfiltdata->HPstates1[0] = 0.0;
     93   postfiltdata->HPstates1[1] = 0.0;
     94 
     95   postfiltdata->HPstates2[0] = 0.0;
     96   postfiltdata->HPstates2[1] = 0.0;
     97 
     98   postfiltdata->HPstates1_float[0] = 0.0f;
     99   postfiltdata->HPstates1_float[1] = 0.0f;
    100 
    101   postfiltdata->HPstates2_float[0] = 0.0f;
    102   postfiltdata->HPstates2_float[1] = 0.0f;
    103 
    104   return;
    105 }
    106 
    107 
    108 void WebRtcIsac_InitPitchFilter(PitchFiltstr *pitchfiltdata)
    109 {
    110   int k;
    111 
    112   for (k = 0; k < PITCH_BUFFSIZE; k++) {
    113     pitchfiltdata->ubuf[k] = 0.0;
    114   }
    115   pitchfiltdata->ystate[0] = 0.0;
    116   for (k = 1; k < (PITCH_DAMPORDER); k++) {
    117     pitchfiltdata->ystate[k] = 0.0;
    118   }
    119   pitchfiltdata->oldlagp[0] = 50.0;
    120   pitchfiltdata->oldgainp[0] = 0.0;
    121 }
    122 
    123 void WebRtcIsac_InitWeightingFilter(WeightFiltstr *wfdata)
    124 {
    125   int k;
    126   double t, dtmp, dtmp2, denum, denum2;
    127 
    128   for (k=0;k<PITCH_WLPCBUFLEN;k++)
    129     wfdata->buffer[k]=0.0;
    130 
    131   for (k=0;k<PITCH_WLPCORDER;k++) {
    132     wfdata->istate[k]=0.0;
    133     wfdata->weostate[k]=0.0;
    134     wfdata->whostate[k]=0.0;
    135   }
    136 
    137   /* next part should be in Matlab, writing to a global table */
    138   t = 0.5;
    139   denum = 1.0 / ((double) PITCH_WLPCWINLEN);
    140   denum2 = denum * denum;
    141   for (k=0;k<PITCH_WLPCWINLEN;k++) {
    142     dtmp = PITCH_WLPCASYM * t * denum + (1-PITCH_WLPCASYM) * t * t * denum2;
    143     dtmp *= 3.14159265;
    144     dtmp2 = sin(dtmp);
    145     wfdata->window[k] = dtmp2 * dtmp2;
    146     t++;
    147   }
    148 }
    149 
    150 /* clear all buffers */
    151 void WebRtcIsac_InitPitchAnalysis(PitchAnalysisStruct *State)
    152 {
    153   int k;
    154 
    155   for (k = 0; k < PITCH_CORR_LEN2+PITCH_CORR_STEP2+PITCH_MAX_LAG/2-PITCH_FRAME_LEN/2+2; k++)
    156     State->dec_buffer[k] = 0.0;
    157   for (k = 0; k < 2*ALLPASSSECTIONS+1; k++)
    158     State->decimator_state[k] = 0.0;
    159   for (k = 0; k < 2; k++)
    160     State->hp_state[k] = 0.0;
    161   for (k = 0; k < QLOOKAHEAD; k++)
    162     State->whitened_buf[k] = 0.0;
    163   for (k = 0; k < QLOOKAHEAD; k++)
    164     State->inbuf[k] = 0.0;
    165 
    166   WebRtcIsac_InitPitchFilter(&(State->PFstr_wght));
    167 
    168   WebRtcIsac_InitPitchFilter(&(State->PFstr));
    169 
    170   WebRtcIsac_InitWeightingFilter(&(State->Wghtstr));
    171 }
    172