annotate examples/PowerSpectrum.cpp @ 468:1cdcd2ee3d91

Fix long-standing memory leaks (parameter descriptors and feature list container) in plugin adapter
author Chris Cannam
date Fri, 14 Oct 2016 09:32:11 +0100
parents 7d59dd1ba5de
children
rev   line source
cannam@241 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@241 2
cannam@241 3 /*
cannam@241 4 Vamp
cannam@241 5
cannam@241 6 An API for audio analysis and feature extraction plugins.
cannam@241 7
cannam@241 8 Centre for Digital Music, Queen Mary, University of London.
cannam@241 9 Copyright 2008 QMUL.
cannam@241 10
cannam@241 11 Permission is hereby granted, free of charge, to any person
cannam@241 12 obtaining a copy of this software and associated documentation
cannam@241 13 files (the "Software"), to deal in the Software without
cannam@241 14 restriction, including without limitation the rights to use, copy,
cannam@241 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@241 16 of the Software, and to permit persons to whom the Software is
cannam@241 17 furnished to do so, subject to the following conditions:
cannam@241 18
cannam@241 19 The above copyright notice and this permission notice shall be
cannam@241 20 included in all copies or substantial portions of the Software.
cannam@241 21
cannam@241 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@241 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@241 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@241 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@241 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@241 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@241 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@241 29
cannam@241 30 Except as contained in this notice, the names of the Centre for
cannam@241 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@241 32 shall not be used in advertising or otherwise to promote the sale,
cannam@241 33 use or other dealings in this Software without prior written
cannam@241 34 authorization.
cannam@241 35 */
cannam@241 36
cannam@241 37 #include "PowerSpectrum.h"
cannam@241 38
cannam@241 39 using std::string;
cannam@241 40 using std::cerr;
cannam@241 41 using std::endl;
cannam@241 42
cannam@241 43 #include <math.h>
cannam@241 44
cannam@241 45 PowerSpectrum::PowerSpectrum(float inputSampleRate) :
cannam@241 46 Plugin(inputSampleRate),
cannam@241 47 m_blockSize(0)
cannam@241 48 {
cannam@241 49 }
cannam@241 50
cannam@241 51 PowerSpectrum::~PowerSpectrum()
cannam@241 52 {
cannam@241 53 }
cannam@241 54
cannam@241 55 string
cannam@241 56 PowerSpectrum::getIdentifier() const
cannam@241 57 {
cannam@241 58 return "powerspectrum";
cannam@241 59 }
cannam@241 60
cannam@241 61 string
cannam@241 62 PowerSpectrum::getName() const
cannam@241 63 {
cannam@241 64 return "Simple Power Spectrum";
cannam@241 65 }
cannam@241 66
cannam@241 67 string
cannam@241 68 PowerSpectrum::getDescription() const
cannam@241 69 {
cannam@241 70 return "Return the power spectrum of a signal";
cannam@241 71 }
cannam@241 72
cannam@241 73 string
cannam@241 74 PowerSpectrum::getMaker() const
cannam@241 75 {
cannam@241 76 return "Vamp SDK Example Plugins";
cannam@241 77 }
cannam@241 78
cannam@241 79 int
cannam@241 80 PowerSpectrum::getPluginVersion() const
cannam@241 81 {
cannam@241 82 return 1;
cannam@241 83 }
cannam@241 84
cannam@241 85 string
cannam@241 86 PowerSpectrum::getCopyright() const
cannam@241 87 {
cannam@241 88 return "Freely redistributable (BSD license)";
cannam@241 89 }
cannam@241 90
cannam@241 91 bool
Chris@398 92 PowerSpectrum::initialise(size_t channels, size_t, size_t blockSize)
cannam@241 93 {
cannam@241 94 if (channels < getMinChannelCount() ||
cannam@241 95 channels > getMaxChannelCount()) return false;
cannam@241 96
cannam@241 97 m_blockSize = blockSize;
cannam@241 98
cannam@241 99 return true;
cannam@241 100 }
cannam@241 101
cannam@241 102 void
cannam@241 103 PowerSpectrum::reset()
cannam@241 104 {
cannam@241 105 }
cannam@241 106
cannam@241 107 PowerSpectrum::OutputList
cannam@241 108 PowerSpectrum::getOutputDescriptors() const
cannam@241 109 {
cannam@241 110 OutputList list;
cannam@241 111
cannam@241 112 OutputDescriptor d;
cannam@241 113 d.identifier = "powerspectrum";
cannam@241 114 d.name = "Power Spectrum";
cannam@241 115 d.description = "Power values of the frequency spectrum bins calculated from the input signal";
cannam@241 116 d.unit = "";
cannam@241 117 d.hasFixedBinCount = true;
cannam@255 118 if (m_blockSize == 0) {
cannam@255 119 // Just so as not to return "1". This is the bin count that
cannam@255 120 // would result from a block size of 1024, which is a likely
cannam@255 121 // default -- but the host should always set the block size
cannam@255 122 // before querying the bin count for certain.
cannam@255 123 d.binCount = 513;
cannam@255 124 } else {
cannam@255 125 d.binCount = m_blockSize / 2 + 1;
cannam@255 126 }
cannam@241 127 d.hasKnownExtents = false;
cannam@241 128 d.isQuantized = false;
cannam@241 129 d.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@241 130 list.push_back(d);
cannam@241 131
cannam@241 132 return list;
cannam@241 133 }
cannam@241 134
cannam@241 135 PowerSpectrum::FeatureSet
Chris@398 136 PowerSpectrum::process(const float *const *inputBuffers, Vamp::RealTime)
cannam@241 137 {
cannam@241 138 FeatureSet fs;
cannam@241 139
cannam@241 140 if (m_blockSize == 0) {
cannam@241 141 cerr << "ERROR: PowerSpectrum::process: Not initialised" << endl;
cannam@241 142 return fs;
cannam@241 143 }
cannam@241 144
cannam@241 145 size_t n = m_blockSize / 2 + 1;
cannam@241 146 const float *fbuf = inputBuffers[0];
cannam@241 147
cannam@241 148 Feature feature;
cannam@241 149 feature.hasTimestamp = false;
cannam@241 150 feature.values.reserve(n); // optional
cannam@241 151
cannam@241 152 for (size_t i = 0; i < n; ++i) {
cannam@241 153
cannam@241 154 double real = fbuf[i * 2];
cannam@241 155 double imag = fbuf[i * 2 + 1];
cannam@241 156
cannam@241 157 feature.values.push_back(real * real + imag * imag);
cannam@241 158 }
cannam@241 159
cannam@241 160 fs[0].push_back(feature);
cannam@241 161
cannam@241 162 return fs;
cannam@241 163 }
cannam@241 164
cannam@241 165 PowerSpectrum::FeatureSet
cannam@241 166 PowerSpectrum::getRemainingFeatures()
cannam@241 167 {
cannam@241 168 return FeatureSet();
cannam@241 169 }
cannam@241 170