From 1571fd3e68f1f80883330e5531c23b93bb3c168b Mon Sep 17 00:00:00 2001 From: drowe67 Date: Wed, 7 Nov 2012 07:58:26 +0000 Subject: [PATCH] first pass at ear protection so that listeners ears are not hurt when there are bit errors or random data fed to the decoder git-svn-id: https://svn.code.sf.net/p/freetel/code@943 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/src/c2dec.c | 4 ++-- codec2-dev/src/codec2.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/codec2-dev/src/c2dec.c b/codec2-dev/src/c2dec.c index 0a5945e6..d425fdc1 100644 --- a/codec2-dev/src/c2dec.c +++ b/codec2-dev/src/c2dec.c @@ -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); diff --git a/codec2-dev/src/codec2.c b/codec2-dev/src/codec2.c index 89de671d..fd9a97e3 100644 --- a/codec2-dev/src/codec2.c +++ b/codec2-dev/src/codec2.c @@ -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; iSn_[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 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