OPAL  Version 3.10.10
silencedetect.h
Go to the documentation of this file.
1 /*
2  * silencedetect.h
3  *
4  * Open Phone Abstraction Library (OPAL)
5  * Formally known as the Open H323 project.
6  *
7  * Copyright (c) 2004 Post Increment
8  *
9  * The contents of this file are subject to the Mozilla Public License
10  * Version 1.0 (the "License"); you may not use this file except in
11  * compliance with the License. You may obtain a copy of the License at
12  * http://www.mozilla.org/MPL/
13  *
14  * Software distributed under the License is distributed on an "AS IS"
15  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
16  * the License for the specific language governing rights and limitations
17  * under the License.
18  *
19  * The Original Code is Open Phone Abstraction Library.
20  *
21  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
22  *
23  * Contributor(s): ______________________________________.
24  *
25  * $Revision: 24320 $
26  * $Author: csoutheren $
27  * $Date: 2010-05-06 10:20:51 -0500 (Thu, 06 May 2010) $
28  */
29 
30 #ifndef OPAL_CODEC_SILENCEDETECT_H
31 #define OPAL_CODEC_SILENCEDETECT_H
32 
33 #ifdef P_USE_PRAGMA
34 #pragma interface
35 #endif
36 
37 #include <opal/buildopts.h>
38 #include <rtp/rtp.h>
39 
40 
42 
43 class OpalSilenceDetector : public PObject
44 {
45  PCLASSINFO(OpalSilenceDetector, PObject);
46  public:
47  enum Mode {
52  };
53 
54  struct Params {
57  unsigned threshold = 0,
58  unsigned signalDeadband = 10,
59  unsigned silenceDeadband = 400,
60  unsigned adaptivePeriod = 600
61  )
62  : m_mode(mode),
63  m_threshold(threshold),
67  { }
68 
70  unsigned m_threshold;
71  unsigned m_signalDeadband;
72  unsigned m_silenceDeadband;
73  unsigned m_adaptivePeriod;
74  };
75 
81  const Params & newParam
82  );
84 
87  const PNotifier & GetReceiveHandler() const { return receiveHandler; }
88 
96  void SetParameters(
97  const Params & params,
98  const int clockRate = 0
99  );
100 
106  void SetClockRate(
107  const int clockRate
108  );
109 
112  int GetClockRate() const { return clockRate; }
113 
122  Mode GetStatus(
123  PBoolean * isInTalkBurst,
124  unsigned * currentThreshold
125  ) const;
126 
135  virtual unsigned GetAverageSignalLevel(
136  const BYTE * buffer,
137  PINDEX size
138  ) = 0;
139 
140  private:
143  void AdaptiveReset();
144 
145  protected:
147 
148  PNotifier receiveHandler;
149 
151  unsigned signalDeadband; // #samples of signal needed
152  unsigned silenceDeadband; // #samples of silence needed
153  unsigned adaptivePeriod; // #samples window for adaptive threshold
154  int clockRate; // audio sampling rate
155 
156  unsigned lastTimestamp; // Last timestamp received
157  unsigned receivedTime; // Signal/Silence duration received so far.
158  unsigned levelThreshold; // Threshold level for silence/signal
159  unsigned signalMinimum; // Minimum of frames above threshold
160  unsigned silenceMaximum; // Maximum of frames below threshold
161  unsigned signalReceivedTime; // Duration of signal received
162  unsigned silenceReceivedTime; // Duration of silence received
163  bool inTalkBurst; // Currently sending RTP data
164  PMutex inUse; // Protects values to allow change while running
165 };
166 
167 
169 {
171  public:
175  const Params & newParam
176  ) : OpalSilenceDetector(newParam) { }
177 
188  virtual unsigned GetAverageSignalLevel(
189  const BYTE * buffer,
190  PINDEX size
191  );
193 };
194 
195 
196 extern ostream & operator<<(ostream & strm, OpalSilenceDetector::Mode mode);
197 
198 
199 #endif // OPAL_CODEC_SILENCEDETECT_H
200 
201