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_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]; }
float run_a_test(char raw_file_name[], int bit_to_corrupt) { FILE *fin; short buf[N]; struct CODEC2 *c2; kiss_fft_cfg fft_fwd_cfg; MODEL model; float ak[LPC_ORD+1]; float lsps[LPC_ORD], e; int lsp_indexes[LPC_ORD], found_bit; float snr, snr_sum; int frames, i, mask, index; c2 = codec2_create(CODEC2_MODE_2400); fft_fwd_cfg = kiss_fft_alloc(FFT_ENC, 0, NULL, NULL); fin = fopen(raw_file_name, "rb"); assert(fin != NULL); /* find bit we are corrupting */ found_bit = 0; for(i=0; i<LSP_SCALAR_INDEXES; i++) { if (!found_bit) { if (bit_to_corrupt > lsp_bits(i)) bit_to_corrupt -= lsp_bits(i); else { index = i; mask = (1 << bit_to_corrupt); printf(" index: %d bit: %d mask: 0x%x ", index, bit_to_corrupt, mask); found_bit = 1; } } } assert(found_bit == 1); /* OK test a sample file, flipping bit */ snr_sum = 0.0; frames = 0; while(fread(buf, sizeof(short), N, fin) == N) { analyse_one_frame(c2, &model, buf); e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); /* find and flip bit we are testing */ lsp_indexes[index] ^= mask; /* decode LSPs and measure SNR */ decode_lsps_scalar(lsps, lsp_indexes, LPC_ORD); check_lsp_order(lsps, LPC_ORD); bw_expand_lsps(lsps, LPC_ORD); lsp_to_lpc(lsps, ak, LPC_ORD); aks_to_M2(fft_fwd_cfg, ak, LPC_ORD, &model, e, &snr, 0, 0, 1, 1, LPCPF_BETA, LPCPF_GAMMA); snr_sum += snr; frames++; } codec2_destroy(c2); fclose(fin); return snr_sum/frames; }