cannam@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@0: cannam@0: /* cannam@0: Vamp cannam@0: cannam@0: An API for audio analysis and feature extraction plugins. cannam@0: cannam@0: Centre for Digital Music, Queen Mary, University of London. cannam@0: Copyright 2006 Chris Cannam. cannam@0: cannam@0: Permission is hereby granted, free of charge, to any person cannam@0: obtaining a copy of this software and associated documentation cannam@0: files (the "Software"), to deal in the Software without cannam@0: restriction, including without limitation the rights to use, copy, cannam@0: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@0: of the Software, and to permit persons to whom the Software is cannam@0: furnished to do so, subject to the following conditions: cannam@0: cannam@0: The above copyright notice and this permission notice shall be cannam@0: included in all copies or substantial portions of the Software. cannam@0: cannam@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@6: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@0: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@0: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@0: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@0: cannam@0: Except as contained in this notice, the names of the Centre for cannam@0: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@0: shall not be used in advertising or otherwise to promote the sale, cannam@0: use or other dealings in this Software without prior written cannam@0: authorization. cannam@0: */ cannam@0: cannam@0: #include "SpectralCentroid.h" cannam@0: cannam@0: using std::string; cannam@0: using std::vector; cannam@0: using std::cerr; cannam@0: using std::endl; cannam@0: cannam@210: #include cannam@7: cannam@210: #ifdef WIN32 cannam@210: #define isnan(x) false cannam@210: #define isinf(x) false cannam@210: #endif cannam@0: cannam@0: SpectralCentroid::SpectralCentroid(float inputSampleRate) : cannam@0: Plugin(inputSampleRate), cannam@0: m_stepSize(0), cannam@21: m_blockSize(0) cannam@0: { cannam@0: } cannam@0: cannam@0: SpectralCentroid::~SpectralCentroid() cannam@0: { cannam@0: } cannam@0: cannam@0: string cannam@49: SpectralCentroid::getIdentifier() const cannam@0: { cannam@0: return "spectralcentroid"; cannam@0: } cannam@0: cannam@0: string cannam@49: SpectralCentroid::getName() const cannam@49: { cannam@49: return "Spectral Centroid"; cannam@49: } cannam@49: cannam@49: string cannam@0: SpectralCentroid::getDescription() const cannam@0: { cannam@49: return "Calculate the centroid frequency of the spectrum of the input signal"; cannam@0: } cannam@0: cannam@0: string cannam@0: SpectralCentroid::getMaker() const cannam@0: { cannam@43: return "Vamp SDK Example Plugins"; cannam@0: } cannam@0: cannam@0: int cannam@0: SpectralCentroid::getPluginVersion() const cannam@0: { cannam@0: return 2; cannam@0: } cannam@0: cannam@0: string cannam@0: SpectralCentroid::getCopyright() const cannam@0: { cannam@7: return "Freely redistributable (BSD license)"; cannam@0: } cannam@0: cannam@0: bool cannam@0: SpectralCentroid::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@0: { cannam@0: if (channels < getMinChannelCount() || cannam@0: channels > getMaxChannelCount()) return false; cannam@0: cannam@0: m_stepSize = stepSize; cannam@0: m_blockSize = blockSize; cannam@0: cannam@0: return true; cannam@0: } cannam@0: cannam@0: void cannam@0: SpectralCentroid::reset() cannam@0: { cannam@0: } cannam@0: cannam@0: SpectralCentroid::OutputList cannam@0: SpectralCentroid::getOutputDescriptors() const cannam@0: { cannam@0: OutputList list; cannam@0: cannam@0: OutputDescriptor d; cannam@49: d.identifier = "logcentroid"; cannam@49: d.name = "Log Frequency Centroid"; cannam@49: d.description = "Centroid of the log weighted frequency spectrum"; cannam@0: d.unit = "Hz"; cannam@9: d.hasFixedBinCount = true; cannam@9: d.binCount = 1; cannam@0: d.hasKnownExtents = false; cannam@0: d.isQuantized = false; cannam@0: d.sampleType = OutputDescriptor::OneSamplePerStep; cannam@0: list.push_back(d); cannam@0: cannam@49: d.identifier = "linearcentroid"; cannam@49: d.name = "Linear Frequency Centroid"; cannam@49: d.description = "Centroid of the linear frequency spectrum"; cannam@0: list.push_back(d); cannam@0: cannam@0: return list; cannam@0: } cannam@0: cannam@190: //static int scount = 0; cannam@190: cannam@0: SpectralCentroid::FeatureSet cannam@190: SpectralCentroid::process(const float *const *inputBuffers, Vamp::RealTime timestamp) cannam@0: { cannam@0: if (m_stepSize == 0) { cannam@0: cerr << "ERROR: SpectralCentroid::process: " cannam@0: << "SpectralCentroid has not been initialised" cannam@0: << endl; cannam@0: return FeatureSet(); cannam@0: } cannam@0: cannam@190: // std::cerr << "SpectralCentroid::process: count = " << scount++ << ", timestamp = " << timestamp << ", total power = "; cannam@190: cannam@0: double numLin = 0.0, numLog = 0.0, denom = 0.0; cannam@0: cannam@47: for (size_t i = 1; i <= m_blockSize/2; ++i) { cannam@0: double freq = (double(i) * m_inputSampleRate) / m_blockSize; cannam@0: double real = inputBuffers[0][i*2]; cannam@0: double imag = inputBuffers[0][i*2 + 1]; cannam@0: double power = sqrt(real * real + imag * imag) / (m_blockSize/2); cannam@0: numLin += freq * power; cannam@0: numLog += log10f(freq) * power; cannam@0: denom += power; cannam@0: } cannam@0: cannam@190: // std::cerr << denom << std::endl; cannam@190: cannam@0: FeatureSet returnFeatures; cannam@0: cannam@0: if (denom != 0.0) { cannam@0: float centroidLin = float(numLin / denom); cannam@19: float centroidLog = powf(10, float(numLog / denom)); cannam@47: cannam@0: Feature feature; cannam@0: feature.hasTimestamp = false; cannam@210: if (!isnan(centroidLog) && !isinf(centroidLog)) { cannam@210: feature.values.push_back(centroidLog); cannam@210: } cannam@0: returnFeatures[0].push_back(feature); cannam@47: cannam@210: feature.values.clear(); cannam@210: if (!isnan(centroidLin) && !isinf(centroidLin)) { cannam@210: feature.values.push_back(centroidLin); cannam@210: } cannam@0: returnFeatures[1].push_back(feature); cannam@0: } cannam@0: cannam@0: return returnFeatures; cannam@0: } cannam@0: cannam@0: SpectralCentroid::FeatureSet cannam@0: SpectralCentroid::getRemainingFeatures() cannam@0: { cannam@0: return FeatureSet(); cannam@0: } cannam@0: