Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/OrcaSnip.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include "../common_source.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   //Add any specific controls needed by OrcaSnip
00030   //(default controls all MarSystems should have
00031   //were already added by MarSystem::addControl(),
00032   //called by :MarSystem(name) constructor).
00033   //If no specific controls are needed by a MarSystem
00034   //there is no need to implement and call this addControl()
00035   //method (see for e.g. Rms.cpp)
00036   addControls();
00037 }
00038 
00039 OrcaSnip::OrcaSnip(const OrcaSnip& a) : MarSystem(a)
00040 {
00041   // For any MarControlPtr in a MarSystem
00042   // it is necessary to perform this getctrl
00043   // in the copy constructor in order for cloning to work
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   //Add specific controls needed by this MarSystem.
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   // no need to do anything OrcaSnip-specific in myUpdate
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   // normalize rms
00086   mrs_real tmpMax = 0;
00087   for (mrs_natural t = 0; t < inSamples_; t++)
00088   {
00089     mrs_real tmp = in(1,t);
00090     if (tmp > tmpMax)
00091       tmpMax = tmp;
00092   }
00093   for (mrs_natural t = 0; t < inSamples_; t++)
00094     out(1,t)    /= tmpMax;
00095 
00096 
00097   while ((startStopIdx[0] >= (inSamples_-1)) && (decisionThresh > .01))
00098   {
00099     // set start Idx
00100     for (mrs_natural 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     // set stop Idx
00111     for (mrs_natural 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     // lower threshold until we find something
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