1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 An API for audio analysis and feature extraction plugins.
8 Centre for Digital Music, Queen Mary, University of London.
9 Copyright 2006 Chris Cannam.
11 Permission is hereby granted, free of charge, to any person
12 obtaining a copy of this software and associated documentation
13 files (the "Software"), to deal in the Software without
14 restriction, including without limitation the rights to use, copy,
15 modify, merge, publish, distribute, sublicense, and/or sell copies
16 of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
19 The above copyright notice and this permission notice shall be
20 included in all copies or substantial portions of the Software.
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 Except as contained in this notice, the names of the Centre for
31 Digital Music; Queen Mary, University of London; and Chris Cannam
32 shall not be used in advertising or otherwise to promote the sale,
33 use or other dealings in this Software without prior written
42 // 'std::isinf()' and 'std::isnan()' are not available in MSVC.
43 #define isinf_local(val) !((bool)_finite((double)val))
44 #define isnan_local(val) (bool)_isnan((double)val)
46 #define isinf_local std::isinf
47 #define isnan_local std::isnan
50 #include "SpectralCentroid.h"
58 SpectralCentroid::SpectralCentroid(float inputSampleRate) :
59 Plugin(inputSampleRate),
65 SpectralCentroid::~SpectralCentroid()
70 SpectralCentroid::getIdentifier() const
72 return "spectralcentroid";
76 SpectralCentroid::getName() const
78 return "Spectral Centroid";
82 SpectralCentroid::getDescription() const
84 return "Calculate the centroid frequency of the spectrum of the input signal";
88 SpectralCentroid::getMaker() const
90 return "Vamp SDK Example Plugins";
94 SpectralCentroid::getPluginVersion() const
100 SpectralCentroid::getCopyright() const
102 return "Freely redistributable (BSD license)";
106 SpectralCentroid::initialise(size_t channels, size_t stepSize, size_t blockSize)
108 if (channels < getMinChannelCount() ||
109 channels > getMaxChannelCount()) return false;
111 m_stepSize = stepSize;
112 m_blockSize = blockSize;
118 SpectralCentroid::reset()
122 SpectralCentroid::OutputList
123 SpectralCentroid::getOutputDescriptors() const
128 d.identifier = "logcentroid";
129 d.name = "Log Frequency Centroid";
130 d.description = "Centroid of the log weighted frequency spectrum";
132 d.hasFixedBinCount = true;
134 d.hasKnownExtents = false;
135 d.isQuantized = false;
136 d.sampleType = OutputDescriptor::OneSamplePerStep;
139 d.identifier = "linearcentroid";
140 d.name = "Linear Frequency Centroid";
141 d.description = "Centroid of the linear frequency spectrum";
147 SpectralCentroid::FeatureSet
148 SpectralCentroid::process(const float *const *inputBuffers, Vamp::RealTime)
150 if (m_stepSize == 0) {
151 cerr << "ERROR: SpectralCentroid::process: "
152 << "SpectralCentroid has not been initialised"
157 double numLin = 0.0, numLog = 0.0, denom = 0.0;
159 for (size_t i = 1; i <= m_blockSize/2; ++i) {
160 double freq = (double(i) * m_inputSampleRate) / m_blockSize;
161 double real = inputBuffers[0][i*2];
162 double imag = inputBuffers[0][i*2 + 1];
163 double power = sqrt(real * real + imag * imag) / (m_blockSize/2);
164 numLin += freq * power;
165 numLog += log10f(freq) * power;
169 FeatureSet returnFeatures;
171 // std::cerr << "power " << denom << ", block size " << m_blockSize << std::endl;
174 float centroidLin = float(numLin / denom);
175 float centroidLog = powf(10, float(numLog / denom));
178 feature.hasTimestamp = false;
179 if (!isnan_local(centroidLog) && !isinf_local(centroidLog)) {
180 feature.values.push_back(centroidLog);
182 returnFeatures[0].push_back(feature);
184 feature.values.clear();
185 if (!isnan_local(centroidLin) && !isinf_local(centroidLin)) {
186 feature.values.push_back(centroidLin);
188 returnFeatures[1].push_back(feature);
191 return returnFeatures;
194 SpectralCentroid::FeatureSet
195 SpectralCentroid::getRemainingFeatures()