_declspec(dllexport) void Init_Decod_ld8k(void) { /* Initialize static pointer */ exc = old_exc + PIT_MAX + L_INTERPOL; /* Static vectors to zero */ Set_zero(old_exc, PIT_MAX+L_INTERPOL); Set_zero(mem_syn, M); sharp = SHARPMIN; old_T0 = 60; gain_code = 0; gain_pitch = 0; Lsp_decw_reset(); /* for G.729B */ seed_fer = 21845; past_ftyp = 1; seed = INIT_SEED; sid_sav = 0; sh_sid_sav = 1; Init_lsfq_noise(); return; }
void Init_Post_Filter(void) { res2 = res2_buf + PIT_MAX; scal_res2 = scal_res2_buf + PIT_MAX; Set_zero(mem_syn_pst, M); Set_zero(res2_buf, PIT_MAX+L_SUBFR); Set_zero(scal_res2_buf, PIT_MAX+L_SUBFR); return; }
void Init_Post_Filter(void) { pg729dec->ppost_filt->res2 = pg729dec->ppost_filt->res2_buf + PIT_MAX; pg729dec->ppost_filt->scal_res2 = pg729dec->ppost_filt->scal_res2_buf + PIT_MAX; Set_zero(pg729dec->ppost_filt->mem_syn_pst, M); Set_zero(pg729dec->ppost_filt->res2_buf, PIT_MAX+L_SUBFR); Set_zero(pg729dec->ppost_filt->scal_res2_buf, PIT_MAX+L_SUBFR); pg729dec->ppost_filt->mem_pre = 0; pg729dec->ppost_filt->past_gain = 4096; return; }
void Init_Post_Filter (DecState *decoder) { decoder->res2 = decoder->res2_buf + PIT_MAX; decoder->scal_res2 = decoder->scal_res2_buf + PIT_MAX; Set_zero (decoder->mem_syn_pst, M); Set_zero (decoder->res2_buf, PIT_MAX + L_SUBFR); Set_zero (decoder->scal_res2_buf, PIT_MAX + L_SUBFR); decoder->mem_pre = 0; decoder->past_gain = 4096; return; }
/************************************************************************* * * Function: Post_Filter_reset * Purpose: Initializes state memory to zero * ************************************************************************** */ int Post_Filter_reset (Post_FilterState *state) { if (state == (Post_FilterState *) NULL) { fprintf(stderr, "Post_Filter_reset: invalid parameter\n"); return -1; } Set_zero (state->mem_syn_pst, M); Set_zero (state->res2, L_SUBFR); Set_zero (state->synth_buf, L_FRAME + M); agc_reset(state->agc_state); preemphasis_reset(state->preemph_state); return 0; }
CodState* Init_Coder_ld8a (void) { CodState *coder; coder = (CodState *) malloc (sizeof (CodState)); /*----------------------------------------------------------------------* * Initialize pointers to speech vector. * * * * * * |--------------------|-------------|-------------|------------| * * previous speech sf1 sf2 L_NEXT * * * * <---------------- Total speech vector (L_TOTAL) -----------> * * <---------------- LPC analysis window (L_WINDOW) -----------> * * | <-- present frame (L_FRAME) --> * * old_speech | <-- new speech (L_FRAME) --> * * p_window | | * * speech | * * new_speech * *-----------------------------------------------------------------------*/ coder->new_speech = coder->old_speech + L_TOTAL - L_FRAME; /* New speech */ coder->speech = coder->new_speech - L_NEXT; /* Present frame */ coder->p_window = coder->old_speech + L_TOTAL - L_WINDOW; /* For LPC window */ /* Initialize pointers */ coder->wsp = coder->old_wsp + PIT_MAX; coder->exc = coder->old_exc + PIT_MAX + L_INTERPOL; /* Static vectors to zero */ Set_zero (coder->old_speech, L_TOTAL); Set_zero (coder->old_exc, PIT_MAX + L_INTERPOL); Set_zero (coder->old_wsp, PIT_MAX); Set_zero (coder->mem_w, M); Set_zero (coder->mem_w0, M); Set_zero (coder->mem_zero, M); coder->sharp = SHARPMIN; /* Initialize lsp_old_q[] */ Copy (lsp_old_init, coder->lsp_old, M); Copy (lsp_old_init, coder->lsp_old_q, M); Lsp_encw_reset (coder); Init_exc_err (coder); Copy (past_qua_en_init, coder->past_qua_en, 4); Set_zero (coder->old_A, M + 1); coder->old_A [0] = 4096; Set_zero (coder->old_rc, 2); return coder; }
void Decod_ACELP( Word16 sign, /* (i) : signs of 4 pulses. */ Word16 index, /* (i) : Positions of the 4 pulses. */ Word16 cod[] /* (o) Q13 : algebraic (fixed) codebook excitation */ ) { Word16 i, j; /* Decode the positions */ /* decode the signs and build the codeword */
/* ************************************************************************** * * Function : dtx_dec_reset * ************************************************************************** */ int dtx_dec_reset (dtx_decState *st) { int i; if (st == (dtx_decState *) NULL){ fprintf(stderr, "dtx_dec_reset: invalid parameter\n"); return -1; } st->since_last_sid = 0; st->true_sid_period_inv = (1 << 13); st->log_en = 3500; st->old_log_en = 3500; /* low level noise for better performance in DTX handover cases*/ st->L_pn_seed_rx = PN_INITIAL_SEED; /* Initialize state->lsp [] and state->lsp_old [] */ Copy(lsp_init_data, &st->lsp[0], M); Copy(lsp_init_data, &st->lsp_old[0], M); st->lsf_hist_ptr = 0; st->log_pg_mean = 0; st->log_en_hist_ptr = 0; /* initialize decoder lsf history */ Copy(mean_lsf, &st->lsf_hist[0], M); for (i = 1; i < DTX_HIST_SIZE; i++) { Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M); } Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE); /* initialize decoder log frame energy */ for (i = 0; i < DTX_HIST_SIZE; i++) { st->log_en_hist[i] = st->log_en; } st->log_en_adjust = 0; st->dtxHangoverCount = DTX_HANG_CONST; st->decAnaElapsedCount = 32767; st->sid_frame = 0; st->valid_data = 0; st->dtxHangoverAdded = 0; st->dtxGlobalState = DTX; st->data_updated = 0; return 0; }
void Init_Decod_ld8k(void) { /* Initialize static pointer */ exc = old_exc + PIT_MAX + L_INTERPOL; /* Static vectors to zero */ Set_zero(old_exc, PIT_MAX+L_INTERPOL); Set_zero(mem_syn, M); sharp = SHARPMIN; old_T0 = 60; gain_code = 0; gain_pitch = 0; Lsp_decw_reset(); return; }
/************************************************************************* * * Function: gainQuant_reset * Purpose: Initializes state memory to zero * ************************************************************************** */ int gainQuant_reset (gainQuantState *state) { if (state == (gainQuantState *) NULL){ fprintf(stderr, "gainQuant_reset: invalid parameter\n"); return -1; } state->sf0_exp_gcode0 = 0; state->sf0_frac_gcode0 = 0; state->sf0_exp_target_en = 0; state->sf0_frac_target_en = 0; Set_zero (state->sf0_exp_coeff, 5); Set_zero (state->sf0_frac_coeff, 5); state->gain_idx_ptr = NULL; gc_pred_reset(state->gc_predSt); gc_pred_reset(state->gc_predUnqSt); gain_adapt_reset(state->adaptSt); return 0; }
void G729aCoder(Word16 SpeechBuf[],Word16 serial[]) { extern Word16 *new_speech; /* Pointer to new speech data */ Word16 prm[PRM_SIZE]; /* Analysis parameters. */ Word16 i; for(i=0;i<L_FRAME;i++) new_speech[i]=SpeechBuf[i]; Set_zero(prm, PRM_SIZE); Pre_Process(new_speech, L_FRAME); Coder_ld8a(prm); prm2bits_ld8k( prm, serial); }
/* ************************************************************************** * * Function : Bgn_scd_reset * Purpose : Resets state memory * ************************************************************************** */ Word16 Bgn_scd_reset (Bgn_scdState *state) { if (state == (Bgn_scdState *) NULL){ fprintf(stderr, "Bgn_scd_reset: invalid parameter\n"); return -1; } /* Static vectors to zero */ Set_zero (state->frameEnergyHist, L_ENERGYHIST); /* Initialize hangover handling */ state->bgHangover = 0; return 0; }
void Init_Decod_ld8c(void) { /* Initialize static pointer */ exc = old_exc + PIT_MAX + L_INTERPOL; /* Static vectors to zero */ Set_zero(old_exc, PIT_MAX+L_INTERPOL); Set_zero(mem_syn, M_BWD); sharp = SHARPMIN; prev_T0 = 60; prev_T0_frac = 0; gain_code = 0; gain_pitch = 0; Lsp_decw_resete(freq_prev, prev_lsp, &prev_ma); Set_zero(A_bwd_mem, M_BWDP1); Set_zero(A_t_bwd_mem, M_BWDP1); A_bwd_mem[0] = 4096; A_t_bwd_mem[0] = 4096; prev_voicing = 0; prev_bfi = 0; prev_lp_mode = 0; C_fe_fix = 0; /* Q12 */ C_int = 4506; /* Filter interpolation parameter */ Set_zero(prev_filter, M_BWDP1); prev_filter[0] = 4096; prev_pitch = 30; stat_pitch = 0; Set_zero(old_A_bwd, M_BWDP1); old_A_bwd[0]= 4096; Set_zero(old_rc_bwd, 2); gain_pit_mem = 0; gain_cod_mem = 0; c_muting = 32767; count_bfi = 0; stat_bwd = 0; /* for G.729B */ seed_fer = (Word16)21845; past_ftyp = 3; seed = INIT_SEED; sid_sav = 0; sh_sid_sav = 1; Init_lsfq_noise(); return; }
void Init_Coder_ld8a(void) { /*----------------------------------------------------------------------* * Initialize pointers to speech vector. * * * * * * |--------------------|-------------|-------------|------------| * * previous speech sf1 sf2 L_NEXT * * * * <---------------- Total speech vector (L_TOTAL) -----------> * * <---------------- LPC analysis window (L_WINDOW) -----------> * * | <-- present frame (L_FRAME) --> * * old_speech | <-- new speech (L_FRAME) --> * * p_window | | * * speech | * * new_speech * *-----------------------------------------------------------------------*/ new_speech = old_speech + L_TOTAL - L_FRAME; /* New speech */ speech = new_speech - L_NEXT; /* Present frame */ p_window = old_speech + L_TOTAL - L_WINDOW; /* For LPC window */ /* Initialize static pointers */ wsp = old_wsp + PIT_MAX; exc = old_exc + PIT_MAX + L_INTERPOL; /* Static vectors to zero */ Set_zero(old_speech, L_TOTAL); Set_zero(old_exc, PIT_MAX+L_INTERPOL); Set_zero(old_wsp, PIT_MAX); Set_zero(mem_w, M); Set_zero(mem_w0, M); Set_zero(mem_zero, M); sharp = SHARPMIN; /* Initialize lsp_old_q[] */ Copy(lsp_old, lsp_old_q, M); Lsp_encw_reset(); Init_exc_err(); /* For G.729B */ /* Initialize VAD/DTX parameters */ pastVad = 1; ppastVad = 1; seed = INIT_SEED; vad_init(); Init_lsfq_noise(); return; }
void Init_Coder_ld8a(g729a_encoder_state *state) { /*----------------------------------------------------------------------* * Initialize pointers to speech vector. * * * * * * |--------------------|-------------|-------------|------------| * * previous speech sf1 sf2 L_NEXT * * * * <---------------- Total speech vector (L_TOTAL) -----------> * * <---------------- LPC analysis window (L_WINDOW) -----------> * * | <-- present frame (L_FRAME) --> * * old_speech | <-- new speech (L_FRAME) --> * * p_window | | * * speech | * * new_speech * *-----------------------------------------------------------------------*/ state->new_speech = state->old_speech + L_TOTAL - L_FRAME; /* New speech */ state->speech = state->new_speech - L_NEXT; /* Present frame */ state->p_window = state->old_speech + L_TOTAL - L_WINDOW; /* For LPC window */ /* Initialize static pointers */ state->wsp = state->old_wsp + PIT_MAX; state->exc = state->old_exc + PIT_MAX + L_INTERPOL; /* Static vectors to zero */ Set_zero(state->old_speech, L_TOTAL); Set_zero(state->old_exc, PIT_MAX+L_INTERPOL); Set_zero(state->old_wsp, PIT_MAX); Set_zero(state->mem_w, M); Set_zero(state->mem_w0, M); Set_zero(state->mem_zero, M); state->sharp = SHARPMIN; /* Initialize lsp_old_q[] */ state->lsp_old[0] = 30000; state->lsp_old[1] = 26000; state->lsp_old[2] = 21000; state->lsp_old[3] = 15000; state->lsp_old[4] = 8000; state->lsp_old[5] = 0; state->lsp_old[6] = -8000; state->lsp_old[7] = -15000; state->lsp_old[8] = -21000; state->lsp_old[9] = -26000; Copy(state->lsp_old, state->lsp_old_q, M); Lsp_encw_reset(state); Init_exc_err(state); }
Flag g729a_dec_init (void *decState) { g729a_decode_frame_state *state; if (decState == NULL) return 0; state = (g729a_decode_frame_state *)decState; Set_zero(state->synth_buf, M); state->synth = state->synth_buf + M; Init_Decod_ld8a(&state->decoderState); Init_Post_Filter(&state->postFilterState); Init_Post_Process(&state->postProcessState); return 1; }
/*---------------------------------------------------------------------------* * Function vad_init * * ~~~~~~~~~~~~~~~~~~ * * * * -> Initialization of variables for voice activity detection * * * *---------------------------------------------------------------------------*/ void vad_init(void) { /* Static vectors to zero */ Set_zero(MeanLSF, M); /* Initialize VAD parameters */ MeanSE = 0; MeanSLE = 0; MeanE = 0; MeanSZC = 0; count_sil = 0; count_update = 0; count_ext = 0; less_count = 0; flag = 1; Min = MAX_16; }
static void Calc_pastfilt(DtxStatus *handle, Word16 *Coeff) { Word16 i; Word16 s_sumAcf[MP1]; Word16 bid[M], zero[MP1]; Word16 temp; Calc_sum_acf(handle->sumAcf, handle->sh_sumAcf, s_sumAcf, &temp, NB_SUMACF); if(s_sumAcf[0] == 0L) { Coeff[0] = 4096; for(i=1; i<=M; i++) Coeff[i] = 0; return; } Set_zero(zero, MP1); Levinson(handle, s_sumAcf, zero, Coeff, bid, &temp); return; }
static void Calc_pastfilt(Word16 *Coeff, Word16 old_A[], Word16 old_rc[]) { Word16 i; Word16 s_sumAcf[MP1]; Word16 bid[M], zero[MP1]; Word16 temp; Calc_sum_acf(sumAcf, sh_sumAcf, s_sumAcf, &temp, NB_SUMACF); if(s_sumAcf[0] == 0L) { Coeff[0] = 4096; for(i=1; i<=M; i++) Coeff[i] = 0; return; } Set_zero(zero, MP1); Levinsoncp(M, s_sumAcf, zero, Coeff, bid, old_A, old_rc, &temp); return; }
void Decod_ACELP( Word16 sign, /* (i) : signs of 4 pulses. */ Word16 index, /* (i) : Positions of the 4 pulses. */ Word16 cod[] /* (o) Q13 : algebraic (fixed) codebook excitation */ ) { Word16 i, j; Word16 pos[4]; /* Decode the positions */ i = index & (Word16)7; pos[0] = 5*i; index >>= 3; i = index & (Word16)7; pos[1] = 5*i+1; index >>= 3; i = index & (Word16)7; pos[2] = 5*i+2; index >>= 3; j = index & (Word16)1; index >>= 1; i = index & (Word16)7; pos[3] = 5*i+3+j; /* decode the signs and build the codeword */ Set_zero(cod, L_SUBFR); for (j=0; j<4; j++) { i = sign & (Word16)1; sign >>= 1; /* Q13 +1.0 or Q13 -1.0 */ cod[pos[j]] = (i != 0 ? 8191 : -8192); } }
static int tdav_codec_g729ab_open(tmedia_codec_t* self) { tdav_codec_g729ab_t* g729a = (tdav_codec_g729ab_t*)self; // Initialize the decoder bad_lsf = 0; g729a->decoder.synth = (g729a->decoder.synth_buf + M); Init_Decod_ld8a(); Init_Post_Filter(); Init_Post_Process(); /* for G.729B */ Init_Dec_cng(); // Initialize the encoder Init_Pre_Process(); Init_Coder_ld8a(); Set_zero(g729a->encoder.prm, PRM_SIZE + 1); /* for G.729B */ Init_Cod_cng(); return 0; }
void Coder_ld8a( Word16 ana[], /* output : Analysis parameters */ Word16 frame, /* input : frame counter */ Word16 vad_enable /* input : VAD enable flag */ ) { /* LPC analysis */ Word16 Aq_t[(MP1)*2]; /* A(z) quantized for the 2 subframes */ Word16 Ap_t[(MP1)*2]; /* A(z/gamma) for the 2 subframes */ Word16 *Aq, *Ap; /* Pointer on Aq_t and Ap_t */ /* Other vectors */ Word16 h1[L_SUBFR]; /* Impulse response h1[] */ Word16 xn[L_SUBFR]; /* Target vector for pitch search */ Word16 xn2[L_SUBFR]; /* Target vector for codebook search */ Word16 code[L_SUBFR]; /* Fixed codebook excitation */ Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */ Word16 y2[L_SUBFR]; /* Filtered fixed codebook excitation */ Word16 g_coeff[4]; /* Correlations between xn & y1 */ Word16 g_coeff_cs[5]; Word16 exp_g_coeff_cs[5]; /* Correlations between xn, y1, & y2 <y1,y1>, -2<xn,y1>, <y2,y2>, -2<xn,y2>, 2<y1,y2> */ /* Scalars */ Word16 i, j, k, i_subfr; Word16 T_op, T0, T0_min, T0_max, T0_frac; Word16 gain_pit, gain_code, index; Word16 temp, taming; Word32 L_temp; /*------------------------------------------------------------------------* * - Perform LPC analysis: * * * autocorrelation + lag windowing * * * Levinson-durbin algorithm to find a[] * * * convert a[] to lsp[] * * * quantize and code the LSPs * * * find the interpolated LSPs and convert to a[] for the 2 * * subframes (both quantized and unquantized) * *------------------------------------------------------------------------*/ { /* Temporary vectors */ Word16 r_l[NP+1], r_h[NP+1]; /* Autocorrelations low and hi */ Word16 rc[M]; /* Reflection coefficients. */ Word16 lsp_new[M], lsp_new_q[M]; /* LSPs at 2th subframe */ /* For G.729B */ Word16 rh_nbe[MP1]; Word16 lsf_new[M]; Word16 lsfq_mem[MA_NP][M]; Word16 exp_R0, Vad; /* LP analysis */ Autocorr(p_window, NP, r_h, r_l, &exp_R0); /* Autocorrelations */ Copy(r_h, rh_nbe, MP1); Lag_window(NP, r_h, r_l); /* Lag windowing */ Levinson(r_h, r_l, Ap_t, rc, &temp); /* Levinson Durbin */ Az_lsp(Ap_t, lsp_new, lsp_old); /* From A(z) to lsp */ /* For G.729B */ /* ------ VAD ------- */ Lsp_lsf(lsp_new, lsf_new, M); vad(rc[1], lsf_new, r_h, r_l, exp_R0, p_window, frame, pastVad, ppastVad, &Vad); Update_cng(rh_nbe, exp_R0, Vad); /* ---------------------- */ /* Case of Inactive frame */ /* ---------------------- */ if ((Vad == 0) && (vad_enable == 1)){ Get_freq_prev(lsfq_mem); Cod_cng(exc, pastVad, lsp_old_q, Aq_t, ana, lsfq_mem, &seed); Update_freq_prev(lsfq_mem); ppastVad = pastVad; pastVad = Vad; /* Update wsp, mem_w and mem_w0 */ Aq = Aq_t; for(i_subfr=0; i_subfr < L_FRAME; i_subfr += L_SUBFR) { /* Residual signal in xn */ Residu(Aq, &speech[i_subfr], xn, L_SUBFR); Weight_Az(Aq, GAMMA1, M, Ap_t); /* Compute wsp and mem_w */ Ap = Ap_t + MP1; Ap[0] = 4096; for(i=1; i<=M; i++) /* Ap[i] = Ap_t[i] - 0.7 * Ap_t[i-1]; */ Ap[i] = sub(Ap_t[i], mult(Ap_t[i-1], 22938)); Syn_filt(Ap, xn, &wsp[i_subfr], L_SUBFR, mem_w, 1); /* Compute mem_w0 */ for(i=0; i<L_SUBFR; i++) { xn[i] = sub(xn[i], exc[i_subfr+i]); /* residu[] - exc[] */ } Syn_filt(Ap_t, xn, xn, L_SUBFR, mem_w0, 1); Aq += MP1; } sharp = SHARPMIN; /* Update memories for next frames */ Copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME); Copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX); Copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL); return; } /* End of inactive frame case */ /* -------------------- */ /* Case of Active frame */ /* -------------------- */ /* Case of active frame */ *ana++ = 1; seed = INIT_SEED; ppastVad = pastVad; pastVad = Vad; /* LSP quantization */ Qua_lsp(lsp_new, lsp_new_q, ana); ana += 2; /* Advance analysis parameters pointer */ /*--------------------------------------------------------------------* * Find interpolated LPC parameters in all subframes * * The interpolated parameters are in array Aq_t[]. * *--------------------------------------------------------------------*/ Int_qlpc(lsp_old_q, lsp_new_q, Aq_t); /* Compute A(z/gamma) */ Weight_Az(&Aq_t[0], GAMMA1, M, &Ap_t[0]); Weight_Az(&Aq_t[MP1], GAMMA1, M, &Ap_t[MP1]); /* update the LSPs for the next frame */ Copy(lsp_new, lsp_old, M); Copy(lsp_new_q, lsp_old_q, M); } /*----------------------------------------------------------------------* * - Find the weighted input speech w_sp[] for the whole speech frame * * - Find the open-loop pitch delay * *----------------------------------------------------------------------*/ Residu(&Aq_t[0], &speech[0], &exc[0], L_SUBFR); Residu(&Aq_t[MP1], &speech[L_SUBFR], &exc[L_SUBFR], L_SUBFR); { Word16 Ap1[MP1]; Ap = Ap_t; Ap1[0] = 4096; for(i=1; i<=M; i++) /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */ Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938)); Syn_filt(Ap1, &exc[0], &wsp[0], L_SUBFR, mem_w, 1); Ap += MP1; for(i=1; i<=M; i++) /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */ Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938)); Syn_filt(Ap1, &exc[L_SUBFR], &wsp[L_SUBFR], L_SUBFR, mem_w, 1); } /* Find open loop pitch lag */ T_op = Pitch_ol_fast(wsp, PIT_MAX, L_FRAME); /* Range for closed loop pitch search in 1st subframe */ T0_min = sub(T_op, 3); if (sub(T0_min,PIT_MIN)<0) { T0_min = PIT_MIN; } T0_max = add(T0_min, 6); if (sub(T0_max ,PIT_MAX)>0) { T0_max = PIT_MAX; T0_min = sub(T0_max, 6); } /*------------------------------------------------------------------------* * Loop for every subframe in the analysis frame * *------------------------------------------------------------------------* * To find the pitch and innovation parameters. The subframe size is * * L_SUBFR and the loop is repeated 2 times. * * - find the weighted LPC coefficients * * - find the LPC residual signal res[] * * - compute the target signal for pitch search * * - compute impulse response of weighted synthesis filter (h1[]) * * - find the closed-loop pitch parameters * * - encode the pitch delay * * - find target vector for codebook search * * - codebook search * * - VQ of pitch and codebook gains * * - update states of weighting filter * *------------------------------------------------------------------------*/ Aq = Aq_t; /* pointer to interpolated quantized LPC parameters */ Ap = Ap_t; /* pointer to weighted LPC coefficients */ for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) { /*---------------------------------------------------------------* * Compute impulse response, h1[], of weighted synthesis filter * *---------------------------------------------------------------*/ h1[0] = 4096; Set_zero(&h1[1], L_SUBFR-1); Syn_filt(Ap, h1, h1, L_SUBFR, &h1[1], 0); /*----------------------------------------------------------------------* * Find the target vector for pitch search: * *----------------------------------------------------------------------*/ Syn_filt(Ap, &exc[i_subfr], xn, L_SUBFR, mem_w0, 0); /*---------------------------------------------------------------------* * Closed-loop fractional pitch search * *---------------------------------------------------------------------*/ T0 = Pitch_fr3_fast(&exc[i_subfr], xn, h1, L_SUBFR, T0_min, T0_max, i_subfr, &T0_frac); index = Enc_lag3(T0, T0_frac, &T0_min, &T0_max,PIT_MIN,PIT_MAX,i_subfr); *ana++ = index; if (i_subfr == 0) { *ana++ = Parity_Pitch(index); } /*-----------------------------------------------------------------* * - find filtered pitch exc * * - compute pitch gain and limit between 0 and 1.2 * * - update target vector for codebook search * *-----------------------------------------------------------------*/ Syn_filt(Ap, &exc[i_subfr], y1, L_SUBFR, mem_zero, 0); gain_pit = G_pitch(xn, y1, g_coeff, L_SUBFR); /* clip pitch gain if taming is necessary */ taming = test_err(T0, T0_frac); if( taming == 1){ if (sub(gain_pit, GPCLIP) > 0) { gain_pit = GPCLIP; } } /* xn2[i] = xn[i] - y1[i] * gain_pit */ for (i = 0; i < L_SUBFR; i++) { L_temp = L_mult(y1[i], gain_pit); L_temp = L_shl(L_temp, 1); /* gain_pit in Q14 */ xn2[i] = sub(xn[i], extract_h(L_temp)); } /*-----------------------------------------------------* * - Innovative codebook search. * *-----------------------------------------------------*/ index = ACELP_Code_A(xn2, h1, T0, sharp, code, y2, &i); *ana++ = index; /* Positions index */ *ana++ = i; /* Signs index */ /*-----------------------------------------------------* * - Quantization of gains. * *-----------------------------------------------------*/ g_coeff_cs[0] = g_coeff[0]; /* <y1,y1> */ exp_g_coeff_cs[0] = negate(g_coeff[1]); /* Q-Format:XXX -> JPN */ g_coeff_cs[1] = negate(g_coeff[2]); /* (xn,y1) -> -2<xn,y1> */ exp_g_coeff_cs[1] = negate(add(g_coeff[3], 1)); /* Q-Format:XXX -> JPN */ Corr_xy2( xn, y1, y2, g_coeff_cs, exp_g_coeff_cs ); /* Q0 Q0 Q12 ^Qx ^Q0 */ /* g_coeff_cs[3]:exp_g_coeff_cs[3] = <y2,y2> */ /* g_coeff_cs[4]:exp_g_coeff_cs[4] = -2<xn,y2> */ /* g_coeff_cs[5]:exp_g_coeff_cs[5] = 2<y1,y2> */ *ana++ = Qua_gain(code, g_coeff_cs, exp_g_coeff_cs, L_SUBFR, &gain_pit, &gain_code, taming); /*------------------------------------------------------------* * - Update pitch sharpening "sharp" with quantized gain_pit * *------------------------------------------------------------*/ sharp = gain_pit; if (sub(sharp, SHARPMAX) > 0) { sharp = SHARPMAX; } if (sub(sharp, SHARPMIN) < 0) { sharp = SHARPMIN; } /*------------------------------------------------------* * - Find the total excitation * * - update filters memories for finding the target * * vector in the next subframe * *------------------------------------------------------*/ for (i = 0; i < L_SUBFR; i++) { /* exc[i] = gain_pit*exc[i] + gain_code*code[i]; */ /* exc[i] in Q0 gain_pit in Q14 */ /* code[i] in Q13 gain_cod in Q1 */ L_temp = L_mult(exc[i+i_subfr], gain_pit); L_temp = L_mac(L_temp, code[i], gain_code); L_temp = L_shl(L_temp, 1); exc[i+i_subfr] = round(L_temp); } update_exc_err(gain_pit, T0); for (i = L_SUBFR-M, j = 0; i < L_SUBFR; i++, j++) { temp = extract_h(L_shl( L_mult(y1[i], gain_pit), 1) ); k = extract_h(L_shl( L_mult(y2[i], gain_code), 2) ); mem_w0[j] = sub(xn[i], add(temp, k)); } Aq += MP1; /* interpolated LPC parameters for next subframe */ Ap += MP1; } /*--------------------------------------------------* * Update signal for next frame. * * -> shift to the left by L_FRAME: * * speech[], wsp[] and exc[] * *--------------------------------------------------*/ Copy(&old_speech[L_FRAME], &old_speech[0], L_TOTAL-L_FRAME); Copy(&old_wsp[L_FRAME], &old_wsp[0], PIT_MAX); Copy(&old_exc[L_FRAME], &old_exc[0], PIT_MAX+L_INTERPOL); return; }
void Init_Coder_ld8h(void) { Word16 i; /*----------------------------------------------------------------------* * Initialize pointers to speech vector. * * * * * * |--------------------|-------------|-------------|------------| * * previous speech sf1 sf2 L_NEXT * * * * <---------------- Total speech vector (L_TOTAL) -----------> * * <---------------- LPC analysis window (L_WINDOW) -----------> * * | <-- present frame (L_FRAME) --> * * old_speech | <-- new speech (L_FRAME) --> * * p_window | | * * speech | * * new_speech * *-----------------------------------------------------------------------*/ new_speech = old_speech + L_TOTAL - L_FRAME; /* New speech */ speech = new_speech - L_NEXT; /* Present frame */ p_window = old_speech + L_TOTAL - L_WINDOW; /* For LPC window */ /* Initialize static pointers */ wsp = old_wsp + PIT_MAX; exc = old_exc + PIT_MAX + L_INTERPOL; zero = ai_zero + M_BWDP1; error = mem_err + M_BWD; /* Static vectors to zero */ Set_zero(old_speech, L_TOTAL); Set_zero(old_exc, PIT_MAX+L_INTERPOL); Set_zero(old_wsp, PIT_MAX); Set_zero(mem_syn, M_BWD); Set_zero(mem_w, M_BWD); Set_zero(mem_w0, M_BWD); Set_zero(mem_err, M_BWD); Set_zero(zero, L_SUBFR); sharp = SHARPMIN; /* Initialize lsp_old_q[] */ Copy(lsp_old, lsp_old_q, M); Lsp_encw_resete(freq_prev); /* to tame the coder */ Init_exc_err(); /* G.729 maintenance */ /* Before : for(i=0; i<4; i++) L_exc_err[i] = 0x00004000L; /* Q14 */ /* for the backward analysis */ Set_zero(synth, L_ANA_BWD); /*Q14 */ synth_ptr = synth + MEM_SYN_BWD; prev_lp_mode = 0; bwd_dominant = 0; /* See file bwfw.c */ C_int = 4506; /* Filter interpolation parameter */ glob_stat = 10000; /* Mesure of global stationnarity Q8 */ stat_bwd = 0; /* Nbre of consecutive backward frames */ val_stat_bwd = 0; /* Value associated with stat_bwd */ for(i=0; i<M_BWDP1; i++) rexp[i] = 0L; A_t_bwd_mem[0] = 4096; for (i=1; i<M_BWDP1; i++) A_t_bwd_mem[i] = 0; Set_zero(prev_filter, M_BWDP1); prev_filter[0] = 4096; Set_zero(old_A_bwd, M_BWDP1); old_A_bwd[0]=4096; Set_zero(old_rc_bwd, 2); Set_zero(old_A_fwd, MP1); old_A_fwd[0]=4096; Set_zero(old_rc_fwd, 2); return; }
int main(int argc, char *argv[] ) { FILE *f_speech; /* File of speech data */ FILE *f_serial; /* File of serial bits for transmission */ extern Word16 *new_speech; /* Pointer to new speech data */ Word16 prm[PRM_SIZE+1]; /* Analysis parameters + frame type */ Word16 serial[SERIAL_SIZE]; /* Output bitstream buffer */ Word16 frame; /* frame counter */ Word32 count_frame; /* For G.729B */ Word16 nb_words; Word16 vad_enable; printf("\n"); printf("*********** ITU G.729A 8 KBIT/S SPEECH CODER ***********\n"); printf(" (WITH ANNEX B) \n"); printf("\n"); printf("------------------- Fixed point C simulation -----------------\n"); printf("\n"); printf("------------ Version 1.5 (Release 2, November 2006) ----------\n"); printf("\n"); /*--------------------------------------------------------------------------* * Open speech file and result file (output serial bit stream) * *--------------------------------------------------------------------------*/ if ( argc != 4 ){ printf("Usage :%s speech_file bitstream_file VAD_flag\n", argv[0]); printf("\n"); printf("Format for speech_file:\n"); printf(" Speech is read from a binary file of 16 bits PCM data.\n"); printf("\n"); printf("Format for bitstream_file:\n"); printf(" One (2-byte) synchronization word \n"); printf(" One (2-byte) size word,\n"); printf(" 80 words (2-byte) containing 80 bits.\n"); printf("\n"); printf("VAD flag:\n"); printf(" 0 to disable the VAD\n"); printf(" 1 to enable the VAD\n"); exit(1); } if ( (f_speech = fopen(argv[1], "rb")) == NULL) { printf("%s - Error opening file %s !!\n", argv[0], argv[1]); exit(0); } printf(" Input speech file: %s\n", argv[1]); if ( (f_serial = fopen(argv[2], "wb")) == NULL) { printf("%s - Error opening file %s !!\n", argv[0], argv[2]); exit(0); } printf(" Output bitstream file: %s\n", argv[2]); vad_enable = (Word16)atoi(argv[3]); if (vad_enable == 1) printf(" VAD enabled\n"); else printf(" VAD disabled\n"); #ifndef OCTET_TX_MODE printf(" OCTET TRANSMISSION MODE is disabled\n"); #endif /*--------------------------------------------------------------------------* * Initialization of the coder. * *--------------------------------------------------------------------------*/ Init_Pre_Process(); Init_Coder_ld8a(); Set_zero(prm, PRM_SIZE+1); /* for G.729B */ Init_Cod_cng(); /* Loop for each "L_FRAME" speech data. */ frame = 0; count_frame = 0L; while( fread(new_speech, sizeof(Word16), L_FRAME, f_speech) == L_FRAME) { printf("Frame = %ld\r", count_frame++); if (frame == 32767) frame = 256; else frame++; Pre_Process(new_speech, L_FRAME); Coder_ld8a(prm, frame, vad_enable); prm2bits_ld8k( prm, serial); nb_words = serial[1] + (Word16)2; fwrite(serial, sizeof(Word16), nb_words, f_serial); } printf("%ld frames processed\n", count_frame); return (0); }
void Coder_ld8a( g729a_encoder_state *state, Word16 ana[] /* output : Analysis parameters */ ) { /* LPC analysis */ Word16 Aq_t[(MP1)*2]; /* A(z) quantized for the 2 subframes */ Word16 Ap_t[(MP1)*2]; /* A(z/gamma) for the 2 subframes */ Word16 *Aq, *Ap; /* Pointer on Aq_t and Ap_t */ /* Other vectors */ Word16 h1[L_SUBFR]; /* Impulse response h1[] */ Word16 xn[L_SUBFR]; /* Target vector for pitch search */ Word16 xn2[L_SUBFR]; /* Target vector for codebook search */ Word16 code[L_SUBFR]; /* Fixed codebook excitation */ Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */ Word16 y2[L_SUBFR]; /* Filtered fixed codebook excitation */ Word16 g_coeff[4]; /* Correlations between xn & y1 */ Word16 g_coeff_cs[5]; Word16 exp_g_coeff_cs[5]; /* Correlations between xn, y1, & y2 <y1,y1>, -2<xn,y1>, <y2,y2>, -2<xn,y2>, 2<y1,y2> */ /* Scalars */ Word16 i, j, k, i_subfr; Word16 T_op, T0, T0_min, T0_max, T0_frac; Word16 gain_pit, gain_code, index; Word16 temp, taming; Word32 L_temp; /*------------------------------------------------------------------------* * - Perform LPC analysis: * * * autocorrelation + lag windowing * * * Levinson-durbin algorithm to find a[] * * * convert a[] to lsp[] * * * quantize and code the LSPs * * * find the interpolated LSPs and convert to a[] for the 2 * * subframes (both quantized and unquantized) * *------------------------------------------------------------------------*/ { /* Temporary vectors */ Word16 r_l[MP1], r_h[MP1]; /* Autocorrelations low and hi */ Word16 rc[M]; /* Reflection coefficients. */ Word16 lsp_new[M], lsp_new_q[M]; /* LSPs at 2th subframe */ /* LP analysis */ Autocorr(state->p_window, M, r_h, r_l); /* Autocorrelations */ Lag_window(M, r_h, r_l); /* Lag windowing */ Levinson(r_h, r_l, Ap_t, rc); /* Levinson Durbin */ Az_lsp(Ap_t, lsp_new, state->lsp_old); /* From A(z) to lsp */ /* LSP quantization */ Qua_lsp(state, lsp_new, lsp_new_q, ana); ana += 2; /* Advance analysis parameters pointer */ /*--------------------------------------------------------------------* * Find interpolated LPC parameters in all subframes * * The interpolated parameters are in array Aq_t[]. * *--------------------------------------------------------------------*/ Int_qlpc(state->lsp_old_q, lsp_new_q, Aq_t); /* Compute A(z/gamma) */ Weight_Az(&Aq_t[0], GAMMA1, M, &Ap_t[0]); Weight_Az(&Aq_t[MP1], GAMMA1, M, &Ap_t[MP1]); /* update the LSPs for the next frame */ Copy(lsp_new, state->lsp_old, M); Copy(lsp_new_q, state->lsp_old_q, M); } /*----------------------------------------------------------------------* * - Find the weighted input speech w_sp[] for the whole speech frame * * - Find the open-loop pitch delay * *----------------------------------------------------------------------*/ Residu(&Aq_t[0], &(state->speech[0]), &(state->exc[0]), L_SUBFR); Residu(&Aq_t[MP1], &(state->speech[L_SUBFR]), &(state->exc[L_SUBFR]), L_SUBFR); { Word16 Ap1[MP1]; Ap = Ap_t; Ap1[0] = 4096; for(i=1; i<=M; i++) /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */ Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938)); Syn_filt(Ap1, &(state->exc[0]), &(state->wsp[0]), L_SUBFR, state->mem_w, 1); Ap += MP1; for(i=1; i<=M; i++) /* Ap1[i] = Ap[i] - 0.7 * Ap[i-1]; */ Ap1[i] = sub(Ap[i], mult(Ap[i-1], 22938)); Syn_filt(Ap1, &(state->exc[L_SUBFR]), &(state->wsp[L_SUBFR]), L_SUBFR, state->mem_w, 1); } /* Find open loop pitch lag */ T_op = Pitch_ol_fast(state->wsp, PIT_MAX, L_FRAME); /* Range for closed loop pitch search in 1st subframe */ T0_min = T_op - 3; T0_max = T0_min + 6; if (T0_min < PIT_MIN) { T0_min = PIT_MIN; T0_max = PIT_MIN + 6; } else if (T0_max > PIT_MAX) { T0_max = PIT_MAX; T0_min = PIT_MAX - 6; } /*------------------------------------------------------------------------* * Loop for every subframe in the analysis frame * *------------------------------------------------------------------------* * To find the pitch and innovation parameters. The subframe size is * * L_SUBFR and the loop is repeated 2 times. * * - find the weighted LPC coefficients * * - find the LPC residual signal res[] * * - compute the target signal for pitch search * * - compute impulse response of weighted synthesis filter (h1[]) * * - find the closed-loop pitch parameters * * - encode the pitch delay * * - find target vector for codebook search * * - codebook search * * - VQ of pitch and codebook gains * * - update states of weighting filter * *------------------------------------------------------------------------*/ Aq = Aq_t; /* pointer to interpolated quantized LPC parameters */ Ap = Ap_t; /* pointer to weighted LPC coefficients */ for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) { /*---------------------------------------------------------------* * Compute impulse response, h1[], of weighted synthesis filter * *---------------------------------------------------------------*/ h1[0] = 4096; Set_zero(&h1[1], L_SUBFR-1); Syn_filt(Ap, h1, h1, L_SUBFR, &h1[1], 0); /*----------------------------------------------------------------------* * Find the target vector for pitch search: * *----------------------------------------------------------------------*/ Syn_filt(Ap, &(state->exc[i_subfr]), xn, L_SUBFR, state->mem_w0, 0); /*---------------------------------------------------------------------* * Closed-loop fractional pitch search * *---------------------------------------------------------------------*/ T0 = Pitch_fr3_fast(&(state->exc[i_subfr]), xn, h1, L_SUBFR, T0_min, T0_max, i_subfr, &T0_frac); index = Enc_lag3(T0, T0_frac, &T0_min, &T0_max,PIT_MIN,PIT_MAX,i_subfr); *ana++ = index; if (i_subfr == 0) { *ana++ = Parity_Pitch(index); } /*-----------------------------------------------------------------* * - find filtered pitch exc * * - compute pitch gain and limit between 0 and 1.2 * * - update target vector for codebook search * *-----------------------------------------------------------------*/ Syn_filt(Ap, &(state->exc[i_subfr]), y1, L_SUBFR, state->mem_zero, 0); gain_pit = G_pitch(xn, y1, g_coeff, L_SUBFR); /* clip pitch gain if taming is necessary */ taming = test_err(state, T0, T0_frac); if( taming == 1){ if (gain_pit > GPCLIP) { gain_pit = GPCLIP; } } /* xn2[i] = xn[i] - y1[i] * gain_pit */ for (i = 0; i < L_SUBFR; i++) { //L_temp = L_mult(y1[i], gain_pit); //L_temp = L_shl(L_temp, 1); /* gain_pit in Q14 */ L_temp = ((Word32)y1[i] * gain_pit) << 2; xn2[i] = sub(xn[i], extract_h(L_temp)); } /*-----------------------------------------------------* * - Innovative codebook search. * *-----------------------------------------------------*/ index = ACELP_Code_A(xn2, h1, T0, state->sharp, code, y2, &i); *ana++ = index; /* Positions index */ *ana++ = i; /* Signs index */ /*-----------------------------------------------------* * - Quantization of gains. * *-----------------------------------------------------*/ g_coeff_cs[0] = g_coeff[0]; /* <y1,y1> */ exp_g_coeff_cs[0] = negate(g_coeff[1]); /* Q-Format:XXX -> JPN */ g_coeff_cs[1] = negate(g_coeff[2]); /* (xn,y1) -> -2<xn,y1> */ exp_g_coeff_cs[1] = negate(add(g_coeff[3], 1)); /* Q-Format:XXX -> JPN */ Corr_xy2( xn, y1, y2, g_coeff_cs, exp_g_coeff_cs ); /* Q0 Q0 Q12 ^Qx ^Q0 */ /* g_coeff_cs[3]:exp_g_coeff_cs[3] = <y2,y2> */ /* g_coeff_cs[4]:exp_g_coeff_cs[4] = -2<xn,y2> */ /* g_coeff_cs[5]:exp_g_coeff_cs[5] = 2<y1,y2> */ *ana++ = Qua_gain(code, g_coeff_cs, exp_g_coeff_cs, L_SUBFR, &gain_pit, &gain_code, taming); /*------------------------------------------------------------* * - Update pitch sharpening "sharp" with quantized gain_pit * *------------------------------------------------------------*/ state->sharp = gain_pit; if (state->sharp > SHARPMAX) { state->sharp = SHARPMAX; } else if (state->sharp < SHARPMIN) { state->sharp = SHARPMIN; } /*------------------------------------------------------* * - Find the total excitation * * - update filters memories for finding the target * * vector in the next subframe * *------------------------------------------------------*/ for (i = 0; i < L_SUBFR; i++) { /* exc[i] = gain_pit*exc[i] + gain_code*code[i]; */ /* exc[i] in Q0 gain_pit in Q14 */ /* code[i] in Q13 gain_cod in Q1 */ //L_temp = L_mult(exc[i+i_subfr], gain_pit); //L_temp = L_mac(L_temp, code[i], gain_code); //L_temp = L_shl(L_temp, 1); L_temp = (Word32)(state->exc[i+i_subfr]) * (Word32)gain_pit + (Word32)code[i] * (Word32)gain_code; L_temp <<= 2; state->exc[i+i_subfr] = g_round(L_temp); } update_exc_err(state, gain_pit, T0); for (i = L_SUBFR-M, j = 0; i < L_SUBFR; i++, j++) { temp = ((Word32)y1[i] * (Word32)gain_pit) >> 14; k = ((Word32)y2[i] * (Word32)gain_code) >> 13; state->mem_w0[j] = sub(xn[i], add(temp, k)); } Aq += MP1; /* interpolated LPC parameters for next subframe */ Ap += MP1; } /*--------------------------------------------------* * Update signal for next frame. * * -> shift to the left by L_FRAME: * * speech[], wsp[] and exc[] * *--------------------------------------------------*/ Copy(&(state->old_speech[L_FRAME]), &(state->old_speech[0]), L_TOTAL-L_FRAME); Copy(&(state->old_wsp[L_FRAME]), &(state->old_wsp[0]), PIT_MAX); Copy(&(state->old_exc[L_FRAME]), &(state->old_exc[0]), PIT_MAX+L_INTERPOL); }
/* ************************************************************************** * Function: Post_Filter * Purpose: postfiltering of synthesis speech. * Description: * The postfiltering process is described as follows: * * - inverse filtering of syn[] through A(z/0.7) to get res2[] * - tilt compensation filtering; 1 - MU*k*z^-1 * - synthesis filtering through 1/A(z/0.75) * - adaptive gain control * ************************************************************************** */ int Post_Filter ( Post_FilterState *st, /* i/o : post filter states */ enum Mode mode, /* i : AMR mode */ Word16 *syn, /* i/o : synthesis speech (postfiltered is output) */ Word16 *Az_4 /* i : interpolated LPC parameters in all subfr. */ ) { /*-------------------------------------------------------------------* * Declaration of parameters * *-------------------------------------------------------------------*/ Word16 Ap3[MP1], Ap4[MP1]; /* bandwidth expanded LP parameters */ Word16 *Az; /* pointer to Az_4: */ /* LPC parameters in each subframe */ Word16 i_subfr; /* index for beginning of subframe */ Word16 h[L_H]; Word16 i; Word16 temp1, temp2; Word32 L_tmp; Word16 *syn_work = &st->synth_buf[M]; move16 (); /*-----------------------------------------------------* * Post filtering * *-----------------------------------------------------*/ Copy (syn, syn_work , L_FRAME); Az = Az_4; for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) { /* Find weighted filter coefficients Ap3[] and ap[4] */ test (); test (); if (sub(mode, MR122) == 0 || sub(mode, MR102) == 0) { Weight_Ai (Az, gamma3_MR122, Ap3); Weight_Ai (Az, gamma4_MR122, Ap4); } else { Weight_Ai (Az, gamma3, Ap3); Weight_Ai (Az, gamma4, Ap4); } /* filtering of synthesis speech by A(z/0.7) to find res2[] */ Residu (Ap3, &syn_work[i_subfr], st->res2, L_SUBFR); /* tilt compensation filter */ /* impulse response of A(z/0.7)/A(z/0.75) */ Copy (Ap3, h, M + 1); Set_zero (&h[M + 1], L_H - M - 1); Syn_filt (Ap4, h, h, L_H, &h[M + 1], 0); /* 1st correlation of h[] */ L_tmp = L_mult (h[0], h[0]); for (i = 1; i < L_H; i++) { L_tmp = L_mac (L_tmp, h[i], h[i]); } temp1 = extract_h (L_tmp); L_tmp = L_mult (h[0], h[1]); for (i = 1; i < L_H - 1; i++) { L_tmp = L_mac (L_tmp, h[i], h[i + 1]); } temp2 = extract_h (L_tmp); test (); if (temp2 <= 0) { temp2 = 0; move16 (); } else { temp2 = mult (temp2, MU); temp2 = div_s (temp2, temp1); } preemphasis (st->preemph_state, st->res2, temp2, L_SUBFR); /* filtering through 1/A(z/0.75) */ Syn_filt (Ap4, st->res2, &syn[i_subfr], L_SUBFR, st->mem_syn_pst, 1); /* scale output to input */ agc (st->agc_state, &syn_work[i_subfr], &syn[i_subfr], AGC_FAC, L_SUBFR); Az += MP1; } /* update syn_work[] buffer */ Copy (&syn_work[L_FRAME - M], &syn_work[-M], M); return 0; }
void Post_Filter( Word16 *syn, /* in/out: synthesis speech (postfiltered is output) */ Word16 *Az_4, /* input : interpolated LPC parameters in all subframes */ Word16 *T, /* input : decoded pitch lags in all subframes */ Word16 Vad ) { /*-------------------------------------------------------------------* * Declaration of parameters * *-------------------------------------------------------------------*/ Word16 res2_pst[L_SUBFR]; /* res2[] after pitch postfiltering */ Word16 syn_pst[L_FRAME]; /* post filtered synthesis speech */ Word16 Ap3[MP1], Ap4[MP1]; /* bandwidth expanded LP parameters */ Word16 *Az; /* pointer to Az_4: */ /* LPC parameters in each subframe */ Word16 t0_max, t0_min; /* closed-loop pitch search range */ Word16 i_subfr; /* index for beginning of subframe */ Word16 h[L_H]; Word16 i, j; Word16 temp1, temp2; Word32 L_tmp; postfilt_type* ppost_filt = pg729dec->ppost_filt; /*-----------------------------------------------------* * Post filtering * *-----------------------------------------------------*/ Az = Az_4; for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) { /* Find pitch range t0_min - t0_max */ t0_min = sub(*T++, 3); t0_max = add(t0_min, 6); if (sub(t0_max, PIT_MAX) > 0) { t0_max = PIT_MAX; t0_min = sub(t0_max, 6); } /* Find weighted filter coefficients Ap3[] and ap[4] */ Weight_Az(Az, GAMMA2_PST, M, Ap3); Weight_Az(Az, GAMMA1_PST, M, Ap4); /* filtering of synthesis speech by A(z/GAMMA2_PST) to find res2[] */ Residu(Ap3, &syn[i_subfr], ppost_filt->res2, L_SUBFR); /* scaling of "res2[]" to avoid energy overflow */ for (j=0; j<L_SUBFR; j++) { ppost_filt->scal_res2[j] = shr(ppost_filt->res2[j], 2); } /* pitch postfiltering */ if (sub(Vad, 1) == 0) pit_pst_filt(ppost_filt->res2, ppost_filt->scal_res2, t0_min, t0_max, L_SUBFR, res2_pst); else for (j=0; j<L_SUBFR; j++) res2_pst[j] = ppost_filt->res2[j]; /* tilt compensation filter */ /* impulse response of A(z/GAMMA2_PST)/A(z/GAMMA1_PST) */ Copy(Ap3, h, M+1); Set_zero(&h[M+1], L_H-M-1); Syn_filt(Ap4, h, h, L_H, &h[M+1], 0); /* 1st correlation of h[] */ L_tmp = L_mult(h[0], h[0]); for (i=1; i<L_H; i++) L_tmp = L_mac(L_tmp, h[i], h[i]); temp1 = extract_h(L_tmp); L_tmp = L_mult(h[0], h[1]); for (i=1; i<L_H-1; i++) L_tmp = L_mac(L_tmp, h[i], h[i+1]); temp2 = extract_h(L_tmp); if(temp2 <= 0) { temp2 = 0; } else { temp2 = mult(temp2, MU); temp2 = div_s(temp2, temp1); } preemphasis(res2_pst, temp2, L_SUBFR); /* filtering through 1/A(z/GAMMA1_PST) */ Syn_filt(Ap4, res2_pst, &syn_pst[i_subfr], L_SUBFR, ppost_filt->mem_syn_pst, 1); /* scale output to input */ agc(&syn[i_subfr], &syn_pst[i_subfr], L_SUBFR); /* update res2[] buffer; shift by L_SUBFR */ Copy(&ppost_filt->res2[L_SUBFR-PIT_MAX], &ppost_filt->res2[-PIT_MAX], PIT_MAX); Copy(&ppost_filt->scal_res2[L_SUBFR-PIT_MAX], &ppost_filt->scal_res2[-PIT_MAX], PIT_MAX); Az += MP1; } /* update syn[] buffer */ Copy(&syn[L_FRAME-M], &syn[-M], M); /* overwrite synthesis speech by postfiltered synthesis speech */ Copy(syn_pst, syn, L_FRAME); return; }
void Init_HP50_12k8(Word16 mem[]) { Set_zero(mem, 6); }
/*-----------------------------------------------------------* * procedure Cod_cng: * * ~~~~~~~~ * * computes DTX decision * * encodes SID frames * * computes CNG excitation for encoder update * *-----------------------------------------------------------*/ void Cod_cng( DtxStatus *handle, Word16 *exc, /* (i/o) : excitation array */ Word16 pastVad, /* (i) : previous VAD decision */ Word16 *lsp_old_q, /* (i/o) : previous quantized lsp */ Word16 *Aq, /* (o) : set of interpolated LPC coefficients */ Word16 *ana, /* (o) : coded SID parameters */ Word16 freq_prev[MA_NP][M], /* (i/o) : previous LPS for quantization */ Word16 *seed /* (i/o) : random generator seed */ ) { Word16 i; Word16 curAcf[MP1]; Word16 bid[M], zero[MP1]; Word16 curCoeff[MP1]; Word16 lsp_new[M]; Word16 *lpcCoeff; Word16 cur_igain; Word16 energyq, temp; /* Update Ener and sh_ener */ for(i = NB_GAIN-1; i>=1; i--) { handle->ener[i] = handle->ener[i-1]; handle->sh_ener[i] = handle->sh_ener[i-1]; } /* Compute current Acfs */ Calc_sum_acf(handle->Acf, handle->sh_Acf, curAcf, &(handle->sh_ener[0]), NB_CURACF); /* Compute LPC coefficients and residual energy */ if(curAcf[0] == 0) { handle->ener[0] = 0; /* should not happen */ } else { Set_zero(zero, MP1); Levinson(handle, curAcf, zero, curCoeff, bid, &(handle->ener[0])); } /* if first frame of silence => SID frame */ if(pastVad != 0) { ana[0] = 2; handle->count_fr0 = 0; handle->nb_ener = 1; Qua_Sidgain(handle->ener, handle->sh_ener, handle->nb_ener, &energyq, &cur_igain); } else { handle->nb_ener = add(handle->nb_ener, 1); if(sub(handle->nb_ener, NB_GAIN) > 0) handle->nb_ener = NB_GAIN; Qua_Sidgain(handle->ener, handle->sh_ener, handle->nb_ener, &energyq, &cur_igain); /* Compute stationarity of current filter */ /* versus reference filter */ if(Cmp_filt(handle->RCoeff, handle->sh_RCoeff, curAcf, handle->ener[0], FRAC_THRESH1) != 0) { handle->flag_chang = 1; } /* compare energy difference between current frame and last frame */ temp = abs_s(sub(handle->prev_energy, energyq)); temp = sub(temp, 2); if (temp > 0) handle->flag_chang = 1; handle->count_fr0 = add(handle->count_fr0, 1); if(sub(handle->count_fr0, FR_SID_MIN) < 0) { ana[0] = 0; /* no transmission */ } else { if(handle->flag_chang != 0) { ana[0] = 2; /* transmit SID frame */ } else{ ana[0] = 0; } handle->count_fr0 = FR_SID_MIN; /* to avoid overflow */ } } if(sub(ana[0], 2) == 0) { /* Reset frame count and change flag */ handle->count_fr0 = 0; handle->flag_chang = 0; /* Compute past average filter */ Calc_pastfilt(handle, handle->pastCoeff); Calc_RCoeff(handle->pastCoeff, handle->RCoeff, &(handle->sh_RCoeff)); /* Compute stationarity of current filter */ /* versus past average filter */ /* if stationary */ /* transmit average filter => new ref. filter */ if(Cmp_filt(handle->RCoeff, handle->sh_RCoeff, curAcf, handle->ener[0], FRAC_THRESH2) == 0) { lpcCoeff = handle->pastCoeff; } /* else */ /* transmit current filter => new ref. filter */ else { lpcCoeff = curCoeff; Calc_RCoeff(curCoeff, handle->RCoeff, &(handle->sh_RCoeff)); } /* Compute SID frame codes */ Az_lsp(lpcCoeff, lsp_new, lsp_old_q); /* From A(z) to lsp */ /* LSP quantization */ lsfq_noise(lsp_new, handle->lspSid_q, freq_prev, &ana[1]); handle->prev_energy = energyq; ana[4] = cur_igain; handle->sid_gain = tab_Sidgain[cur_igain]; } /* end of SID frame case */ /* Compute new excitation */ if(pastVad != 0) { handle->cur_gain = handle->sid_gain; } else { handle->cur_gain = mult_r(handle->cur_gain, A_GAIN0); handle->cur_gain = add(handle->cur_gain, mult_r(handle->sid_gain, A_GAIN1)); } Calc_exc_rand(handle->L_exc_err, handle->cur_gain, exc, seed, FLAG_COD); Int_qlpc(lsp_old_q, handle->lspSid_q, Aq); for(i=0; i<M; i++) { lsp_old_q[i] = handle->lspSid_q[i]; } /* Update sumAcf if fr_cur = 0 */ if(handle->fr_cur == 0) { Update_sumAcf(handle); } return; }
void Init_Filt_6k_7k(Word16 mem[]) /* mem[30] */ { Set_zero(mem, L_FIR - 1); return; }