/* Copyright (C) 2007 Hong Zhiqian */ /** @file preprocess_tm.h @author Hong Zhiqian @brief Various compatibility routines for Speex (TriMedia version) */ /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - 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. - Neither the name of the Xiph.org Foundation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``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 FOUNDATION OR CONTRIBUTORS 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. */ #include #include "profile_tm.h" #ifdef FIXED_POINT #define OVERRIDE_PREPROCESS_ANALYSIS static void preprocess_analysis(SpeexPreprocessState * restrict st, spx_int16_t * restrict x) { register int i, j, framesize = st->frame_size; register int N = st->ps_size; register int N3 = 2*N - framesize; register int N4 = framesize - N3; register int * restrict ps = st->ps; register int * restrict frame; register int * restrict inbuf; register int * restrict ptr; register int max_val; frame = (int*)(st->frame); inbuf = (int*)(st->inbuf); ptr = (int*)(st->frame+N3); TMDEBUG_ALIGNMEM(x); TMDEBUG_ALIGNMEM(frame); TMDEBUG_ALIGNMEM(inbuf); PREPROCESSANAYLSIS_START(); N3 >>= 1; framesize >>= 1; max_val = 0; for ( i=0,j=0 ; iwindow ; i> 15; r1 = (asri(16,f10) * asri(16,w10)) >> 15; max_val = imax(iabs(sex16(r0)), max_val); max_val = imax(iabs(sex16(r1)), max_val); r0 = pack16lsb(r1, r0); st32d(j, frame, r0); } #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif max_val = 14 - spx_ilog2(max_val); st->frame_shift = max_val; if ( max_val != 0 ) { #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0,j=0 ; ifft_lookup, st->frame, st->ft); power_spectrum(st->ft, ps, N << 1); #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0,ptr=(int*)st->ps,max_val<<=1,j=((1<<((max_val))>>1)) ;i> max_val; } #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif filterbank_compute_bank32(st->bank, ps, ps+N); PREPROCESSANAYLSIS_STOP(); } #define _MULT16_32_Q15(a,b,c) ADD32(MULT16_16((a),(b)), SHR(MULT16_16((a),(c)),15)) #define OVERRIDE_UPDATE_NOISE_PROB static void update_noise_prob(SpeexPreprocessState * restrict st) { register int i; register int min_range, nb_adapt; register int N = st->ps_size; register int * restrict Smin = (int*)st->Smin; register int * restrict Stmp = (int*)st->Stmp; register int * restrict S = (int*)st->S; UPDATENOISEPROB_START(); { register int psi_lsb, psi_msb, ips_lsb, ips_msb, psii_lsb, psii_msb; register int psiii_lsb, psiii_msb; register int q8, q05, q2, q1; register int *ps = (int*)st->ps; register int si_lsb, si_msb, sii_lsb, sii_msb; q8 = QCONST16(.8f,15); q05 = QCONST16(.05f,15); q2 = QCONST16(.2f,15); q1 = QCONST16(.1f,15); ips_lsb = ps[0]; psi_lsb = ps[1]; si_lsb = S[0]; ips_msb = ips_lsb >> 15; psi_msb = psi_lsb >> 15; si_msb = si_lsb >> 15; ips_lsb &= 0x00007fff; psi_lsb &= 0x00007fff; si_lsb &= 0x00007fff; S[0] = _MULT16_32_Q15(q8,si_msb,si_lsb) + _MULT16_32_Q15(q2,ips_msb,ips_lsb); for ( i=1 ; i> 15; si_msb = si_lsb >> 15; si_lsb &= 0x00007fff; psii_lsb &= 0x00007fff; S[i]= _MULT16_32_Q15(q8,si_msb,si_lsb) + _MULT16_32_Q15(q05,ips_msb,ips_lsb) + _MULT16_32_Q15(q1,psi_msb,psi_lsb) + _MULT16_32_Q15(q05,psii_msb,psii_lsb); psiii_lsb = ps[i+2]; sii_lsb = S[i+1]; sii_msb = sii_lsb >> 15; psiii_msb= psiii_lsb >> 15; sii_lsb &= 0x00007fff; psiii_lsb&= 0x00007fff; S[i+1]= _MULT16_32_Q15(q8,sii_msb,sii_lsb) + _MULT16_32_Q15(q05,psi_msb,psi_lsb) + _MULT16_32_Q15(q1,psii_msb,psii_lsb) + _MULT16_32_Q15(q05,psiii_msb,psiii_lsb); ips_lsb = psii_lsb; ips_msb = psii_msb; psi_lsb = psiii_lsb; psi_msb = psiii_msb; } S[N-1] = MULT16_32_Q15(q8,S[N-1]) + MULT16_32_Q15(q2,ps[N-1]); } nb_adapt = st->nb_adapt; if ( nb_adapt==1 ) { for ( i=0 ; imin_count > min_range ) { st->min_count = 0; #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unroll=2 #pragma TCS_unrollexact=1 #endif for ( i=0 ; iupdate_prob; q4 = QCONST16(.4f,15); #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; i ADD32(smini,20), 1, 0); } #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif } UPDATENOISEPROB_STOP(); } #else #define OVERRIDE_PREPROCESS_ANALYSIS static void preprocess_analysis(SpeexPreprocessState * restrict st, spx_int16_t * restrict x) { register int i; register int framesize = st->frame_size; register int N = st->ps_size; register int N3 = 2*N - framesize; register int N4 = framesize - N3; register float * restrict ps = st->ps; register float * restrict frame = st->frame; register float * restrict inbuf = st->inbuf; PREPROCESSANAYLSIS_START(); #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; iwindow; #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; i<2*N ; ++i ) { frame[i] = frame[i] * inbuf[i]; } #if (TM_UNROLL && TM_UNROLL_PREPROCESSANALYSIS) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif spx_fft(st->fft_lookup, frame, st->ft); power_spectrum(st->ft, ps, N << 1); filterbank_compute_bank32(st->bank, ps, ps+N); PREPROCESSANAYLSIS_STOP(); } #define OVERRIDE_UPDATE_NOISE_PROB static void update_noise_prob(SpeexPreprocessState * restrict st) { register float * restrict S = st->S; register float * restrict ps = st->ps; register int N = st->ps_size; register int min_range; register int i; register int nb_adapt; register float * restrict Smin = st->Smin; register float * restrict Stmp = st->Stmp; UPDATENOISEPROB_START(); { register float ips, psi; ips = ps[0]; psi = ps[1]; S[0] = .8f * S[0] + .2f * ips; for ( i=1 ; inb_adapt; if ( nb_adapt==1 ) { for (i=0;iStmp[i] = 0; } min_range = mux(nb_adapt < 100, 15, mux(nb_adapt < 1000, 50, mux(nb_adapt < 10000, 150, 300))); if ( st->min_count > min_range ) { st->min_count = 0; #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; iSmin; #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; iupdate_prob; #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for (i=0;i (smini + 20.f), 1, 0); } #if (TM_UNROLL && TM_UNROLL_UPDATENOISEPROB) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif } UPDATENOISEPROB_STOP(); } #define OVERRIDE_COMPUTE_GAIN_FLOOR static void compute_gain_floor( int noise_suppress, int effective_echo_suppress, float * restrict noise, float * restrict echo, float * gain_floor, int len ) { register int i; register float echo_floor; register float noise_floor; COMPUTEGAINFLOOR_START(); noise_floor = exp(.2302585f*noise_suppress); echo_floor = exp(.2302585f*effective_echo_suppress); #if (TM_UNROLL && TM_UNROLL_COMPUTEGAINFLOOR) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for (i=0;iecho_state) { register spx_word32_t * restrict r_echo = st->residual_echo; register spx_word32_t * restrict e_noise = st->echo_noise; register int i; #ifndef FIXED_POINT register spx_word32_t r; #endif speex_echo_get_residual(st->echo_state, r_echo, N); #ifndef FIXED_POINT r = r_echo[0]; if (!(r >=0 && r < N*1e9f) ) { memset(r_echo, 0, N * sizeof(spx_word32_t)); } #endif #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for (i=0;ibank, e_noise, e_noise+N); } else { memset(st->echo_noise, 0, (NM) * sizeof(spx_word32_t)); } } void preprocess_update_noise( SpeexPreprocessState * restrict st, spx_word32_t * restrict ps, int N ) { register spx_word16_t beta, beta_1; register int * restrict up = st->update_prob; register spx_word32_t * restrict noise = st->noise; register int i; beta = MAX16(QCONST16(.03,15),DIV32_16(Q15_ONE,st->nb_adapt)); beta_1 = Q15_ONE-beta; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for (i=0;ibank, noise, noise+N); } void preprocess_compute_SNR( SpeexPreprocessState * restrict st, spx_word32_t * restrict ps, int NM ) { register spx_word32_t * restrict noise = st->noise; register spx_word32_t * restrict echo = st->echo_noise; register spx_word32_t * restrict reverb = st->reverb_estimate; register spx_word16_t * restrict post = st->post; register spx_word32_t * restrict old_ps = st->old_ps; register spx_word16_t * restrict prior = st->prior; register int i; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; izeta; register spx_word16_t * restrict prior = st->prior; register spx_word32_t Zframe; register spx_word16_t iprior, priori; register int _N = N-1; register int i; iprior = prior[0]; priori = prior[1]; zeta[0] = PSHR32(ADD32(MULT16_16(QCONST16(.7f,15),zeta[0]), MULT16_16(QCONST16(.3f,15),iprior)),15); #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=2 #pragma TCS_unrollexact=1 #endif for ( i=1 ; i<_N ; i++) { register spx_word16_t zetai = zeta[i]; register spx_word16_t priorii = prior[i+1]; zeta[i] = PSHR32(ADD32(ADD32(ADD32(MULT16_16(QCONST16(.7f,15),zetai), MULT16_16(QCONST16(.15f,15),priori)), MULT16_16(QCONST16(.075f,15),iprior)), MULT16_16(QCONST16(.075f,15),priorii)),15); iprior = priori; priori = priorii; } #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif for (i=_N; izeta; register spx_word16_t * restrict prior = st->prior; register spx_word16_t * restrict gain = st->gain; register spx_word32_t * restrict old_ps = st->old_ps; register spx_word16_t * restrict post = st->post; register spx_word16_t * restrict gain2 = st->gain2; register int i; register int N=st->ps_size; for ( i=N ; ibank,gain2+N, gain2); filterbank_compute_psd16(st->bank,gain+N, gain); } void preprocess_compute_linear_gain( SpeexPreprocessState * restrict st, spx_word32_t * restrict ps, int N ) { register spx_word16_t * restrict gain_floor = st->gain_floor; register spx_word16_t * restrict prior = st->prior; register spx_word16_t * restrict gain = st->gain; register spx_word32_t * restrict old_ps = st->old_ps; register spx_word16_t * restrict post = st->post; register spx_word16_t * restrict gain2 = st->gain2; register int i; filterbank_compute_psd16(st->bank,gain_floor+N,gain_floor); #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for (i=0;iprior[i]), 15), ADD16(prior[i], SHL32(1,SNR_SHIFT))); theta = MULT16_32_P15(prior_ratio, QCONST32(1.f,EXPIN_SHIFT)+SHL32(EXTEND32(post[i]),EXPIN_SHIFT-SNR_SHIFT)); MM = hypergeom_gain(theta); g = EXTRACT16(MIN32(Q15_ONE, MULT16_32_Q15(prior_ratio, MM))); p = gain2[i]; g = VMUX( MULT16_16_Q15(QCONST16(.333f,15),g) > gain[i], MULT16_16(3,gain[i]), g); old_ps[i]= MULT16_32_P15(QCONST16(.2f,15),old_ps[i]) + MULT16_32_P15(MULT16_16_P15(QCONST16(.8f,15),SQR16_Q15(g)),ps[i]); g = VMUX( g < gfi, gfi, g ); gain[i] = g; tmp = MULT16_16_P15(p,spx_sqrt(SHL32(EXTEND32(g),15))) + MULT16_16_P15(SUB16(Q15_ONE,p),spx_sqrt(SHL32(EXTEND32(gfi),15))); gain2[i]=SQR16_Q15(tmp); /* Use this if you want a log-domain MMSE estimator instead */ /* gain2[i] = pow(g, p) * pow(gfi,1.f-p);*/ } #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif } #if 0 void preprocess_compute_bark_gain( SpeexPreprocessState * restrict st, int N, int NM ) { register spx_word16_t * restrict gain_floor = st->gain_floor; register spx_word16_t * restrict gain = st->gain; register spx_word16_t * restrict gain2 = st->gain2; register int i; for (i=N;ibank,gain2+N, gain2); } #endif void preprocess_apply_gain( SpeexPreprocessState * restrict st, int N ) { register spx_word16_t * restrict ft = st->ft; register spx_word16_t * restrict gain2 = st->gain2; register int j, i; ft[0] = MULT16_16_P15(gain2[0],ft[0]); for (i=1,j=1; iframe; register int shift = st->frame_shift; register int i; register int N2 = N << 1; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; iframe; register int i; register int N2 = N << 1; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for (i=0;i max_sample, framei, max_sample); } #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif if ( max_sample > 28000.f ) { float damp = 28000.f/max_sample; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; i< N2 ; i++ ) { frame[i] *= damp; } #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unrollexact=0 #pragma TCS_unroll=0 #endif } } #endif void preprocess_update( SpeexPreprocessState * restrict st, spx_int16_t * restrict x, int N ) { register spx_word16_t * restrict frame = st->frame; register spx_word16_t * restrict window = st->window; register spx_word16_t * restrict outbuf = st->outbuf; register int framesize = st->frame_size; register int N2 = N << 1; register int N3 = N2 - framesize; register int N4 = (framesize) - N3; register int i; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; ips; register spx_word32_t Zframe; register spx_word16_t Pframe; st->nb_adapt++; st->min_count++; N = st->ps_size; M = st->nbands; NM = N + M; preprocess_residue_echo(st, N, NM); preprocess_analysis(st, x); update_noise_prob(st); preprocess_update_noise(st, ps, N); if ( st->nb_adapt == 1 ) { memcpy(st->old_ps, ps, (NM) * sizeof(spx_word32_t)); } preprocess_compute_SNR(st, ps, NM); Zframe = preprocess_smooth_SNR(st, N, NM); { register spx_word16_t effective_echo_suppress; Pframe = QCONST16(.1f,15)+MULT16_16_Q15(QCONST16(.899f,15),qcurve(DIV32_16(Zframe,M))); effective_echo_suppress = EXTRACT16(PSHR32(ADD32(MULT16_16(SUB16(Q15_ONE,Pframe), st->echo_suppress), MULT16_16(Pframe, st->echo_suppress_active)),15)); compute_gain_floor(st->noise_suppress, effective_echo_suppress, st->noise+N, st->echo_noise+N, st->gain_floor+N, M); } preprocess_compute_emgain(st, ps, Pframe, NM); preprocess_compute_linear_gain(st, ps, N); if (!st->denoise_enabled) { register spx_word16_t * restrict gain2 = st->gain2; #if (TM_UNROLL && TM_UNROLL_SPEEXPREPROCESSRUN) #pragma TCS_unroll=4 #pragma TCS_unrollexact=1 #endif for ( i=0 ; iagc_enabled) { speex_compute_agc(st, Pframe, st->ft); } #endif spx_ifft(st->fft_lookup, st->ft, st->frame); #ifdef FIXED_POINT preprocess_scale(st, N); #endif #ifndef FIXED_POINT if ( st->agc_enabled ) { preprocess_apply_agc(st, N); } #endif preprocess_update(st, x, N); if ( st->vad_enabled ) { if (Pframe > st->speech_prob_start || (st->was_speech && Pframe > st->speech_prob_continue)) { st->was_speech=1; return 1; } else { st->was_speech=0; return 0; } } else { return 1; } }