void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[]) { MODEL model; float lsps[LPC_ORD]; float ak[LPC_ORD+1]; float e; int lsp_indexes[LPC_ORD]; int Wo_index, e_index; int i; unsigned int nbit = 0; #ifdef PROFILE unsigned int quant_start; #endif assert(c2 != NULL); memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); /* frame 1: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, speech); pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray); /* frame 2: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, &speech[N]); pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray); /* frame 3: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, &speech[2*N]); pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray); /* frame 4: - voicing, scalar Wo & E, scalar LSPs ------------------*/ analyse_one_frame(c2, &model, &speech[3*N]); pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray); Wo_index = encode_Wo(model.Wo); pack_natural_or_gray(bits, &nbit, Wo_index, WO_BITS, c2->gray); #ifdef PROFILE quant_start = machdep_profile_sample(); #endif e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); e_index = encode_energy(e); pack_natural_or_gray(bits, &nbit, e_index, E_BITS, c2->gray); encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); for(i=0; i<LSP_SCALAR_INDEXES; i++) { pack_natural_or_gray(bits, &nbit, lsp_indexes[i], lsp_bits(i), c2->gray); } #ifdef PROFILE machdep_profile_sample_and_log(quant_start, " quant/packing"); #endif assert(nbit == (unsigned)codec2_bits_per_frame(c2)); }
void codec2_encode_1600(struct CODEC2 *c2, unsigned char * bits, short speech[]) { MODEL model; float lsps[LPC_ORD]; float ak[LPC_ORD+1]; float e; int lsp_indexes[LPC_ORD]; int Wo_index, e_index; int i; unsigned int nbit = 0; assert(c2 != NULL); memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); /* frame 1: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, speech); pack(bits, &nbit, model.voiced, 1); /* frame 2: - voicing, scalar Wo & E -------------------------------*/ analyse_one_frame(c2, &model, &speech[N]); pack(bits, &nbit, model.voiced, 1); Wo_index = encode_Wo(model.Wo); pack(bits, &nbit, Wo_index, WO_BITS); /* need to run this just to get LPC energy */ e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); e_index = encode_energy(e); pack(bits, &nbit, e_index, E_BITS); /* frame 3: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, &speech[2*N]); pack(bits, &nbit, model.voiced, 1); /* frame 4: - voicing, scalar Wo & E, scalar LSPs ------------------*/ analyse_one_frame(c2, &model, &speech[3*N]); pack(bits, &nbit, model.voiced, 1); Wo_index = encode_Wo(model.Wo); pack(bits, &nbit, Wo_index, WO_BITS); e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); e_index = encode_energy(e); pack(bits, &nbit, e_index, E_BITS); encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); for(i=0; i<LSP_SCALAR_INDEXES; i++) { pack(bits, &nbit, lsp_indexes[i], lsp_bits(i)); } assert(nbit == (unsigned)codec2_bits_per_frame(c2)); }
void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]) { MODEL model; float lsps[LPC_ORD]; float lsps_[LPC_ORD]; float ak[LPC_ORD+1]; float e; int lsp_indexes[LPC_ORD]; int WoE_index; int i; int spare = 0; unsigned int nbit = 0; assert(c2 != NULL); memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); /* frame 1: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, speech); pack(bits, &nbit, model.voiced, 1); /* frame 2: - voicing, joint Wo & E -------------------------------*/ analyse_one_frame(c2, &model, &speech[N]); pack(bits, &nbit, model.voiced, 1); /* need to run this just to get LPC energy */ e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); WoE_index = encode_WoE(&model, e, c2->xq_enc); pack(bits, &nbit, WoE_index, WO_E_BITS); /* frame 3: - voicing ---------------------------------------------*/ analyse_one_frame(c2, &model, &speech[2*N]); pack(bits, &nbit, model.voiced, 1); /* frame 4: - voicing, joint Wo & E, scalar LSPs ------------------*/ analyse_one_frame(c2, &model, &speech[3*N]); pack(bits, &nbit, model.voiced, 1); e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); WoE_index = encode_WoE(&model, e, c2->xq_enc); pack(bits, &nbit, WoE_index, WO_E_BITS); encode_lsps_vq(lsp_indexes, lsps, lsps_, LPC_ORD); for(i=0; i<LSP_PRED_VQ_INDEXES; i++) { pack(bits, &nbit, lsp_indexes[i], lsp_pred_vq_bits(i)); } pack(bits, &nbit, spare, 1); assert(nbit == (unsigned)codec2_bits_per_frame(c2)); }
void codec2_encode(void *codec2_state, unsigned char * bits, short speech[]) { CODEC2 *c2; MODEL model; int voiced1, voiced2; int lsp_indexes[LPC_ORD]; int energy_index; int Wo_index; int i; unsigned int nbit = 0; assert(codec2_state != NULL); c2 = (CODEC2*)codec2_state; /* first 10ms analysis frame - we just want voicing */ analyse_one_frame(c2, &model, speech); voiced1 = model.voiced; /* second 10ms analysis frame */ analyse_one_frame(c2, &model, &speech[N]); voiced2 = model.voiced; Wo_index = encode_Wo(model.Wo); encode_amplitudes(lsp_indexes, &energy_index, &model, c2->Sn, c2->w); memset(bits, '\0', ((CODEC2_BITS_PER_FRAME + 7) / 8)); pack(bits, &nbit, Wo_index, WO_BITS); for(i=0; i<LPC_ORD; i++) { pack(bits, &nbit, lsp_indexes[i], lsp_bits(i)); } pack(bits, &nbit, energy_index, E_BITS); pack(bits, &nbit, voiced1, 1); pack(bits, &nbit, voiced2, 1); assert(nbit == CODEC2_BITS_PER_FRAME); }
void codec2_encode_2400(struct CODEC2 *c2, unsigned char * bits, short speech[]) { MODEL model; float ak[LPC_ORD+1]; float lsps[LPC_ORD]; float e; int WoE_index; int lsp_indexes[LPC_ORD]; int i; int spare = 0; unsigned int nbit = 0; assert(c2 != NULL); memset(bits, '\0', ((codec2_bits_per_frame(c2) + 7) / 8)); /* first 10ms analysis frame - we just want voicing */ analyse_one_frame(c2, &model, speech); pack(bits, &nbit, model.voiced, 1); /* second 10ms analysis frame */ analyse_one_frame(c2, &model, &speech[N]); pack(bits, &nbit, model.voiced, 1); e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD); WoE_index = encode_WoE(&model, e, c2->xq_enc); pack(bits, &nbit, WoE_index, WO_E_BITS); encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD); for(i=0; i<LSP_SCALAR_INDEXES; i++) { pack(bits, &nbit, lsp_indexes[i], lsp_bits(i)); } pack(bits, &nbit, spare, 2); assert(nbit == (unsigned)codec2_bits_per_frame(c2)); }
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; }