OPAL  Version 3.18.8
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 
26 #ifndef OPAL_CODEC_SILENCEDETECT_H
27 #define OPAL_CODEC_SILENCEDETECT_H
28 
29 #ifdef P_USE_PRAGMA
30 #pragma interface
31 #endif
32 
33 #include <opal_config.h>
34 #include <rtp/rtp.h>
35 
36 
38 
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),
64  m_signalDeadband(signalDeadband),
65  m_silenceDeadband(silenceDeadband),
66  m_adaptivePeriod(adaptivePeriod)
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 m_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 m_clockRate; }
122 
123  enum Result
124  {
128  };
129 
140  unsigned * currentThreshold = NULL,
141  unsigned * currentLevel = NULL
142  ) const;
143 
146  Result Detect(
147  const BYTE * audioPtr,
148  PINDEX audioLen,
149  unsigned timestamp
150  );
151 
160  virtual unsigned GetAverageSignalLevel(
161  const BYTE * buffer,
162  PINDEX size
163  ) = 0;
164 
173  static unsigned GetAverageSignalLevelPCM16(
174  const BYTE * buffer,
175  PINDEX size,
176  bool asPercentage
177  );
178 
179  private:
182  void AdaptiveReset();
183 
184  protected:
186 
187  PNotifier m_receiveHandler;
188 
190  unsigned m_signalDeadband; // #samples of signal needed
191  unsigned m_silenceDeadband; // #samples of silence needed
192  unsigned m_adaptivePeriod; // #samples window for adaptive threshold
193  unsigned m_clockRate; // audio sampling rate
194 
195  unsigned m_lastTimestamp; // Last timestamp received
196  unsigned m_receivedTime; // Signal/Silence duration received so far.
197  unsigned m_levelThreshold; // Threshold level for silence/signal
198  unsigned m_signalMinimum; // Minimum of frames above threshold
199  unsigned m_silenceMaximum; // Maximum of frames below threshold
200  unsigned m_signalReceivedTime; // Duration of signal received
201  unsigned m_silenceReceivedTime; // Duration of silence received
202  unsigned m_lastSignalLevel; // Energy level from last data frame
203  Result m_lastResult; // What it says
204  PDECLARE_MUTEX(m_inUse); // Protects values to allow change while running
205 };
206 
207 
209 {
211  public:
215  const Params & newParam
216  ) : OpalSilenceDetector(newParam) { }
217 
220  virtual unsigned GetAverageSignalLevel(
221  const BYTE * buffer,
222  PINDEX size
223  );
225 };
226 
227 
228 extern ostream & operator<<(ostream & strm, OpalSilenceDetector::Mode mode);
229 
230 
231 #endif // OPAL_CODEC_SILENCEDETECT_H
232 
233 
Definition: silencedetect.h:54
unsigned m_adaptivePeriod
Definition: silencedetect.h:192
void FromString(const PString &str)
Definition: silencedetect.h:208
Result Detect(const BYTE *audioPtr, PINDEX audioLen, unsigned timestamp)
unsigned m_signalReceivedTime
Definition: silencedetect.h:200
unsigned m_lastSignalLevel
Definition: silencedetect.h:202
PDECLARE_NOTIFIER(RTP_DataFrame, OpalSilenceDetector, ReceivedPacket)
unsigned m_silenceDeadband
milliseconds of signal needed
Definition: silencedetect.h:75
unsigned m_receivedTime
Definition: silencedetect.h:196
Definition: silencedetect.h:125
Mode m_mode
Definition: silencedetect.h:189
void SetParameters(const Params &params, const int clockRate=0)
virtual unsigned GetAverageSignalLevel(const BYTE *buffer, PINDEX size)=0
PString AsString() const
Result
Definition: silencedetect.h:123
unsigned m_silenceDeadband
Definition: silencedetect.h:191
unsigned m_adaptivePeriod
milliseconds of silence needed
Definition: silencedetect.h:76
unsigned m_silenceMaximum
Definition: silencedetect.h:199
unsigned m_signalMinimum
Definition: silencedetect.h:198
unsigned m_levelThreshold
Definition: silencedetect.h:197
Modes Mode
Definition: silencedetect.h:52
Definition: silencedetect.h:43
Params(Modes mode=AdaptiveSilenceDetection, unsigned threshold=0, unsigned signalDeadband=10, unsigned silenceDeadband=400, unsigned adaptivePeriod=600)
Definition: silencedetect.h:55
Modes m_mode
Definition: silencedetect.h:72
unsigned m_signalDeadband
Definition: silencedetect.h:190
Definition: rtp.h:540
static unsigned GetAverageSignalLevelPCM16(const BYTE *buffer, PINDEX size, bool asPercentage)
void SetClockRate(unsigned clockRate)
Definition: silencedetect.h:126
unsigned m_clockRate
Definition: silencedetect.h:193
ostream & operator<<(ostream &strm, OpalSilenceDetector::Mode mode)
void GetParameters(Params &params)
PNotifier m_receiveHandler
Definition: silencedetect.h:187
P_DECLARE_STREAMABLE_ENUM(Modes, NoSilenceDetection, FixedSilenceDetection, AdaptiveSilenceDetection)
OpalPCM16SilenceDetector(const Params &newParam)
Definition: silencedetect.h:214
OpalSilenceDetector(const Params &newParam)
unsigned m_lastTimestamp
Definition: silencedetect.h:195
virtual unsigned GetAverageSignalLevel(const BYTE *buffer, PINDEX size)
Result m_lastResult
Definition: silencedetect.h:203
unsigned m_silenceReceivedTime
Definition: silencedetect.h:201
unsigned m_signalDeadband
Threshold value if FixedSilenceDetection.
Definition: silencedetect.h:74
const PNotifier & GetReceiveHandler() const
Definition: silencedetect.h:90
unsigned GetClockRate() const
Definition: silencedetect.h:121
unsigned m_threshold
Silence detection mode.
Definition: silencedetect.h:73
Definition: silencedetect.h:127
Result GetResult(unsigned *currentThreshold=NULL, unsigned *currentLevel=NULL) const