first pass at ear protection so that listeners ears are not hurt when there are bit...
authordrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 7 Nov 2012 07:58:26 +0000 (07:58 +0000)
committerdrowe67 <drowe67@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 7 Nov 2012 07:58:26 +0000 (07:58 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@943 01035d8c-6547-0410-b346-abe4f91aad63

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

index 0a5945e6e2d49baa1484abb167b28e0354f5646f..d425fdc102452db0ae4481c96a85f0c07e1b9b11 100644 (file)
@@ -44,7 +44,7 @@ int main(int argc, char *argv[])
     float          ber, r;
 
     if (argc < 4) {
-       printf("usage: c2dec 3200|2400|1400|1200 InputBitFile OutputRawSpeechFile\n");
+       printf("usage: c2dec 3200|2400|1400|1200 InputBitFile OutputRawSpeechFile [ber]\n");
        printf("e.g    c2dec 1400 hts1a.c2 hts1a_1400.raw\n");
        exit(1);
     }
@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
     }
 
     if (ber != 0.0)
-       printf("actual BER: %1.3f\n", (float)bit_errors/(frames*nbit));
+       fprintf(stderr, "actual BER: %1.3f\n", (float)bit_errors/(frames*nbit));
 
     codec2_destroy(codec2);
 
index 89de671d5739a58c34839db0a210de5a70b248a4..fd9a97e387b7c9d9cbf4e23671e141e218285b20 100644 (file)
@@ -62,6 +62,7 @@ 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_1200(struct CODEC2 *c2, unsigned char * bits, short speech[]);
 void codec2_decode_1200(struct CODEC2 *c2, short speech[], const unsigned char * bits);
+void ear_protection(float in_out[], int n);
 
 /*---------------------------------------------------------------------------*\
                                                        
@@ -902,6 +903,7 @@ void synthesise_one_frame(struct CODEC2 *c2, short speech[], MODEL *model, float
     phase_synth_zero_order(c2->fft_fwd_cfg, model, ak, &c2->ex_phase, LPC_ORD);
     postfilter(model, &c2->bg_est);
     synthesise(c2->fft_inv_cfg, c2->Sn_, model, c2->Pn, 1);
+    ear_protection(c2->Sn_, N);
 
     for(i=0; i<N; i++) {
        if (c2->Sn_[i] > 32767.0)
@@ -957,3 +959,42 @@ void analyse_one_frame(struct CODEC2 *c2, MODEL *model, short speech[])
     //    snr, model->voiced, model->Wo, c2->prev_Wo_enc);
     c2->prev_Wo_enc = model->Wo;
 }
+
+/*---------------------------------------------------------------------------*\
+                                                       
+  FUNCTION....: ear_protection()   
+  AUTHOR......: David Rowe                           
+  DATE CREATED: Nov 7 2012
+
+  Limits output level to protect ears when there are bit errors or the input
+  is overdriven.  This doesn't correct or mask bit erros, just reduces the
+  worst of their damage.
+
+\*---------------------------------------------------------------------------*/
+
+void ear_protection(float in_out[], int n) {
+    float max_sample, over, gain;
+    int   i;
+
+    /* find maximum sample in frame */
+
+    max_sample = 0.0;
+    for(i=0; i<n; i++)
+        if (in_out[i] > max_sample)
+            max_sample = in_out[i];
+
+    /* determine how far above set point */
+
+    over = max_sample/30000.0;
+
+    /* If we are x dB over set point we reduce level by 2x dB, this
+       attenuates major excursions in amplitude (likely to be caused
+       by bit errors) more than smaller ones */
+
+    if (over > 1.0) {
+        gain = 1.0/(over*over);
+        fprintf(stderr, "gain: %f\n", gain);
+        for(i=0; i<n; i++)
+            in_out[i] *= gain;
+    }
+}