示例#1
0
int main(int argc, char **argv)
{
   int m = ORDER, l = FLNG, flag, t = 0;
   FILE *fp = stdin;
   double *x, *a, f = MINDET;

   if ((cmnd = strrchr(argv[0], '/')) == NULL)
      cmnd = argv[0];
   else
      cmnd++;
   while (--argc)
      if (**++argv == '-') {
         switch (*(*argv + 1)) {
         case 'm':
            m = atoi(*++argv);
            --argc;
            break;
         case 'l':
            l = atoi(*++argv);
            --argc;
            break;
         case 'f':
            f = atof(*++argv);
            --argc;
            break;
         case 'h':
            usage(0);
         default:
            fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, *(*argv + 1));
            usage(1);
         }
      } else
         fp = getfp(*argv, "rb");

   x = dgetmem(l + m + 1);
   a = x + l;

   while (freadf(x, sizeof(*x), l, fp) == l) {
      flag = lpc(x, l, a, m, f);
      switch (flag) {
      case -1:
         fprintf(stderr,
                 "%s : The coefficient matrix of the normal equation is singular at %dth frame!\n",
                 cmnd, t);
         exit(1);
         break;
      case -2:
         fprintf(stderr,
                 "%s : Extracted LPC coefficients become unstable at %dth frame!\n",
                 cmnd, t);
         break;
      }

      fwritef(a, sizeof(*a), m + 1, stdout);
      t++;
   }

   return (0);
}
示例#2
0
int main(int argc, const char * argv[]) {
    // insert code here...
    srand(0);
    int i;
    float noise[500];
    float x[500];
    float x_pred[100];
    float h[4] = {1.0, 1./2, 1./3, 1./4}; // reference coefficient produced by MATLAB lpc(x,3)
    float a[1] = {1.0};
    for (i = 0; i < 500; ++i){
        float drand = (rand()+1.0)/(RAND_MAX+1.0);
        noise[i] = sqrt(-2*log(drand)) * cos(2*PI*drand);
    }
    memset(x, 0, sizeof(float)*500);
    memset(x_pred, 0, sizeof(float)*100);
    ffilter(a, 1, h, 4, noise, 500, x);

    for (i = 0; i < 500; ++i){
        printf("%f ", noise[i]);
    }
    printf("\n");
    for (i = 0; i < 500; ++i){
        printf("%f ", x[i]);
    }
    printf("\n");

    float aut[4];
    float coef[4];
    float var;
    // lpc(float* input,int len, int N, int stride, float* aut, float* coef, float* pred, float &error)
    lpc(&(x[401]), 100, 3, 1, aut, coef, x_pred, &var);
    printf("Linear prediction filter coefficients: ");
    for (i = 0; i < 4; i++){
        printf("%f ",coef[i]);
    }
    printf("\n");
    printf("Prediction error variances: %f\n", var);
    return 0;
}
示例#3
0
void Formant_Frame_into_LPC_Frame (Formant_Frame me, LPC_Frame thee, double samplingPeriod) {
	long m = 2, n = 2 * my nFormants;

	if (my nFormants < 1) {
		return;
	}
	autoNUMvector<double> lpc (-1, n);

	lpc[0] = 1;
	double nyquist = 2.0 / samplingPeriod;
	for (long i = 1; i <= my nFormants; i++) {
		double f = my formant[i].frequency;

		if (f > nyquist) {
			continue;
		}

		// D(z): 1 + p z^-1 + q z^-2

		double r = exp (- NUMpi * my formant[i].bandwidth * samplingPeriod);
		double p = - 2 * r * cos (2 * NUMpi * f * samplingPeriod);
		double q = r * r;

		for (long j = m; j > 0; j--) {
			lpc[j] += p * lpc[j - 1] + q * lpc[j - 2];
		}

		m += 2;
	}

	n = thy nCoefficients < n ? thy nCoefficients : n;

	for (long i = 1; i <= n ; i++) {
		thy a[i] = lpc[i];
	}

	thy gain = my intensity;
}
示例#4
0
/*
 * Main standard loop for LPC analysis.
 */
