00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "NoiseGate.h"
00020
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023
00024 NoiseGate::NoiseGate(mrs_string name):MarSystem("NoiseGate",name)
00025 {
00026
00027
00028
00029 state_ = 1.0;
00030 xdprev_ = 0.0;
00031 alpha_ = 0.0;
00032 gainsprev_ = 1.0;
00033
00034 addControls();
00035 }
00036
00037
00038 NoiseGate::~NoiseGate()
00039 {
00040 }
00041
00042
00043 MarSystem*
00044 NoiseGate::clone() const
00045 {
00046 return new NoiseGate(*this);
00047 }
00048
00049 void
00050 NoiseGate::addControls()
00051 {
00052 addctrl("mrs_real/thresh", 0.1);
00053 addctrl("mrs_real/release", 0.5);
00054 addctrl("mrs_real/rolloff", .130);
00055 addctrl("mrs_real/at", 0.0001);
00056 addctrl("mrs_real/rt", 0.130);
00057 addctrl("mrs_real/slope", 1.0);
00058 }
00059
00060 void
00061 NoiseGate::myUpdate(MarControlPtr sender)
00062 {
00063 (void) sender;
00064 MRSDIAG("NoiseGate.cpp - NoiseGate:myUpdate");
00065
00066 setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00067 setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00068 setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00069
00070
00071 inSamples_ = getctrl("mrs_natural/inSamples")->to<mrs_natural>();
00072
00073 xd_.create(inSamples_);
00074 gains_.create(inSamples_);
00075 }
00076
00077
00078 void
00079 NoiseGate::myProcess(realvec& in, realvec& out)
00080 {
00081 mrs_natural t,o;
00082
00083
00084 mrs_real thresh = getctrl("mrs_real/thresh")->to<mrs_real>();
00085 mrs_real release = getctrl("mrs_real/release")->to<mrs_real>();
00086 mrs_real rolloff = getctrl("mrs_real/rolloff")->to<mrs_real>();
00087 mrs_real at = getctrl("mrs_real/at")->to<mrs_real>();
00088 mrs_real rt = getctrl("mrs_real/rt")->to<mrs_real>();
00089
00090
00091
00092
00093 at = 1 - exp(-2.2/(22050*at));
00094 rt = 1 - exp(-2.2/(22050*rt));
00095
00096 for (o = 0; o < inObservations_; o++)
00097 for (t = 0; t < inSamples_; t++)
00098 {
00099
00100
00101 alpha_ = fabs(in(o,t)) - xdprev_;
00102 if (alpha_ < 0)
00103 {
00104 alpha_ = 0;
00105 }
00106 xdprev_=xdprev_*(1-rt)+at*alpha_;
00107
00108 if (state_ == 1.0)
00109 {
00110 if (xdprev_ < thresh)
00111 {
00112 gains_(o,t) = gainsprev_*rolloff;
00113 state_ = 0.0;
00114 }
00115 else
00116 {
00117 gains_(o,t) = 1;
00118 }
00119 }
00120 else
00121 {
00122 if (xdprev_ < release)
00123 {
00124 gains_(o,t) = gainsprev_*rolloff;
00125
00126 }
00127 else
00128 if (xdprev_ > release)
00129 {
00130 gains_(o,t) = 1.0;
00131 state_ = 1.0;
00132 }
00133 else
00134 {
00135 gains_(o,t) = 0.0;
00136 }
00137 }
00138
00139 gainsprev_ = gains_(o,t);
00140 out(o,t) = gainsprev_ * in(o,t);
00141
00142 }
00143 }
00144
00145
00146
00147
00148
00149
00150
00151