00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "common.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
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
00060
00061 }
00062
00063 void
00064 BeatHistogram::myUpdate(MarControlPtr sender)
00065 {
00066 (void) sender;
00067 MRSDIAG("BeatHistogram.cpp - BeatHistogram:myUpdate");
00068
00069 startBin_ = getctrl("mrs_natural/startBin")->to<mrs_natural>();
00070 endBin_ = getctrl("mrs_natural/endBin")->to<mrs_natural>();
00071 reset_ = getctrl("mrs_bool/reset")->to<mrs_bool>();
00072 factor_ = getctrl("mrs_real/factor")->to<mrs_real>();
00073 setctrl("mrs_natural/onSamples", endBin_ - startBin_);
00074 setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00075 setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00076 }
00077
00078
00079 void
00080 BeatHistogram::myProcess(realvec& in, realvec& out)
00081 {
00082
00083 if (reset_)
00084 {
00085 out.setval(0.0);
00086 reset_ = false;
00087 setctrl("mrs_bool/reset", false);
00088 }
00089
00090
00091 out.setval(0.0);
00092
00093 mrs_natural bin=0;
00094 mrs_real amp;
00095 mrs_real srate = getctrl("mrs_real/israte")->to<mrs_real>();
00096 mrs_natural count = 1;
00097 mrs_natural prev_bin =endBin_-1;
00098 mrs_natural pprev_bin =endBin_-1;
00099 mrs_real sumamp = 0.0;
00100 mrs_real tempo_weight = 0.0;
00101
00102
00103 #ifdef MARSYAS_MATLAB
00104 #ifdef MTLB_DBG_LOG
00105 MATLAB_PUT(in, "acr");
00106 MATLAB_EVAL("figure(1);plot(acr),grid on");
00107 #endif
00108 #endif
00109
00110
00111 for (mrs_natural o=0; o < inObservations_; o++)
00112 {
00113 for (mrs_natural t = 1; t < inSamples_; t++)
00114 {
00115 bin = (mrs_natural)((srate * 60.0 * factor_ / (t+1)) + 0.5);
00116
00117 amp = in(o,t);
00118
00119
00120
00121
00122 if (getctrl("mrs_bool/tempoWeighting")->to<mrs_bool>())
00123 {
00124 tempo_weight = 5.0 *
00125 log10((t+1) * 400.0 / (srate * 60.0 * factor_)) *
00126 log10((t+1) * 400.0 / (srate * 60.0 * factor_));
00127 tempo_weight = exp(0.5 * tempo_weight * tempo_weight);
00128 }
00129 else
00130 {
00131 tempo_weight = 1.0;
00132 }
00133
00134 amp = tempo_weight * amp;
00135
00136 if (amp < 0.0)
00137 amp = 0.0;
00138
00139
00140 if ((bin > 40)&&(bin < endBin_))
00141 {
00142 if (prev_bin == bin)
00143 {
00144 sumamp += amp;
00145 count++;
00146 }
00147 else
00148 {
00149 sumamp += amp;
00150 out(o,prev_bin) += ((sumamp / count));
00151 count = 1;
00152 sumamp = 0.0;
00153 }
00154
00155
00156 if (pprev_bin-prev_bin > 1)
00157 {
00158 mrs_natural len = prev_bin-pprev_bin-1;
00159 for (mrs_natural k = prev_bin+1; k < pprev_bin; k++)
00160 out (o,k) = (k-prev_bin)*(out(o,prev_bin)-out(o,pprev_bin))/len + out(o,prev_bin);
00161 }
00162
00163 pprev_bin = prev_bin;
00164 prev_bin = bin;
00165 }
00166 }
00167
00168
00169
00170 }
00171
00172
00173 #ifdef MARSYAS_MATLAB
00174 #ifdef MTLB_DBG_LOG
00175 MATLAB_PUT(out, "bh");
00176 MATLAB_EVAL("figure(2);plot(bh),grid on");
00177 #endif
00178 #endif
00179
00180 }