Beispiel #1
0
VorbisPsy *vorbis_psy_init(int rate, int n)
{
  long i,j,lo=-99,hi=1;
  VorbisPsy *p = speex_alloc(sizeof(VorbisPsy));
  memset(p,0,sizeof(*p));
  
  p->n = n;
  spx_drft_init(&p->lookup, n);
  p->bark = speex_alloc(n*sizeof(*p->bark));
  p->rate=rate;
  p->vi = &example_tuning;

  /* BH4 window */
  p->window = speex_alloc(sizeof(*p->window)*n);
  float a0 = .35875f;
  float a1 = .48829f;
  float a2 = .14128f;
  float a3 = .01168f;
  for(i=0;i<n;i++)
    p->window[i] = //a0 - a1*cos(2.*M_PI/n*(i+.5)) + a2*cos(4.*M_PI/n*(i+.5)) - a3*cos(6.*M_PI/n*(i+.5));
      sin((i+.5)/n * M_PI)*sin((i+.5)/n * M_PI);
  /* bark scale lookups */
  for(i=0;i<n;i++){
    float bark=toBARK(rate/(2*n)*i); 
    
    for(;lo+p->vi->noisewindowlomin<i && 
	  toBARK(rate/(2*n)*lo)<(bark-p->vi->noisewindowlo);lo++);
    
    for(;hi<=n && (hi<i+p->vi->noisewindowhimin ||
	  toBARK(rate/(2*n)*hi)<(bark+p->vi->noisewindowhi));hi++);
    
    p->bark[i]=((lo-1)<<16)+(hi-1);

  }

  /* set up rolling noise median */
  p->noiseoffset=speex_alloc(n*sizeof(*p->noiseoffset));
  
  for(i=0;i<n;i++){
    float halfoc=toOC((i+.5)*rate/(2.*n))*2.;
    int inthalfoc;
    float del;
    
    if(halfoc<0)halfoc=0;
    if(halfoc>=P_BANDS-1)halfoc=P_BANDS-1;
    inthalfoc=(int)halfoc;
    del=halfoc-inthalfoc;
    
    p->noiseoffset[i]=
      p->vi->noiseoff[inthalfoc]*(1.-del) + 
      p->vi->noiseoff[inthalfoc+1]*del;
    
  }
#if 0
  _analysis_output_always("noiseoff0",ls,p->noiseoffset,n,1,0,0);
#endif

   return p;
}
Beispiel #2
0
/** Creates a new echo canceller state */
SpeexEchoState *speex_echo_state_init(int frame_size, int filter_length)
{
    int i,j,N,M;
    SpeexEchoState *st = (SpeexEchoState *)speex_alloc(sizeof(SpeexEchoState));

    st->frame_size = frame_size;
    st->window_size = 2*frame_size;
    N = st->window_size;
    M = st->M = (filter_length+st->frame_size-1)/frame_size;
    st->cancel_count=0;
    st->adapt_rate = .01f;
    st->sum_adapt = 0;
    st->Sey = 0;
    st->Syy = 0;
    st->See = 0;

    st->fft_lookup = (struct drft_lookup*)speex_alloc(sizeof(struct drft_lookup));
    spx_drft_init(st->fft_lookup, N);

    st->x = (float*)speex_alloc(N*sizeof(float));
    st->d = (float*)speex_alloc(N*sizeof(float));
    st->y = (float*)speex_alloc(N*sizeof(float));
    st->y2 = (float*)speex_alloc(N*sizeof(float));
    st->Yps = (float*)speex_alloc(N*sizeof(float));
    st->last_y = (float*)speex_alloc(N*sizeof(float));
    st->Yf = (float*)speex_alloc((st->frame_size+1)*sizeof(float));
    st->Rf = (float*)speex_alloc((st->frame_size+1)*sizeof(float));
    st->Xf = (float*)speex_alloc((st->frame_size+1)*sizeof(float));
    st->fratio = (float*)speex_alloc((st->frame_size+1)*sizeof(float));
    st->regul = (float*)speex_alloc(N*sizeof(float));

    st->X = (float*)speex_alloc(M*N*sizeof(float));
    st->D = (float*)speex_alloc(N*sizeof(float));
    st->Y = (float*)speex_alloc(N*sizeof(float));
    st->Y2 = (float*)speex_alloc(N*sizeof(float));
    st->E = (float*)speex_alloc(N*sizeof(float));
    st->W = (float*)speex_alloc(M*N*sizeof(float));
    st->PHI = (float*)speex_alloc(M*N*sizeof(float));
    st->power = (float*)speex_alloc((frame_size+1)*sizeof(float));
    st->power_1 = (float*)speex_alloc((frame_size+1)*sizeof(float));
    st->grad = (float*)speex_alloc(N*M*sizeof(float));

    for (i=0; i<N*M; i++)
    {
        st->W[i] = st->PHI[i] = 0;
    }

    st->regul[0] = (.01+(10.)/((4.)*(4.)))/M;
    for (i=1,j=1; i<N-1; i+=2,j++)
    {
        st->regul[i] = .01+((10.)/((j+4.)*(j+4.)))/M;
        st->regul[i+1] = .01+((10.)/((j+4.)*(j+4.)))/M;
    }
    st->regul[i] = .01+((10.)/((j+4.)*(j+4.)))/M;

    st->adapted = 0;
    return st;
}
Beispiel #3
0
EXPORT SpeexDecorrState *speex_decorrelate_new(int rate, int channels,
					       int frame_size)
{
	int i, ch;
	SpeexDecorrState *st = speex_alloc(sizeof(SpeexDecorrState));
	st->rate = rate;
	st->channels = channels;
	st->frame_size = frame_size;
#ifdef VORBIS_PSYCHO
	st->psy = vorbis_psy_init(rate, 2 * frame_size);
	spx_drft_init(&st->lookup, 2 * frame_size);
	st->wola_mem = speex_alloc(frame_size * sizeof(float));
	st->curve = speex_alloc(frame_size * sizeof(float));
#endif
	st->y = speex_alloc(frame_size * sizeof(float));

	st->buff = speex_alloc(channels * 2 * frame_size * sizeof(float));
	st->ringID = speex_alloc(channels * sizeof(int));
	st->order = speex_alloc(channels * sizeof(int));
	st->alpha = speex_alloc(channels * sizeof(float));
	st->ring = speex_alloc(channels * ALLPASS_ORDER * sizeof(float));

	/*FIXME: The +20 is there only as a kludge for ALL_PASS_OLA */
	st->vorbis_win = speex_alloc((2 * frame_size + 20) * sizeof(float));
	for (i = 0; i < 2 * frame_size; i++)
		st->vorbis_win[i] =
		    sin(.5 * M_PI * sin(M_PI * i / (2 * frame_size)) *
			sin(M_PI * i / (2 * frame_size)));
	st->seed = rand();

	for (ch = 0; ch < channels; ch++) {
		for (i = 0; i < ALLPASS_ORDER; i++)
			st->ring[ch][i] = 0;
		st->ringID[ch] = 0;
		st->alpha[ch] = 0;
		st->order[ch] = 10;
	}
	return st;
}
Beispiel #4
0
SpeexPreprocessState *speex_preprocess_state_init(int frame_size, int sampling_rate)
{
   int i;
   int N, N3, N4;

   SpeexPreprocessState *st = (SpeexPreprocessState *)speex_alloc(sizeof(SpeexPreprocessState));
   st->frame_size = frame_size;

   /* Round ps_size down to the nearest power of two */
#if 0
   i=1;
   st->ps_size = st->frame_size;
   while(1)
   {
      if (st->ps_size & ~i)
      {
         st->ps_size &= ~i;
         i<<=1;
      } else {
         break;
      }
   }
   
   
   if (st->ps_size < 3*st->frame_size/4)
      st->ps_size = st->ps_size * 3 / 2;
#else
   st->ps_size = st->frame_size;
#endif

   N = st->ps_size;
   N3 = 2*N - st->frame_size;
   N4 = st->frame_size - N3;
   
   st->sampling_rate = sampling_rate;
   st->denoise_enabled = 1;
   st->agc_enabled = 0;
   st->agc_level = 8000;
   st->vad_enabled = 0;
   st->dereverb_enabled = 0;
   st->reverb_decay = .5;
   st->reverb_level = .2;

   st->frame = (float*)speex_alloc(2*N*sizeof(float));
   st->ps = (float*)speex_alloc(N*sizeof(float));
   st->gain2 = (float*)speex_alloc(N*sizeof(float));
   st->window = (float*)speex_alloc(2*N*sizeof(float));
   st->noise = (float*)speex_alloc(N*sizeof(float));
   st->reverb_estimate = (float*)speex_alloc(N*sizeof(float));
   st->old_ps = (float*)speex_alloc(N*sizeof(float));
   st->gain = (float*)speex_alloc(N*sizeof(float));
   st->prior = (float*)speex_alloc(N*sizeof(float));
   st->post = (float*)speex_alloc(N*sizeof(float));
   st->loudness_weight = (float*)speex_alloc(N*sizeof(float));
   st->inbuf = (float*)speex_alloc(N3*sizeof(float));
   st->outbuf = (float*)speex_alloc(N3*sizeof(float));
   st->echo_noise = (float*)speex_alloc(N*sizeof(float));

   st->S = (float*)speex_alloc(N*sizeof(float));
   st->Smin = (float*)speex_alloc(N*sizeof(float));
   st->Stmp = (float*)speex_alloc(N*sizeof(float));
   st->update_prob = (float*)speex_alloc(N*sizeof(float));

   st->zeta = (float*)speex_alloc(N*sizeof(float));
   st->Zpeak = 0;
   st->Zlast = 0;

   st->noise_bands = (float*)speex_alloc(NB_BANDS*sizeof(float));
   st->noise_bands2 = (float*)speex_alloc(NB_BANDS*sizeof(float));
   st->speech_bands = (float*)speex_alloc(NB_BANDS*sizeof(float));
   st->speech_bands2 = (float*)speex_alloc(NB_BANDS*sizeof(float));
   st->noise_bandsN = st->speech_bandsN = 1;

   conj_window(st->window, 2*N3);
   for (i=2*N3;i<2*st->ps_size;i++)
      st->window[i]=1;
   
   if (N4>0)
   {
      for (i=N3-1;i>=0;i--)
      {
         st->window[i+N3+N4]=st->window[i+N3];
         st->window[i+N3]=1;
      }
   }
   for (i=0;i<N;i++)
   {
      st->noise[i]=1e4;
      st->reverb_estimate[i]=0.;
      st->old_ps[i]=1e4;
      st->gain[i]=1;
      st->post[i]=1;
      st->prior[i]=1;
   }

   for (i=0;i<N3;i++)
   {
      st->inbuf[i]=0;
      st->outbuf[i]=0;
   }

   for (i=0;i<N;i++)
   {
      float ff=((float)i)*.5*sampling_rate/((float)N);
      st->loudness_weight[i] = .35f-.35f*ff/16000.f+.73f*exp(-.5f*(ff-3800)*(ff-3800)/9e5f);
      if (st->loudness_weight[i]<.01f)
         st->loudness_weight[i]=.01f;
      st->loudness_weight[i] *= st->loudness_weight[i];
   }

   st->speech_prob = 0;
   st->last_speech = 1000;
   st->loudness = pow(6000,LOUDNESS_EXP);
   st->loudness2 = 6000;
   st->nb_loudness_adapt = 0;

   st->fft_lookup = (struct drft_lookup*)speex_alloc(sizeof(struct drft_lookup));
   spx_drft_init(st->fft_lookup,2*N);

   st->nb_adapt=0;
   st->consec_noise=0;
   st->nb_preprocess=0;
   return st;
}