modified c2dec for better cmd line option handling, compiles OK, but not tested
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 25 Apr 2014 02:15:52 +0000 (02:15 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Fri, 25 Apr 2014 02:15:52 +0000 (02:15 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1559 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/c2dec.c
codec2-dev/src/c2demo.c
codec2-dev/src/c2sim.c
codec2-dev/src/codec2.c
codec2-dev/src/codec2.h
codec2-dev/src/codec2_internal.h
codec2-dev/src/dump.c
codec2-dev/src/dump.h
codec2-dev/src/nlp.c
codec2-dev/src/pack.c
codec2-dev/src/quantise.h

index 7e3026477980ac101706d3e4540b0d6bd1b94aab..b63c69219883c97f2e011b8c3791861c55761a43 100644 (file)
 */
 
 #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;
@@ -52,19 +56,22 @@ int main(int argc, char *argv[])
     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)
@@ -113,31 +120,45 @@ int main(int argc, char *argv[])
     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);
@@ -210,14 +231,8 @@ int main(int argc, char *argv[])
         }
         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
@@ -239,3 +254,29 @@ int main(int argc, char *argv[])
 
     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);
+}
index 84308fd441718255f9fee19aecdd8eef7621c2c3..7014baa83800525a89efdf650003ca98c64c7feb 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "codec2.h"
 #include "sine.h"
+#include "dump.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -85,7 +86,7 @@ int main(int argc, char *argv[])
 
     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);
     }
 
index 750b55d60076f97b2cab19772067ad2d86091e33..9b3b8208f14e913b51f3b03189ed82f4661ce361 100644 (file)
@@ -587,13 +587,19 @@ int main(int argc, char *argv[])
                        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);
            }
index 5bc3be6e9c6b866150af0a9b9ff0ee60f2470ccd..395d83d4fa165fa1e99ed15edcc6d20c0162f0bf 100644 (file)
@@ -138,6 +138,8 @@ struct CODEC2 * CODEC2_WIN32SUPPORT codec2_create(int mode)
        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;
@@ -248,7 +250,12 @@ void CODEC2_WIN32SUPPORT codec2_encode(struct CODEC2 *c2, unsigned char *bits, s
        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(
@@ -978,36 +985,36 @@ void codec2_encode_1300(struct CODEC2 *c2, unsigned char * bits, short speech[])
     /* 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"); 
@@ -1054,20 +1061,20 @@ void codec2_decode_1300(struct CODEC2 *c2, short speech[], const unsigned char *
     /* 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);
@@ -1519,3 +1526,10 @@ int CODEC2_WIN32SUPPORT codec2_rebuild_spare_bit(struct CODEC2 *c2, int unpacked
 
     return -1;
 }
+
+void CODEC2_WIN32SUPPORT codec2_set_natural_or_gray(struct CODEC2 *c2, int gray)
+{
+    assert(c2 != NULL);
+    c2->gray = gray;
+}
+
index 6e82f8f20d6c489acceb3cabacfaef3fac3c9fad..ea007065092a55e5be28a3ca03afbec5d06d6ddf 100644 (file)
@@ -58,13 +58,15 @@ struct CODEC2;
 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
 
index b9efcc4cf213e69e35db5dcea3c4f0a24220f6be..aaaf0baa74b6f2bb0dfb21451943111e3185041a 100644 (file)
@@ -38,6 +38,7 @@ struct CODEC2 {
     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                 */
index db4b1cc95f966f546087de35652f9575534ce276..00ce80626ff243424927b55f05d66db920371b16 100644 (file)
@@ -54,6 +54,7 @@ static FILE *frw = NULL;
 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;
@@ -101,6 +102,8 @@ void dump_off(){
        fclose(fweights);
     if (flsp_ != NULL)
        fclose(flsp_);
+    if (fmel != NULL)
+       fclose(fmel);
     if (fphase != NULL)
        fclose(fphase);
     if (fphase_ != NULL)
@@ -453,6 +456,23 @@ void dump_lsp_(float lsp_[]) {
     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];
index 3f26b266777f31ffb0118f77aef9ea779a2427a0..a7138301751039b8452a9e81ab12e44829d8dcc6 100644 (file)
@@ -49,6 +49,7 @@ void dump_Rw(float Rw[]);
 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);
index ea88e45c251e2417ce34e448377485191953bd37..7d842e536c3dd12d55329b53004e443937c7e96d 100644 (file)
@@ -53,7 +53,7 @@
 #define CNLP        0.3                /* post processor constant              */
 #define NLP_NTAP 48            /* Decimation LPF order */
 
-#undef DUMP
+//#undef DUMP
 
 /*---------------------------------------------------------------------------*\
                                                                             
index 51b3c4ae5254ea22f719efb9e997d083c939ea38..b0625645016fdba7c18bfad3d3e25f7da07f1b1a 100644 (file)
@@ -52,16 +52,16 @@ pack(
  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) {
@@ -95,7 +95,7 @@ unpack(
  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
index 74359eb2a999c2840e6c5511b5e76404511923c2..007d34d6f115b49e968b33e58afcc115ae1b8904 100644 (file)
@@ -96,9 +96,9 @@ int encode_energy(float e);
 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);