FILE *fout;
short *buf;
unsigned char *bits;
- int nsam, nbit, nbyte, i, byte, frames, bit_errors, error_mode;
+ int nsam, nbit, nbyte, i, byte, frames, bits_proc, bit_errors, error_mode;
int nstart_bit, nend_bit, bit_rate;
int state, next_state;
float ber, r, burst_length, burst_period, burst_timer;
+ unsigned char mask;
- if (argc < 4) {
- printf("basic usage.................: c2dec 3200|2400|1600|1400|1200 InputBitFile OutputRawSpeechFile\n");
- printf("uniform errors usage........: c2dec 3200|2400|1600|1400|1200 InputBitFile OutputRawSpeechFile uniformBER startBit endBit\n");
- printf("uniform error on range usage: c2dec 3200|2400|1600|1400|1200 InputBitFile OutputRawSpeechFile uniformBER\n");
- printf("two state fading usage......: c2dec 3200|2400|1600|1400|1200 InputBitFile OutputRawSpeechFile burstLength burstPeriod\n");
+ if ((argc != 4) && (argc != 5) && (argc != 6) && (argc != 7)) {
+ printf("basic usage.................: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile\n");
+ printf("uniform errors usage........: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile uniformBER startBit endBit smoothingFlag\n");
+ printf("uniform error on range usage: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile uniformBER\n");
+ printf("two state fading usage......: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile burstLength burstPeriod\n");
printf("e.g c2dec 1400 hts1a.c2 hts1a_1400.raw\n");
printf("e.g c2dec 1400 hts1a.c2 hts1a_1400.raw 0.9\n");
printf("e.g c2dec 1400 hts1a.c2 hts1a_1400.raw 0.99 0.9\n");
mode = CODEC2_MODE_1600;
else if (strcmp(argv[1],"1400") == 0)
mode = CODEC2_MODE_1400;
+ else if (strcmp(argv[1],"1300") == 0)
+ mode = CODEC2_MODE_1300;
else if (strcmp(argv[1],"1200") == 0)
mode = CODEC2_MODE_1200;
else {
- fprintf(stderr, "Error in mode: %s. Must be 3200, 2400, 1600, 1400 or 1200\n", argv[1]);
+ fprintf(stderr, "Error in mode: %s. Must be 3200, 2400, 1600, 1400, 1300 or 1200\n", argv[1]);
exit(1);
}
bit_rate = atoi(argv[1]);
buf = (short*)malloc(nsam*sizeof(short));
nbyte = (nbit + 7) / 8;
bits = (unsigned char*)malloc(nbyte*sizeof(char));
- frames = bit_errors = 0;
+ frames = bit_errors = bits_proc = 0;
nstart_bit = 0;
nend_bit = nbit-1;
nend_bit = 2;
state = 0;
}
-
+
if (argc == 7) {
error_mode = UNIFORM_RANGE;
ber = atof(argv[4]);
r = (float)rand()/RAND_MAX;
if (r < ber) {
byte = i/8;
- //printf("nbyte %d nbit %d i %d byte %d\n", nbyte, nbit, i, byte);
- bits[byte] ^= 1 << (7 - i - byte*8);
+ //printf("nbyte %d nbit %d i %d byte %d bits[%d] 0x%0x ", nbyte, nbit, i, byte, byte, bits[byte]);
+ mask = 1 << (7 - i + byte*8);
+ bits[byte] ^= mask;
+ //printf("shift: %d mask: 0x%0x bits[%d] 0x%0x\n", 7 - i + byte*8, mask, byte, bits[byte] );
bit_errors++;
- }
+ }
+ bits_proc++;
}
}
r = (float)rand()/RAND_MAX;
if (r < 0.5) {
byte = i/8;
- bits[byte] ^= 1 << (7 - i - byte*8);
+ bits[byte] ^= 1 << (7 - i + byte*8);
bit_errors++;
}
+ bits_proc++;
}
if (burst_timer > burst_period) {
}
if (error_mode)
- fprintf(stderr, "actual BER: %1.3f\n", (float)bit_errors/(frames*nbit));
+ fprintf(stderr, "actual BER: %1.3f\n", (float)bit_errors/bits_proc);
codec2_destroy(codec2);
void codec2_decode_1600(struct CODEC2 *c2, short speech[], const unsigned char * bits);
void codec2_encode_1400(struct CODEC2 *c2, unsigned char * bits, short speech[]);
void codec2_decode_1400(struct CODEC2 *c2, short speech[], const unsigned char * bits);
+void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[]);
+void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits);
void codec2_encode_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]);
void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits);
static void ear_protection(float in_out[], int n);
(mode == CODEC2_MODE_2400) ||
(mode == CODEC2_MODE_1600) ||
(mode == CODEC2_MODE_1400) ||
+ (mode == CODEC2_MODE_1300) ||
(mode == CODEC2_MODE_1200)
);
c2->mode = mode;
c2->xq_enc[0] = c2->xq_enc[1] = 0.0;
c2->xq_dec[0] = c2->xq_dec[1] = 0.0;
+ c2->smoothing = 0;
+
return c2;
}
return 64;
if (c2->mode == CODEC2_MODE_1400)
return 56;
+ if (c2->mode == CODEC2_MODE_1300)
+ return 52;
if (c2->mode == CODEC2_MODE_1200)
return 48;
return 320;
if (c2->mode == CODEC2_MODE_1400)
return 320;
+ if (c2->mode == CODEC2_MODE_1300)
+ return 320;
if (c2->mode == CODEC2_MODE_1200)
return 320;
(c2->mode == CODEC2_MODE_2400) ||
(c2->mode == CODEC2_MODE_1600) ||
(c2->mode == CODEC2_MODE_1400) ||
+ (c2->mode == CODEC2_MODE_1300) ||
(c2->mode == CODEC2_MODE_1200)
);
codec2_encode_1600(c2, bits, speech);
if (c2->mode == CODEC2_MODE_1400)
codec2_encode_1400(c2, bits, speech);
+ if (c2->mode == CODEC2_MODE_1300)
+ codec2_encode_1300(c2, bits, speech);
if (c2->mode == CODEC2_MODE_1200)
codec2_encode_1200(c2, bits, speech);
}
(c2->mode == CODEC2_MODE_2400) ||
(c2->mode == CODEC2_MODE_1600) ||
(c2->mode == CODEC2_MODE_1400) ||
+ (c2->mode == CODEC2_MODE_1300) ||
(c2->mode == CODEC2_MODE_1200)
);
codec2_decode_1600(c2, speech, bits);
if (c2->mode == CODEC2_MODE_1400)
codec2_decode_1400(c2, speech, bits);
+ if (c2->mode == CODEC2_MODE_1300)
+ codec2_decode_1300(c2, speech, bits);
if (c2->mode == CODEC2_MODE_1200)
codec2_decode_1200(c2, speech, bits);
}
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 */
}
+/*---------------------------------------------------------------------------*\
+
+ FUNCTION....: codec2_encode_1300
+ AUTHOR......: David Rowe
+ DATE CREATED: March 14 2013
+
+ Encodes 320 speech samples (40ms of speech) into 52 bits.
+
+ The codec2 algorithm actually operates internally on 10ms (80
+ sample) frames, so we run the encoding algorithm 4 times:
+
+ frame 0: voicing bit
+ frame 1: voicing bit,
+ frame 2: voicing bit
+ frame 3: voicing bit, Wo and E, scalar LSPs
+
+ The bit allocation is:
+
+ Parameter frame 2 frame 4 Total
+ -------------------------------------------------------
+ Harmonic magnitudes (LSPs) 0 36 36
+ Pitch (Wo) 0 7 7
+ Energy 0 5 5
+ Voicing (10ms update) 2 2 4
+ TOTAL 2 50 52
+
+\*---------------------------------------------------------------------------*/
+
+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;
+
+ 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 ---------------------------------------------*/
+
+ analyse_one_frame(c2, &model, &speech[N]);
+ pack(bits, &nbit, model.voiced, 1);
+
+ /* 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));
+}
+
+
+/*---------------------------------------------------------------------------*\
+
+ FUNCTION....: codec2_decode_1300
+ AUTHOR......: David Rowe
+ DATE CREATED: 11 May 2012
+
+ Decodes frames of 52 bits into 320 samples (40ms) of speech.
+
+\*---------------------------------------------------------------------------*/
+
+void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char * bits)
+{
+ 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;
+
+ 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);
+ model[2].voiced = unpack(bits, &nbit, 1);
+ model[3].voiced = unpack(bits, &nbit, 1);
+
+ Wo_index = unpack(bits, &nbit, WO_BITS);
+ model[3].Wo = decode_Wo(Wo_index);
+ model[3].L = PI/model[3].Wo;
+
+ e_index = unpack(bits, &nbit, E_BITS);
+ e[3] = decode_energy(e_index);
+
+ for(i=0; i<LSP_SCALAR_INDEXES; i++) {
+ lsp_indexes[i] = unpack(bits, &nbit, lsp_bits(i));
+ }
+ 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);
+
+ /* interpolate ------------------------------------------------*/
+
+ /* Wo, energy, and LSPs are sampled every 40ms so we interpolate
+ the 3 frames in between */
+
+ 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);
+ 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];
+
+}
+
/*---------------------------------------------------------------------------*\
return -1;
}
-
-