00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "common.h"
00020 #include "OrcaSnip.h"
00021
00022 #define MTLB_DBG_LOG
00023
00024 using std::ostringstream;
00025 using namespace Marsyas;
00026
00027 OrcaSnip::OrcaSnip(mrs_string name):MarSystem("OrcaSnip", name)
00028 {
00029
00030
00031
00032
00033
00034
00035
00036 addControls();
00037 }
00038
00039 OrcaSnip::OrcaSnip(const OrcaSnip& a) : MarSystem(a)
00040 {
00041
00042
00043
00044 ctrl_startSnip_ = getctrl("mrs_natural/startSnip");
00045 ctrl_stopSnip_ = getctrl("mrs_natural/stopSnip");
00046 ctrl_decisionThresh_ = getctrl("mrs_real/decisionThresh");
00047 }
00048
00049 OrcaSnip::~OrcaSnip()
00050 {
00051 }
00052
00053 MarSystem*
00054 OrcaSnip::clone() const
00055 {
00056 return new OrcaSnip(*this);
00057 }
00058
00059 void
00060 OrcaSnip::addControls()
00061 {
00062
00063 addctrl("mrs_natural/startSnip", -1, ctrl_startSnip_);
00064 addctrl("mrs_natural/stopSnip", -1, ctrl_stopSnip_);
00065 addctrl("mrs_real/decisionThresh", .4, ctrl_decisionThresh_);
00066 }
00067
00068 void
00069 OrcaSnip::myUpdate(MarControlPtr sender)
00070 {
00071
00072 MarSystem::myUpdate(sender);
00073
00074 }
00075
00076
00077 void
00078 OrcaSnip::myProcess(realvec& in, realvec& out)
00079 {
00080 mrs_real decisionThresh = ctrl_decisionThresh_->to<mrs_real>();
00081 mrs_natural startStopIdx[2] = {inSamples_-1,0};
00082
00083 out = in;
00084
00085
00086 mrs_real tmpMax = 0;
00087 for (t = 0; t < inSamples_; t++)
00088 {
00089 mrs_real tmp = in(1,t);
00090 if (tmp > tmpMax)
00091 tmpMax = tmp;
00092 }
00093 for (t = 0; t < inSamples_; t++)
00094 out(1,t) /= tmpMax;
00095
00096
00097 while ((startStopIdx[0] >= (inSamples_-1)) && (decisionThresh > .01))
00098 {
00099
00100 for (t = 0; t < inSamples_; t++)
00101 {
00102 mrs_real avg = .5*(out(0,t) + out(1,t));
00103 if (avg > decisionThresh)
00104 {
00105 startStopIdx[0] = t;
00106 break;
00107 }
00108 }
00109
00110
00111 for (t = inSamples_-1; t >= 0; t--)
00112 {
00113 mrs_real avg = .5*(out(0,t) + out(1,t));
00114 if (avg > decisionThresh)
00115 {
00116 startStopIdx[1] = t;
00117 break;
00118 }
00119 }
00120
00121 decisionThresh *= .9;
00122 }
00123
00124 if (startStopIdx[0] == inSamples_-1)
00125 startStopIdx[0] = 0;
00126 if (startStopIdx[1] == 0)
00127 startStopIdx[1] = inSamples_-1;
00128
00129 ctrl_startSnip_->setValue (startStopIdx[0], false);
00130 ctrl_stopSnip_->setValue (startStopIdx[1], false);
00131 #ifdef MARSYAS_MATLAB
00132 #ifdef MTLB_DBG_LOG
00133 MATLAB_PUT(startStopIdx[0], "iStart");
00134 MATLAB_PUT(startStopIdx[1], "iStop");
00135 MATLAB_PUT(out, "output");
00136 MATLAB_EVAL ("figure(513),plot([output; mean(output,1)]'), hold on, stem([iStart iStop], [.9 .9],'r','fill'),axis([0 size(output,2) 0 1]), grid on,hold off");
00137 #endif
00138 #endif
00139 }
00140
00141
00142
00143
00144
00145
00146
00147