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;
void nb_decode(void *state, SpeexBits *bits, float *out, int lost){   DecState *st;   int i, sub;   int pitch;   float pitch_gain[3];   float ol_gain;   int ol_pitch=0;   float ol_pitch_coef=0;   int best_pitch=40;   float best_pitch_gain=-1;   st=state;   /* Get the sub-mode that was used */   st->submodeID = speex_bits_unpack_unsigned(bits, NB_SUBMODE_BITS);   /* Shift all buffers by one frame */   memmove(st->inBuf, st->inBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   memmove(st->excBuf, st->excBuf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   memmove(st->exc2Buf, st->exc2Buf+st->frameSize, (st->bufSize-st->frameSize)*sizeof(float));   /* Unquantize LSPs */   SUBMODE(lsp_unquant)(st->qlsp, st->lpcSize, bits);   /* Handle first frame and lost-packet case */   if (st->first || st->count_lost)   {      for (i=0;i<st->lpcSize;i++)         st->old_qlsp[i] = st->qlsp[i];   }   /* Get open-loop pitch estimation for low bit-rate pitch coding */   if (SUBMODE(lbr_pitch)!=-1 && SUBMODE(ltp_params))   {      ol_pitch = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);   } else if (SUBMODE(lbr_pitch)==0)   {      int quant;      ol_pitch = st->min_pitch+speex_bits_unpack_unsigned(bits, 7);      quant = speex_bits_unpack_unsigned(bits, 4);      ol_pitch_coef=0.066667*quant;   }      /* Get global excitation gain */   {      int qe;      qe = speex_bits_unpack_unsigned(bits, 5);      ol_gain = exp(qe/3.5);      /*printf ("decode_ol_gain: %f\n", ol_gain);*/   }   /*Loop on subframes */   for (sub=0;sub<st->nbSubframes;sub++)   {      int offset;      float *sp, *exc, *exc2, tmp;            /* Offset relative to start of frame */      offset = st->subframeSize*sub;      /* Original signal */      sp=st->frame+offset;      /* Excitation */      exc=st->exc+offset;      /* Excitation after post-filter*/      exc2=st->exc2+offset;      /* LSP interpolation (quantized and unquantized) */      tmp = (1.0 + sub)/st->nbSubframes;      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_qlsp, st->lpcSize, .002);      /* Compute interpolated LPCs (unquantized) */      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 at w=pi */      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;      }      /* Reset excitation */      for (i=0;i<st->subframeSize;i++)         exc[i]=0;      /*Adaptive codebook contribution*/      if (SUBMODE(ltp_unquant))      {         if (SUBMODE(lbr_pitch) != -1)         {            int pit_min, pit_max;            int margin;            margin = SUBMODE(lbr_pitch);            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;            if (margin)            {               pit_min = ol_pitch-margin+1;               pit_max = ol_pitch+margin;            } else {               pit_min=pit_max=ol_pitch;            }            SUBMODE(ltp_unquant)(exc, pit_min, pit_max, SUBMODE(ltp_params), st->subframeSize, &pitch, &pitch_gain[0], bits, st->stack, 0);         } else {            SUBMODE(ltp_unquant)(exc, st->min_pitch, st->max_pitch, SUBMODE(ltp_params), st->subframeSize, &pitch, &pitch_gain[0], bits, st->stack, 0);         }                  if (!lost)         {            /* If the frame was not lost... */            tmp = fabs(pitch_gain[0])+fabs(pitch_gain[1])+fabs(pitch_gain[2]);            tmp = fabs(pitch_gain[0]+pitch_gain[1]+pitch_gain[2]);            if (tmp>best_pitch_gain)            {               best_pitch = pitch;               while (best_pitch+pitch<st->max_pitch)               {                  best_pitch+=pitch;               }               best_pitch_gain = tmp*.9;               if (best_pitch_gain>.85)                  best_pitch_gain=.85;            }         } else {            /* What to do with pitch if we lost the frame */            for (i=0;i<st->subframeSize;i++)               exc[i]=0;            /*printf ("best_pitch: %d %f\n", st->last_pitch, st->last_pitch_gain);*/            for (i=0;i<st->subframeSize;i++)               exc[i]=st->last_pitch_gain*exc[i-st->last_pitch];         }      } else if (SUBMODE(lbr_pitch==0)) {         for (i=0;i<st->subframeSize;i++)         {            exc[i]=exc[i-ol_pitch]*ol_pitch_coef;         }      }            /* Unquantize the innovation */      {         int q_energy;         float ener;         float *innov;                  innov = PUSH(st->stack, st->subframeSize);         for (i=0;i<st->subframeSize;i++)            innov[i]=0;         if (SUBMODE(have_subframe_gain))         {            q_energy = speex_bits_unpack_unsigned(bits, 3);            ener = ol_gain*exp(exc_gain_quant_scal[q_energy]);         } else {            ener = ol_gain;         }                  /*printf ("unquant_energy: %d %f\n", q_energy, ener);*/                  if (SUBMODE(innovation_unquant))         {            /*Fixed codebook contribution*/            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), st->subframeSize, bits, st->stack);         } else {            float scale;            scale = 3*sqrt(1.2-ol_pitch_coef);            for (i=0;i<st->subframeSize;i++)               innov[i] = scale*((((float)rand())/RAND_MAX)-.5);                     }         if (st->count_lost)            ener*=pow(.8,st->count_lost);         for (i=0;i<st->subframeSize;i++)            exc[i]+=ener*innov[i];         POP(st->stack);      }      for (i=0;i<st->subframeSize;i++)         exc2[i]=exc[i];      /* Apply post-filter */      if (st->pf_enabled && SUBMODE(post_filter_func))         SUBMODE(post_filter_func)(exc, exc2, st->interp_qlpc, st->lpcSize, st->subframeSize,                              pitch, pitch_gain, SUBMODE(post_filter_params), st->mem_pf,                               st->mem_pf2, st->stack);            /* Apply synthesis filter */      syn_filt_mem(exc2, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp);   }      /*Copy output signal*/   for (i=0;i<st->frameSize;i++)      out[i]=st->frame[i];   out[0] = st->frame[0] + st->preemph*st->pre_mem;   for (i=1;i<st->frameSize;i++)     out[i]=st->frame[i] + st->preemph*out[i-1];   st->pre_mem=out[st->frameSize-1];   /* Store the LSPs for interpolation in the next frame */   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;   if (!lost)      st->count_lost=0;   else      st->count_lost++;   if (!lost)   {      st->last_pitch = best_pitch;      st->last_pitch_gain = best_pitch_gain;   }}