tracking down some subtle differences between c2sim and enc/dec, making progres at...
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sat, 19 Nov 2011 22:47:17 +0000 (22:47 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Sat, 19 Nov 2011 22:47:17 +0000 (22:47 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@308 01035d8c-6547-0410-b346-abe4f91aad63

codec2-dev/src/c2dec.c
codec2-dev/src/c2enc.c
codec2-dev/src/c2sim.c
codec2-dev/src/codec2.c

index bf3499e4a15c8bb24cf065541ed641fbf4d9caab..d8b4e3c3e17dc67aceb6518ec57273e6e323b274 100644 (file)
@@ -44,30 +44,30 @@ int main(int argc, char *argv[])
     unsigned char *bits;
     int            nsam, nbit;
 
-    if (argc != 3) {
-       printf("usage: %s 2500|1400 InputBitFile OutputRawSpeechFile\n", argv[0]);
-       printf("e.g    %s 1400 hts1a.c2 hts1a_1400.raw\n", argv[0]);
+    if (argc != 4) {
+       printf("usage: c2dec 2500|1400 InputBitFile OutputRawSpeechFile\n");
+       printf("e.g    c2dec 1400 hts1a.c2 hts1a_1400.raw\n");
        exit(1);
     }
 
     if (strcmp(argv[1],"1400") == 0)
-       mode = CODEC2_MODE_2500;
-    else if (strcmp(argv[1],"2500") == 0)
        mode = CODEC2_MODE_1400;
+    else if (strcmp(argv[1],"2500") == 0)
+       mode = CODEC2_MODE_2500;
     else {
        fprintf(stderr, "Error in mode: %s.  Must be 2500 or 1400\n", argv[1]);
        exit(1);
     }
-
+    printf("mode: %d\n", mode);
     if (strcmp(argv[2], "-")  == 0) fin = stdin;
-    else if ( (fin = fopen(argv[1],"rb")) == NULL ) {
+    else if ( (fin = fopen(argv[2],"rb")) == NULL ) {
        fprintf(stderr, "Error opening input bit file: %s: %s.\n",
          argv[1], strerror(errno));
        exit(1);
     }
 
     if (strcmp(argv[3], "-") == 0) fout = stdout;
-    else if ( (fout = fopen(argv[2],"wb")) == NULL ) {
+    else if ( (fout = fopen(argv[3],"wb")) == NULL ) {
        fprintf(stderr, "Error opening output speech file: %s: %s.\n",
          argv[2], strerror(errno));
        exit(1);
@@ -77,7 +77,7 @@ int main(int argc, char *argv[])
     nsam = codec2_samples_per_frame(codec2);
     nbit = codec2_bits_per_frame(codec2);
     buf = (short*)malloc(nsam*sizeof(short));
-    bits = (char*)malloc(nbit*sizeof(char));
+    bits = (unsigned char*)malloc(nbit*sizeof(char));
 
     while(fread(bits, sizeof(char), nbit, fin) == nbit) {
        codec2_decode(codec2, buf, bits);
index 5ca503f8e108e5f6c2e6722dda23e547decbf35b..361f34c4d2d5ec56bd6a864a57fb7fd07b4e5bdb 100644 (file)
@@ -45,30 +45,30 @@ int main(int argc, char *argv[])
     unsigned char *bits;
     int            nsam, nbit;
 
-    if (argc != 3) {
-       printf("usage: %s 2500|1400 InputRawspeechFile OutputBitFile\n", argv[0]);
-       printf("e.g    %s 1400 ../raw/hts1a.raw hts1a.c2\n", argv[0]);
+    if (argc != 4) {
+       printf("usage: c2enc 2500|1400 InputRawspeechFile OutputBitFile\n");
+       printf("e.g    c2enc 1400 ../raw/hts1a.raw hts1a.c2\n");
        exit(1);
     }
  
     if (strcmp(argv[1],"1400") == 0)
-       mode = CODEC2_MODE_2500;
-    else if (strcmp(argv[1],"2500") == 0)
        mode = CODEC2_MODE_1400;
+    else if (strcmp(argv[1],"2500") == 0)
+       mode = CODEC2_MODE_2500;
     else {
        fprintf(stderr, "Error in mode: %s.  Must be 2500 or 1400\n", argv[1]);
        exit(1);
     }
 
     if (strcmp(argv[2], "-")  == 0) fin = stdin;
-    else if ( (fin = fopen(argv[1],"rb")) == NULL ) {
+    else if ( (fin = fopen(argv[2],"rb")) == NULL ) {
        fprintf(stderr, "Error opening input bit file: %s: %s.\n",
          argv[1], strerror(errno));
        exit(1);
     }
 
     if (strcmp(argv[3], "-") == 0) fout = stdout;
-    else if ( (fout = fopen(argv[2],"wb")) == NULL ) {
+    else if ( (fout = fopen(argv[3],"wb")) == NULL ) {
        fprintf(stderr, "Error opening output speech file: %s: %s.\n",
          argv[2], strerror(errno));
        exit(1);
@@ -78,7 +78,7 @@ int main(int argc, char *argv[])
     nsam = codec2_samples_per_frame(codec2);
     nbit = codec2_bits_per_frame(codec2);
     buf = (short*)malloc(nsam*sizeof(short));
-    bits = (char*)malloc(nbit*sizeof(char));
+    bits = (unsigned char*)malloc(nbit*sizeof(char));
 
     while(fread(buf, sizeof(short), nsam, fin) == nsam) {
        codec2_encode(codec2, bits, buf);
index a6eed4ae10d0d5ddc36702b631077bc5ac6b61c0..b03f2029f10df2ea551eaa38e42ddddbff6568f3 100644 (file)
@@ -103,6 +103,8 @@ int main(int argc, char *argv[])
     float lsps__prev2[LPC_ORD];
     float e, prev_e;
     float ak_interp[LPC_MAX];
+    int   lsp_indexes[LPC_MAX];
+    float lsps_[LPC_ORD];
 
     void *nlp_states;
     float hpf_states[2];
@@ -273,7 +275,7 @@ int main(int argc, char *argv[])
     sum_snr = 0;
     while(fread(buf,sizeof(short),N,fin)) {
        frames++;
-       //printf("frame: %d", frames);
+       printf("frame: %d", frames);
 
        /* Read input speech */
 
@@ -340,8 +342,6 @@ int main(int argc, char *argv[])
        /* optional LPC model amplitudes and LSP quantisation -----------------*/
 
        if (lpc_model) {
-           int   lsp_indexes[LPC_MAX];
-           float lsps_[LPC_ORD];
 
            e = speech_to_uq_lsps(lsps, ak, Sn, w, order);
 
@@ -372,9 +372,9 @@ int main(int argc, char *argv[])
                lsp_to_lpc(lsps_, ak, LPC_ORD);
            }
 
-           /* we need lsp__prev[] for lspdt.  If no other
-              LSP quantisation is used we use original LSPs 
-              as there is no quantised version available. */
+           /* we need lsp__prev[] for lspdt and decimate.  If no
+              other LSP quantisation is used we use original LSPs as
+              there is no quantised version available. */
 
            if (!lsp && !lspd && !lspvq && !lspres)
                for(i=0; i<LPC_ORD; i++)
@@ -407,14 +407,14 @@ int main(int argc, char *argv[])
               of 4) by quantising the difference between the 1st
               frames LSPs and the 3rd frames:
 
-              10ms, frame 1: send "full" LSP frame
-              20ms, frame 2: discard (interpolate at decoder)
-              30ms, frame 3: send LSPs differences between frame 3 and frame 1
-              40ms, frame 4: discard (interpolate at decoder)
+              10ms, frame 1: discard (interpolate at decoder)
+              20ms, frame 2: send "full" LSP frame
+              30ms, frame 3: discard (interpolate at decoder)
+              40ms, frame 4: send LSPs differences between frame 4 and frame 2
            */
 
            if (lspdt && decimate) {
-               if ((frames%4) == 3) {
+               if ((frames%4) == 0) {
                    lspdt_quantise(lsps, lsps_, lsps__prev2, lspdt_mode);
                    bw_expand_lsps(lsps_, LPC_ORD);
                    lsp_to_lpc(lsps_, ak, LPC_ORD);
@@ -435,16 +435,25 @@ int main(int argc, char *argv[])
 
            e = decode_energy(encode_energy(e));
 
-           if (!decimate && dt && (frames % 2)) {
-               model.Wo = decode_Wo_dt(encode_Wo_dt(model.Wo, prev_Wo),prev_Wo);
-               model.L  = PI/model.Wo;
+           if (!decimate) {
+               /* we send paams every 10ms, delta-time every 20ms */
+               if (dt && (frames % 2)) 
+                   model.Wo = decode_Wo_dt(encode_Wo_dt(model.Wo, prev_Wo),prev_Wo);
+               else
+                   model.Wo = decode_Wo(encode_Wo(model.Wo));
            }
 
-           if (decimate && dt && ((frames % 4) == 3)) {
-               model.Wo = decode_Wo_dt(encode_Wo_dt(model.Wo, prev__Wo),prev__Wo);
-               model.L  = PI/model.Wo;
-               //printf("dWo");
+           if (decimate) {
+               /* we send params every 20ms */
+               if (dt && ((frames % 4) == 3)) {
+                   /* delta-time every 40ms */
+                   model.Wo = decode_Wo_dt(encode_Wo_dt(model.Wo, prev__Wo),prev__Wo);
+               }
+               else
+                   model.Wo = decode_Wo(encode_Wo(model.Wo));              
            }
+
+           model.L  = PI/model.Wo; /* if we quantise Wo re-compute L */
           
            if (!decimate && !dt)
                model.Wo = decode_Wo(encode_Wo(model.Wo));
@@ -489,16 +498,24 @@ int main(int argc, char *argv[])
            /* 
               Each 20ms we synthesise two 10ms frames:
 
-              frame 1: interpolate frame 0 LSPs from frame -1 and frame 1
-                       synthesise frame 0 and frame 1 speech
-              frame 2: discard except for voicing bit
-              frame 3: interpolate frame 2 LSPs from frame 1 and frame 3
-                       synthesise frame 2 and frame 3 speech
-              frame 4: discard except for voicing bit
+              frame 1: discard except for voicing bit
+              frame 2: interpolate frame 1 LSPs from frame 2 and frame 0
+                       synthesise frame 1 and frame 2 speech
+              frame 3: discard except for voicing bit
+              frame 4: interpolate frame 3 LSPs from frame 4 and frame 2
+                       synthesise frame 3 and frame 4 speech
            */
 
-           if (frames%2) {
-               //printf("interp\n");
+           if ((frames%2) == 0) {
+               printf("interp\n");
+               printf("Wo: %1.5f  L: %d e: %3.2f \n", model.Wo, model.L, e);
+               for(i=0; i<LPC_ORD; i++)
+                   printf("lsp_indexes: %d lsps_: %2.3f prev_lsps_: %2.3f\n", 
+                          lsp_indexes[i], lsps_[i], prev_lsps[i]);
+               printf("ak: ");
+               for(i=0; i<LPC_ORD; i++)
+                   printf("%2.3f  ", ak[i]);
+               printf("\n");
 
                /* decode interpolated frame */
 
@@ -508,10 +525,17 @@ int main(int argc, char *argv[])
                interpolate(&interp_model, &prev_model, &model);
 #else
                interpolate_lsp(&interp_model, &prev_model, &model,
-                               prev_lsps, prev_e, lsps, e, ak_interp);
+                               prev_lsps, prev_e, lsps_, e, ak_interp);
                apply_lpc_correction(&interp_model);
 #endif
-           
+               printf("Wo: %1.5f  L: %d  prev_e: %3.2f\n", 
+                      interp_model.Wo, interp_model.L, prev_e);
+               printf("ak_interp: ");
+               for(i=0; i<LPC_ORD; i++)
+                   printf("%2.3f  ", ak_interp[i]);
+               printf("\n");
+               //if (frames==40) 
+               //    exit(0);
                if (phase0)
                    phase_synth_zero_order(&interp_model, ak_interp, ex_phase,
                                           order);      
@@ -535,7 +559,7 @@ int main(int argc, char *argv[])
 
                prev_model = model;
                for(i=0; i<LPC_ORD; i++)
-                   prev_lsps[i] = lsps[i];
+                   prev_lsps[i] = lsps_[i];
                prev_e = e;
            }
            else {
index 48c0f27f2ef7e921638ca330da526260edce183f..b973fce818c5dbff67748b17b73e97c084e19af8 100644 (file)
@@ -273,6 +273,9 @@ void codec2_encode_2500(struct CODEC2 *c2, unsigned char * bits, short speech[])
     e = speech_to_uq_lsps(lsps, ak, c2->Sn, c2->w, LPC_ORD);
     encode_lsps_scalar(lsp_indexes, lsps, LPC_ORD);
     energy_index = encode_energy(e);
+    //for(i=0; i<LPC_ORD; i++)
+    // printf("lsp_indexes: %d lsps: %2.3f\n", lsp_indexes[i], lsps[i]);
+    //exit(0);
 
     pack(bits, &nbit, Wo_index, WO_BITS);
     for(i=0; i<LSP_SCALAR_INDEXES; i++) {
@@ -311,9 +314,11 @@ void codec2_decode_2500(struct CODEC2 *c2, short speech[], const unsigned char *
     int     i;
     unsigned int nbit = 0;
     MODEL   model_interp;
+    static  int frames;
 
+    printf("frame: %d\n", frames+=2);
     assert(c2 != NULL);
-
+    
     /* unpack bit stream to integer codes */
 
     Wo_index = unpack(bits, &nbit, WO_BITS);
@@ -340,6 +345,15 @@ void codec2_decode_2500(struct CODEC2 *c2, short speech[], const unsigned char *
     aks_to_M2(ak, LPC_ORD, &model, energy, &snr, 1); 
     apply_lpc_correction(&model);
 
+    printf("Wo: %1.5f  L: %d e: %3.2f \n", model.Wo, model.L, energy);
+    for(i=0; i<LPC_ORD; i++)
+       printf("lsp_indexes: %d lsp_: %2.3f prev_lsp_: %2.3f\n", 
+              lsp_indexes[i], lsps_[i], c2->prev_lsps_[i]);
+    printf("ak: ");
+    for(i=0; i<LPC_ORD; i++)
+       printf("%2.3f  ", ak[i]);
+    printf("\n");
+    
     /* interpolate odd frame model parameters from adjacent frames */
 
     model.voiced = voiced2;
@@ -350,6 +364,14 @@ void codec2_decode_2500(struct CODEC2 *c2, short speech[], const unsigned char *
     interpolate_lsp(&model_interp, &c2->prev_model, &model,
                    c2->prev_lsps_, c2->prev_energy, lsps_, energy, ak_interp);
     apply_lpc_correction(&model_interp);
+    printf("Wo: %1.5f  L: %d  prev_e: %3.2f\n", 
+          model_interp.Wo, model_interp.L, c2->prev_energy );
+    printf("ak_interp: ");
+    for(i=0; i<LPC_ORD; i++)
+       printf("%2.3f  ", ak_interp[i]);
+    printf("\n");
+    //if (frames ==40)
+    // exit(0);
 
     /* synthesise two 10ms frames */