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 "PeakViewSource.h" 00020 #include <marsyas/peakView.h> 00021 00022 using std::ostringstream; 00023 using namespace Marsyas; 00024 00025 PeakViewSource::PeakViewSource(mrs_string name):MarSystem("PeakViewSource", name) 00026 { 00027 addControls(); 00028 filename_ = "EMPTY_STRING"; 00029 frameIdx_ = 0; 00030 numFrames_ = 0; 00031 frameSize_ = 0; 00032 } 00033 00034 PeakViewSource::PeakViewSource(const PeakViewSource& a) : MarSystem(a) 00035 { 00036 ctrl_filename_ = getctrl("mrs_string/filename"); 00037 ctrl_pos_= getctrl("mrs_natural/pos"); 00038 ctrl_size_ = getctrl("mrs_natural/size"); 00039 ctrl_hasData_ = getctrl("mrs_bool/hasData"); 00040 ctrl_frameMaxNumPeaks_ = getctrl("mrs_natural/frameMaxNumPeaks"); 00041 ctrl_totalNumPeaks_ = getctrl("mrs_natural/totalNumPeaks"); 00042 ctrl_nTimes_ = getctrl("mrs_natural/nTimes"); 00043 ctrl_ignGroups_ = getctrl("mrs_bool/ignoreGroups"); 00044 ctrl_noNegativeGroups_ = getctrl("mrs_bool/discardNegativeGroups"); 00045 00046 filename_ = a.filename_; 00047 frameIdx_ = a.frameIdx_; 00048 numFrames_ = a.numFrames_; 00049 frameSize_ = a.frameSize_; 00050 } 00051 00052 PeakViewSource::~PeakViewSource() 00053 { 00054 } 00055 00056 MarSystem* 00057 PeakViewSource::clone() const 00058 { 00059 return new PeakViewSource(*this); 00060 } 00061 00062 void 00063 PeakViewSource::addControls() 00064 { 00065 addctrl("mrs_string/filename", "", ctrl_filename_); 00066 setctrlState("mrs_string/filename", true); 00067 00068 addctrl("mrs_natural/frameMaxNumPeaks", 0, ctrl_frameMaxNumPeaks_); 00069 addctrl("mrs_natural/totalNumPeaks", 0, ctrl_totalNumPeaks_); 00070 addctrl("mrs_natural/nTimes", 1, ctrl_nTimes_); 00071 00072 addctrl("mrs_bool/hasData", false, ctrl_hasData_); 00073 addctrl("mrs_natural/size", 0, ctrl_size_); 00074 addctrl("mrs_natural/pos", 0, ctrl_pos_); 00075 00076 addControl("mrs_bool/ignoreGroups", false, ctrl_ignGroups_); 00077 addControl("mrs_bool/discardNegativeGroups", false, ctrl_noNegativeGroups_); 00078 00079 } 00080 00081 void 00082 PeakViewSource::defaultConfig() 00083 { 00084 ctrl_onSamples_->setValue(1, NOUPDATE); 00085 ctrl_onObservations_->setValue(0, NOUPDATE); 00086 ctrl_osrate_->setValue(0.0, NOUPDATE); 00087 ctrl_onObsNames_->setValue(",", NOUPDATE); 00088 00089 ctrl_pos_->setValue(0, NOUPDATE); 00090 ctrl_size_->setValue(0, NOUPDATE); 00091 ctrl_hasData_->setValue(false, NOUPDATE); 00092 peakData_.create(0); 00093 frameIdx_ = 0; 00094 numFrames_ = 0; 00095 } 00096 00097 void 00098 PeakViewSource::myUpdate(MarControlPtr sender) 00099 { 00100 (void) sender; //suppress warning of unused parameter(s) 00101 //check for a new filename (this is also true for the first call to myUpdate()) 00102 // hack: disabled this so other control can be updated after setting the filename 00103 //if(ctrl_filename_->to<mrs_string>() != filename_) 00104 { 00105 //check if this is the first call to myUpdate() 00106 if(ctrl_filename_->to<mrs_string>().empty()) 00107 { 00108 defaultConfig(); 00109 } 00110 else //not the first call to myUpdate() 00111 { 00112 //try to read entire file into memory [!] 00113 peakView peakDataView(peakData_); 00114 if(peakDataView.peakRead(ctrl_filename_->to<mrs_string>())) 00115 { 00116 numFrames_ = peakDataView.getNumFrames(); 00117 frameSize_ = peakDataView.getFrameSize(); 00118 00119 ctrl_frameMaxNumPeaks_->setValue (peakDataView.getFrameMaxNumPeaks()); 00120 //mrs_natural frameMaxNumPeaks = peakDataView.getFrameMaxNumPeaks(); 00121 00122 ctrl_onSamples_->setValue(ctrl_nTimes_->to<mrs_natural>(), NOUPDATE); 00123 ctrl_onObservations_->setValue(ctrl_frameMaxNumPeaks_->to<mrs_natural>() * peakView::nbPkParameters, NOUPDATE); 00124 ctrl_osrate_->setValue(peakDataView.getFs(), NOUPDATE); 00125 ostringstream oss; 00126 for(mrs_natural j=0; j< peakView::nbPkParameters; ++j) //j = param index 00127 { 00128 for (mrs_natural i=0; i < ctrl_frameMaxNumPeaks_->to<mrs_natural>(); ++i) //i = peak index 00129 oss << peakView::getParamName(j) << "_" << i+j*ctrl_frameMaxNumPeaks_->to<mrs_natural>() << ","; 00130 } 00131 ctrl_onObsNames_->setValue(oss.str(), NOUPDATE); 00132 00133 filename_ = ctrl_filename_->to<mrs_string>(); 00134 frameIdx_ = 0; 00135 ctrl_size_->setValue(numFrames_*frameSize_, NOUPDATE); 00136 ctrl_pos_->setValue(0, NOUPDATE); 00137 ctrl_hasData_->setValue(true, NOUPDATE); 00138 } 00139 else //failed to load the file 00140 { 00141 MRSERR("PeakViewSource::myUpdate() : error opening file: " << filename_); 00142 defaultConfig(); 00143 } 00144 } 00145 } 00146 00147 //[TODO]: Rewind? Reset? Done? [!] 00148 //[TODO]: read directly from file and not load entire .peak file into memory 00149 //[TODO]: allow changing pos control 00150 } 00151 00152 void 00153 PeakViewSource::myProcess(realvec& in, realvec& out) 00154 { 00155 (void) in; 00156 00157 mrs_natural totalNumPeaks = 0, 00158 frameMaxNumPeaks = ctrl_frameMaxNumPeaks_->to<mrs_natural>(); 00159 mrs_natural nTimes = ctrl_nTimes_->to<mrs_natural>(); 00160 mrs_natural numRows = peakData_.getRows(); 00161 const mrs_bool ignoreGroups = ctrl_ignGroups_->to<mrs_bool>(); 00162 00163 00164 for (mrs_natural f = 0; f < nTimes; f++) 00165 { 00166 //at each tick, output peaks for corresponding frame 00167 if(ctrl_hasData_->isTrue()) 00168 { 00169 ctrl_pos_->setValue(frameIdx_*frameSize_); 00170 00171 for(mrs_natural o=0; o < numRows; ++o) 00172 { 00173 mrs_real currData = peakData_(o, frameIdx_); 00174 out(o,f) = currData; 00175 if (o / frameMaxNumPeaks == peakView::pkFrequency) 00176 if (currData != 0) 00177 totalNumPeaks++; 00178 if (ignoreGroups && (o / frameMaxNumPeaks == peakView::pkGroup)) 00179 if (currData < 0) 00180 out(o, f) = 0; 00181 00182 } 00183 00184 frameIdx_++; 00185 if(frameIdx_ == numFrames_)//if EOF 00186 ctrl_hasData_->setValue(false); 00187 } 00188 } 00189 if (ctrl_noNegativeGroups_->to<mrs_bool>()) 00190 { 00191 discardNegativeGroups (out); 00192 totalNumPeaks = peakView(out).getTotalNumPeaks (); 00193 } 00194 00195 ctrl_totalNumPeaks_->setValue(totalNumPeaks); 00196 } 00197 00198 void PeakViewSource::discardNegativeGroups (mrs_realvec &output) 00199 { 00200 peakView Out(output); 00201 mrs_natural numFrames = Out.getNumFrames (); 00202 for (mrs_natural f = numFrames-1; f >= 0 ; f--) 00203 { 00204 mrs_natural frameNumPeaks = Out.getFrameNumPeaks (); 00205 00206 for (mrs_natural i = frameNumPeaks-1; i >= 0; i--) 00207 { 00208 if (Out(i,peakView::pkGroup, f) < 0) 00209 Out.removePeak(i, f); 00210 } 00211 } 00212 }