Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/RealvecSink.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 "RealvecSink.h"
00021 
00022 using namespace std;
00023 using namespace Marsyas;
00024 
00025 RealvecSink::RealvecSink(mrs_string name):MarSystem("RealvecSink",name)
00026 {
00027   //type_ = "RealvecSink";
00028   //name_ = name;
00029   oriName_ = "MARSYAS_EMPTY";
00030   count_= 0;
00031   write_ = 0 ;
00032   addControls();
00033 }
00034 
00035 RealvecSink::RealvecSink(const RealvecSink& a):MarSystem(a)
00036 {
00037   count_ = 0;
00038   write_ = 0 ;
00039   oriName_ = "MARSYAS_EMPTY";
00040   ctrl_data_ = getctrl("mrs_realvec/data");
00041 }
00042 
00043 
00044 RealvecSink::~RealvecSink()
00045 {
00046 }
00047 
00048 
00049 MarSystem*
00050 RealvecSink::clone() const
00051 {
00052   return new RealvecSink(*this);
00053 }
00054 
00055 void
00056 RealvecSink::addControls()
00057 {
00058   addctrl("mrs_bool/done", false);
00059   setctrlState("mrs_bool/done", true);
00060   addctrl("mrs_realvec/data", realvec(), ctrl_data_);
00061   setctrlState("mrs_real/israte", true);
00062   addctrl("mrs_string/fileName", "MARSYAS_EMPTY");
00063   setctrlState("mrs_string/fileName", true);
00064 }
00065 
00066 
00067 void
00068 RealvecSink::myUpdate(MarControlPtr sender)
00069 {
00070   (void) sender;  //suppress warning of unused parameter(s)
00071   MRSDIAG("RealvecSink.cpp - RealvecSink:myUpdate");
00072 
00073   setctrl("mrs_natural/onObservations", getctrl("mrs_natural/inObservations")->to<mrs_natural>());
00074   setctrl("mrs_natural/onSamples", getctrl("mrs_natural/inSamples")->to<mrs_natural>());
00075   setctrl("mrs_real/osrate", getctrl("mrs_real/israte")->to<mrs_real>());
00076   ctrl_onObsNames_->setValue(ctrl_inObsNames_->to<mrs_string>(), NOUPDATE);
00077 
00078   if( getctrl("mrs_bool/done")->isTrue()) {
00079     if(write_)
00080     {
00081       // closing output file
00082       outputFile_.close();
00083       // copy to tmp
00084       mrs_string tmp  = oriName_.c_str();
00085       tmp+="tmp";
00086       ofstream out;
00087       out.open(tmp.c_str(), ios::out);
00088       ifstream in;
00089       in.open(oriName_.c_str(), ios::in);
00090       out << in.rdbuf();
00091       in.close();
00092       out.close();
00093 
00094       //reopen
00095       out.open(oriName_.c_str(), ios::out);
00096       // print header
00097       out << "# MARSYAS mrs_realvec" << endl;
00098       out << "# Size = " << inObservations_*count_ << endl << endl;
00099       out << endl;
00100 
00101 
00102       out << "# type: matrix" << endl;
00103       out << "# rows: " << count_ << endl;
00104       out << "# columns: " << inObservations_ << endl;
00105       // fill core
00106       in.open(tmp.c_str(), ios::in);
00107       out << in.rdbuf();
00108       in.close();
00109       // remove tmp file
00110 #ifdef MARSYAS_WIN32
00111       _unlink(tmp.c_str());
00112 #else
00113       unlink(tmp.c_str());
00114 #endif
00115       // write bottom
00116       out << endl;
00117       out << "# Size = " << inObservations_*count_ << endl;
00118       out << "# MARSYAS mrs_realvec" << endl;
00119       out.close();
00120     }
00121     else
00122     {
00123       MarControlAccessor acc(ctrl_data_, NOUPDATE);
00124       realvec& data = acc.to<mrs_realvec>();
00125       data.stretch(0);
00126     }
00127     count_=0;
00128     setctrl("mrs_bool/done", false);
00129   }
00130 
00131 
00132   if(getctrl("mrs_string/fileName")->to<mrs_string>().compare(oriName_))
00133   {
00134     if(write_)
00135       outputFile_.close();
00136     oriName_ = getctrl("mrs_string/fileName")->to<mrs_string>();
00137     outputFile_.open(oriName_.c_str(), ios::out);
00138     write_ = 1;
00139   }
00140 }
00141 
00142 void
00143 RealvecSink::myProcess(realvec& in, realvec& out)
00144 {
00145   mrs_natural o,t;
00146   out=in;
00147 
00148   if(write_)
00149   {
00150     for (t=0; t < inSamples_; t++)
00151     {
00152       for (o=0 ; o<inObservations_ ; o++)
00153         outputFile_ << in(o, t) << " " ;
00154       outputFile_ << endl;
00155     }
00156   }
00157   else
00158   {
00159     MarControlAccessor acc(ctrl_data_);
00160     realvec& data = acc.to<mrs_realvec>();
00161     data.stretch(inObservations_, count_+inSamples_);
00162 
00163     for (o=0; o < inObservations_; o++)
00164       for (t=0; t < inSamples_; t++)
00165         data(o, count_+t) = in(o, t);
00166 
00167     //out.dump();
00168   }
00169 
00170   count_+=inSamples_;
00171 }