annotate src/vamp-plugin-sdk-2.5/examples/SpectralCentroid.cpp @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents
children
rev   line source
Chris@23 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@23 2
Chris@23 3 /*
Chris@23 4 Vamp
Chris@23 5
Chris@23 6 An API for audio analysis and feature extraction plugins.
Chris@23 7
Chris@23 8 Centre for Digital Music, Queen Mary, University of London.
Chris@23 9 Copyright 2006 Chris Cannam.
Chris@23 10
Chris@23 11 Permission is hereby granted, free of charge, to any person
Chris@23 12 obtaining a copy of this software and associated documentation
Chris@23 13 files (the "Software"), to deal in the Software without
Chris@23 14 restriction, including without limitation the rights to use, copy,
Chris@23 15 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@23 16 of the Software, and to permit persons to whom the Software is
Chris@23 17 furnished to do so, subject to the following conditions:
Chris@23 18
Chris@23 19 The above copyright notice and this permission notice shall be
Chris@23 20 included in all copies or substantial portions of the Software.
Chris@23 21
Chris@23 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@23 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@23 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@23 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@23 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@23 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@23 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@23 29
Chris@23 30 Except as contained in this notice, the names of the Centre for
Chris@23 31 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@23 32 shall not be used in advertising or otherwise to promote the sale,
Chris@23 33 use or other dealings in this Software without prior written
Chris@23 34 authorization.
Chris@23 35 */
Chris@23 36
Chris@23 37 #include "SpectralCentroid.h"
Chris@23 38
Chris@23 39 using std::string;
Chris@23 40 using std::vector;
Chris@23 41 using std::cerr;
Chris@23 42 using std::endl;
Chris@23 43
Chris@23 44 #include <math.h>
Chris@23 45
Chris@23 46 #ifdef __SUNPRO_CC
Chris@23 47 #include <ieeefp.h>
Chris@23 48 #define isinf(x) (!finite(x))
Chris@23 49 #endif
Chris@23 50
Chris@23 51 #ifdef WIN32
Chris@23 52 #define isnan(x) false
Chris@23 53 #define isinf(x) false
Chris@23 54 #endif
Chris@23 55
Chris@23 56 SpectralCentroid::SpectralCentroid(float inputSampleRate) :
Chris@23 57 Plugin(inputSampleRate),
Chris@23 58 m_stepSize(0),
Chris@23 59 m_blockSize(0)
Chris@23 60 {
Chris@23 61 }
Chris@23 62
Chris@23 63 SpectralCentroid::~SpectralCentroid()
Chris@23 64 {
Chris@23 65 }
Chris@23 66
Chris@23 67 string
Chris@23 68 SpectralCentroid::getIdentifier() const
Chris@23 69 {
Chris@23 70 return "spectralcentroid";
Chris@23 71 }
Chris@23 72
Chris@23 73 string
Chris@23 74 SpectralCentroid::getName() const
Chris@23 75 {
Chris@23 76 return "Spectral Centroid";
Chris@23 77 }
Chris@23 78
Chris@23 79 string
Chris@23 80 SpectralCentroid::getDescription() const
Chris@23 81 {
Chris@23 82 return "Calculate the centroid frequency of the spectrum of the input signal";
Chris@23 83 }
Chris@23 84
Chris@23 85 string
Chris@23 86 SpectralCentroid::getMaker() const
Chris@23 87 {
Chris@23 88 return "Vamp SDK Example Plugins";
Chris@23 89 }
Chris@23 90
Chris@23 91 int
Chris@23 92 SpectralCentroid::getPluginVersion() const
Chris@23 93 {
Chris@23 94 return 2;
Chris@23 95 }
Chris@23 96
Chris@23 97 string
Chris@23 98 SpectralCentroid::getCopyright() const
Chris@23 99 {
Chris@23 100 return "Freely redistributable (BSD license)";
Chris@23 101 }
Chris@23 102
Chris@23 103 bool
Chris@23 104 SpectralCentroid::initialise(size_t channels, size_t stepSize, size_t blockSize)
Chris@23 105 {
Chris@23 106 if (channels < getMinChannelCount() ||
Chris@23 107 channels > getMaxChannelCount()) return false;
Chris@23 108
Chris@23 109 m_stepSize = stepSize;
Chris@23 110 m_blockSize = blockSize;
Chris@23 111
Chris@23 112 return true;
Chris@23 113 }
Chris@23 114
Chris@23 115 void
Chris@23 116 SpectralCentroid::reset()
Chris@23 117 {
Chris@23 118 }
Chris@23 119
Chris@23 120 SpectralCentroid::OutputList
Chris@23 121 SpectralCentroid::getOutputDescriptors() const
Chris@23 122 {
Chris@23 123 OutputList list;
Chris@23 124
Chris@23 125 OutputDescriptor d;
Chris@23 126 d.identifier = "logcentroid";
Chris@23 127 d.name = "Log Frequency Centroid";
Chris@23 128 d.description = "Centroid of the log weighted frequency spectrum";
Chris@23 129 d.unit = "Hz";
Chris@23 130 d.hasFixedBinCount = true;
Chris@23 131 d.binCount = 1;
Chris@23 132 d.hasKnownExtents = false;
Chris@23 133 d.isQuantized = false;
Chris@23 134 d.sampleType = OutputDescriptor::OneSamplePerStep;
Chris@23 135 list.push_back(d);
Chris@23 136
Chris@23 137 d.identifier = "linearcentroid";
Chris@23 138 d.name = "Linear Frequency Centroid";
Chris@23 139 d.description = "Centroid of the linear frequency spectrum";
Chris@23 140 list.push_back(d);
Chris@23 141
Chris@23 142 return list;
Chris@23 143 }
Chris@23 144
Chris@23 145 SpectralCentroid::FeatureSet
Chris@23 146 SpectralCentroid::process(const float *const *inputBuffers, Vamp::RealTime timestamp)
Chris@23 147 {
Chris@23 148 if (m_stepSize == 0) {
Chris@23 149 cerr << "ERROR: SpectralCentroid::process: "
Chris@23 150 << "SpectralCentroid has not been initialised"
Chris@23 151 << endl;
Chris@23 152 return FeatureSet();
Chris@23 153 }
Chris@23 154
Chris@23 155 double numLin = 0.0, numLog = 0.0, denom = 0.0;
Chris@23 156
Chris@23 157 for (size_t i = 1; i <= m_blockSize/2; ++i) {
Chris@23 158 double freq = (double(i) * m_inputSampleRate) / m_blockSize;
Chris@23 159 double real = inputBuffers[0][i*2];
Chris@23 160 double imag = inputBuffers[0][i*2 + 1];
Chris@23 161 double scalemag = sqrt(real * real + imag * imag) / (m_blockSize/2);
Chris@23 162 numLin += freq * scalemag;
Chris@23 163 numLog += log10f(freq) * scalemag;
Chris@23 164 denom += scalemag;
Chris@23 165 }
Chris@23 166
Chris@23 167 FeatureSet returnFeatures;
Chris@23 168
Chris@23 169 if (denom != 0.0) {
Chris@23 170 float centroidLin = float(numLin / denom);
Chris@23 171 float centroidLog = powf(10, float(numLog / denom));
Chris@23 172
Chris@23 173 Feature feature;
Chris@23 174 feature.hasTimestamp = false;
Chris@23 175
Chris@23 176 if (!isnan(centroidLog) && !isinf(centroidLog)) {
Chris@23 177 feature.values.push_back(centroidLog);
Chris@23 178 }
Chris@23 179 returnFeatures[0].push_back(feature);
Chris@23 180
Chris@23 181 feature.values.clear();
Chris@23 182 if (!isnan(centroidLin) && !isinf(centroidLin)) {
Chris@23 183 feature.values.push_back(centroidLin);
Chris@23 184 }
Chris@23 185 returnFeatures[1].push_back(feature);
Chris@23 186 }
Chris@23 187
Chris@23 188 return returnFeatures;
Chris@23 189 }
Chris@23 190
Chris@23 191 SpectralCentroid::FeatureSet
Chris@23 192 SpectralCentroid::getRemainingFeatures()
Chris@23 193 {
Chris@23 194 return FeatureSet();
Chris@23 195 }
Chris@23 196