void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits) { MODEL model[4]; int lsp_indexes[LPC_ORD]; float lsps[4][LPC_ORD]; int WoE_index; float e[4]; float snr; float ak[4][LPC_ORD+1]; int i,j; unsigned int nbit = 0; float weight; assert(c2 != NULL); /* only need to zero these out due to (unused) snr calculation */ for(i=0; i<4; i++) for(j=1; j<=MAX_AMP; j++) model[i].A[j] = 0.0; /* unpack bits from channel ------------------------------------*/ /* this will partially fill the model params for the 4 x 10ms frames */ model[0].voiced = unpack(bits, &nbit, 1); model[1].voiced = unpack(bits, &nbit, 1); WoE_index = unpack(bits, &nbit, WO_E_BITS); decode_WoE(&model[1], &e[1], c2->xq_dec, WoE_index); model[2].voiced = unpack(bits, &nbit, 1); model[3].voiced = unpack(bits, &nbit, 1); WoE_index = unpack(bits, &nbit, WO_E_BITS); decode_WoE(&model[3], &e[3], c2->xq_dec, WoE_index); for(i=0; i<LSP_PRED_VQ_INDEXES; i++) { lsp_indexes[i] = unpack(bits, &nbit, lsp_pred_vq_bits(i)); } decode_lsps_vq(lsp_indexes, &lsps[3][0], LPC_ORD); check_lsp_order(&lsps[3][0], LPC_ORD); bw_expand_lsps(&lsps[3][0], LPC_ORD); /* interpolate ------------------------------------------------*/ /* Wo and energy are sampled every 20ms, so we interpolate just 1 10ms frame between 20ms samples */ interp_Wo(&model[0], &c2->prev_model_dec, &model[1]); e[0] = interp_energy(c2->prev_e_dec, e[1]); interp_Wo(&model[2], &model[1], &model[3]); e[2] = interp_energy(e[1], e[3]); /* LSPs are sampled every 40ms so we interpolate the 3 frames in between, then recover spectral amplitudes */ for(i=0, weight=0.25; i<3; i++, weight += 0.25) { interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight); } for(i=0; i<4; i++) { lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma); apply_lpc_correction(&model[i]); } /* synthesise ------------------------------------------------*/ for(i=0; i<4; i++) synthesise_one_frame(c2, &speech[N*i], &model[i], &ak[i][0]); /* update memories for next frame ----------------------------*/ c2->prev_model_dec = model[3]; c2->prev_e_dec = e[3]; for(i=0; i<LPC_ORD; i++) c2->prev_lsps_dec[i] = lsps[3][i]; }
void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits, float ber_est) { MODEL model[4]; int lsp_indexes[LPC_ORD]; float lsps[4][LPC_ORD]; int Wo_index, e_index; float e[4]; float snr; float ak[4][LPC_ORD+1]; int i,j; unsigned int nbit = 0; float weight; COMP Aw[FFT_ENC]; PROFILE_VAR(recover_start); assert(c2 != NULL); /* only need to zero these out due to (unused) snr calculation */ for(i=0; i<4; i++) for(j=1; j<=MAX_AMP; j++) model[i].A[j] = 0.0; /* unpack bits from channel ------------------------------------*/ /* this will partially fill the model params for the 4 x 10ms frames */ model[0].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray); model[1].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray); model[2].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray); model[3].voiced = unpack_natural_or_gray(bits, &nbit, 1, c2->gray); Wo_index = unpack_natural_or_gray(bits, &nbit, WO_BITS, c2->gray); model[3].Wo = decode_Wo(Wo_index); model[3].L = PI/model[3].Wo; e_index = unpack_natural_or_gray(bits, &nbit, E_BITS, c2->gray); e[3] = decode_energy(e_index); for(i=0; i<LSP_SCALAR_INDEXES; i++) { lsp_indexes[i] = unpack_natural_or_gray(bits, &nbit, lsp_bits(i), c2->gray); } decode_lsps_scalar(&lsps[3][0], lsp_indexes, LPC_ORD); check_lsp_order(&lsps[3][0], LPC_ORD); bw_expand_lsps(&lsps[3][0], LPC_ORD, 50.0, 100.0); if (ber_est > 0.15) { model[0].voiced = model[1].voiced = model[2].voiced = model[3].voiced = 0; e[3] = decode_energy(10); bw_expand_lsps(&lsps[3][0], LPC_ORD, 200.0, 200.0); fprintf(stderr, "soft mute\n"); } /* interpolate ------------------------------------------------*/ /* Wo, energy, and LSPs are sampled every 40ms so we interpolate the 3 frames in between */ PROFILE_SAMPLE(recover_start); for(i=0, weight=0.25; i<3; i++, weight += 0.25) { interpolate_lsp_ver2(&lsps[i][0], c2->prev_lsps_dec, &lsps[3][0], weight); interp_Wo2(&model[i], &c2->prev_model_dec, &model[3], weight); e[i] = interp_energy2(c2->prev_e_dec, e[3],weight); } /* then recover spectral amplitudes */ for(i=0; i<4; i++) { lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma, Aw); apply_lpc_correction(&model[i]); synthesise_one_frame(c2, &speech[N*i], &model[i], Aw); } PROFILE_SAMPLE_AND_LOG2(recover_start, " recover"); #ifdef DUMP dump_lsp_(&lsps[3][0]); dump_ak_(&ak[3][0], LPC_ORD); #endif /* update memories for next frame ----------------------------*/ c2->prev_model_dec = model[3]; c2->prev_e_dec = e[3]; for(i=0; i<LPC_ORD; i++) c2->prev_lsps_dec[i] = lsps[3][i]; }
void codec2_decode_3200(struct CODEC2 *c2, short speech[], const unsigned char * bits) { MODEL model[2]; int lspd_indexes[LPC_ORD]; float lsps[2][LPC_ORD]; int Wo_index, e_index; float e[2]; float snr; float ak[2][LPC_ORD+1]; int i,j; unsigned int nbit = 0; assert(c2 != NULL); /* only need to zero these out due to (unused) snr calculation */ for(i=0; i<2; i++) for(j=1; j<=MAX_AMP; j++) model[i].A[j] = 0.0; /* unpack bits from channel ------------------------------------*/ /* this will partially fill the model params for the 2 x 10ms frames */ model[0].voiced = unpack(bits, &nbit, 1); model[1].voiced = unpack(bits, &nbit, 1); Wo_index = unpack(bits, &nbit, WO_BITS); model[1].Wo = decode_Wo(Wo_index); model[1].L = PI/model[1].Wo; e_index = unpack(bits, &nbit, E_BITS); e[1] = decode_energy(e_index); for(i=0; i<LSPD_SCALAR_INDEXES; i++) { lspd_indexes[i] = unpack(bits, &nbit, lspd_bits(i)); } decode_lspds_scalar(&lsps[1][0], lspd_indexes, LPC_ORD); /* interpolate ------------------------------------------------*/ /* Wo and energy are sampled every 20ms, so we interpolate just 1 10ms frame between 20ms samples */ interp_Wo(&model[0], &c2->prev_model_dec, &model[1]); e[0] = interp_energy(c2->prev_e_dec, e[1]); /* LSPs are sampled every 20ms so we interpolate the frame in between, then recover spectral amplitudes */ interpolate_lsp_ver2(&lsps[0][0], c2->prev_lsps_dec, &lsps[1][0], 0.5); for(i=0; i<2; i++) { lsp_to_lpc(&lsps[i][0], &ak[i][0], LPC_ORD); aks_to_M2(c2->fft_fwd_cfg, &ak[i][0], LPC_ORD, &model[i], e[i], &snr, 0, 0, c2->lpc_pf, c2->bass_boost, c2->beta, c2->gamma); apply_lpc_correction(&model[i]); } /* synthesise ------------------------------------------------*/ for(i=0; i<2; i++) synthesise_one_frame(c2, &speech[N*i], &model[i], &ak[i][0]); /* update memories for next frame ----------------------------*/ c2->prev_model_dec = model[1]; c2->prev_e_dec = e[1]; for(i=0; i<LPC_ORD; i++) c2->prev_lsps_dec[i] = lsps[1][i]; }
void codec2_decode(void *codec2_state, short speech[], const unsigned char * bits) { CODEC2 *c2; MODEL model; int voiced1, voiced2; int lsp_indexes[LPC_ORD]; float lsps[LPC_ORD]; int energy_index; float energy; int Wo_index; float ak[LPC_ORD+1]; float ak_interp[LPC_ORD+1]; int i; unsigned int nbit = 0; MODEL model_interp; assert(codec2_state != NULL); c2 = (CODEC2*)codec2_state; /* unpack bit stream to integer codes */ Wo_index = unpack(bits, &nbit, WO_BITS); for(i=0; i<LPC_ORD; i++) { lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i)); } energy_index = unpack(bits, &nbit, E_BITS); voiced1 = unpack(bits, &nbit, 1); voiced2 = unpack(bits, &nbit, 1); assert(nbit == CODEC2_BITS_PER_FRAME); /* decode integer codes to model parameters */ model.Wo = decode_Wo(Wo_index); model.L = PI/model.Wo; memset(&model.A, 0, (model.L+1)*sizeof(model.A[0])); decode_amplitudes(&model, ak, lsp_indexes, energy_index, lsps, &energy); model.voiced = voiced2; model_interp.voiced = voiced1; model_interp.Wo = P_MAX/2; memset(&model_interp.A, 0, MAX_AMP*sizeof(model_interp.A[0])); /* interpolate middle frame's model parameters for adjacent frames */ interpolate_lsp(&model_interp, &c2->prev_model, &model, c2->prev_lsps, c2->prev_energy, lsps, energy, ak_interp); apply_lpc_correction(&model_interp); /* synthesis two 10ms frames */ synthesise_one_frame(c2, speech, &model_interp, ak_interp); synthesise_one_frame(c2, &speech[N], &model, ak); /* update memories (decode states) for next time */ memcpy(&c2->prev_model, &model, sizeof(MODEL)); memcpy(c2->prev_lsps, lsps, sizeof(lsps)); c2->prev_energy = energy; }