Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PitchDiff.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 "PitchDiff.h"
00020 #include "../common_source.h"
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 PitchDiff::PitchDiff(mrs_string name) : MarSystem("PitchDiff", name)
00026 {
00028   addControls();
00029 }
00030 
00031 PitchDiff::PitchDiff(const PitchDiff& a) : MarSystem(a)
00032 {
00035   ctrl_expectedPitch_ = getctrl("mrs_real/expectedPitch");
00036   ctrl_ignoreOctaves_ = getctrl("mrs_bool/ignoreOctaves");
00037   ctrl_absoluteValue_ = getctrl("mrs_bool/absoluteValue");
00038 }
00039 
00040 
00041 PitchDiff::~PitchDiff()
00042 {
00043 }
00044 
00045 MarSystem*
00046 PitchDiff::clone() const
00047 {
00048   return new PitchDiff(*this);
00049 }
00050 
00051 void
00052 PitchDiff::addControls()
00053 {
00055   addctrl("mrs_real/expectedPitch", 440.0, ctrl_expectedPitch_);
00056   addctrl("mrs_bool/ignoreOctaves", false, ctrl_ignoreOctaves_);
00057   addctrl("mrs_bool/absoluteValue", false, ctrl_absoluteValue_);
00058   setctrlState("mrs_real/expectedPitch", true);
00059 }
00060 
00061 void
00062 PitchDiff::myUpdate(MarControlPtr sender)
00063 {
00064   MRSDIAG("PitchDiff.cpp - PitchDiff:myUpdate");
00065 
00067   MarSystem::myUpdate(sender);
00068 
00069   // Add prefix to the observation names.
00070   mrs_string inObsNames = ctrl_inObsNames_->to<mrs_string>();
00071   ctrl_onObsNames_->setValue(obsNamesAddPrefix(inObsNames, "PitchDiff_"),
00072                              NOUPDATE);
00073 
00074   expectedMidiPitch_ = hertz2pitch(ctrl_expectedPitch_->to<mrs_real>());
00075 }
00076 
00077 void
00078 PitchDiff::myProcess(realvec& in, realvec& out)
00079 {
00081   for (mrs_natural o = 0; o < inObservations_; o++)
00082   {
00083     // we should only have one sample input
00084 
00085     // convert to midi
00086     mrs_real in_midi = hertz2pitch(in(o,0));
00087     // difference in midi
00088     mrs_real diff = in_midi - expectedMidiPitch_;
00089     if (ctrl_ignoreOctaves_->isTrue()) {
00090       diff = fmod(diff, 12.0);
00091       // seeing -1.5 is much more intuitive than
00092       // seeing 9.5.
00093       if (diff > 6) {
00094         diff -= 12.0;
00095       }
00096       if (diff < -6) {
00097         diff += 12.0;
00098       }
00099     }
00100     if (ctrl_absoluteValue_->isTrue()) {
00101       diff = fabs(diff);
00102     }
00103     out(o, 0) = diff;
00104   }
00105 }