annotate src/vamp-plugin-sdk-2.4/examples/SpectralCentroid.cpp @ 169:223a55898ab9 tip default

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