00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "PeakerAdaptive.h"
00020
00021 using std::ostringstream;
00022 using namespace Marsyas;
00023
00024 PeakerAdaptive::PeakerAdaptive(mrs_string name):MarSystem("PeakerAdaptive",name)
00025 {
00026 rms_ = 0.0;
00027 winCount_ = 1;
00028 peakHysterisis_ = 0;
00029 addControls();
00030 }
00031
00032 PeakerAdaptive::~PeakerAdaptive()
00033 {
00034 }
00035
00036 MarSystem*
00037 PeakerAdaptive::clone() const
00038 {
00039 return new PeakerAdaptive(*this);
00040 }
00041
00042 void
00043 PeakerAdaptive::addControls()
00044 {
00045 addctrl("mrs_real/peakSpacing", 0.0);
00046 addctrl("mrs_real/peakStrength", 0.7);
00047 addctrl("mrs_natural/peakStart", (mrs_natural)0);
00048 addctrl("mrs_natural/peakEnd", (mrs_natural)0);
00049 addctrl("mrs_real/peakGain", 1.0);
00050 addctrl("mrs_natural/peakStrengthReset", 4);
00051 addctrl("mrs_real/peakDecay", 0.9);
00052 addctrl("mrs_bool/peakFound", false);
00053 }
00054
00055 void
00056 PeakerAdaptive::myProcess(realvec& in, realvec& out)
00057 {
00058 mrs_natural t,o;
00059 mrs_real peakSpacing;
00060 mrs_real peakStrength;
00061 mrs_real peakGain;
00062
00063 mrs_natural peakStart;
00064 mrs_natural peakEnd;
00065 mrs_natural peakStrengthReset;
00066 mrs_real peakDecay;
00067
00068 peakSpacing = getctrl("mrs_real/peakSpacing")->to<mrs_real>();
00069 peakStrength = getctrl("mrs_real/peakStrength")->to<mrs_real>();
00070 peakStart = getctrl("mrs_natural/peakStart")->to<mrs_natural>();
00071 peakEnd = getctrl("mrs_natural/peakEnd")->to<mrs_natural>();
00072 peakGain = getctrl("mrs_real/peakGain")->to<mrs_real>();
00073 peakStrengthReset = getctrl("mrs_natural/peakStrengthReset")->to<mrs_natural>();
00074 peakDecay = getctrl("mrs_real/peakDecay")->to<mrs_real>();
00075
00076 out.setval(0.0);
00077 MRSMSG("peakEnd = " << peakEnd);
00078
00079
00080
00081 for (o = 0; o < inObservations_; o++)
00082 {
00083 peakSpacing = (mrs_real)(peakSpacing * inSamples_);
00084 mrs_real max;
00085 mrs_natural maxIndex = 0;
00086
00087 bool peakFound = false;
00088
00089 for (t=peakStart+1; t < peakEnd-1; t++)
00090 {
00091 if (fabs(in(o,t)) > rms_)
00092 rms_ = fabs(in(o,t));
00093 }
00094
00095 for (t=peakStart+1; t < peakEnd-1; t++)
00096 {
00097
00098 if ((in(o,t -1) < in(o,t))
00099 && (in(o,t+1) < in(o,t))
00100 && (fabs(in(o,t)) > peakStrength * rms_)
00101 )
00102 {
00103 max = in(o,t);
00104 maxIndex = t;
00105
00106 for (int j=0; j < (mrs_natural)peakSpacing; j++)
00107 {
00108 if (t+j < peakEnd-1)
00109 if (in(o,t+j) > max)
00110 {
00111 max = in(o,t+j);
00112 maxIndex = t+j;
00113 }
00114 }
00115
00116 t += (mrs_natural)peakSpacing;
00117 if ((peakHysterisis_ > peakStrengthReset) ||
00118 (peakHysterisis_ == 0)
00119 )
00120 {
00121 out(o,maxIndex) = fabs(in(o,maxIndex));
00122 peakHysterisis_ = 1;
00123 }
00124
00125 rms_ = fabs(in(o,maxIndex));
00126 peakFound = true;
00127
00128
00129 }
00130 }
00131
00132 if (!peakFound)
00133 {
00134 rms_ *= peakDecay;
00135 setctrl("mrs_bool/peakFound", false);
00136 }
00137 else
00138 setctrl("mrs_bool/peakFound", true);
00139 peakHysterisis_ ++;
00140 }
00141 }