Marsyas
0.6.0-alpha
|
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 "PeakViewSink.h" 00020 #include "../common_source.h" 00021 00022 #include <marsyas/peakView.h> 00023 00024 00025 using std::ostringstream; 00026 using std::ifstream; 00027 using std::ios; 00028 using std::endl; 00029 using std::cout; 00030 using std::istringstream; 00031 00032 00033 00034 using namespace Marsyas; 00035 00036 PeakViewSink::PeakViewSink(mrs_string name):MarSystem("PeakViewSink",name) 00037 { 00038 count_= 0; 00039 tmpFilename_ = "defaultfile.tmp"; 00040 addControls(); 00041 } 00042 00043 PeakViewSink::PeakViewSink(const PeakViewSink& a):MarSystem(a) 00044 { 00045 count_ = a.count_; 00046 tmpFilename_ ="defaultfile.tmp"; 00047 00048 ctrl_accumulate2Disk_ = getctrl("mrs_bool/accumulate2Disk"); 00049 ctrl_fs_ = getctrl("mrs_real/fs"); 00050 ctrl_frameSize_ = getctrl("mrs_natural/frameSize"); 00051 ctrl_filename_ = getctrl("mrs_string/filename"); 00052 ctrl_done_ = getctrl("mrs_bool/done"); 00053 } 00054 00055 PeakViewSink::~PeakViewSink() 00056 { 00057 if(tmpFile_.is_open()) 00058 tmpFile_.close(); 00059 } 00060 00061 MarSystem* 00062 PeakViewSink::clone() const 00063 { 00064 return new PeakViewSink(*this); 00065 } 00066 00067 void 00068 PeakViewSink::addControls() 00069 { 00070 addctrl("mrs_bool/done", false, ctrl_done_); 00071 ctrl_done_->setState(true); 00072 00073 addctrl("mrs_string/filename", "defaultfile.peak", ctrl_filename_); 00074 00075 addctrl("mrs_bool/accumulate2Disk", true, ctrl_accumulate2Disk_); 00076 addctrl("mrs_real/fs", 0.0, ctrl_fs_); 00077 addctrl("mrs_natural/frameSize", 0, ctrl_frameSize_); 00078 } 00079 00080 void 00081 PeakViewSink::done() 00082 { 00083 mrs_natural t,o; 00084 if(ctrl_accumulate2Disk_->isTrue()) 00085 { 00086 00087 //read from tmp file and get rid of it (we're done with it ;-)) 00088 if(tmpFile_.is_open()) 00089 { 00090 //close the accumulated tmp data file (no more writting) 00091 tmpFile_.close(); 00092 00093 //open the tmp file for reading 00094 ifstream tmpFile; 00095 tmpFile.open(tmpFilename_.c_str(), ios::in); 00096 00097 //read data from tmp file intomemory 00098 accumData_.create(inObservations_, count_); 00099 00100 00101 char myline[2048]; 00102 00103 for(t=0; t < count_; ++t) 00104 { 00105 tmpFile.getline(myline, 2048); 00106 istringstream iss(myline); 00107 for(o=0; o < inObservations_; ++o) 00108 { 00109 00110 iss >> accumData_(o,t); 00111 } 00112 } 00113 00114 //close and delete tmp file from disk 00115 tmpFile.close(); 00116 // remove(tmpFilename_.c_str()); 00117 count_ = 0; 00118 } 00119 else 00120 accumData_.create(0,0); //no data was accumulated in the temp file... 00121 } 00122 00123 //save peak data into a .peak formated output file 00124 if(accumData_.getSize() != 0) 00125 { 00126 peakView accumDataView(accumData_); 00127 accumDataView.peakWrite(ctrl_filename_->to<mrs_string>(), 00128 ctrl_fs_->to<mrs_real>(), ctrl_frameSize_->to<mrs_natural>()); 00129 } 00130 00131 //reset internal data 00132 accumData_.create(0, 0); 00133 ctrl_done_->setValue(false, NOUPDATE); 00134 } 00135 00136 void 00137 PeakViewSink::myUpdate(MarControlPtr sender) 00138 { 00139 (void) sender; //suppress warning of unused parameter(s) 00140 MRSDIAG("PeakViewSink.cpp - PeakViewSink:myUpdate"); 00141 00142 ctrl_onObservations_->setValue(ctrl_inObservations_, NOUPDATE); 00143 ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE); 00144 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE); 00145 ctrl_onObsNames_->setValue(ctrl_inObsNames_, NOUPDATE); 00146 00147 if(ctrl_done_->isTrue()) 00148 done(); 00149 } 00150 00151 void 00152 PeakViewSink::myProcess(realvec& in, realvec& out) 00153 { 00154 out = in; 00155 mrs_natural o,t; 00156 00157 00158 00159 if(ctrl_accumulate2Disk_->isTrue()) 00160 { 00161 00162 00163 00164 //if a tmp file is not yet created, create one 00165 if(!tmpFile_.is_open()) 00166 { 00167 tmpFilename_ = this->getType() + "_" + this->getName() + ".tmp"; 00168 tmpFile_.open(tmpFilename_.c_str(), ios::out | ios::trunc); 00169 count_ = 0; 00170 } 00171 00172 //accumulate data into a temp file 00173 for (t=0; t < inSamples_; t++) 00174 { 00175 for (o=0 ; o<inObservations_ ; o++) 00176 tmpFile_ << in(o, t) << " " ; 00177 tmpFile_ << endl; 00178 } 00179 count_ += inSamples_; 00180 } 00181 else 00182 { 00183 //accumulate input data into memory 00184 mrs_natural cols = accumData_.getCols(); 00185 accumData_.stretch(inObservations_, cols+inSamples_); 00186 for (o=0; o < inObservations_; o++) 00187 for (t=0; t < inSamples_; t++) 00188 { 00189 accumData_(o, cols+t) = in(o, t); 00190 } 00191 } 00192 }