int cepstral_analysis(sigstream_t *is, spfstream_t *os)
{
  unsigned long l, d, j;
  float *w = NULL, *r, *lift = NULL;
  sample_t *buf;
  spsig_t *s;
  spf_t *a, *k, *c;
  float sigma;
  int status;

  l = (unsigned long)(fm_l * is->Fs / 1000.0); /* frame length in samples */
  d = (unsigned long)(fm_d * is->Fs / 1000.0); /* frame shift in samples */

  /* ----- initialize some more stuff ----- */
  if ((s = sig_alloc(l)) == NULL) /* frame signal */
    return(SPRO_ALLOC_ERR);

  if (win) {
    if ((buf = (sample_t *)malloc(l * sizeof(sample_t))) == NULL) /* frame buffer */
      return(SPRO_ALLOC_ERR);
    if ((w = set_sig_win(l, win)) == NULL) {
      free(buf); sig_free(s);
      return(SPRO_ALLOC_ERR);    
    }
  }
  else
    buf = s->s;
  
  if ((r = (spf_t *)malloc((nlpc + 1) * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    return(SPRO_ALLOC_ERR);
  }

  if ((a = (spf_t *)malloc(nlpc * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    free(r);
    return(SPRO_ALLOC_ERR);
  }

  if ((k = (spf_t *)malloc(nlpc * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    free(r); free(a);
    return(SPRO_ALLOC_ERR);
  }
  
  if ((c = (spf_t *)malloc((numceps + 1) * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    free(r); free(a); free(k);
    return(SPRO_ALLOC_ERR);
  }

  if (lifter)
    if ((lift = set_lifter(lifter, numceps)) == NULL) {
      if (win) free(buf); sig_free(s); if (win) free(w);
      free(r); free(a); free(k); free(c);
      return(SPRO_ALLOC_ERR);      
    }

  /* ----- loop on each frame ----- */
  while (get_next_sig_frame(is, channel, l, d, emphco, buf)) {

    /* weight signal */
    if (win)
      sig_weight(s, buf, w);

    /* run LPC analysis */
    if ((status = sig_correl(s, alpha, r, nlpc)) != 0) {
      if (win) free(buf); sig_free(s); if (win) free(w);
      free(a); free(k); free(r); free(c); if (lift) free(lift);
      return(status);
    }

    lpc(r, nlpc, a, k, &sigma);    
    lpc_to_cep(a, nlpc, numceps, c);

    if (lifter)
      for (j = 0; j < numceps; j++)
	*(c+j) *= *(lift+j);

    if (flag & WITHE) {
      if (sigma < SPRO_ENERGY_FLOOR)
	sigma = SPRO_ENERGY_FLOOR;
      *(c+numceps) = (spf_t)(2.0 * log(sigma));
    }
    
    /* write vector to stream */
    if (spf_stream_write(os, c, 1) != 1) { 
      if (win) free(buf); sig_free(s); if (win) free(w);
      free(a); free(k); free(r); free(c); if (lift) free(lift);
      return(SPRO_FEATURE_WRITE_ERR);
    }
  }
    
  /* ----- reset memory ----- */
  if (win) {
    free(buf); 
    free(w);
  }
  sig_free(s); 
  free(r);
  free(a); 
  free(k); 
  free(c);
  if (lift)
    free(lift);

  return(0);
}
示例#5
0
void mdaTalkBox::processReplacing(float **inputs, float **outputs, VstInt32 sampleFrames)
{
  float *in1 = inputs[0];
  float *in2 = inputs[1];
  if(swap)
  {
    in1 = inputs[1];
    in2 = inputs[0];
  }
  float *out1 = outputs[0];
  float *out2 = outputs[1];
  VstInt32  p0=pos, p1 = (pos + N/2) % N;
  float e=emphasis, w, o, x, dr, fx=FX;
  float p, q, h0=0.3f, h1=0.77f;

  --in1;
  --in2;
  --out1;
  --out2;
  while(--sampleFrames >= 0)
  {
    o = *++in1;
    x = *++in2;
    dr = o;

    p = d0 + h0 *  x; d0 = d1;  d1 = x  - h0 * p;
    q = d2 + h1 * d4; d2 = d3;  d3 = d4 - h1 * q;  
    d4 = x;
    x = p + q;

    if(K++)
    {
      K = 0;

      car0[p0] = car1[p1] = x; //carrier input
    
      x = o - e;  e = o;  //6dB/oct pre-emphasis

      w = window[p0]; fx = buf0[p0] * w;  buf0[p0] = x * w;  //50% overlapping hanning windows
      if(++p0 >= N) { lpc(buf0, car0, N, O);  p0 = 0; }

      w = 1.0f - w;  fx += buf1[p1] * w;  buf1[p1] = x * w;
      if(++p1 >= N) { lpc(buf1, car1, N, O);  p1 = 0; }
    }

    p = u0 + h0 * fx; u0 = u1;  u1 = fx - h0 * p;
    q = u2 + h1 * u4; u2 = u3;  u3 = u4 - h1 * q;  
    u4 = fx;
    x = p + q;

    o = wet * x + dry * dr;
    *++out1 = o;
    *++out2 = o;
  }
  emphasis = e;
  pos = p0;
  FX = fx;

  float den = 1.0e-10f; //(float)pow(10.0f, -10.0f * param[4]);
  if(fabs(d0) < den) d0 = 0.0f; //anti-denormal (doesn't seem necessary but P4?)
  if(fabs(d1) < den) d1 = 0.0f;
  if(fabs(d2) < den) d2 = 0.0f;
  if(fabs(d3) < den) d3 = 0.0f;
  if(fabs(u0) < den) u0 = 0.0f;
  if(fabs(u1) < den) u1 = 0.0f;
  if(fabs(u2) < den) u2 = 0.0f;
  if(fabs(u3) < den) u3 = 0.0f;
}
示例#6
0
int sp_talkbox_compute(sp_data *sp, sp_talkbox *t, SPFLOAT *src, SPFLOAT *exc, SPFLOAT *out)
{
    int32_t p0=t->pos, p1 = (t->pos + t->N/2) % t->N;
    SPFLOAT e=t->emphasis, w, o, x, fx=t->FX;
    SPFLOAT p, q, h0=0.3f, h1=0.77f;
    SPFLOAT den;

    t->O = (uint32_t)((0.0001f + 0.0004f * t->quality) * sp->sr);

    o = *src;
    x = *exc;

    p = t->d0 + h0 * x; 
    t->d0 = t->d1;  
    t->d1 = x - h0 * p;

    q = t->d2 + h1 * t->d4;
    t->d2 = t->d3;
    t->d3 = t->d4 - h1 * q;  

    t->d4 = x;

    x = p + q;

    if(t->K++) {
        t->K = 0;

        /* carrier input */
        t->car0[p0] = t->car1[p1] = x;

        /* 6dB/oct pre-emphasis */
        x = o - e;  e = o;  

        /* 50% overlapping hanning windows */
        w = t->window[p0]; fx = t->buf0[p0] * w;  t->buf0[p0] = x * w;  
        if(++p0 >= t->N) { lpc(t->buf0, t->car0, t->N, t->O);  p0 = 0; }

        w = 1.0f - w;  fx += t->buf1[p1] * w;  t->buf1[p1] = x * w;
        if(++p1 >= t->N) { lpc(t->buf1, t->car1, t->N, t->O);  p1 = 0; }
    }

    p = t->u0 + h0 * fx; 
    t->u0 = t->u1;  
    t->u1 = fx - h0 * p;

    q = t->u2 + h1 * t->u4;
    t->u2 = t->u3;
    t->u3 = t->u4 - h1 * q;  

    t->u4 = fx;
    x = p + q;

    o = x * 0.5;
    *out = o;
    t->emphasis = e;
    t->pos = p0;
    t->FX = fx;

    den = 1.0e-10f; 
    /* anti-denormal */
    if(fabs(t->d0) < den) t->d0 = 0.0f; 
    if(fabs(t->d1) < den) t->d1 = 0.0f;
    if(fabs(t->d2) < den) t->d2 = 0.0f;
    if(fabs(t->d3) < den) t->d3 = 0.0f;
    if(fabs(t->u0) < den) t->u0 = 0.0f;
    if(fabs(t->u1) < den) t->u1 = 0.0f;
    if(fabs(t->u2) < den) t->u2 = 0.0f;
    if(fabs(t->u3) < den) t->u3 = 0.0f;
    return SP_OK;
}
/***************************************************************************
 *   FUNCTION: cod_amr
 *
 *   PURPOSE:  Main encoder routine.
 *
 *   DESCRIPTION: This function is called every 20 ms speech frame,
 *       operating on the newly read 160 speech samples. It performs the
 *       principle encoding functions to produce the set of encoded parameters
 *       which include the LSP, adaptive codebook, and fixed codebook
 *       quantization indices (addresses and gains).
 *
 *   INPUTS:
 *       No input argument are passed to this function. However, before
 *       calling this function, 160 new speech data should be copied to the
 *       vector new_speech[]. This is a global pointer which is declared in
 *       this file (it points to the end of speech buffer minus 160).
 *
 *   OUTPUTS:
 *
 *       ana[]:     vector of analysis parameters.
 *       synth[]:   Local synthesis speech (for debugging purposes)
 *
 ***************************************************************************/
int cod_amr(
    cod_amrState *st,          /* i/o : State struct                   */
    enum Mode mode,            /* i   : AMR mode                       */
    Word16 new_speech[],       /* i   : speech input (L_FRAME)         */
    Word16 ana[],              /* o   : Analysis parameters            */
    enum Mode *usedMode,       /* o   : used mode                    */
    Word16 synth[]             /* o   : Local synthesis                */
)
{
   /* LPC coefficients */
   Word16 A_t[(MP1) * 4];      /* A(z) unquantized for the 4 subframes */
   Word16 Aq_t[(MP1) * 4];     /* A(z)   quantized for the 4 subframes */
   Word16 *A, *Aq;             /* Pointer on A_t and Aq_t              */
   Word16 lsp_new[M];
   
   /* Other vectors */
   Word16 xn[L_SUBFR];         /* Target vector for pitch search       */
   Word16 xn2[L_SUBFR];        /* Target vector for codebook search    */
   Word16 code[L_SUBFR];       /* Fixed codebook excitation            */
   Word16 y1[L_SUBFR];         /* Filtered adaptive excitation         */
   Word16 y2[L_SUBFR];         /* Filtered fixed codebook excitation   */
   Word16 gCoeff[6];           /* Correlations between xn, y1, & y2:   */
   Word16 res[L_SUBFR];        /* Short term (LPC) prediction residual */
   Word16 res2[L_SUBFR];       /* Long term (LTP) prediction residual  */

   /* Vector and scalars needed for the MR475 */
   Word16 xn_sf0[L_SUBFR];     /* Target vector for pitch search       */
   Word16 y2_sf0[L_SUBFR];     /* Filtered codebook innovation         */   
   Word16 code_sf0[L_SUBFR];   /* Fixed codebook excitation            */
   Word16 h1_sf0[L_SUBFR];     /* The impulse response of sf0          */
   Word16 mem_syn_save[M];     /* Filter memory                        */
   Word16 mem_w0_save[M];      /* Filter memory                        */
   Word16 mem_err_save[M];     /* Filter memory                        */
   Word16 sharp_save;          /* Sharpening                           */
   Word16 evenSubfr;           /* Even subframe indicator              */ 
   Word16 T0_sf0 = 0;          /* Integer pitch lag of sf0             */  
   Word16 T0_frac_sf0 = 0;     /* Fractional pitch lag of sf0          */  
   Word16 i_subfr_sf0 = 0;     /* Position in exc[] for sf0            */
   Word16 gain_pit_sf0;        /* Quantized pitch gain for sf0         */
   Word16 gain_code_sf0;       /* Quantized codebook gain for sf0      */
    
   /* Scalars */
   Word16 i_subfr, subfrNr;
   Word16 T_op[L_FRAME/L_FRAME_BY2];
   Word16 T0, T0_frac;
   Word16 gain_pit, gain_code;

   /* Flags */
   Word16 lsp_flag = 0;        /* indicates resonance in LPC filter */   
   Word16 gp_limit;            /* pitch gain limit value            */
   Word16 vad_flag;            /* VAD decision flag                 */
   Word16 compute_sid_flag;    /* SID analysis  flag                 */

   Copy(new_speech, st->new_speech, L_FRAME);

   *usedMode = mode;                     move16 ();

   /* DTX processing */
   if (st->dtx)
   {  /* no test() call since this if is only in simulation env */
      /* Find VAD decision */

#ifdef  VAD2
      vad_flag = vad2 (st->new_speech,    st->vadSt);
      vad_flag = vad2 (st->new_speech+80, st->vadSt) || vad_flag;      logic16();
#else
      vad_flag = vad1(st->vadSt, st->new_speech);     
#endif
      fwc ();                 /* function worst case */

      /* NB! usedMode may change here */
      compute_sid_flag = tx_dtx_handler(st->dtx_encSt,
                                        vad_flag, 
                                        usedMode);
   }
   else 
   {
      compute_sid_flag = 0;              move16 ();
   }
   
   /*------------------------------------------------------------------------*
    *  - Perform LPC analysis:                                               *
    *       * autocorrelation + lag windowing                                *
    *       * Levinson-durbin algorithm to find a[]                          *
    *       * convert a[] to lsp[]                                           *
    *       * quantize and code the LSPs                                     *
    *       * find the interpolated LSPs and convert to a[] for all          *
    *         subframes (both quantized and unquantized)                     *
    *------------------------------------------------------------------------*/
   
   /* LP analysis */
   lpc(st->lpcSt, mode, st->p_window, st->p_window_12k2, A_t);

   fwc ();                 /* function worst case */

   /* From A(z) to lsp. LSP quantization and interpolation */
   lsp(st->lspSt, mode, *usedMode, A_t, Aq_t, lsp_new, &ana);
   
   fwc ();                 /* function worst case */

   /* Buffer lsp's and energy */
   dtx_buffer(st->dtx_encSt,
	      lsp_new,
	      st->new_speech);

   /* Check if in DTX mode */
   test();
   if (sub(*usedMode, MRDTX) == 0)
   {
      dtx_enc(st->dtx_encSt,
              compute_sid_flag,
              st->lspSt->qSt, 
              st->gainQuantSt->gc_predSt,
              &ana);
      
      Set_zero(st->old_exc,    PIT_MAX + L_INTERPOL);
      Set_zero(st->mem_w0,     M);
      Set_zero(st->mem_err,    M);
      Set_zero(st->zero,       L_SUBFR);
      Set_zero(st->hvec,       L_SUBFR);    /* set to zero "h1[-L_SUBFR..-1]" */
      /* Reset lsp states */
      lsp_reset(st->lspSt);
      Copy(lsp_new, st->lspSt->lsp_old, M);
      Copy(lsp_new, st->lspSt->lsp_old_q, M);
      
      /* Reset clLtp states */
      cl_ltp_reset(st->clLtpSt);
      st->sharp = SHARPMIN;       move16 ();
   }
   else
   {
       /* check resonance in the filter */
      lsp_flag = check_lsp(st->tonStabSt, st->lspSt->lsp_old);  move16 ();
   }
   
   /*----------------------------------------------------------------------*
    * - Find the weighted input speech w_sp[] for the whole speech frame   *
    * - Find the open-loop pitch delay for first 2 subframes               *
    * - Set the range for searching closed-loop pitch in 1st subframe      *
    * - Find the open-loop pitch delay for last 2 subframes                *
    *----------------------------------------------------------------------*/

#ifdef VAD2
   if (st->dtx)
   {  /* no test() call since this if is only in simulation env */
       st->vadSt->L_Rmax = 0;			move32 ();
       st->vadSt->L_R0 = 0;			move32 ();
   }
#endif
   for(subfrNr = 0, i_subfr = 0; 
       subfrNr < L_FRAME/L_FRAME_BY2; 
       subfrNr++, i_subfr += L_FRAME_BY2)
   {
      /* Pre-processing on 80 samples */
      pre_big(mode, gamma1, gamma1_12k2, gamma2, A_t, i_subfr, st->speech,
              st->mem_w, st->wsp);
    
      test (); test ();
      if ((sub(mode, MR475) != 0) && (sub(mode, MR515) != 0))
      {
         /* Find open loop pitch lag for two subframes */
         ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[i_subfr],
                &T_op[subfrNr], st->old_lags, st->ol_gain_flg, subfrNr,
                st->dtx);
      }
   }
   fwc ();                 /* function worst case */

   test (); test();
   if ((sub(mode, MR475) == 0) || (sub(mode, MR515) == 0))
   {
      /* Find open loop pitch lag for ONE FRAME ONLY */
      /* search on 160 samples */
      
      ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[0], &T_op[0],
             st->old_lags, st->ol_gain_flg, 1, st->dtx);
      T_op[1] = T_op[0];                                     move16 ();
   }         
   fwc ();                 /* function worst case */
   
#ifdef VAD2
   if (st->dtx)
   {  /* no test() call since this if is only in simulation env */
      LTP_flag_update(st->vadSt, mode);
   }
#endif

#ifndef VAD2
   /* run VAD pitch detection */
   if (st->dtx)
   {  /* no test() call since this if is only in simulation env */
      vad_pitch_detection(st->vadSt, T_op);
   } 
#endif
   fwc ();                 /* function worst case */

   if (sub(*usedMode, MRDTX) == 0)
   {
      goto the_end;
   }
   
   /*------------------------------------------------------------------------*
    *          Loop for every subframe in the analysis frame                 *
    *------------------------------------------------------------------------*
    *  To find the pitch and innovation parameters. The subframe size is     *
    *  L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times.               *
    *     - find the weighted LPC coefficients                               *
    *     - find the LPC residual signal res[]                               *
    *     - compute the target signal for pitch search                       *
    *     - compute impulse response of weighted synthesis filter (h1[])     *
    *     - find the closed-loop pitch parameters                            *
    *     - encode the pitch dealy                                           *
    *     - update the impulse response h1[] by including fixed-gain pitch   *
    *     - find target vector for codebook search                           *
    *     - codebook search                                                  *
    *     - encode codebook address                                          *
    *     - VQ of pitch and codebook gains                                   *
    *     - find synthesis speech                                            *
    *     - update states of weighting filter                                *
    *------------------------------------------------------------------------*/

   A = A_t;      /* pointer to interpolated LPC parameters */
   Aq = Aq_t;    /* pointer to interpolated quantized LPC parameters */

   evenSubfr = 0;                                                  move16 ();
   subfrNr = -1;                                                   move16 ();
   for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
   {
      subfrNr = add(subfrNr, 1);
      evenSubfr = sub(1, evenSubfr);

      /* Save states for the MR475 mode */
      test(); test();
      if ((evenSubfr != 0) && (sub(*usedMode, MR475) == 0))
      {
         Copy(st->mem_syn, mem_syn_save, M);
         Copy(st->mem_w0, mem_w0_save, M);         
         Copy(st->mem_err, mem_err_save, M);         
         sharp_save = st->sharp;
      }
      
      /*-----------------------------------------------------------------*
       * - Preprocessing of subframe                                     *
       *-----------------------------------------------------------------*/
      test();
      if (sub(*usedMode, MR475) != 0)
      {
         subframePreProc(*usedMode, gamma1, gamma1_12k2,
                         gamma2, A, Aq, &st->speech[i_subfr],
                         st->mem_err, st->mem_w0, st->zero,
                         st->ai_zero, &st->exc[i_subfr],
                         st->h1, xn, res, st->error);
      }
      else
      { /* MR475 */
         subframePreProc(*usedMode, gamma1, gamma1_12k2, 
                         gamma2, A, Aq, &st->speech[i_subfr],
                         st->mem_err, mem_w0_save, st->zero,
                         st->ai_zero, &st->exc[i_subfr],
                         st->h1, xn, res, st->error);

         /* save impulse response (modified in cbsearch) */
         test ();
         if (evenSubfr != 0)
         {
             Copy (st->h1, h1_sf0, L_SUBFR);
         }
      }
      
      /* copy the LP residual (res2 is modified in the CL LTP search)    */
      Copy (res, res2, L_SUBFR);

      fwc ();                 /* function worst case */
    
      /*-----------------------------------------------------------------*
       * - Closed-loop LTP search                                        *
       *-----------------------------------------------------------------*/
      cl_ltp(st->clLtpSt, st->tonStabSt, *usedMode, i_subfr, T_op, st->h1, 
             &st->exc[i_subfr], res2, xn, lsp_flag, xn2, y1, 
             &T0, &T0_frac, &gain_pit, gCoeff, &ana,
             &gp_limit);

      /* update LTP lag history */
      move16 (); test(); test ();
      if ((subfrNr == 0) && (st->ol_gain_flg[0] > 0))
      {
         st->old_lags[1] = T0;     move16 ();
      }
      
      move16 (); test(); test ();
      if ((sub(subfrNr, 3) == 0) && (st->ol_gain_flg[1] > 0))
      {
         st->old_lags[0] = T0;     move16 ();
      }      

      fwc ();                 /* function worst case */
      
      /*-----------------------------------------------------------------*
       * - Inovative codebook search (find index and gain)               *
       *-----------------------------------------------------------------*/
      cbsearch(xn2, st->h1, T0, st->sharp, gain_pit, res2, 
               code, y2, &ana, *usedMode, subfrNr);
      
      fwc ();                 /* function worst case */
    
      /*------------------------------------------------------*
       * - Quantization of gains.                             *
       *------------------------------------------------------*/
      gainQuant(st->gainQuantSt, *usedMode, res, &st->exc[i_subfr], code,
                xn, xn2,  y1, y2, gCoeff, evenSubfr, gp_limit,
                &gain_pit_sf0, &gain_code_sf0,
                &gain_pit, &gain_code, &ana);
      
      fwc ();                 /* function worst case */

      /* update gain history */
      update_gp_clipping(st->tonStabSt, gain_pit);
      
      test(); 
      if (sub(*usedMode, MR475) != 0)
      {
         /* Subframe Post Porcessing */
         subframePostProc(st->speech, *usedMode, i_subfr, gain_pit,
                          gain_code, Aq, synth, xn, code, y1, y2, st->mem_syn,
                          st->mem_err, st->mem_w0, st->exc, &st->sharp);
      }
      else
      {
         test();
         if (evenSubfr != 0)
         {
            i_subfr_sf0 = i_subfr;             move16 ();
            Copy(xn, xn_sf0, L_SUBFR);
            Copy(y2, y2_sf0, L_SUBFR);          
            Copy(code, code_sf0, L_SUBFR);
            T0_sf0 = T0;                       move16 ();
            T0_frac_sf0 = T0_frac;             move16 ();
            
            /* Subframe Post Porcessing */
            subframePostProc(st->speech, *usedMode, i_subfr, gain_pit,
                             gain_code, Aq, synth, xn, code, y1, y2,
                             mem_syn_save, st->mem_err, mem_w0_save,
                             st->exc, &st->sharp);
            st->sharp = sharp_save;                         move16();
         }
         else
         {
            /* update both subframes for the MR475 */
            
            /* Restore states for the MR475 mode */
            Copy(mem_err_save, st->mem_err, M);         
            
            /* re-build excitation for sf 0 */
            Pred_lt_3or6(&st->exc[i_subfr_sf0], T0_sf0, T0_frac_sf0,
                         L_SUBFR, 1);
            Convolve(&st->exc[i_subfr_sf0], h1_sf0, y1, L_SUBFR);
            
            Aq -= MP1;
            subframePostProc(st->speech, *usedMode, i_subfr_sf0,
                             gain_pit_sf0, gain_code_sf0, Aq,
                             synth, xn_sf0, code_sf0, y1, y2_sf0,
                             st->mem_syn, st->mem_err, st->mem_w0, st->exc,
                             &sharp_save); /* overwrites sharp_save */
            Aq += MP1;
            
            /* re-run pre-processing to get xn right (needed by postproc) */
            /* (this also reconstructs the unsharpened h1 for sf 1)       */
            subframePreProc(*usedMode, gamma1, gamma1_12k2,
                            gamma2, A, Aq, &st->speech[i_subfr],
                            st->mem_err, st->mem_w0, st->zero,
                            st->ai_zero, &st->exc[i_subfr],
                            st->h1, xn, res, st->error);
            
            /* re-build excitation sf 1 (changed if lag < L_SUBFR) */
            Pred_lt_3or6(&st->exc[i_subfr], T0, T0_frac, L_SUBFR, 1);
            Convolve(&st->exc[i_subfr], st->h1, y1, L_SUBFR);
            
            subframePostProc(st->speech, *usedMode, i_subfr, gain_pit,
                             gain_code, Aq, synth, xn, code, y1, y2,
                             st->mem_syn, st->mem_err, st->mem_w0,
                             st->exc, &st->sharp);
         }
      }      
               
      fwc ();                 /* function worst case */
          
      A += MP1;    /* interpolated LPC parameters for next subframe */
      Aq += MP1;
   }

   Copy(&st->old_exc[L_FRAME], &st->old_exc[0], PIT_MAX + L_INTERPOL);
   
the_end:
   
   /*--------------------------------------------------*
    * Update signal for next frame.                    *
    *--------------------------------------------------*/
   Copy(&st->old_wsp[L_FRAME], &st->old_wsp[0], PIT_MAX);
   
   Copy(&st->old_speech[L_FRAME], &st->old_speech[0], L_TOTAL - L_FRAME);

   fwc ();                 /* function worst case */
       
   return 0;
}
示例#8
0
/*
 * Main standard loop for LPC analysis.
 */
int lpc_analysis(sigstream_t *is, spfstream_t *os)
{
  unsigned long l, d, j;
  float *w = NULL, *r;
  sample_t *buf, *p;
  spsig_t *s;
  spf_t *a, *k, *c = NULL;
  float sigma;
  int status;

  l = (unsigned long)(fm_l * is->Fs / 1000.0); /* frame length in samples */
  d = (unsigned long)(fm_d * is->Fs / 1000.0); /* frame shift in samples */

  /* ----- initialize some more stuff ----- */
  if ((s = sig_alloc(l)) == NULL) /* frame signal */
    return(SPRO_ALLOC_ERR);

  if (win) {
    if ((buf = (sample_t *)malloc(l * sizeof(sample_t))) == NULL) /* frame buffer */
      return(SPRO_ALLOC_ERR);
    if ((w = set_sig_win(l, win)) == NULL) {
      free(buf); sig_free(s);
      return(SPRO_ALLOC_ERR);    
    }
  }
  else
    buf = s->s;
  
  if ((a = (spf_t *)malloc((ncoeff + 1) * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    return(SPRO_ALLOC_ERR);
  }

  if ((k = (spf_t *)malloc((ncoeff + 1) * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    free(a);
    return(SPRO_ALLOC_ERR);
  }
  
  if ((r = (spf_t *)malloc((ncoeff + 1) * sizeof(spf_t))) == NULL) {
    if (win) free(buf); sig_free(s); if (win) free(w);
    free(a); free(k);
    return(SPRO_ALLOC_ERR);
  }

  /* ----- loop on each frame ----- */
  while (get_next_sig_frame(is, channel, l, d, emphco, buf)) {

    /* weight signal */
    if (win)
      sig_weight(s, buf, w);

    /* run LPC analysis */
    if ((status = sig_correl(s, alpha, r, ncoeff)) != 0) {
      if (win) free(buf); sig_free(s); if (win) free(w);
      free(a); free(k); free(r);
      return(status);
    }

    lpc(r, ncoeff, a, k, &sigma);    

    switch (ctype) {
    case LPC:
      c = a;
      break;
    case REFC:
      c = k;
      break;
    case LAR:
      c = a;
      refc_to_lar(k, ncoeff, c);
      break;
    case LSP:
      c = k;
      if ((status = lpc_to_lsf(a, ncoeff, c)) != 0) {
	if (win) free(buf); sig_free(s); if (win) free(w);
	free(a); free(k); free(r);
	return(status);
      }
      break;
    }
    
    if (flag & WITHE) {
      if (sigma < SPRO_ENERGY_FLOOR)
	sigma = SPRO_ENERGY_FLOOR;
      *(c+ncoeff) = (spf_t)(2.0 * log(sigma));
    }
    
    /* write vector to stream */
    if (spf_stream_write(os, c, 1) != 1) { 
      if (win) free(buf); sig_free(s); if (win) free(w);
      free(a); free(k); free(r);
      return(SPRO_FEATURE_WRITE_ERR);
    }
  }
    
  /* ----- reset memory ----- */
  if (win) {
    free(buf); 
    free(w);
  }
  sig_free(s); 
  free(a); 
  free(k); 
  free(r);

  return(0);
}
示例#9
0
void TalkboxDSP::Process(const float * in1, const float * in2, float * out, UInt32 inFramesToProcess)
{
	UInt32 nFrames = inFramesToProcess;

	// algorithm starts here...
	float fs = GetSampleRate();
	if(fs <  MIN_SUPPORTED_SAMPLE_RATE) fs =  MIN_SUPPORTED_SAMPLE_RATE;
//	else if(fs > 96000.0f) fs = 96000.0f;
	long n = BUF_MAX;
	O = (long)((0.0001f + 0.000004f * GetParameter(_QUAL)) * fs);

	if (GetParameter(_SWAP) > 0.5f) { const float *tmp = in1;  in1 = in2;  in2 = tmp; } 
	
	if (n != N) //recalc hanning window
	{
		N = n;
		float dp = TWO_PI / (float)N;
		float p = 0.0f;
		for (n=0; n<N; n++)
		{
			window[n] = 0.5f - 0.5f * (float)cos(p);
			p += dp;
		}
	}

	float wet = 0.00707f * GetParameter(_WET);  wet *= wet;
	float dry = 0.01410f * GetParameter(_DRY);  dry *= dry;
	
	long  p0=pos, p1 = (pos + N/2) % N;
	float e=emphasis, w, o, x, dr, fx=FX;
	float p, q, h0=0.3f, h1=0.77f;
	
	while (nFrames-- > 0)
	{
		o = *in1;  in1++;
		x = *in2;  in2++;
		dr = o;

		p = d0 + h0 *  x; d0 = d1;  d1 = x  - h0 * p;
		q = d2 + h1 * d4; d2 = d3;  d3 = d4 - h1 * q;
		d4 = x;
		x = p + q;

		if(K++)
		{
			 K = 0;
			 car0[p0] = car1[p1] = x; //carrier input
			 x = o - e;  e = o;  //6dB/oct pre-emphasis

	 		 w = window[p0]; fx = buf0[p0] * w;  buf0[p0] = x * w;  //50% overlapping hanning windows
			 if(++p0 >= N) { lpc(buf0, car0, N, O);  p0 = 0; }

			 w = 1.0f - w;  fx += buf1[p1] * w;  buf1[p1] = x * w;
			 if(++p1 >= N) { lpc(buf1, car1, N, O);  p1 = 0; }
		}

		p = u0 + h0 * fx; u0 = u1;  u1 = fx - h0 * p;
		q = u2 + h1 * u4; u2 = u3;  u3 = u4 - h1 * q;  
		u4 = fx;
		x = p + q;

		o = wet * x + dry * dr;
		*out = o;  out++;
	}
	emphasis = e;
	pos = p0;
	FX = fx;
}
示例#10
0
void
keepRegistered(const std::string& sedType,
               const ExecConfiguration& config,
               const std::string& sedUri,
               boost::shared_ptr<SeD> server){
  std::vector<std::string> services = server.get()->getServices();
  int timeout  = vishnu::DEFAUT_TIMEOUT;
  config.getConfigValue<int>(vishnu::TIMEOUT, timeout);
  std::string dispUri;
  config.getRequiredConfigValue<std::string>(vishnu::DISP_URISUBS, dispUri);
  boost::shared_ptr<Server> srv = boost::make_shared<Server>(sedType, services, sedUri);
  std::string requestData = "1" + srv.get()->toString(); /* prefixed with 1 to say registering request */

  std::string response;
  bool useSsl = false;
  bool connected(false);
  while (true){
    if (config.getConfigValue<bool>(vishnu::USE_SSL, useSsl) && useSsl) {

      std::string host;
      int port;
      host = vishnu::getHostFromUri(dispUri);
      port = vishnu::getPortFromUri(dispUri);

      /* Get TLS trust store if set */
      std::string cafile;
      config.getConfigValue<std::string>(vishnu::SSL_CA, cafile);
      /* Logging */
      /* Now create a TLS client and go ahead */
      TlsClient tlsClient(host, port, cafile);
      requestData.append("\n\n");      /* required for the internal protocol */
      if (tlsClient.send(requestData) == 0) {
        response = tlsClient.recv();
        if (response == "OK\n") { // \n at the end because it's in response see sslhelpers.cpp, \n is added at end of message
          if (!connected) {
            LOG("[INFO] Registered in dispatcher", LogInfo);
          }
          connected = true;
        } else {
          connected = false;
          LOG("[WARN] Not registered in dispatcher", LogInfo);
        }
      }
    } else {
      zmq::context_t ctx(1);
      LazyPirateClient lpc(ctx, dispUri, timeout);
      lpc.send(requestData);
      response = lpc.recv();
      if (response == "OK") {
        if (!connected) {
          LOG("[INFO] Registered in dispatcher", LogInfo);
        }
        connected = true;
      } else {
        connected = false;
        LOG("[WARN] Not registered in dispatcher", LogInfo);
      }
    }
    sleep(timeout);
  }
}