Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/peakView.h
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2004 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 #ifndef MARSYAS_PEAKVIEW_H
00020 #define MARSYAS_PEAKVIEW_H
00021 
00022 #include <marsyas/realvec.h>
00023 #include <vector>
00024 #include <marsyas/export.h>
00025 
00026 namespace Marsyas
00027 {
00034 class marsyas_EXPORT peakView
00035 {
00036 public:
00037   enum pkParameter {
00038     pkFrequency,
00039     pkAmplitude,
00040     pkPhase,
00041     pkDeltaFrequency,
00042     pkDeltaAmplitude,
00043     pkFrame,
00044     pkGroup,
00045     pkVolume,
00046     pkPan,
00047     pkBinLow,
00048     pkBin,
00049     pkBinHigh,
00050     pkTrack,
00051     nbPkParameters
00052   };
00053 
00054 private:
00055   realvec& vec_;
00056 
00057   mrs_real fs_;
00058   mrs_natural frameSize_;
00059 
00060   mrs_natural frameMaxNumPeaks_;
00061   mrs_natural numFrames_;
00062 
00063 public:
00064 
00065   peakView(realvec& vec);
00066   ~peakView();
00067 
00068   void fromTable(const realvec& vecTable);
00069   void toTable(realvec& vecTable);
00070 
00071   mrs_real getFs() const {return fs_;};
00072   mrs_natural getFrameSize() const {return frameSize_;};
00073 
00074   mrs_natural getNumFrames() const {return numFrames_;};
00075   mrs_natural getNumGroups();
00076 
00077   mrs_natural getFrameMaxNumPeaks() const {return frameMaxNumPeaks_;};
00078   mrs_natural getFrameNumPeaks(const mrs_natural frame=0, const mrs_natural group=-1) const;
00079   mrs_natural getTotalNumPeaks(const mrs_natural group=-1) const;
00080 
00081   void getPeaksParam(std::vector<realvec>& result, const pkParameter param, mrs_natural startFrame = 0, mrs_natural endFrame = 0) const;
00082 
00083   mrs_real& operator()(const mrs_natural peakIndex, const pkParameter param, const mrs_natural frame=0, const mrs_natural group=-1);
00084   mrs_real operator()(const mrs_natural peakIndex, const pkParameter param, const mrs_natural frame=0, const mrs_natural group=-1) const;
00085 
00086   static std::string getParamName(mrs_natural paramIdx);
00087 
00088   bool peakWrite(std::string filename, mrs_real fs=0, mrs_natural frameSize=0);
00089   bool peakRead(std::string filename);
00090 
00091   void removePeak (const mrs_natural peakIndex, const mrs_natural frame);
00092 };
00093 
00094 inline
00095 mrs_real& peakView::operator()(const mrs_natural peakIndex, const pkParameter param, const mrs_natural frame, const mrs_natural group)
00096 {
00097   //if peakIndex is a global index (i.e. independent of the group)
00098   //just return the corresponding parameter value
00099   if(group == -1)
00100     return vec_(peakIndex + param * frameMaxNumPeaks_, frame);
00101   else
00102   {
00103     //if peakIndex is an index of a peak belonging to the passed group
00104     //we have to convert it to the absolute (i.e. unrelated to any group) index first
00105     mrs_natural gp = 0;
00106     for(mrs_natural p = 0; p < this->getFrameNumPeaks(frame); ++p)
00107     {
00108       if(vec_(p + pkGroup * frameMaxNumPeaks_, frame) == group)
00109       {
00110         if(peakIndex == gp)
00111           return vec_(p + param * frameMaxNumPeaks_, frame);
00112         gp++;
00113       }
00114     }
00115     //if the passed peakIndex does not exist in the passed group
00116     //return an "invalid" value and issue an error...
00117     MRSERR("peakView::operator() - peakIndex " << peakIndex << " not found in passed group " << group);
00118     return vec_(peakIndex + param * frameMaxNumPeaks_, frame);
00119   }
00120 }
00121 
00122 inline
00123 mrs_real peakView::operator()(const mrs_natural peakIndex, const pkParameter param, const mrs_natural frame, const mrs_natural group) const
00124 {
00125   //if peakIndex is a global index (i.e. independent of the group)
00126   //just return the corresponding parameter value
00127   if(group == -1)
00128     return vec_(peakIndex + param * frameMaxNumPeaks_, frame);
00129   else
00130   {
00131     //if peakIndex is an index of a peak belonging to the passed group
00132     //we have to convert it to the absolute (i.e. unrelated to any group) index first
00133     mrs_natural gp = 0;
00134     for(mrs_natural p = 0; p < this->getFrameNumPeaks(frame); ++p)
00135     {
00136       if(vec_(p + pkGroup * frameMaxNumPeaks_, frame) == group)
00137       {
00138         if(peakIndex == gp)
00139           return vec_(p + param * frameMaxNumPeaks_, frame);
00140         gp++;
00141       }
00142     }
00143     //if the passed peakIndex does not exist in the passed group
00144     //return a "dummy" value and issue an error...
00145     MRSERR("peakView::operator() - peakIndex " << peakIndex << " not found in passed group " << group);
00146     return -1.0;
00147   }
00148 }
00149 
00150 }//namespace Marsyas
00151 
00152 #endif /* !MARSYAS_PEAKVIEW_H */
00153