exc[0]=1;
exc[0]=SIG_SCALING;
int sb_encode(void *state, float *in, SpeexBits *bits){   SBEncState *st;   int i, roots, sub;   char *stack;   spx_mem_t *mem;   float *innov, *syn_resp;   float *low_pi_gain, *low_exc, *low_innov;   SpeexSBMode *mode;   int dtx;   st = (SBEncState*)state;   stack=st->stack;   mode = (SpeexSBMode*)(st->mode->mode);   /* Compute the two sub-bands by filtering with h0 and h1*/   qmf_decomp(in, h0, st->x0d, st->x1d, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);       /* Encode the narrowband part*/   speex_encode(st->st_low, st->x0d, bits);   /* High-band buffering / sync with low band */   for (i=0;i<st->windowSize-st->frame_size;i++)      st->high[i] = st->high[st->frame_size+i];   for (i=0;i<st->frame_size;i++)      st->high[st->windowSize-st->frame_size+i]=st->x1d[i];   speex_move(st->excBuf, st->excBuf+st->frame_size, (st->bufSize-st->frame_size)*sizeof(float));   low_pi_gain = PUSH(stack, st->nbSubframes, float);   low_exc = PUSH(stack, st->frame_size, float);   low_innov = PUSH(stack, st->frame_size, float);   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc);   speex_encoder_ctl(st->st_low, SPEEX_GET_INNOV, low_innov);      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);   if (dtx==0)      dtx=1;   else      dtx=0;   /* Start encoding the high-band */   for (i=0;i<st->windowSize;i++)      st->buf[i] = st->high[i] * st->window[i];   /* Compute auto-correlation */   _spx_autocorr(st->buf, st->autocorr, st->lpcSize+1, st->windowSize);   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 */   _spx_lpc(st->lpc+1, st->autocorr, st->lpcSize);   st->lpc[0]=LPC_SCALING;   /* LPC to LSPs (x-domain) transform */   roots=lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 15, 0.2, stack);   if (roots!=st->lpcSize)   {      roots = lpc_to_lsp (st->lpc, st->lpcSize, st->lsp, 11, 0.02, stack);      if (roots!=st->lpcSize) {         /*If we can't find all LSP's, do some damage control and use a flat filter*/         for (i=0;i<st->lpcSize;i++)         {            st->lsp[i]=cos(M_PI*((float)(i+1))/(st->lpcSize+1));         }      }   }   /* x-domain to angle domain*/   for (i=0;i<st->lpcSize;i++)      st->lsp[i] = acos(st->lsp[i]);   /* VBR code */   if ((st->vbr_enabled || st->vad_enabled) && !dtx)   {      float e_low=0, e_high=0;      float ratio;      if (st->abr_enabled)      {         float qual_change=0;         if (st->abr_drift2 * st->abr_drift > 0)         {            /* Only adapt if long-term and short-term drift are the same sign */            qual_change = -.00001*st->abr_drift/(1+st->abr_count);            if (qual_change>.1)               qual_change=.1;            if (qual_change<-.1)               qual_change=-.1;         }         st->vbr_quality += qual_change;         if (st->vbr_quality>10)            st->vbr_quality=10;         if (st->vbr_quality<0)            st->vbr_quality=0;      }      for (i=0;i<st->frame_size;i++)      {         e_low  += st->x0d[i]* st->x0d[i];         e_high += st->high[i]* st->high[i];      }      ratio = log((1+e_high)/(1+e_low));      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);      if (ratio<-4)         ratio=-4;      if (ratio>2)         ratio=2;      /*if (ratio>-2)*/      if (st->vbr_enabled)       {         int modeid;         modeid = mode->nb_modes-1;         st->relative_quality+=1.0*(ratio+2);	 if (st->relative_quality<-1)            st->relative_quality=-1;         while (modeid)         {            int v1;            float thresh;            v1=(int)floor(st->vbr_quality);            if (v1==10)               thresh = mode->vbr_thresh[modeid][v1];            else               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +                         (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];            if (st->relative_quality >= thresh)               break;            modeid--;         }         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);         if (st->abr_enabled)         {            int bitrate;            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);            st->abr_drift+=(bitrate-st->abr_enabled);            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);            st->abr_count += 1.0;         }      } else {         /* VAD only */         int modeid;         if (st->relative_quality<2.0)            modeid=1;         else            modeid=st->submodeSelect;         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/         st->submodeID=modeid;      }      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/   }   if (st->encode_submode)   {      speex_bits_pack(bits, 1, 1);      if (dtx)         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);      else         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);   }   /* If null mode (no transmission), just set a couple things to zero*/   if (dtx || st->submodes[st->submodeID] == NULL)   {      for (i=0;i<st->frame_size;i++)         st->exc[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 */      iir_mem2(st->exc, st->interp_qlpc, st->high, st->subframeSize, st->lpcSize, st->mem_sp);#ifndef RELEASE      /* Reconstruct the original */      fir_mem_up(st->x0d, h0, st->y0, st->full_frame_size, QMF_ORDER, st->g0_mem, stack);      fir_mem_up(st->high, h1, st->y1, st->full_frame_size, QMF_ORDER, st->g1_mem, stack);      for (i=0;i<st->full_frame_size;i++)         in[i]=2*(st->y0[i]-st->y1[i]);#endif      if (dtx)         return 0;      else         return 1;   }   /* LSP quantization */   SUBMODE(lsp_quant)(st->lsp, st->qlsp, st->lpcSize, bits);      if (st->first)   {      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];   }      mem=PUSH(stack, st->lpcSize, spx_mem_t);   syn_resp=PUSH(stack, st->subframeSize, float);   innov = PUSH(stack, st->subframeSize, float);   for (sub=0;sub<st->nbSubframes;sub++)   {      float *exc, *sp, *res, *target, *sw, tmp, filter_ratio;      int offset;      float rl, rh, eh=0, el=0;      int fold;      offset = st->subframeSize*sub;      sp=st->high+offset;      exc=st->exc+offset;      res=st->res+offset;      target=st->target+offset;      sw=st->sw+offset;            /* 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];            lsp_enforce_margin(st->interp_lsp, st->lpcSize, .05);      lsp_enforce_margin(st->interp_qlsp, st->lpcSize, .05);      /* Compute interpolated LPCs (quantized and unquantized) */      for (i=0;i<st->lpcSize;i++)         st->interp_lsp[i] = cos(st->interp_lsp[i]);      for (i=0;i<st->lpcSize;i++)         st->interp_qlsp[i] = cos(st->interp_qlsp[i]);      lsp_to_lpc(st->interp_lsp, st->interp_lpc, st->lpcSize,stack);      lsp_to_lpc(st->interp_qlsp, st->interp_qlpc, st->lpcSize, 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);      /* Compute mid-band (4000 Hz for wideband) response of low-band and high-band         filters */      rl=rh=0;      tmp=1;      st->pi_gain[sub]=0;      for (i=0;i<=st->lpcSize;i++)      {         rh += tmp*st->interp_qlpc[i];         tmp = -tmp;         st->pi_gain[sub]+=st->interp_qlpc[i];      }      rh /= LPC_SCALING;      st->pi_gain[sub] /= LPC_SCALING;      rl = low_pi_gain[sub];      rl=1/(fabs(rl)+.01);      rh=1/(fabs(rh)+.01);      /* Compute ratio, will help predict the gain */      filter_ratio=fabs(.01+rh)/(.01+fabs(rl));      fold = filter_ratio<5;      /*printf ("filter_ratio %f\n", filter_ratio);*/      fold=0;      /* Compute "real excitation" */      fir_mem2(sp, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, st->mem_sp2);      /* Compute energy of low-band and high-band excitation */      for (i=0;i<st->subframeSize;i++)         eh+=sqr(exc[i]);      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */         float g;         /*speex_bits_pack(bits, 1, 1);*/         for (i=0;i<st->subframeSize;i++)            el+=sqr(low_innov[offset+i]);         /* Gain to use if we want to use the low-band excitation for high-band */         g=eh/(.01+el);         g=sqrt(g);         g *= filter_ratio;         /*print_vec(&g, 1, "gain factor");*/         /* Gain quantization */         {            int quant = (int) floor(.5 + 10 + 8.0 * log((g+.0001)));            /*speex_warning_int("tata", quant);*/            if (quant<0)               quant=0;            if (quant>31)               quant=31;            speex_bits_pack(bits, quant, 5);            g= .1*exp(quant/9.4);         }         /*printf ("folding gain: %f\n", g);*/         g /= filter_ratio;      } else {         float gc, scale, scale_1;         for (i=0;i<st->subframeSize;i++)            el+=sqr(low_exc[offset+i]);         /*speex_bits_pack(bits, 0, 1);*/         gc = sqrt(1+eh)*filter_ratio/sqrt((1+el)*st->subframeSize);         {            int qgc = (int)floor(.5+3.7*(log(gc)+2));            if (qgc<0)               qgc=0;            if (qgc>15)               qgc=15;            speex_bits_pack(bits, qgc, 4);            gc = exp((1/3.7)*qgc-2);         }         scale = gc*sqrt(1+el)/filter_ratio;         scale_1 = 1/scale;         for (i=0;i<st->subframeSize;i++)            exc[i]=0;         exc[0]=1;         syn_percep_zero(exc, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);                  /* Reset excitation */         for (i=0;i<st->subframeSize;i++)            exc[i]=0;                  /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */         for (i=0;i<st->lpcSize;i++)            mem[i]=st->mem_sp[i];         iir_mem2(exc, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, mem);         for (i=0;i<st->lpcSize;i++)            mem[i]=st->mem_sw[i];         filter_mem2(exc, st->bw_lpc1, st->bw_lpc2, res, st->subframeSize, st->lpcSize, mem);         /* Compute weighted signal */         for (i=0;i<st->lpcSize;i++)            mem[i]=st->mem_sw[i];         filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, mem);         /* 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]=0;         for (i=0;i<st->subframeSize;i++)            target[i]*=scale_1;                  /* Reset excitation */         for (i=0;i<st->subframeSize;i++)            innov[i]=0;         /*print_vec(target, st->subframeSize, "\ntarget");*/         SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,                                    SUBMODE(innovation_params), st->lpcSize, st->subframeSize,                                    innov, syn_resp, bits, stack, (st->complexity+1)>>1);         /*print_vec(target, st->subframeSize, "after");*/         for (i=0;i<st->subframeSize;i++)            exc[i] += innov[i]*scale;         if (SUBMODE(double_codebook)) {            char *tmp_stack=stack;            float *innov2 = PUSH(tmp_stack, st->subframeSize, float);            for (i=0;i<st->subframeSize;i++)               innov2[i]=0;            for (i=0;i<st->subframeSize;i++)               target[i]*=2.5;            SUBMODE(innovation_quant)(target, st->interp_qlpc, st->bw_lpc1, st->bw_lpc2,                                       SUBMODE(innovation_params), st->lpcSize, st->subframeSize,                                       innov2, syn_resp, bits, tmp_stack, (st->complexity+1)>>1);            for (i=0;i<st->subframeSize;i++)               innov2[i]*=scale*(1/2.5);            for (i=0;i<st->subframeSize;i++)               exc[i] += innov2[i];         }      }         /*Keep the previous memory*/         for (i=0;i<st->lpcSize;i++)            mem[i]=st->mem_sp[i];         /* Final signal synthesis from excitation */         iir_mem2(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) */         filter_mem2(sp, st->bw_lpc1, st->bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw);   }#ifndef RELEASE   /* Reconstruct the original */   fir_mem_up(st->x0d, h0, st->y0, st->full_frame_size, QMF_ORDER, st->g0_mem, stack);   fir_mem_up(st->high, h1, st->y1, st->full_frame_size, QMF_ORDER, st->g1_mem, stack);   for (i=0;i<st->full_frame_size;i++)      in[i]=2*(st->y0[i]-st->y1[i]);#endif   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];   st->first=0;   return 1;}