OPAL  Version 3.14.3
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: 30051 $
26  * $Author: rjongbloed $
27  * $Date: 2013-06-25 16:11:08 +1000 (Tue, 25 Jun 2013) $
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_config.h>
38 #include <rtp/rtp.h>
39 
40 
42 
43 class OpalSilenceDetector : public PObject
44 {
45  PCLASSINFO(OpalSilenceDetector, PObject);
46  public:
48  NoSilenceDetection,
49  FixedSilenceDetection,
50  AdaptiveSilenceDetection
51  );
52  typedef Modes Mode; // Backward compatibility
53 
54  struct Params {
56  Modes mode = AdaptiveSilenceDetection,
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 
69  PString AsString() const;
70  void FromString(const PString & str);
71 
72  Modes m_mode;
73  unsigned m_threshold;
74  unsigned m_signalDeadband;
75  unsigned m_silenceDeadband;
76  unsigned m_adaptivePeriod;
77  };
78 
84  const Params & newParam
85  );
87 
90  const PNotifier & GetReceiveHandler() const { return receiveHandler; }
91 
99  void SetParameters(
100  const Params & params,
101  const int clockRate = 0
102  );
103 
106  void GetParameters(
107  Params & params
108  );
109 
115  void SetClockRate(
116  unsigned clockRate
117  );
118 
121  unsigned GetClockRate() const { return clockRate; }
122 
131  Mode GetStatus(
132  PBoolean * isInTalkBurst,
133  unsigned * currentThreshold
134  ) const;
135 
144  virtual unsigned GetAverageSignalLevel(
145  const BYTE * buffer,
146  PINDEX size
147  ) = 0;
148 
149  private:
152  void AdaptiveReset();
153 
154  protected:
156 
157  PNotifier receiveHandler;
158 
160  unsigned signalDeadband; // #samples of signal needed
161  unsigned silenceDeadband; // #samples of silence needed
162  unsigned adaptivePeriod; // #samples window for adaptive threshold
163  unsigned clockRate; // audio sampling rate
164 
165  unsigned lastTimestamp; // Last timestamp received
166  unsigned receivedTime; // Signal/Silence duration received so far.
167  unsigned levelThreshold; // Threshold level for silence/signal
168  unsigned signalMinimum; // Minimum of frames above threshold
169  unsigned silenceMaximum; // Maximum of frames below threshold
170  unsigned signalReceivedTime; // Duration of signal received
171  unsigned silenceReceivedTime; // Duration of silence received
172  bool inTalkBurst; // Currently sending RTP data
173  PMutex inUse; // Protects values to allow change while running
174 };
175 
176 
178 {
180  public:
184  const Params & newParam
185  ) : OpalSilenceDetector(newParam) { }
186 
197  virtual unsigned GetAverageSignalLevel(
198  const BYTE * buffer,
199  PINDEX size
200  );
202 };
203 
204 
205 extern ostream & operator<<(ostream & strm, OpalSilenceDetector::Mode mode);
206 
207 
208 #endif // OPAL_CODEC_SILENCEDETECT_H
209 
210