Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/marsystems/PeakFeatureSelect.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2006 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 "PeakFeatureSelect.h"
00020 #include <marsyas/peakView.h>
00021 
00022 using std::ostringstream;
00023 using namespace Marsyas;
00024 
00025 PeakFeatureSelect::PeakFeatureSelect(mrs_string name):MarSystem("PeakFeatureSelect", name)
00026 {
00027   addControls();
00028 }
00029 
00030 PeakFeatureSelect::PeakFeatureSelect(const PeakFeatureSelect& a) : MarSystem(a)
00031 {
00032   ctrl_selectedFeatures_ = getctrl("mrs_natural/selectedFeatures");
00033   ctrl_totalNumPeaks_ = getctrl("mrs_natural/totalNumPeaks");
00034   ctrl_frameMaxNumPeaks_ = getctrl("mrs_natural/frameMaxNumPeaks");
00035 
00036   selectedFeatures_ = 0;
00037   frameMaxNumPeaks_ = 0;
00038   numFeats_ = 0;
00039 }
00040 
00041 PeakFeatureSelect::~PeakFeatureSelect()
00042 {
00043 }
00044 
00045 MarSystem*
00046 PeakFeatureSelect::clone() const
00047 {
00048   return new PeakFeatureSelect(*this);
00049 }
00050 
00051 void
00052 PeakFeatureSelect::addControls()
00053 {
00054   addctrl("mrs_natural/selectedFeatures", 0, ctrl_selectedFeatures_);
00055   ctrl_selectedFeatures_->setState(true);
00056 
00057   addctrl("mrs_natural/totalNumPeaks", 0, ctrl_totalNumPeaks_);
00058   ctrl_totalNumPeaks_->setState(true);
00059 
00060   addctrl("mrs_natural/frameMaxNumPeaks", 0, ctrl_frameMaxNumPeaks_);
00061   ctrl_frameMaxNumPeaks_->setState(true);
00062 
00063   selectedFeatures_ = 0;
00064   frameMaxNumPeaks_ = 0;
00065 
00066   numFeats_ = 0;
00067 }
00068 
00069 void
00070 PeakFeatureSelect::myUpdate(MarControlPtr sender)
00071 {
00072   (void) sender;  //suppress warning of unused parameter(s)
00073   if(ctrl_selectedFeatures_->to<mrs_natural>() != selectedFeatures_ ||
00074       ctrl_frameMaxNumPeaks_->to<mrs_natural>() != frameMaxNumPeaks_)
00075   {
00076     selectedFeatures_ = ctrl_selectedFeatures_->to<mrs_natural>();
00077     frameMaxNumPeaks_ = ctrl_frameMaxNumPeaks_->to<mrs_natural>();
00078 
00079     //determine the number of selected features to output, per peak
00080     numFeats_ = 0;
00081     ostringstream oss;
00082     if(selectedFeatures_ & PeakFeatureSelect::pkFrequency)
00083     {
00084       numFeats_++;
00085       oss << "pkFrequency,";
00086     }
00087     if(selectedFeatures_ & PeakFeatureSelect::pkAmplitude)
00088     {
00089       numFeats_++;
00090       oss << "pkAmplitude,";
00091     }
00092     if(selectedFeatures_ & PeakFeatureSelect::pkDeltaFrequency)
00093     {
00094       numFeats_++;
00095       oss << "pkDeltaFrequency,";
00096     }
00097     if(selectedFeatures_ & PeakFeatureSelect::pkDeltaAmplitude)
00098     {
00099       numFeats_++;
00100       oss << "pkDeltaAmplitude,";
00101     }
00102     if(selectedFeatures_ & PeakFeatureSelect::pkFrame)
00103     {
00104       numFeats_++;
00105       oss << "pkFrame,";
00106     }
00107     if(selectedFeatures_ & PeakFeatureSelect::pkPan)
00108     {
00109       numFeats_++;
00110       oss << "pkPan,";
00111     }
00112     //-----------------------------------------------------
00113     if(selectedFeatures_ & (PeakFeatureSelect::pkSetFrequencies |
00114                             PeakFeatureSelect::pkSetAmplitudes |
00115                             PeakFeatureSelect::pkSetFrames))
00116     {
00117       numFeats_++;
00118       oss << "frameNumPeaks,";
00119     }
00120     if(selectedFeatures_ & PeakFeatureSelect::pkSetFrequencies)
00121     {
00122       for(mrs_natural i = 0; i < frameMaxNumPeaks_; ++i)//These obsNames are incorrect when frameNumPeaks < maxFrameNumPeaks!! [TODO]
00123         oss << "pk_"<< i << "_Frequency,";
00124       numFeats_ += frameMaxNumPeaks_;
00125     }
00126     if(selectedFeatures_ & PeakFeatureSelect::pkSetAmplitudes)
00127     {
00128       for(mrs_natural i = 0; i < frameMaxNumPeaks_; ++i) //These obsNames are incorrect when frameNumPeaks < maxFrameNumPeaks!! [TODO]
00129         oss << "pk_"<< i << "_Amplitude,";
00130       numFeats_ += frameMaxNumPeaks_;
00131     }
00132     if(selectedFeatures_ & PeakFeatureSelect::pkSetFrames)
00133     {
00134       for(mrs_natural i = 0; i < frameMaxNumPeaks_; ++i) //These obsNames are incorrect when frameNumPeaks < maxFrameNumPeaks!! [TODO]
00135         oss << "pk_"<< i << "_Frame,";
00136       numFeats_ += frameMaxNumPeaks_;
00137     }
00138     if(numFeats_ == 0)
00139       oss << ",";
00140 
00141     ctrl_onObsNames_->setValue(oss.str(), NOUPDATE);
00142   }
00143 
00144   ctrl_onSamples_->setValue(ctrl_totalNumPeaks_->to<mrs_natural>(), NOUPDATE);
00145   ctrl_onObservations_->setValue(numFeats_, NOUPDATE);
00146   ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00147 }
00148 
00149 void
00150 PeakFeatureSelect::myProcess(realvec& in, realvec& out)
00151 {
00152   peakView inPeakView(in);
00153 
00154   //get the total number of peaks in input texture window,
00155   //and reconfigure output flow controls (i.e. call update())
00156   //ctrl_frameMaxNumPeaks_->setValue(inPeakView.getFrameMaxNumPeaks(), NOUPDATE);
00157   //ctrl_totalNumPeaks_->setValue(inPeakView.getTotalNumPeaks()); //update is called here!
00158 
00159   //if there is at least one peak at the input, and at least one selected feature
00160   //fill the output realvec (otherwise there is no need to do anything else)
00161   if(ctrl_totalNumPeaks_->to<mrs_natural>() > 0 && ctrl_selectedFeatures_->to<mrs_natural>() != 0)
00162   {
00164     //fill the output realvec with the selected feature vector for each peak
00166     mrs_natural peak_index = 0;
00167     for(mrs_natural f=0; f < inPeakView.getNumFrames(); ++f)//frame loop
00168     {
00169       mrs_natural frameNumPeaks = inPeakView.getFrameNumPeaks(f);
00170       for(mrs_natural p=0; p<frameNumPeaks; ++p)//iterate over peaks in this frame (if any)
00171       {
00172         mrs_natural feat_index = 0;
00173 
00174         if(selectedFeatures_ & PeakFeatureSelect::pkFrequency)
00175         {
00176           out(feat_index, peak_index) = inPeakView(p, peakView::pkFrequency, f);
00177           if(selectedFeatures_ & PeakFeatureSelect::barkPkFreq)
00178           {
00179             out(feat_index, peak_index) = hertz2bark(out(feat_index, peak_index));
00180           }
00181           feat_index++;
00182         }
00183         if(selectedFeatures_ & PeakFeatureSelect::pkAmplitude)
00184         {
00185           out(feat_index, peak_index) = inPeakView(p, peakView::pkAmplitude, f);
00186           if(selectedFeatures_ & PeakFeatureSelect::dBPkAmp)
00187           {
00188             out(feat_index, peak_index) = amplitude2dB(out(feat_index, peak_index));
00189           }
00190           feat_index++;
00191         }
00192         if(selectedFeatures_ & PeakFeatureSelect::pkDeltaFrequency)
00193         {
00194           out(feat_index, peak_index) = inPeakView(p, peakView::pkDeltaFrequency, f);
00195           if(selectedFeatures_ & PeakFeatureSelect::barkPkFreq)
00196           {
00197             out(feat_index, peak_index) = hertz2bark(out(feat_index, peak_index));
00198           }
00199           feat_index++;
00200         }
00201         if(selectedFeatures_ & PeakFeatureSelect::pkDeltaAmplitude)
00202         {
00203           out(feat_index, peak_index) = inPeakView(p, peakView::pkDeltaAmplitude    , f);
00204           if(selectedFeatures_ & PeakFeatureSelect::dBPkAmp)
00205           {
00206             out(feat_index, peak_index) = amplitude2dB(out(feat_index, peak_index));
00207           }
00208           feat_index++;
00209         }
00210         if(selectedFeatures_ & PeakFeatureSelect::pkFrame)
00211         {
00212           out(feat_index, peak_index) = inPeakView(p, peakView::pkFrame, f);
00213           feat_index++;
00214         }
00215         if(selectedFeatures_ & PeakFeatureSelect::pkPan)
00216         {
00217           out(feat_index, peak_index) = inPeakView(p, peakView::pkPan, f);
00218           feat_index++;
00219         }
00220         //-----------------------------------------------------------------------------
00221         if(selectedFeatures_ & (PeakFeatureSelect::pkSetFrequencies |
00222                                 PeakFeatureSelect::pkSetAmplitudes |
00223                                 PeakFeatureSelect::pkSetFrames))
00224         {
00225           out(feat_index, peak_index) = frameNumPeaks;
00226           feat_index++;
00227         }
00228         if(selectedFeatures_ & PeakFeatureSelect::pkSetFrequencies)
00229         {
00230           //for each peak, just get all the feats of the peaks in the same frame
00231           for(mrs_natural i=0; i < frameNumPeaks; ++i)
00232           {
00233             out(feat_index, peak_index) = inPeakView(i, peakView::pkFrequency, f);
00234             feat_index++;
00235           }
00236         }
00237         if(selectedFeatures_ & PeakFeatureSelect::pkSetAmplitudes)
00238         {
00239           //for each peak, just get all the feats of the peaks in the same frame
00240           for(mrs_natural i=0; i < frameNumPeaks; ++i)
00241           {
00242             out(feat_index, peak_index) = inPeakView(i, peakView::pkAmplitude, f);
00243             feat_index++;
00244           }
00245         }
00246         if(selectedFeatures_ & PeakFeatureSelect::pkSetFrames)
00247         {
00248           //for each peak, just get all the feats of the peaks in the same frame
00249           for(mrs_natural i=0; i < frameNumPeaks; ++i)
00250           {
00251             out(feat_index, peak_index) = inPeakView(i, peakView::pkFrame, f);
00252             feat_index++;
00253           }
00254         }
00255         //move on to next peak
00256         peak_index++;
00257       }
00258     }
00259   }
00260 }