123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- /*************************************************************************
- *
- * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
- * R99 Version 3.3.0
- * REL-4 Version 4.1.0
- *
- ********************************************************************************
- *
- * File : bgnscd.c
- * Purpose : Background noise source charateristic detector (SCD)
- *
- ********************************************************************************
- */
- /*
- ********************************************************************************
- * MODULE INCLUDE FILE AND VERSION ID
- ********************************************************************************
- */
- #include "bgnscd.h"
- const char bgnscd_id[] = "@(#)$Id $" bgnscd_h;
- #include <stdlib.h>
- #include <stdio.h>
- #include "typedef.h"
- #include "basic_op.h"
- #include "count.h"
- #include "cnst.h"
- #include "copy.h"
- #include "set_zero.h"
- #include "gmed_n.h"
- #include "sqrt_l.h"
- /*
- ********************************************************************************
- * LOCAL VARIABLES AND TABLES
- ********************************************************************************
- */
- /*-----------------------------------------------------------------*
- * Decoder constant parameters (defined in "cnst.h") *
- *-----------------------------------------------------------------*
- * L_FRAME : Frame size. *
- * L_SUBFR : Sub-frame size. *
- *-----------------------------------------------------------------*/
- /*
- ********************************************************************************
- * PUBLIC PROGRAM CODE
- ********************************************************************************
- */
- /*
- **************************************************************************
- *
- * Function : Bgn_scd_init
- * Purpose : Allocates and initializes state memory
- *
- **************************************************************************
- */
- Word16 Bgn_scd_init (Bgn_scdState **state)
- {
- Bgn_scdState* s;
-
- if (state == (Bgn_scdState **) NULL){
- wfprintf(stderr, "Bgn_scd_init: invalid parameter\n");
- return -1;
- }
- *state = NULL;
-
- /* allocate memory */
- if ((s= (Bgn_scdState *) wmalloc(sizeof(Bgn_scdState))) == NULL){
- wfprintf(stderr, "Bgn_scd_init: can not malloc state structure\n");
- return -1;
- }
-
- Bgn_scd_reset(s);
- *state = s;
-
- return 0;
- }
- /*
- **************************************************************************
- *
- * Function : Bgn_scd_reset
- * Purpose : Resets state memory
- *
- **************************************************************************
- */
- Word16 Bgn_scd_reset (Bgn_scdState *state)
- {
- if (state == (Bgn_scdState *) NULL){
- wfprintf(stderr, "Bgn_scd_reset: invalid parameter\n");
- return -1;
- }
- /* Static vectors to zero */
- Set_zero (state->frameEnergyHist, L_ENERGYHIST);
- /* Initialize hangover handling */
- state->bgHangover = 0;
-
- return 0;
- }
- /*
- **************************************************************************
- *
- * Function : Bgn_scd_exit
- * Purpose : The memory used for state memory is freed
- *
- **************************************************************************
- */
- void Bgn_scd_exit (Bgn_scdState **state)
- {
- if (state == NULL || *state == NULL)
- return;
- /* deallocate memory */
- wfree(*state);
- *state = NULL;
-
- return;
- }
- /*
- **************************************************************************
- *
- * Function : Bgn_scd
- * Purpose : Charaterice synthesis speech and detect background noise
- * Returns : background noise decision; 0 = no bgn, 1 = bgn
- *
- **************************************************************************
- */
- Word16 Bgn_scd (Bgn_scdState *st, /* i : State variables for bgn SCD */
- Word16 ltpGainHist[], /* i : LTP gain history */
- Word16 speech[], /* o : synthesis speech frame */
- Word16 *voicedHangover /* o : # of frames after last
- voiced frame */
- )
- {
- Word16 i;
- Word16 prevVoiced, inbgNoise;
- Word16 temp;
- Word16 ltpLimit, frameEnergyMin;
- Word16 currEnergy, noiseFloor, maxEnergy, maxEnergyLastPart;
- Word32 s;
-
- /* Update the inBackgroundNoise flag (valid for use in next frame if BFI) */
- /* it now works as a energy detector floating on top */
- /* not as good as a VAD. */
- currEnergy = 0; move16 ();
- s = (Word32) 0; move32 ();
- for (i = 0; i < L_FRAME; i++)
- {
- s = L_mac_ex (s, speech[i], speech[i]);
- }
- s = L_shl_ex(s, 2);
- currEnergy = extract_h_ex (s);
- frameEnergyMin = 32767; move16 ();
- for (i = 0; i < L_ENERGYHIST; i++)
- {
- test ();
- if (sub_ex(st->frameEnergyHist[i], frameEnergyMin) < 0)
- frameEnergyMin = st->frameEnergyHist[i]; move16 ();
- }
- noiseFloor = shl_ex (frameEnergyMin, 4); /* Frame Energy Margin of 16 */
- maxEnergy = st->frameEnergyHist[0]; move16 ();
- for (i = 1; i < L_ENERGYHIST-4; i++)
- {
- test ();
- if ( sub_ex (maxEnergy, st->frameEnergyHist[i]) < 0)
- {
- maxEnergy = st->frameEnergyHist[i]; move16 ();
- }
- }
-
- maxEnergyLastPart = st->frameEnergyHist[2*L_ENERGYHIST/3]; move16 ();
- for (i = 2*L_ENERGYHIST/3+1; i < L_ENERGYHIST; i++)
- {
- test ();
- if ( sub_ex (maxEnergyLastPart, st->frameEnergyHist[i] ) < 0)
- {
- maxEnergyLastPart = st->frameEnergyHist[i]; move16 ();
- }
- }
- inbgNoise = 0; /* false */ move16 ();
- /* Do not consider silence as noise */
- /* Do not consider continuous high volume as noise */
- /* Or if the current noise level is very low */
- /* Mark as noise if under current noise limit */
- /* OR if the maximum energy is below the upper limit */
- test (); test (); test (); test (); test ();
- if ( (sub_ex(maxEnergy, LOWERNOISELIMIT) > 0) &&
- (sub_ex(currEnergy, FRAMEENERGYLIMIT) < 0) &&
- (sub_ex(currEnergy, LOWERNOISELIMIT) > 0) &&
- ( (sub_ex(currEnergy, noiseFloor) < 0) ||
- (sub_ex(maxEnergyLastPart, UPPERNOISELIMIT) < 0)))
- {
- test ();
- if (sub_ex(add_ex(st->bgHangover, 1), 30) > 0)
- {
- st->bgHangover = 30; move16 ();
- } else
- {
- st->bgHangover = add_ex(st->bgHangover, 1);
- }
- }
- else
- {
- st->bgHangover = 0; move16 ();
- }
-
- /* make final decision about frame state , act somewhat cautiosly */
- test ();
- if (sub_ex(st->bgHangover,1) > 0)
- inbgNoise = 1; /* true */ move16 ();
- for (i = 0; i < L_ENERGYHIST-1; i++)
- {
- st->frameEnergyHist[i] = st->frameEnergyHist[i+1]; move16 ();
- }
- st->frameEnergyHist[L_ENERGYHIST-1] = currEnergy; move16 ();
-
- /* prepare for voicing decision; tighten the threshold after some
- time in noise */
- ltpLimit = 13926; /* 0.85 Q14 */ move16 ();
- test ();
- if (sub_ex(st->bgHangover, 8) > 0)
- {
- ltpLimit = 15565; /* 0.95 Q14 */ move16 ();
- }
- test ();
- if (sub_ex(st->bgHangover, 15) > 0)
- {
- ltpLimit = 16383; /* 1.00 Q14 */ move16 ();
- }
- /* weak sort of voicing indication. */
- prevVoiced = 0; /* false */ move16 ();
- test ();
- if (sub_ex(gmed_n(<pGainHist[4], 5), ltpLimit) > 0)
- {
- prevVoiced = 1; /* true */ move16 ();
- }
- test ();
- if (sub_ex(st->bgHangover, 20) > 0) {
- if (sub_ex(gmed_n(ltpGainHist, 9), ltpLimit) > 0)
- {
- prevVoiced = 1; /* true */ move16 ();
- }
- else
- {
- prevVoiced = 0; /* false */ move16 ();
- }
- }
-
- test ();
- if (prevVoiced)
- {
- *voicedHangover = 0; move16 ();
- }
- else
- {
- temp = add_ex(*voicedHangover, 1);
- test ();
- if (sub_ex(temp, 10) > 0)
- {
- *voicedHangover = 10; move16 ();
- }
- else
- {
- *voicedHangover = temp; move16 ();
- }
- }
- return inbgNoise;
- }
|