cannam@0: cannam@0: cannam@0: VampPluginSDK: SpectralCentroid.cpp Source File cannam@35: cannam@0: cannam@0: cannam@35: cannam@0: cannam@21:
cannam@0:

SpectralCentroid.cpp

Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
cannam@0: 00002 
cannam@0: 00003 /*
cannam@0: 00004     Vamp
cannam@0: 00005 
cannam@0: 00006     An API for audio analysis and feature extraction plugins.
cannam@0: 00007 
cannam@0: 00008     Centre for Digital Music, Queen Mary, University of London.
cannam@0: 00009     Copyright 2006 Chris Cannam.
cannam@0: 00010   
cannam@0: 00011     Permission is hereby granted, free of charge, to any person
cannam@0: 00012     obtaining a copy of this software and associated documentation
cannam@0: 00013     files (the "Software"), to deal in the Software without
cannam@0: 00014     restriction, including without limitation the rights to use, copy,
cannam@0: 00015     modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@0: 00016     of the Software, and to permit persons to whom the Software is
cannam@0: 00017     furnished to do so, subject to the following conditions:
cannam@0: 00018 
cannam@0: 00019     The above copyright notice and this permission notice shall be
cannam@0: 00020     included in all copies or substantial portions of the Software.
cannam@0: 00021 
cannam@0: 00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@0: 00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@0: 00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@0: 00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@0: 00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@0: 00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@0: 00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@0: 00029 
cannam@0: 00030     Except as contained in this notice, the names of the Centre for
cannam@0: 00031     Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@0: 00032     shall not be used in advertising or otherwise to promote the sale,
cannam@0: 00033     use or other dealings in this Software without prior written
cannam@0: 00034     authorization.
cannam@0: 00035 */
cannam@0: 00036 
cannam@0: 00037 #include "SpectralCentroid.h"
cannam@0: 00038 
cannam@0: 00039 using std::string;
cannam@0: 00040 using std::vector;
cannam@0: 00041 using std::cerr;
cannam@0: 00042 using std::endl;
cannam@0: 00043 
cannam@21: 00044 #include <math.h>
cannam@0: 00045 
cannam@35: 00046 #ifdef __SUNPRO_CC
cannam@35: 00047 #include <ieeefp.h>
cannam@35: 00048 #define isinf(x) (!finite(x))
cannam@21: 00049 #endif
cannam@21: 00050 
cannam@35: 00051 #ifdef WIN32
cannam@35: 00052 #define isnan(x) false
cannam@35: 00053 #define isinf(x) false
cannam@35: 00054 #endif
cannam@35: 00055 
cannam@35: 00056 SpectralCentroid::SpectralCentroid(float inputSampleRate) :
cannam@35: 00057     Plugin(inputSampleRate),
cannam@35: 00058     m_stepSize(0),
cannam@35: 00059     m_blockSize(0)
cannam@35: 00060 {
cannam@35: 00061 }
cannam@35: 00062 
cannam@35: 00063 SpectralCentroid::~SpectralCentroid()
cannam@35: 00064 {
cannam@35: 00065 }
cannam@35: 00066 
cannam@35: 00067 string
cannam@35: 00068 SpectralCentroid::getIdentifier() const
cannam@35: 00069 {
cannam@35: 00070     return "spectralcentroid";
cannam@35: 00071 }
cannam@35: 00072 
cannam@35: 00073 string
cannam@35: 00074 SpectralCentroid::getName() const
cannam@35: 00075 {
cannam@35: 00076     return "Spectral Centroid";
cannam@35: 00077 }
cannam@35: 00078 
cannam@35: 00079 string
cannam@35: 00080 SpectralCentroid::getDescription() const
cannam@35: 00081 {
cannam@35: 00082     return "Calculate the centroid frequency of the spectrum of the input signal";
cannam@35: 00083 }
cannam@35: 00084 
cannam@35: 00085 string
cannam@35: 00086 SpectralCentroid::getMaker() const
cannam@35: 00087 {
cannam@35: 00088     return "Vamp SDK Example Plugins";
cannam@35: 00089 }
cannam@35: 00090 
cannam@35: 00091 int
cannam@35: 00092 SpectralCentroid::getPluginVersion() const
cannam@35: 00093 {
cannam@35: 00094     return 2;
cannam@35: 00095 }
cannam@35: 00096 
cannam@35: 00097 string
cannam@35: 00098 SpectralCentroid::getCopyright() const
cannam@35: 00099 {
cannam@35: 00100     return "Freely redistributable (BSD license)";
cannam@35: 00101 }
cannam@35: 00102 
cannam@35: 00103 bool
cannam@35: 00104 SpectralCentroid::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@35: 00105 {
cannam@35: 00106     if (channels < getMinChannelCount() ||
cannam@35: 00107         channels > getMaxChannelCount()) return false;
cannam@35: 00108 
cannam@35: 00109     m_stepSize = stepSize;
cannam@35: 00110     m_blockSize = blockSize;
cannam@35: 00111 
cannam@35: 00112     return true;
cannam@21: 00113 }
cannam@21: 00114 
cannam@35: 00115 void
cannam@35: 00116 SpectralCentroid::reset()
cannam@35: 00117 {
cannam@35: 00118 }
cannam@21: 00119 
cannam@35: 00120 SpectralCentroid::OutputList
cannam@35: 00121 SpectralCentroid::getOutputDescriptors() const
cannam@35: 00122 {
cannam@35: 00123     OutputList list;
cannam@35: 00124 
cannam@35: 00125     OutputDescriptor d;
cannam@35: 00126     d.identifier = "logcentroid";
cannam@35: 00127     d.name = "Log Frequency Centroid";
cannam@35: 00128     d.description = "Centroid of the log weighted frequency spectrum";
cannam@35: 00129     d.unit = "Hz";
cannam@35: 00130     d.hasFixedBinCount = true;
cannam@35: 00131     d.binCount = 1;
cannam@35: 00132     d.hasKnownExtents = false;
cannam@35: 00133     d.isQuantized = false;
cannam@35: 00134     d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@21: 00135     list.push_back(d);
cannam@21: 00136 
cannam@35: 00137     d.identifier = "linearcentroid";
cannam@35: 00138     d.name = "Linear Frequency Centroid";
cannam@35: 00139     d.description = "Centroid of the linear frequency spectrum";
cannam@35: 00140     list.push_back(d);
cannam@35: 00141 
cannam@35: 00142     return list;
cannam@35: 00143 }
cannam@35: 00144 
cannam@35: 00145 SpectralCentroid::FeatureSet
cannam@35: 00146 SpectralCentroid::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
cannam@35: 00147 {
cannam@35: 00148     if (m_stepSize == 0) {
cannam@35: 00149         cerr << "ERROR: SpectralCentroid::process: "
cannam@35: 00150              << "SpectralCentroid has not been initialised"
cannam@35: 00151              << endl;
cannam@35: 00152         return FeatureSet();
cannam@35: 00153     }
cannam@35: 00154 
cannam@35: 00155     double numLin = 0.0, numLog = 0.0, denom = 0.0;
cannam@35: 00156 
cannam@35: 00157     for (size_t i = 1; i <= m_blockSize/2; ++i) {
cannam@35: 00158         double freq = (double(i) * m_inputSampleRate) / m_blockSize;
cannam@35: 00159         double real = inputBuffers[0][i*2];
cannam@35: 00160         double imag = inputBuffers[0][i*2 + 1];
cannam@35: 00161         double scalemag = sqrt(real * real + imag * imag) / (m_blockSize/2);
cannam@35: 00162         numLin += freq * scalemag;
cannam@35: 00163         numLog += log10f(freq) * scalemag;
cannam@35: 00164         denom += scalemag;
cannam@35: 00165     }
cannam@35: 00166 
cannam@35: 00167     FeatureSet returnFeatures;
cannam@35: 00168 
cannam@35: 00169     if (denom != 0.0) {
cannam@35: 00170         float centroidLin = float(numLin / denom);
cannam@35: 00171         float centroidLog = powf(10, float(numLog / denom));
cannam@35: 00172 
cannam@35: 00173         Feature feature;
cannam@35: 00174         feature.hasTimestamp = false;
cannam@21: 00175 
cannam@35: 00176         if (!isnan(centroidLog) && !isinf(centroidLog)) {
cannam@35: 00177             feature.values.push_back(centroidLog);
cannam@35: 00178         }
cannam@35: 00179         returnFeatures[0].push_back(feature);
cannam@35: 00180 
cannam@35: 00181         feature.values.clear();
cannam@35: 00182         if (!isnan(centroidLin) && !isinf(centroidLin)) {
cannam@35: 00183             feature.values.push_back(centroidLin);
cannam@35: 00184         }
cannam@35: 00185         returnFeatures[1].push_back(feature);
cannam@35: 00186     }
cannam@35: 00187 
cannam@35: 00188     return returnFeatures;
cannam@35: 00189 }
cannam@35: 00190 
cannam@35: 00191 SpectralCentroid::FeatureSet
cannam@35: 00192 SpectralCentroid::getRemainingFeatures()
cannam@35: 00193 {
cannam@35: 00194     return FeatureSet();
cannam@35: 00195 }
cannam@35: 00196 
cannam@0: 
cannam@35:
Generated on Thu Sep 24 13:40:13 2009 for VampPluginSDK by  cannam@0: cannam@35: doxygen 1.5.8
cannam@0: cannam@0: