annotate src/vamp-plugin-sdk-2.4/examples/AmplitudeFollower.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 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 This file copyright 2006 Dan Stowell.
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 "AmplitudeFollower.h"
Chris@12 38
Chris@12 39 #include <cmath>
Chris@12 40
Chris@12 41 #include <string>
Chris@12 42 #include <vector>
Chris@12 43 #include <iostream>
Chris@12 44
Chris@12 45 using std::string;
Chris@12 46 using std::vector;
Chris@12 47 using std::cerr;
Chris@12 48 using std::endl;
Chris@12 49
Chris@12 50 /**
Chris@12 51 * An implementation of SuperCollider's amplitude-follower algorithm
Chris@12 52 * as a simple Vamp plugin.
Chris@12 53 */
Chris@12 54
Chris@12 55 AmplitudeFollower::AmplitudeFollower(float inputSampleRate) :
Chris@12 56 Plugin(inputSampleRate),
Chris@12 57 m_stepSize(0),
Chris@12 58 m_previn(0.0f),
Chris@12 59 m_clampcoef(0.01f),
Chris@12 60 m_relaxcoef(0.01f)
Chris@12 61 {
Chris@12 62 }
Chris@12 63
Chris@12 64 AmplitudeFollower::~AmplitudeFollower()
Chris@12 65 {
Chris@12 66 }
Chris@12 67
Chris@12 68 string
Chris@12 69 AmplitudeFollower::getIdentifier() const
Chris@12 70 {
Chris@12 71 return "amplitudefollower";
Chris@12 72 }
Chris@12 73
Chris@12 74 string
Chris@12 75 AmplitudeFollower::getName() const
Chris@12 76 {
Chris@12 77 return "Amplitude Follower";
Chris@12 78 }
Chris@12 79
Chris@12 80 string
Chris@12 81 AmplitudeFollower::getDescription() const
Chris@12 82 {
Chris@12 83 return "Track the amplitude of the audio signal";
Chris@12 84 }
Chris@12 85
Chris@12 86 string
Chris@12 87 AmplitudeFollower::getMaker() const
Chris@12 88 {
Chris@12 89 return "Vamp SDK Example Plugins";
Chris@12 90 }
Chris@12 91
Chris@12 92 int
Chris@12 93 AmplitudeFollower::getPluginVersion() const
Chris@12 94 {
Chris@12 95 return 1;
Chris@12 96 }
Chris@12 97
Chris@12 98 string
Chris@12 99 AmplitudeFollower::getCopyright() const
Chris@12 100 {
Chris@12 101 return "Code copyright 2006 Dan Stowell; method from SuperCollider. Freely redistributable (BSD license)";
Chris@12 102 }
Chris@12 103
Chris@12 104 bool
Chris@12 105 AmplitudeFollower::initialise(size_t channels, size_t stepSize, size_t blockSize)
Chris@12 106 {
Chris@12 107 if (channels < getMinChannelCount() ||
Chris@12 108 channels > getMaxChannelCount()) return false;
Chris@12 109
Chris@12 110 m_stepSize = std::min(stepSize, blockSize);
Chris@12 111
Chris@12 112 // Translate the coefficients
Chris@12 113 // from their "convenient" 60dB convergence-time values
Chris@12 114 // to real coefficients
Chris@12 115 m_clampcoef = m_clampcoef==0.0 ? 0.0 : exp(log(0.1)/(m_clampcoef * m_inputSampleRate));
Chris@12 116 m_relaxcoef = m_relaxcoef==0.0 ? 0.0 : exp(log(0.1)/(m_relaxcoef * m_inputSampleRate));
Chris@12 117
Chris@12 118 return true;
Chris@12 119 }
Chris@12 120
Chris@12 121 void
Chris@12 122 AmplitudeFollower::reset()
Chris@12 123 {
Chris@12 124 m_previn = 0.0f;
Chris@12 125 }
Chris@12 126
Chris@12 127 AmplitudeFollower::OutputList
Chris@12 128 AmplitudeFollower::getOutputDescriptors() const
Chris@12 129 {
Chris@12 130 OutputList list;
Chris@12 131
Chris@12 132 OutputDescriptor sca;
Chris@12 133 sca.identifier = "amplitude";
Chris@12 134 sca.name = "Amplitude";
Chris@12 135 sca.description = "The peak tracked amplitude for the current processing block";
Chris@12 136 sca.unit = "V";
Chris@12 137 sca.hasFixedBinCount = true;
Chris@12 138 sca.binCount = 1;
Chris@12 139 sca.hasKnownExtents = false;
Chris@12 140 sca.isQuantized = false;
Chris@12 141 sca.sampleType = OutputDescriptor::OneSamplePerStep;
Chris@12 142 list.push_back(sca);
Chris@12 143
Chris@12 144 return list;
Chris@12 145 }
Chris@12 146
Chris@12 147 AmplitudeFollower::ParameterList
Chris@12 148 AmplitudeFollower::getParameterDescriptors() const
Chris@12 149 {
Chris@12 150 ParameterList list;
Chris@12 151
Chris@12 152 ParameterDescriptor att;
Chris@12 153 att.identifier = "attack";
Chris@12 154 att.name = "Attack time";
Chris@12 155 att.description = "The 60dB convergence time for an increase in amplitude";
Chris@12 156 att.unit = "s";
Chris@12 157 att.minValue = 0.0f;
Chris@12 158 att.maxValue = 1.f;
Chris@12 159 att.defaultValue = 0.01f;
Chris@12 160 att.isQuantized = false;
Chris@12 161
Chris@12 162 list.push_back(att);
Chris@12 163
Chris@12 164 ParameterDescriptor dec;
Chris@12 165 dec.identifier = "release";
Chris@12 166 dec.name = "Release time";
Chris@12 167 dec.description = "The 60dB convergence time for a decrease in amplitude";
Chris@12 168 dec.unit = "s";
Chris@12 169 dec.minValue = 0.0f;
Chris@12 170 dec.maxValue = 1.f;
Chris@12 171 dec.defaultValue = 0.01f;
Chris@12 172 dec.isQuantized = false;
Chris@12 173
Chris@12 174 list.push_back(dec);
Chris@12 175
Chris@12 176 return list;
Chris@12 177 }
Chris@12 178
Chris@12 179 void AmplitudeFollower::setParameter(std::string paramid, float newval)
Chris@12 180 {
Chris@12 181 if (paramid == "attack") {
Chris@12 182 m_clampcoef = newval;
Chris@12 183 } else if (paramid == "release") {
Chris@12 184 m_relaxcoef = newval;
Chris@12 185 }
Chris@12 186 }
Chris@12 187
Chris@12 188 float AmplitudeFollower::getParameter(std::string paramid) const
Chris@12 189 {
Chris@12 190 if (paramid == "attack") {
Chris@12 191 return m_clampcoef;
Chris@12 192 } else if (paramid == "release") {
Chris@12 193 return m_relaxcoef;
Chris@12 194 }
Chris@12 195
Chris@12 196 return 0.0f;
Chris@12 197 }
Chris@12 198
Chris@12 199 AmplitudeFollower::FeatureSet
Chris@12 200 AmplitudeFollower::process(const float *const *inputBuffers,
Chris@12 201 Vamp::RealTime timestamp)
Chris@12 202 {
Chris@12 203 if (m_stepSize == 0) {
Chris@12 204 cerr << "ERROR: AmplitudeFollower::process: "
Chris@12 205 << "AmplitudeFollower has not been initialised"
Chris@12 206 << endl;
Chris@12 207 return FeatureSet();
Chris@12 208 }
Chris@12 209
Chris@12 210 float previn = m_previn;
Chris@12 211
Chris@12 212 FeatureSet returnFeatures;
Chris@12 213
Chris@12 214 float val;
Chris@12 215 float peak = 0.0f;
Chris@12 216
Chris@12 217 for (size_t i = 0; i < m_stepSize; ++i) {
Chris@12 218
Chris@12 219 val = fabs(inputBuffers[0][i]);
Chris@12 220
Chris@12 221 if (val < previn) {
Chris@12 222 val = val + (previn - val) * m_relaxcoef;
Chris@12 223 } else {
Chris@12 224 val = val + (previn - val) * m_clampcoef;
Chris@12 225 }
Chris@12 226
Chris@12 227 if (val > peak) peak = val;
Chris@12 228 previn = val;
Chris@12 229 }
Chris@12 230
Chris@12 231 m_previn = previn;
Chris@12 232
Chris@12 233 // Now store the "feature" (peak amp) for this sample
Chris@12 234 Feature feature;
Chris@12 235 feature.hasTimestamp = false;
Chris@12 236 feature.values.push_back(peak);
Chris@12 237 returnFeatures[0].push_back(feature);
Chris@12 238
Chris@12 239 return returnFeatures;
Chris@12 240 }
Chris@12 241
Chris@12 242 AmplitudeFollower::FeatureSet
Chris@12 243 AmplitudeFollower::getRemainingFeatures()
Chris@12 244 {
Chris@12 245 return FeatureSet();
Chris@12 246 }
Chris@12 247