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