/* Copyright (C) 2003 Epic Games
Written by Jean-Marc Valin
File: preprocess.c
Preprocessor with denoising based on the algorithm by Ephraim and Malah
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <math.h>
#include "speex_preprocess.h"
#include "misc.h"
#include "smallft.h"
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#ifndef M_PI
#define M_PI 3.14159263
#endif
#define SQRT_M_PI_2 0.88623
#define LOUDNESS_EXP 2.5
#define NB_BANDS 8
#define SPEEX_PROB_START_DEFAULT 0.35f
#define SPEEX_PROB_CONTINUE_DEFAULT 0.20f
#define ZMIN .1
#define ZMAX .316
#define ZMIN_1 10
#define LOG_MIN_MAX_1 0.86859
static void conj_window(float *w, int len)
{
int i;
for (i=0;i<len;i++)
{
float x=4*((float)i)/len;
int inv=0;
if (x<1)
{
} else if (x<2)
{
x=2-x;
inv=1;
} else if (x<3)
{
x=x-2;
inv=1;
} else {
x=4-x;
}
x*=1.9979;
w[i]=(.5-.5*cos(x))*(.5-.5*cos(x));
if (inv)
w[i]=1-w[i];
w[i]=sqrt(w[i]);
}
}
/* This function approximates the gain function
y = gamma(1.25)^2 * M(-.25;1;-x) / sqrt(x)
which multiplied by xi/(1+xi) is the optimal gain
in the loudness domain ( sqrt[amplitude] )
*/
static float hypergeom_gain(float x)
{
int ind;
float integer, frac;
static const float table[21] = {
0.82157f, 1.02017f, 1.20461f, 1.37534f, 1.53363f, 1.68092f, 1.81865f,
1.94811f, 2.07038f, 2.18638f, 2.29688f, 2.40255f, 2.50391f, 2.60144f,
2.69551f, 2.78647f, 2.87458f, 2.96015f, 3.04333f, 3.12431f, 3.20326f};
integer = floor(2*x);
ind = (int)integer;
if (ind<0)
return 1;
if (ind>19)
return 1+.1296/x;
frac = 2*x-integer;
return ((1-frac)*table[ind] + frac*table[ind+1])/sqrt(x+.0001f);
}
static float qcurve(float x)
{
return 1.f/(1.f+.1f/(x*x));
}
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->speech_prob_start = SPEEX_PROB_START_DEFAULT;
st->speech_prob_continue = SPEEX_PROB_CONTINUE_DEFAULT;
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;
}
void speex_preprocess_state_destroy(SpeexPreprocessState *st)
{
speex_free(st->frame);
speex_free(st->ps);
speex_free(st->gain2);
speex_free(st->window);
speex_free(st->noise);
speex_free(st->reverb_estimate);
speex_free(st->old_ps);
speex_free(st->gain);
speex_free(st->prior);
speex_free(st->post);
speex_free(st->loudness_weight);
speex_free(st->echo_noise);
speex_free(st->S);
speex_free(st->Smin);
speex_free(st->Stmp);
speex_free(st->update_prob);
speex_free(st->zeta);
speex_free(st->noise_bands);
speex_free(st->noise_bands2);
speex_free(st->speech_bands);
speex_free(st->speech_bands2);
speex_free(st->inbuf);
speex_free(st->outbuf);
spx_drft_clear(st->fft_lookup);
speex_free(st->fft_lookup);
speex_free(st);
}
static void update_noise(SpeexPreprocessState *st, float *ps, spx_int32_t *echo)
{
int i;
float beta;
st->nb_adapt++;
beta=1.0f/st->nb_adapt;
if (beta < .05f)
beta=.05f;
if (!echo)
{
for (i=0;i<st->ps_size;i++)
st->noise[i]
评论0