*/
#include "codec2.h"
+#include "dump.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <getopt.h>
#define NONE 0 /* no bit errors */
#define UNIFORM 1 /* random bit errors */
#define TWO_STATE 2 /* Two state error model */
#define UNIFORM_RANGE 3 /* random bit errors over a certain range */
+void print_help(const struct option *long_options, int num_opts, char* argv[]);
+
int main(int argc, char *argv[])
{
int mode;
int state, next_state;
float ber, r, burst_length, burst_period, burst_timer, ber_est;
unsigned char mask;
-
- 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\n");
- printf("uniform error on range usage: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile uniformBER\n");
- printf("demod BER estimate..........: c2dec 3200|2400|1600|1400|1300|1200 InputBitFile OutputRawSpeechFile BERfile\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");
- exit(1);
- }
-
+ int natural, dump;
+
+ char* opt_string = ":";
+ struct option long_options[] = {
+ { "ber", required_argument, NULL, 0 },
+ { "range", required_argument, NULL, 0 },
+ { "berfile", required_argument, NULL, 0 },
+ { "natural", no_argument, &natural, 1 },
+ #ifdef DUMP
+ { "dump", required_argument, &dump, 1 },
+ #endif
+ { "help", no_argument, NULL, 'h' },
+ { NULL, no_argument, NULL, 0 }
+ };
+ int num_opts=sizeof(long_options)/sizeof(struct option);
+
if (strcmp(argv[1],"3200") == 0)
mode = CODEC2_MODE_3200;
else if (strcmp(argv[1],"2400") == 0)
nstart_bit = 0;
nend_bit = nbit-1;
- if (argc == 5) {
- /* see if 4th argument is a valid file name */
- if ( (fber = fopen(argv[4],"rb")) == NULL ) {
- /* otherwise it must be BER value for uniform errors */
- ber = atof(argv[4]);
- error_mode = UNIFORM;
- }
- }
+ while(1) {
+ int option_index = 0;
+ int opt = getopt_long(argc, argv, opt_string,
+ long_options, &option_index);
+ if (opt == -1)
+ break;
+
+ switch (opt) {
+ case 0:
+ if(strcmp(long_options[option_index].name, "ber") == 0) {
+ ber = atof(optarg);
+ error_mode = UNIFORM;
+ } else if(strcmp(long_options[option_index].name, "range") == 0) {
+ ber = atof(optarg);
+ nstart_bit = atoi(optarg+1);
+ nend_bit = atoi(optarg+2);
+ error_mode = UNIFORM_RANGE;
+ } else if(strcmp(long_options[option_index].name, "berfile") == 0) {
+ if ((fber = fopen(optarg,"wt")) == NULL) {
+ fprintf(stderr, "Error opening BER file: %s %s.\n",
+ optarg, strerror(errno));
+ exit(1);
+ }
+ #ifdef DUMP
+ } else if(strcmp(long_options[option_index].name, "dump") == 0) {
+ if (dump)
+ dump_on(optarg);
+ #endif
+ break;
+ }
- if (argc == 6) {
- error_mode = TWO_STATE;
- burst_length = atof(argv[4]);
- burst_period = atof(argv[5]);
- nstart_bit = 0;
- nend_bit = 2;
- state = 0;
- }
-
- if (argc == 7) {
- error_mode = UNIFORM_RANGE;
- ber = atof(argv[4]);
- nstart_bit = atoi(argv[5]);
- nend_bit = atoi(argv[6]);
- fprintf(stderr, "ber: %f nstart_bit: %d nend_bit: %d\n", ber, nstart_bit, nend_bit);
- state = 0;
+ case 'h':
+ print_help(long_options, num_opts, argv);
+ break;
+
+ default:
+ /* This will never be reached */
+ break;
+ }
}
assert(nend_bit <= nbit);
}
else
ber_est = 0.0;
-
- /* frame repeat logic */
- if (ber_est > 0.15) {
- //memcpy(bits, prev_bits, nbyte);
- // fprintf(stderr, "repeat\n");
- }
- codec2_decode(codec2, buf, bits, ber_est);
+ codec2_decode_ber(codec2, buf, bits, ber_est);
fwrite(buf, sizeof(short), nsam, fout);
//if this is in a pipeline, we probably don't want the usual
//buffering to occur
return 0;
}
+
+void print_help(const struct option* long_options, int num_opts, char* argv[])
+{
+ int i;
+ char *option_parameters;
+ fprintf(stderr, "\nc2dec - Codec 2 decoder and bit error simulation program\n"
+ "usage: %s 3200|2400|1400}1300|1200 InputFile OutputRawFile [OPTIONS]\n\n"
+ "Options:\n", argv[0]);
+ for(i=0; i<num_opts-1; i++) {
+ if(long_options[i].has_arg == no_argument) {
+ option_parameters="";
+ } else if (strcmp("ber", long_options[i].name) == 0) {
+ option_parameters = " BER";
+ } else if (strcmp("range", long_options[i].name) == 0) {
+ option_parameters = " BER startBit EndBit";
+ } else if (strcmp("berfile", long_options[i].name) == 0) {
+ option_parameters = " berFileName";
+ } else if (strcmp("dump", long_options[i].name) == 0) {
+ option_parameters = " dumpFilePrefix";
+ } else {
+ option_parameters = " <UNDOCUMENTED parameter>";
+ }
+ fprintf(stderr, "\t--%s%s\n", long_options[i].name, option_parameters);
+ }
+ exit(1);
+}
#include "codec2.h"
#include "sine.h"
+#include "dump.h"
#include <stdio.h>
#include <stdlib.h>
while(fread(buf, sizeof(short), nsam, fin) == (size_t)nsam) {
codec2_encode(codec2, bits, buf);
- codec2_decode(codec2, buf, bits, 0.0);
+ codec2_decode(codec2, buf, bits);
fwrite(buf, sizeof(short), nsam, fout);
}
mel[i]++;
}
+ #ifdef DUMP
+ dump_mel(mel);
+ #endif
+
for(i=0; i<LPC_ORD; i++) {
f_ = 700.0*( pow(10.0, (float)mel[i]/100.0) - 1.0);
lsps_[i] = f_*(PI/4000.0);
}
- for(i=5; i<10; i++) {
+ /*
+ for(i=5; i<10; i++) {
lsps_[i] = lsps[i];
}
+ */
lsp_to_lpc(lsps_, ak, LPC_ORD);
}
return NULL;
}
+ c2->gray = 1;
+
c2->lpc_pf = 1; c2->bass_boost = 1; c2->beta = LPCPF_BETA; c2->gamma = LPCPF_GAMMA;
c2->xq_enc[0] = c2->xq_enc[1] = 0.0;
codec2_encode_1200(c2, bits, speech);
}
-void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *c2, short speech[], const unsigned char *bits, float ber_est)
+void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *c2, short speech[], const unsigned char *bits)
+{
+ codec2_decode_ber(c2, speech, bits, 0.0);
+}
+
+void CODEC2_WIN32SUPPORT codec2_decode_ber(struct CODEC2 *c2, short speech[], const unsigned char *bits, float ber_est)
{
assert(c2 != NULL);
assert(
/* frame 1: - voicing ---------------------------------------------*/
analyse_one_frame(c2, &model, speech);
- pack(bits, &nbit, model.voiced, 1);
+ pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
/* frame 2: - voicing ---------------------------------------------*/
analyse_one_frame(c2, &model, &speech[N]);
- pack(bits, &nbit, model.voiced, 1);
+ pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
/* frame 3: - voicing ---------------------------------------------*/
analyse_one_frame(c2, &model, &speech[2*N]);
- pack(bits, &nbit, model.voiced, 1);
+ 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(bits, &nbit, model.voiced, 1);
+ pack_natural_or_gray(bits, &nbit, model.voiced, 1, c2->gray);
Wo_index = encode_Wo(model.Wo);
- pack(bits, &nbit, Wo_index, WO_BITS);
+ pack_natural_or_gray(bits, &nbit, Wo_index, WO_BITS, c2->gray);
#ifdef TIMER
quant_start = machdep_timer_sample();
#endif
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);
+ 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(bits, &nbit, lsp_indexes[i], lsp_bits(i));
+ pack_natural_or_gray(bits, &nbit, lsp_indexes[i], lsp_bits(i), c2->gray);
}
#ifdef TIMER
machdep_timer_sample_and_log(quant_start, " quant/packing");
/* 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);
+ 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(bits, &nbit, WO_BITS);
+ 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(bits, &nbit, E_BITS);
+ 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(bits, &nbit, lsp_bits(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);
return -1;
}
+
+void CODEC2_WIN32SUPPORT codec2_set_natural_or_gray(struct CODEC2 *c2, int gray)
+{
+ assert(c2 != NULL);
+ c2->gray = gray;
+}
+
struct CODEC2 * CODEC2_WIN32SUPPORT codec2_create(int mode);
void CODEC2_WIN32SUPPORT codec2_destroy(struct CODEC2 *codec2_state);
void CODEC2_WIN32SUPPORT codec2_encode(struct CODEC2 *codec2_state, unsigned char * bits, short speech_in[]);
- void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *codec2_state, short speech_out[], const unsigned char *bits, float ber_est);
+void CODEC2_WIN32SUPPORT codec2_decode(struct CODEC2 *codec2_state, short speech_out[], const unsigned char *bits);
+void CODEC2_WIN32SUPPORT codec2_decode_ber(struct CODEC2 *codec2_state, short speech_out[], const unsigned char *bits, float ber_est);
int CODEC2_WIN32SUPPORT codec2_samples_per_frame(struct CODEC2 *codec2_state);
int CODEC2_WIN32SUPPORT codec2_bits_per_frame(struct CODEC2 *codec2_state);
void CODEC2_WIN32SUPPORT codec2_set_lpc_post_filter(struct CODEC2 *codec2_state, int enable, int bass_boost, float beta, float gamma);
int CODEC2_WIN32SUPPORT codec2_get_spare_bit_index(struct CODEC2 *codec2_state);
int CODEC2_WIN32SUPPORT codec2_rebuild_spare_bit(struct CODEC2 *codec2_state, int unpacked_bits[]);
+void CODEC2_WIN32SUPPORT codec2_set_natural_or_gray(struct CODEC2 *codec2_state, int gray);
#endif
float Sn[M]; /* input speech */
float hpf_states[2]; /* high pass filter states */
void *nlp; /* pitch predictor states */
+ int gray; /* non-zero for gray encoding */
kiss_fft_cfg fft_inv_cfg; /* inverse FFT config */
float Sn_[2*N]; /* synthesised output speech */
static FILE *flsp = NULL;
static FILE *fweights = NULL;
static FILE *flsp_ = NULL;
+static FILE *fmel = NULL;
static FILE *fphase = NULL;
static FILE *fphase_ = NULL;
static FILE *ffw = NULL;
fclose(fweights);
if (flsp_ != NULL)
fclose(flsp_);
+ if (fmel != NULL)
+ fclose(fmel);
if (fphase != NULL)
fclose(fphase);
if (fphase_ != NULL)
fprintf(flsp_,"\n");
}
+void dump_mel(int mel[]) {
+ int i;
+ char s[MAX_STR];
+
+ if (!dumpon) return;
+
+ if (fmel == NULL) {
+ sprintf(s,"%s_mel.txt", prefix);
+ fmel = fopen(s, "wt");
+ assert(fmel != NULL);
+ }
+
+ for(i=0; i<10; i++)
+ fprintf(fmel,"%d\t",mel[i]);
+ fprintf(fmel,"\n");
+}
+
void dump_ak(float ak[], int order) {
int i;
char s[MAX_STR];
void dump_lsp(float lsp[]);
void dump_weights(float w[], int ndim);
void dump_lsp_(float lsp_[]);
+void dump_mel(int mel[]);
void dump_ak(float ak[], int order);
void dump_ak_(float ak[], int order);
void dump_E(float E);
#define CNLP 0.3 /* post processor constant */
#define NLP_NTAP 48 /* Decimation LPF order */
-#undef DUMP
+//#undef DUMP
/*---------------------------------------------------------------------------*\
unsigned int fieldWidth/* Width of the field in BITS, not bytes. */
)
{
- pack_natural_or_grey(bitArray, bitIndex, field, fieldWidth, 1);
+ pack_natural_or_gray(bitArray, bitIndex, field, fieldWidth, 1);
}
void
-pack_natural_or_grey(
+pack_natural_or_gray(
unsigned char * bitArray, /* The output bit string. */
unsigned int * bitIndex, /* Index into the string in BITS, not bytes.*/
int field, /* The bit field to be packed. */
unsigned int fieldWidth,/* Width of the field in BITS, not bytes. */
- int gray /* non-zero for gray coding */
+ unsigned int gray /* non-zero for gray coding */
)
{
if (gray) {
unsigned int fieldWidth/* Width of the field in BITS, not bytes. */
)
{
- unpack_natural_or_gray(bitArray, bitIndex, fieldWidth, 1);
+ return unpack_natural_or_gray(bitArray, bitIndex, fieldWidth, 1);
}
/** Unpack a field from a bit string, to binary, optionally using
float decode_energy(int index);
void pack(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits);
-void pack_natural_or_grey(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits, int gray);
+void pack_natural_or_gray(unsigned char * bits, unsigned int *nbit, int index, unsigned int index_bits, unsigned int gray);
int unpack(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits);
-int unpack_natural_or_grey(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits, int gray);
+int unpack_natural_or_gray(const unsigned char * bits, unsigned int *nbit, unsigned int index_bits, unsigned int gray);
int lsp_bits(int i);
int lspd_bits(int i);