if (delta_qual<0)
void nb_encode(void *state, float *in, SpeexBits *bits){   EncState *st;   int i, sub, roots;   float error;   int ol_pitch;   float ol_pitch_coef;   float ol_gain;   float delta_qual=0;   st=state;      /* Copy new data in input buffer */   speex_move(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   st->inBuf[st->bufSize-st->frameSize] = in[0] - st->preemph*st->pre_mem;   for (i=1;i<st->frameSize;i++)      st->inBuf[st->bufSize-st->frameSize+i] = in[i] - st->preemph*in[i-1];   st->pre_mem = in[st->frameSize-1];   speex_move(st->exc2Buf, st->exc2Buf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   speex_move(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   speex_move(st->swBuf, st->swBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   /* Window for analysis */   for (i=0;i<st->windowSize;i++)      st->buf2[i] = st->frame[i] * st->window[i];   /* Compute auto-correlation */   autocorr(st->buf2, st->autocorr, st->lpcSize+1, st->windowSize);   st->autocorr[0] += 10;        /* prevents NANs */   st->autocorr[0] *= st->lpc_floor; /* Noise floor in auto-correlation domain */   /* Lag windowing: equivalent to filtering in the power-spectrum domain */   for (i=0;i<st->lpcSize+1;i++)      st->autocorr[i] *= st->lagWindow[i];   /* Levinson-Durbin */   error = wld(st->lpc+1, st->autocorr, st->rc, st->lpcSize);   st->lpc[0]=1;   /* LPC to LSPs (x-domain) transform */   roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 6, 0.002, st->stack);   if (roots!=st->lpcSize)   {      fprintf (stderr, "roots!=st->lpcSize (found only %d roots)\n", roots);      exit(1);   }   /* x-domain to angle domain*/   for (i=0;i<st->lpcSize;i++)      st->lsp[i] = acos(st->lsp[i]);   /*print_vec(st->lsp, 10, "LSP:");*/   /* LSP Quantization */   if (st->first)   {      for (i=0;i<st->lpcSize;i++)         st->old_lsp[i] = st->lsp[i];   }   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */   {      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = .5*st->old_lsp[i] + .5*st->lsp[i];      lsp_enforce_margin(st->interp_lsp, st->lpcSize, .002);      /* Compute interpolated LPCs (unquantized) for whole frame*/      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = cos(st->interp_lsp[i]);      lsp_to_lpc(st->interp_lsp, st->interp_lpc, st->lpcSize,st->stack);      bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);      bw_lpc(st->gamma2, st->interp_lpc, st->bw_lpc2, st->lpcSize);      residue(st->frame, st->bw_lpc1, st->exc, st->frameSize, st->lpcSize);      syn_filt(st->exc, st->bw_lpc2, st->sw, st->frameSize, st->lpcSize);            /*Open-loop pitch*/      {         int nol_pitch[4];         float nol_pitch_coef[4];         open_loop_nbest_pitch(st->sw, st->min_pitch, st->max_pitch, st->frameSize,                                nol_pitch, nol_pitch_coef, 4, st->stack);         ol_pitch=nol_pitch[0];         ol_pitch_coef = nol_pitch_coef[0];         /*Try to remove pitch multiples*/         for (i=1;i<4;i++)         {            if ((nol_pitch_coef[i] > .85*ol_pitch_coef) &&                 (fabs(2*nol_pitch[i]-ol_pitch)<=2 || fabs(3*nol_pitch[i]-ol_pitch)<=4 ||                  fabs(4*nol_pitch[i]-ol_pitch)<=6 || fabs(5*nol_pitch[i]-ol_pitch)<=8))            {               /*ol_pitch_coef=nol_pitch_coef[i];*/               ol_pitch = nol_pitch[i];            }         }         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/         /*printf ("ol_pitch: %d %f\n", ol_pitch, ol_pitch_coef);*/      }      /*Compute "real" excitation*/      residue(st->frame, st->interp_lpc, st->exc, st->frameSize, st->lpcSize);      /* Compute open-loop excitation gain */      ol_gain=0;      for (i=0;i<st->frameSize;i++)         ol_gain += st->exc[i]*st->exc[i];            ol_gain=sqrt(1+ol_gain/st->frameSize);   }   /*Experimental VBR stuff*/   if (st->vbr)   {      delta_qual = vbr_analysis(st->vbr, in, st->frameSize, ol_pitch, ol_pitch_coef);      if (delta_qual<0)         delta_qual*=.1*(3+st->vbr_quality);      if (st->vbr_enabled)       {         int qual = (int)floor(st->vbr_quality+delta_qual+.5);         if (qual<0)            qual=0;         if (qual>10)            qual=10;         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &qual);      }   }   /*printf ("VBR quality = %f\n", vbr_qual);*/   /* First, transmit a zero for narrowband */   speex_bits_pack(bits, 0, 1);   /* Transmit the sub-mode we use for this frame */   speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);   /* If null mode (no transmission), just set a couple things to zero*/   if (st->submodes[st->submodeID] == NULL)   {      for (i=0;i<st->frameSize;i++)         st->exc[i]=st->exc2[i]=st->sw[i]=0;      for (i=0;i<st->lpcSize;i++)         st->mem_sw[i]=0;      st->first=1;      /* Final signal synthesis from excitation */      syn_filt_mem(st->exc, st->interp_qlpc, st->frame, st->subframeSize, st->lpcSize, st->mem_sp);      in[0] = st->frame[0] + st->preemph*st->pre_mem2;      for (i=1;i<st->frameSize;i++)         in[i]=st->frame[i] + st->preemph*in[i-1];      st->pre_mem2=in[st->frameSize-1];      return;   }   /*Quantize LSPs*/#if 1 /*0 for unquantized*/   SUBMODE(lsp_quant)(st->lsp, st->qlsp, st->lpcSize, bits);#else   for (i=0;i<st->lpcSize;i++)     st->qlsp[i]=st->lsp[i];#endif   /*If we use low bit-rate pitch mode, transmit open-loop pitch*/   if (SUBMODE(lbr_pitch)!=-1)   {      speex_bits_pack(bits, ol_pitch-st->min_pitch, 7);   }       if (SUBMODE(forced_pitch_gain))   {      int quant;      quant = (int)floor(.5+15*ol_pitch_coef);      if (quant>15)         quant=0;      if (quant<0)         quant=0;      speex_bits_pack(bits, quant, 4);      ol_pitch_coef=0.066667*quant;   }         /*Quantize and transmit open-loop excitation gain*/   {      int qe = (int)(floor(3.5*log(ol_gain)));      if (qe<0)         qe=0;      if (qe>31)         qe=31;      ol_gain = exp(qe/3.5);      speex_bits_pack(bits, qe, 5);   }   /* Special case for first frame */   if (st->first)   {      for (i=0;i<st->lpcSize;i++)         st->old_qlsp[i] = st->qlsp[i];   }   /* Loop on sub-frames */   for (sub=0;sub<st->nbSubframes;sub++)   {      float esig, enoise, snr, tmp;      int   offset;      float *sp, *sw, *res, *exc, *target, *mem, *exc2;      int pitch;      /* Offset relative to start of frame */      offset = st->subframeSize*sub;      /* Original signal */      sp=st->frame+offset;      /* Excitation */      exc=st->exc+offset;      /* Weighted signal */      sw=st->sw+offset;      exc2=st->exc2+offset;      /* Filter response */      res = PUSH(st->stack, st->subframeSize);      /* Target signal */      target = PUSH(st->stack, st->subframeSize);      mem = PUSH(st->stack, st->lpcSize);      /* LSP interpolation (quantized and unquantized) */      tmp = (1.0 + sub)/st->nbSubframes;      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = (1-tmp)*st->old_lsp[i] + tmp*st->lsp[i];      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = (1-tmp)*st->old_qlsp[i] + tmp*st->qlsp[i];      /* Make sure the filters are stable */      lsp_enforce_margin(st->interp_lsp, st->lpcSize, .002);      lsp_enforce_margin(st->interp_qlsp, st->lpcSize, .002);      /* Compute interpolated LPCs (quantized and unquantized) */      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = cos(st->interp_lsp[i]);      lsp_to_lpc(st->interp_lsp, st->interp_lpc, st->lpcSize,st->stack);      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = cos(st->interp_qlsp[i]);      lsp_to_lpc(st->interp_qlsp, st->interp_qlpc, st->lpcSize, st->stack);      /* Compute analysis filter gain at w=pi (for use in SB-CELP) */      tmp=1;      st->pi_gain[sub]=0;      for (i=0;i<=st->lpcSize;i++)      {         st->pi_gain[sub] += tmp*st->interp_qlpc[i];         tmp = -tmp;      }           /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */      bw_lpc(st->gamma1, st->interp_lpc, st->bw_lpc1, st->lpcSize);      if (st->gamma2>=0)         bw_lpc(st->gamma2, st->interp_lpc, st->bw_lpc2, st->lpcSize);      else      {         st->bw_lpc2[0]=1;         st->bw_lpc2[1]=-st->preemph;         for (i=2;i<=st->lpcSize;i++)            st->bw_lpc2[i]=0;      }      /* Reset excitation */      for (i=0;i<st->subframeSize;i++)         exc[i]=0;      for (i=0;i<st->subframeSize;i++)         exc2[i]=0;      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      syn_filt_mem(exc, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, mem);      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      residue_mem(exc, st->bw_lpc1, res, st->subframeSize, st->lpcSize, mem);      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sw[i];      syn_filt_mem(res, st->bw_lpc2, res, st->subframeSize, st->lpcSize, mem);      /* Compute weighted signal */      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      residue_mem(sp, st->bw_lpc1, sw, st->subframeSize, st->lpcSize, mem);      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sw[i];      syn_filt_mem(sw, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, mem);            esig=0;      for (i=0;i<st->subframeSize;i++)         esig+=sw[i]*sw[i];            /* Compute target signal */      for (i=0;i<st->subframeSize;i++)         target[i]=sw[i]-res[i];      for (i=0;i<st->subframeSize;i++)         exc[i]=exc2[i]=0;      /* If we have a long-term predictor (not all sub-modes have one) */      if (SUBMODE(ltp_quant))      {         int pit_min, pit_max;         /* Long-term prediction */         if (SUBMODE(lbr_pitch) != -1)         {            /* Low bit-rate pitch handling */            int margin;            margin = SUBMODE(lbr_pitch);            if (margin)            {               if (ol_pitch < st->min_pitch+margin-1)                  ol_pitch=st->min_pitch+margin-1;               if (ol_pitch > st->max_pitch-margin)                  ol_pitch=st->max_pitch-margin;               pit_min = ol_pitch-margin+1;               pit_max = ol_pitch+margin;            } else {               pit_min=pit_max=ol_pitch;            }         } else {            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 {         fprintf (stderr, "No pitch prediction, what's wrong\n");      }      /* Update target for adaptive codebook contribution */      residue_zero(exc, st->bw_lpc1, res, st->subframeSize, st->lpcSize);      syn_filt_zero(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize);      syn_filt_zero(res, st->bw_lpc2, res, st->subframeSize, st->lpcSize);      for (i=0;i<st->subframeSize;i++)        target[i]-=res[i];      /* Compute noise energy and SNR */      enoise=0;      for (i=0;i<st->subframeSize;i++)         enoise += target[i]*target[i];      snr = 10*log10((esig+1)/(enoise+1));      /*st->pitch[sub]=(int)snr;*/#ifdef DEBUG      printf ("pitch SNR = %f\n", snr);#endif      /* Quantization of innovation */      {         float *innov;         float ener=0, ener_1;         /*innov=PUSH(st->stack, st->subframeSize);*/         innov = st->innov+sub*st->subframeSize;         for (i=0;i<st->subframeSize;i++)            innov[i]=0;         syn_filt_zero(target, st->bw_lpc1, res, st->subframeSize, st->lpcSize);         residue_zero(res, st->interp_qlpc, st->buf2, st->subframeSize, st->lpcSize);         residue_zero(st->buf2, st->bw_lpc2, st->buf2, st->subframeSize, st->lpcSize);         for (i=0;i<st->subframeSize;i++)            ener+=st->buf2[i]*st->buf2[i];         ener=sqrt(.1+ener/st->subframeSize);                  ener /= ol_gain;         if (0)            printf ("ener: %f %f %f\n", ener, ol_gain, ol_pitch_coef);         if (SUBMODE(have_subframe_gain))          {            int qe;            ener=log(ener);            if (SUBMODE(have_subframe_gain)==3)            {               qe = vq_index(&ener, exc_gain_quant_scal3, 1, 8);               speex_bits_pack(bits, qe, 3);               ener=exc_gain_quant_scal3[qe];            } else {               qe = vq_index(&ener, exc_gain_quant_scal1, 1, 2);               speex_bits_pack(bits, qe, 1);               ener=exc_gain_quant_scal1[qe];                           }            ener=exp(ener);            /*printf ("encode gain: %d %f\n", qe, ener);*/         } else {            ener=1;         }         ener*=ol_gain;         /*printf ("transmit gain: %f\n", ener);*/         ener_1 = 1/ener;         if (0) {            int start=rand()%35;            printf ("norm_exc: ");            for (i=start;i<start+5;i++)               printf ("%f ", ener_1*st->buf2[i]);            printf ("\n");         }                  for (i=0;i<st->subframeSize;i++)            target[i]*=ener_1;                  if (SUBMODE(innovation_quant))         {            /* Normal quantization */            SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,                                       SUBMODE(innovation_params), st->lpcSize, st->subframeSize,                                       innov, bits, st->stack, st->complexity);            for (i=0;i<st->subframeSize;i++)               innov[i]*=ener;            for (i=0;i<st->subframeSize;i++)               exc[i] += innov[i];         } else {            fprintf(stderr, "No fixed codebook\n");         }         /*POP(st->stack);*/         for (i=0;i<st->subframeSize;i++)            target[i]*=ener;      }      /* Compute weighted noise energy and SNR */      enoise=0;      for (i=0;i<st->subframeSize;i++)         enoise += target[i]*target[i];      snr = 10*log10((esig+1)/(enoise+1));#ifdef DEBUG      printf ("seg SNR = %f\n", snr);#endif      /*Keep the previous memory*/      for (i=0;i<st->lpcSize;i++)         mem[i]=st->mem_sp[i];      /* Final signal synthesis from excitation */      syn_filt_mem(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp);      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */      residue_mem(sp, st->bw_lpc1, sw, st->subframeSize, st->lpcSize, mem);      syn_filt_mem(sw, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);      for (i=0;i<st->subframeSize;i++)         exc2[i]=exc[i];      POP(st->stack);      POP(st->stack);      POP(st->stack);   }   /* Store the LSPs for interpolation in the next frame */   for (i=0;i<st->lpcSize;i++)      st->old_lsp[i] = st->lsp[i];   for (i=0;i<st->lpcSize;i++)      st->old_qlsp[i] = st->qlsp[i];   /* The next frame will not be the first (Duh!) */   st->first = 0;   /* Replace input by synthesized speech */   in[0] = st->frame[0] + st->preemph*st->pre_mem2;   for (i=1;i<st->frameSize;i++)     in[i]=st->frame[i] + st->preemph*in[i-1];   st->pre_mem2=in[st->frameSize-1];}