Linux Audio

Check our new training course

Loading...
v6.13.7
 1/* SPDX-License-Identifier: GPL-2.0-or-later */
 2/*
 3 * SpanDSP - a series of DSP components for telephony
 4 *
 5 * ec_disable_detector.h - A detector which should eventually meet the
 6 *                         G.164/G.165 requirements for detecting the
 7 *                         2100Hz echo cancellor disable tone.
 8 *
 9 * Written by Steve Underwood <steveu@coppice.org>
10 *
11 * Copyright (C) 2001 Steve Underwood
12 *
13 * All rights reserved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14 */
15
16#include "dsp_biquad.h"
17
18struct ec_disable_detector_state {
19	struct biquad2_state notch;
20	int notch_level;
21	int channel_level;
22	int tone_present;
23	int tone_cycle_duration;
24	int good_cycles;
25	int hit;
26};
27
28
29#define FALSE 0
30#define TRUE (!FALSE)
31
32static inline void
33echo_can_disable_detector_init(struct ec_disable_detector_state *det)
34{
35	/* Elliptic notch */
36	/* This is actually centred at 2095Hz, but gets the balance we want, due
37	   to the asymmetric walls of the notch */
38	biquad2_init(&det->notch,
39		     (int32_t)(-0.7600000 * 32768.0),
40		     (int32_t)(-0.1183852 * 32768.0),
41		     (int32_t)(-0.5104039 * 32768.0),
42		     (int32_t)(0.1567596 * 32768.0),
43		     (int32_t)(1.0000000 * 32768.0));
44
45	det->channel_level = 0;
46	det->notch_level = 0;
47	det->tone_present = FALSE;
48	det->tone_cycle_duration = 0;
49	det->good_cycles = 0;
50	det->hit = 0;
51}
52/*- End of function --------------------------------------------------------*/
53
54static inline int
55echo_can_disable_detector_update(struct ec_disable_detector_state *det,
56				 int16_t amp)
57{
58	int16_t notched;
59
60	notched = biquad2(&det->notch, amp);
61	/* Estimate the overall energy in the channel, and the energy in
62	   the notch (i.e. overall channel energy - tone energy => noise).
63	   Use abs instead of multiply for speed (is it really faster?).
64	   Damp the overall energy a little more for a stable result.
65	   Damp the notch energy a little less, so we don't damp out the
66	   blip every time the phase reverses */
67	det->channel_level += ((abs(amp) - det->channel_level) >> 5);
68	det->notch_level += ((abs(notched) - det->notch_level) >> 4);
69	if (det->channel_level > 280) {
70		/* There is adequate energy in the channel.
71		   Is it mostly at 2100Hz? */
72		if (det->notch_level * 6 < det->channel_level) {
73			/* The notch says yes, so we have the tone. */
74			if (!det->tone_present) {
75				/* Do we get a kick every 450+-25ms? */
76				if (det->tone_cycle_duration >= 425 * 8
77				    && det->tone_cycle_duration <= 475 * 8) {
78					det->good_cycles++;
79					if (det->good_cycles > 2)
80						det->hit = TRUE;
81				}
82				det->tone_cycle_duration = 0;
83			}
84			det->tone_present = TRUE;
85		} else
86			det->tone_present = FALSE;
87		det->tone_cycle_duration++;
88	} else {
89		det->tone_present = FALSE;
90		det->tone_cycle_duration = 0;
91		det->good_cycles = 0;
92	}
93	return det->hit;
94}
95/*- End of function --------------------------------------------------------*/
96/*- End of file ------------------------------------------------------------*/
v4.6
 
  1/*
  2 * SpanDSP - a series of DSP components for telephony
  3 *
  4 * ec_disable_detector.h - A detector which should eventually meet the
  5 *                         G.164/G.165 requirements for detecting the
  6 *                         2100Hz echo cancellor disable tone.
  7 *
  8 * Written by Steve Underwood <steveu@coppice.org>
  9 *
 10 * Copyright (C) 2001 Steve Underwood
 11 *
 12 * All rights reserved.
 13 *
 14 * This program is free software; you can redistribute it and/or modify
 15 * it under the terms of the GNU General Public License as published by
 16 * the Free Software Foundation; either version 2 of the License, or
 17 * (at your option) any later version.
 18 *
 19 * This program is distributed in the hope that it will be useful,
 20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 22 * GNU General Public License for more details.
 23 *
 24 * You should have received a copy of the GNU General Public License
 25 * along with this program; if not, write to the Free Software
 26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 27 *
 28 */
 29
 30#include "dsp_biquad.h"
 31
 32struct ec_disable_detector_state {
 33	struct biquad2_state notch;
 34	int notch_level;
 35	int channel_level;
 36	int tone_present;
 37	int tone_cycle_duration;
 38	int good_cycles;
 39	int hit;
 40};
 41
 42
 43#define FALSE 0
 44#define TRUE (!FALSE)
 45
 46static inline void
 47echo_can_disable_detector_init(struct ec_disable_detector_state *det)
 48{
 49	/* Elliptic notch */
 50	/* This is actually centred at 2095Hz, but gets the balance we want, due
 51	   to the asymmetric walls of the notch */
 52	biquad2_init(&det->notch,
 53		     (int32_t)(-0.7600000 * 32768.0),
 54		     (int32_t)(-0.1183852 * 32768.0),
 55		     (int32_t)(-0.5104039 * 32768.0),
 56		     (int32_t)(0.1567596 * 32768.0),
 57		     (int32_t)(1.0000000 * 32768.0));
 58
 59	det->channel_level = 0;
 60	det->notch_level = 0;
 61	det->tone_present = FALSE;
 62	det->tone_cycle_duration = 0;
 63	det->good_cycles = 0;
 64	det->hit = 0;
 65}
 66/*- End of function --------------------------------------------------------*/
 67
 68static inline int
 69echo_can_disable_detector_update(struct ec_disable_detector_state *det,
 70				 int16_t amp)
 71{
 72	int16_t notched;
 73
 74	notched = biquad2(&det->notch, amp);
 75	/* Estimate the overall energy in the channel, and the energy in
 76	   the notch (i.e. overall channel energy - tone energy => noise).
 77	   Use abs instead of multiply for speed (is it really faster?).
 78	   Damp the overall energy a little more for a stable result.
 79	   Damp the notch energy a little less, so we don't damp out the
 80	   blip every time the phase reverses */
 81	det->channel_level += ((abs(amp) - det->channel_level) >> 5);
 82	det->notch_level += ((abs(notched) - det->notch_level) >> 4);
 83	if (det->channel_level > 280) {
 84		/* There is adequate energy in the channel.
 85		   Is it mostly at 2100Hz? */
 86		if (det->notch_level * 6 < det->channel_level) {
 87			/* The notch says yes, so we have the tone. */
 88			if (!det->tone_present) {
 89				/* Do we get a kick every 450+-25ms? */
 90				if (det->tone_cycle_duration >= 425 * 8
 91				    && det->tone_cycle_duration <= 475 * 8) {
 92					det->good_cycles++;
 93					if (det->good_cycles > 2)
 94						det->hit = TRUE;
 95				}
 96				det->tone_cycle_duration = 0;
 97			}
 98			det->tone_present = TRUE;
 99		} else
100			det->tone_present = FALSE;
101		det->tone_cycle_duration++;
102	} else {
103		det->tone_present = FALSE;
104		det->tone_cycle_duration = 0;
105		det->good_cycles = 0;
106	}
107	return det->hit;
108}
109/*- End of function --------------------------------------------------------*/
110/*- End of file ------------------------------------------------------------*/