Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PlotSink.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 
00020 #include "../common_source.h"
00021 
00022 #include "PlotSink.h"
00023 #include <iomanip>
00024 
00025 
00026 using std::ostringstream;
00027 using std::cout;
00028 using std::endl;
00029 using std::setw;
00030 using std::setfill;
00031 
00032 using namespace Marsyas;
00033 
00034 PlotSink::PlotSink(mrs_string name):MarSystem("PlotSink",name)
00035 {
00036   counter_ = 0;
00037   single_file_ = NULL;
00038   addControls();
00039 }
00040 
00041 PlotSink::PlotSink(const PlotSink& a):MarSystem(a)
00042 {
00043   counter_ = 0;
00044   single_file_ = NULL;
00045   ctrl_messages_ = getctrl("mrs_bool/messages");
00046   ctrl_separator_ = getctrl("mrs_string/separator");
00047   ctrl_sequence_ = getctrl("mrs_bool/sequence");
00048   ctrl_single_file_ = getctrl("mrs_bool/single_file");
00049   ctrl_no_ticks_ = getctrl("mrs_bool/no_ticks");
00050   ctrl_filename_ = getctrl("mrs_string/filename");
00051   ctrl_matlab_ = getctrl("mrs_bool/matlab");
00052   ctrl_matlabCommand_ = getctrl("mrs_string/matlabCommand");
00053 }
00054 
00055 PlotSink::~PlotSink()
00056 {
00057   if (ctrl_single_file_->isTrue() && single_file_ != NULL) {
00058     single_file_->close();
00059     delete single_file_;
00060     single_file_ = NULL;
00061   }
00062 }
00063 
00064 MarSystem*
00065 PlotSink::clone() const
00066 {
00067   return new PlotSink(*this);
00068 }
00069 
00070 void
00071 PlotSink::addControls()
00072 {
00073   addctrl("mrs_bool/messages", false, ctrl_messages_);
00074   addctrl("mrs_string/separator", ",", ctrl_separator_);
00075   addctrl("mrs_bool/sequence", true, ctrl_sequence_);
00076   addctrl("mrs_bool/single_file", false, ctrl_single_file_);
00077   addctrl("mrs_bool/no_ticks", false, ctrl_no_ticks_);
00078   setctrlState("mrs_bool/single_file", true);
00079   addctrl("mrs_string/filename", "", ctrl_filename_);
00080   setctrlState("mrs_string/filename", true);
00081   addctrl("mrs_bool/matlab", false, ctrl_matlab_);
00082   addctrl("mrs_string/matlabCommand",
00083           "plot("+type_+"_"+name_+"_indata);", ctrl_matlabCommand_);
00084 }
00085 
00086 void
00087 PlotSink::myUpdate(MarControlPtr sender)
00088 {
00089   // no change to network flow
00090   MarSystem::myUpdate(sender);
00091 
00092   if (single_file_ && (!ctrl_single_file_->isTrue() || ctrl_filename_->to<mrs_string>() != filename_))
00093   {
00094     single_file_->close();
00095     delete single_file_;
00096     single_file_ = NULL;
00097   }
00098 
00099   filename_ = ctrl_filename_->to<mrs_string>();
00100 
00101   if (!single_file_ && ctrl_single_file_->isTrue() &&
00102       !filename_.empty())
00103   {
00104     single_file_ = new std::ofstream(filename_.c_str());
00105   }
00106 }
00107 
00108 void
00109 PlotSink::myProcess(realvec& in, realvec& out)
00110 {
00111   out = in;
00112   mrs_natural t,o;
00113 
00114   //if using MATLABengine, plot the input data in MATLAB
00115 #ifdef MARSYAS_MATLAB
00116   if(ctrl_matlab_->isTrue())
00117   {
00118     MATLAB_PUT(in, type_+"_"+name_+"_indata")
00119     MATLAB_EVAL(ctrl_matlabCommand_->to<mrs_string>());
00120   }
00121 #endif
00122 
00123   counter_++;
00124 
00125   if (ctrl_sequence_->isTrue())
00126   {
00127     //save current input to a sequence of numbered output files
00128     ostringstream oss;
00129     oss << ctrl_filename_->to<mrs_string>() <<
00130         setfill('0') << setw(4) << counter_ << ".plot";
00131     cout << "name = " << name_ << " " << oss.str() << endl;
00132     in.write(oss.str());
00133   }
00134 
00135   if (ctrl_single_file_->isTrue() && single_file_) {
00136     for (o=0; o < inObservations_; o++) {
00137       for (t = 0; t < inSamples_; t++) {
00138         //(*single_file_) << counter_ << " " << t << " ";
00139         (*single_file_) << std::setprecision(20) << in(o,t);
00140         (*single_file_) << std::endl;
00141         //cout << in(o,t);
00142       }
00143     }
00144     if (ctrl_no_ticks_->isTrue()) {
00145     } else {
00146       (*single_file_) << std::endl;
00147     }
00148   }
00149 
00150   if(ctrl_messages_->isTrue())
00151   {
00152     mrs_string sep =ctrl_separator_->to<mrs_string>();
00153     //ostringstream oss;
00154     //output input content as a Marsyas Message (stdout by default)
00155 //      for (t = 0; t < inSamples_; t++)
00156 //      {
00157 //          for (o=0; o < inObservations_; o++)
00158 //          {
00159 //              if (o < inObservations_ - 1)
00160 //              {
00161 //                  oss << out(o,t) << sep;
00162 //              }
00163 //              else
00164 //              {
00165 //                  oss << out(o,t);
00166 //              }
00167 //          }
00168 //          mrs_string s = oss.str();
00169 //          MRSMSG(s << endl);
00170 //      }//FIXME: confirm that code below is correct and remove commented code above
00171 
00172 
00173     for (o=0; o < inObservations_; o++)
00174     {
00175       ostringstream oss;
00176       for (t = 0; t < inSamples_; t++)
00177       {
00178         if (t < inSamples_ - 1)
00179         {
00180           oss << out(o,t) << sep;
00181         }
00182         else
00183         {
00184           oss << out(o,t);
00185         }
00186       }
00187       mrs_string s = oss.str();
00188       MRSMSG(s << endl);
00189     }
00190   }
00191 }