Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/ClassOutputSink.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 "ClassOutputSink.h"
00020 #include "../common_source.h"
00021 
00022 
00023 using std::ostringstream;
00024 using std::ofstream;
00025 using std::cout;
00026 using std::endl;
00027 
00028 using namespace Marsyas;
00029 
00030 
00031 ClassOutputSink::ClassOutputSink(mrs_string name):MarSystem("ClassOutputSink", name)
00032 {
00033   //type_ = "ClassOutputSink";
00034   //name_ = name;
00035   mos_ = NULL;
00036 
00037   addControls();
00038 }
00039 
00040 ClassOutputSink::~ClassOutputSink()
00041 {
00042   if (mos_ != NULL)
00043     mos_->close();
00044   delete mos_;
00045 }
00046 
00047 MarSystem*
00048 ClassOutputSink::clone() const
00049 {
00050   return new ClassOutputSink(*this);
00051 }
00052 
00053 void
00054 ClassOutputSink::addControls()
00055 {
00056   addctrl("mrs_natural/memSize", 40);
00057   addctrl("mrs_natural/nLabels", 2);
00058 
00059   addctrl("mrs_string/filename", "mugle.mf");
00060   setctrlState("mrs_string/filename", true);
00061   setctrlState("mrs_natural/nLabels", true);
00062   addctrl("mrs_string/labelNames", "Music,Speech");
00063   setctrlState("mrs_string/labelNames", true);
00064   addctrl("mrs_bool/silent", true);
00065 }
00066 
00067 void
00068 ClassOutputSink::putHeader()
00069 {
00070   if ((filename_ != getctrl("mrs_string/filename")->to<mrs_string>()))
00071   {
00072     if (mos_ != NULL)
00073     {
00074       mos_->close();
00075       delete mos_;
00076       if (filename_ == "mugle.mf")
00077         remove(filename_.c_str());
00078     }
00079 
00080 
00081     filename_ = getctrl("mrs_string/filename")->to<mrs_string>();
00082 
00083     mos_ = new ofstream;
00084     mos_->open(filename_.c_str());
00085 
00086   }
00087 }
00088 
00089 
00090 void
00091 ClassOutputSink::myUpdate(MarControlPtr sender)
00092 {
00093   (void) sender;  //suppress warning of unused parameter(s)
00094   MRSDIAG("ClassOutputSink.cpp - ClassOutputSink:myUpdate");
00095 
00096   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples"));
00097   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations"));
00098   setctrl("mrs_real/osrate", getctrl("mrs_real/israte"));
00099 
00100   mrs_string labelNames = getctrl("mrs_string/labelNames")->to<mrs_string>();
00101 
00102   labelNames_.clear();
00103 
00104   mrs_string temp;
00105 
00106   for (int i = 0; i < getctrl("mrs_natural/nLabels")->to<mrs_natural>(); ++i)
00107   {
00108     mrs_string labelName;
00109 
00110 
00111     labelName = labelNames.substr(0, labelNames.find(","));
00112     temp = labelNames.substr(labelNames.find(",")+1, labelNames.length());
00113     labelNames = temp;
00114     labelNames_.push_back(labelName);
00115   }
00116   count_ = 0;
00117 
00118   putHeader();
00119 }
00120 
00121 
00122 void
00123 ClassOutputSink::myProcess(realvec& in, realvec& out)
00124 {
00125   //checkFlow(in,out);
00126 
00127   mrs_natural p;
00128   //mrs_natural g;
00129   mrs_natural o,t;
00130 
00131 
00132   for (o=0; o < inObservations_; o++)
00133     for (t = 0; t < inSamples_; t++)
00134     {
00135       out(o,t) = in(o,t);
00136     }
00137 
00138   if (!getctrl("mrs_bool/silent")->isTrue())
00139   {
00140     for (o = 0; o < inObservations_; o++)
00141       for (t = 0; t < inSamples_; t++)
00142       {
00143         p = (mrs_natural)in(o,t);
00144         //g = (mrs_natural)in(1,t);
00145         if (o < inObservations_-1)
00146           cout << "Predicted: " << labelNames_[p] << endl;
00147         (*mos_) << labelNames_[p] << endl;
00148       }
00149   }
00150 
00151 }
00152 
00153 
00154 
00155 
00156 
00157 
00158 
00159 
00160 
00161 
00162 
00163