annotate src/vamp-plugin-sdk-2.4/examples/SpectralCentroid.cpp @ 83:ae30d91d2ffe

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