00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _VrSQUELCH_H_
00019 #define _VrSQUELCH_H_
00020
00021 #include <VrHistoryProc.h>
00022
00023 template<class T>
00024 class VrSquelch : public VrHistoryProc<T, T>
00025 {
00026 protected:
00027 float thresh;
00028 float duration;
00029
00030 bool squelching;
00031 int bufferCount;
00032
00033 T *zeroBuf;
00034
00035 public:
00036 VrSquelch(float th, float d) :
00037 thresh(th), duration(d), squelching(false), bufferCount(0) {}
00038
00039 virtual void work(timestamp ts, unsigned int size, T *i[], T *o[]);
00040 virtual void initialize()
00041 {
00042 int h= ((int)(duration*getInputSamplingFrequencyN(0)));
00043 history=h;
00044 setOutputSize (h);
00045 zeroBuf = new T[h];
00046 for(int i=0;i<h;i++) zeroBuf[i]=0;
00047 }
00048 void setThresh(float th) { thresh = th; }
00049 float getThresh() { return thresh; }
00050 void setDuration(float d)
00051 {
00052 duration = d;
00053 delete[] zeroBuf;
00054 int h = (int)(duration*getInputSamplingFrequencyN(0));
00055 history=h;
00056 setOutputSize (h);
00057 zeroBuf = new T[h];
00058 }
00059 float getDuration() { return duration; }
00060 };
00061
00062 template<class T>
00063 void
00064 VrSquelch<T>::work(timestamp ts, unsigned int size, T *i[], T *o[])
00065 {
00066 while (size > 0)
00067 {
00068 float total = 0;
00069 T *buf=i[0];
00070 for (unsigned int ii=0; ii<history; ii++)
00071 {
00072 total += abs(buf[ii]);
00073 }
00074 if (squelching)
00075 {
00076 if (total/history > thresh)
00077 {
00078 squelching = false;
00079 bufferCount = 0;
00080 memcpy(o[0],buf,history*sizeof(T));
00081 }
00082 else
00083 memcpy(o[0],zeroBuf,history*sizeof(T));
00084 }
00085 else
00086 {
00087 if (total/history < thresh)
00088 bufferCount++;
00089 if (bufferCount > 2)
00090 {
00091 squelching = true;
00092 memcpy(o[0],zeroBuf,history*sizeof(T));
00093 }
00094 else
00095 memcpy(o[0],buf,history*sizeof(T));
00096 }
00097 size -= history;
00098 i[0]+=history;
00099 o[0]+=history;
00100 }
00101 }
00102
00103 #endif