Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/BeatHistogram.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2010 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 "BeatHistogram.h"
00021 
00022 using std::ostringstream;
00023 using std::cout;
00024 using std::endl;
00025 
00026 using namespace Marsyas;
00027 
00028 //#define MTLB_DBG_LOG
00029 
00030 BeatHistogram::BeatHistogram(mrs_string name):MarSystem("BeatHistogram",name)
00031 {
00032   addControls();
00033 }
00034 
00035 
00036 BeatHistogram::~BeatHistogram()
00037 {
00038 }
00039 
00040 
00041 MarSystem*
00042 BeatHistogram::clone() const
00043 {
00044   return new BeatHistogram(*this);
00045 }
00046 
00047 void
00048 BeatHistogram::addControls()
00049 {
00050   addctrl("mrs_real/gain", 1.0);
00051   addctrl("mrs_bool/reset", false);
00052   setctrlState("mrs_bool/reset", true);
00053   addctrl("mrs_natural/startBin", 0);
00054   setctrlState("mrs_natural/startBin", true);
00055   addctrl("mrs_natural/endBin", 100);
00056   setctrlState("mrs_natural/endBin", true);
00057   addctrl("mrs_real/factor", 1.0);
00058   addctrl("mrs_bool/tempoWeighting", false);
00059   addctrl("mrs_real/alpha", 0.5);
00060   setctrlState("mrs_real/alpha", true);
00061   setctrlState("mrs_real/factor", true);
00062 
00063 
00064 }
00065 
00066 void
00067 BeatHistogram::myUpdate(MarControlPtr sender)
00068 {
00069   (void) sender;  //suppress warning of unused parameter(s)
00070   MRSDIAG("BeatHistogram.cpp - BeatHistogram:myUpdate");
00071 
00072   startBin_ = getctrl("mrs_natural/startBin")->to<mrs_natural>();
00073   endBin_ = getctrl("mrs_natural/endBin")->to<mrs_natural>();
00074   reset_ = getctrl("mrs_bool/reset")->to<mrs_bool>();
00075   factor_ = getctrl("mrs_real/factor")->to<mrs_real>();
00076   alpha_ = getctrl("mrs_real/alpha")->to<mrs_real>();
00077   setctrl("mrs_natural/onSamples", endBin_ - startBin_);
00078   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00079   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00080 }
00081 
00082 
00083 void
00084 BeatHistogram::myProcess(realvec& in, realvec& out)
00085 {
00086 
00087   if (reset_)
00088   {
00089     out.setval(0.0);
00090     reset_ = false;
00091     setctrl("mrs_bool/reset", false);
00092   }
00093 
00094 
00095   //out.setval(0.0);
00096 
00097   mrs_natural bin=0;
00098   mrs_real amp;
00099   mrs_real srate = getctrl("mrs_real/israte")->to<mrs_real>();
00100   mrs_natural count = 1;
00101   mrs_natural prev_bin =endBin_-1;
00102   mrs_natural pprev_bin =endBin_-1;
00103   mrs_real sumamp = 0.0;
00104   mrs_real tempo_weight = 0.0;
00105 
00106 
00107 #ifdef MARSYAS_MATLAB
00108 #ifdef MTLB_DBG_LOG
00109   MATLAB_PUT(in, "acr");
00110   MATLAB_EVAL("figure(1);plot(acr),grid on");
00111 #endif
00112 #endif
00113 
00114 
00115   for (mrs_natural o=0; o < inObservations_; o++)
00116   {
00117     for (mrs_natural t = 1; t < inSamples_; t++)
00118     {
00119       bin = (mrs_natural)(( (2*srate) * 60.0 * factor_ / (t+1)) + 0.5);
00120 
00121       amp = in(o,t);
00122 
00123 
00124       // optional tempo weight
00125 
00126       if (getctrl("mrs_bool/tempoWeighting")->to<mrs_bool>())
00127       {
00128         tempo_weight = 5.0 *
00129                        log10((t+1) * 400.0 / (srate * 60.0 * factor_)) *
00130                        log10((t+1) * 400.0 / (srate * 60.0 * factor_));
00131         tempo_weight = exp(0.5 * tempo_weight * tempo_weight);
00132       }
00133       else
00134       {
00135         tempo_weight = 1.0;
00136       }
00137 
00138       amp = tempo_weight * amp;
00139 
00140       if (amp < 0.0)
00141         amp = 0.0;
00142 
00143 
00144       if ((bin > 40)&&(bin < endBin_))
00145       {
00146         if (prev_bin == bin)
00147         {
00148           sumamp += amp;
00149           count++;
00150         }
00151         else
00152         {
00153           sumamp += amp;
00154           //out(o,prev_bin) += ((sumamp / count));
00155           out(o,prev_bin) = alpha_*out(o, prev_bin) + (1.0-alpha_)*((sumamp / count));
00156           //out(o,prev_bin) = out(o, prev_bin) + ((sumamp / count));
00157           count = 1;
00158           sumamp = 0.0;
00159         }
00160 
00161         // linear interpolation of the "not-set" bins...
00162         if (pprev_bin-prev_bin > 1)
00163         {
00164           // prev is x0, pprev is x1
00165           mrs_real x0 = prev_bin;
00166           mrs_real x1 = pprev_bin;
00167           mrs_real y0 = out(o, prev_bin);
00168           mrs_real y1 = out(o, pprev_bin);
00169           for (mrs_natural k = prev_bin+1; k < pprev_bin; k++)
00170             out (o,k)   = y0 + (y1-y0)*(k-x0)/(x1-x0);
00171         }
00172 
00173         pprev_bin = prev_bin;
00174         prev_bin = bin;
00175       }
00176     }
00177 
00178 
00179 
00180   }
00181 
00182 
00183 #ifdef MARSYAS_MATLAB
00184 #ifdef MTLB_DBG_LOG
00185   MATLAB_PUT(out, "bh");
00186   MATLAB_EVAL("figure(2);plot(bh),grid on");
00187 #endif
00188 #endif
00189 
00190 }