annotate examples/AmplitudeFollower.cpp @ 41:31cd55174467

* Add Dan Stowell's simple implementation of the SuperCollider amplitude follower method
author cannam
date Wed, 11 Oct 2006 11:39:02 +0000
parents
children 3bbe244611bb
rev   line source
cannam@41 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@41 2
cannam@41 3 /*
cannam@41 4 Vamp
cannam@41 5
cannam@41 6 An API for audio analysis and feature extraction plugins.
cannam@41 7
cannam@41 8 Centre for Digital Music, Queen Mary, University of London.
cannam@41 9 This file copyright 2006 Dan Stowell.
cannam@41 10
cannam@41 11 Permission is hereby granted, free of charge, to any person
cannam@41 12 obtaining a copy of this software and associated documentation
cannam@41 13 files (the "Software"), to deal in the Software without
cannam@41 14 restriction, including without limitation the rights to use, copy,
cannam@41 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@41 16 of the Software, and to permit persons to whom the Software is
cannam@41 17 furnished to do so, subject to the following conditions:
cannam@41 18
cannam@41 19 The above copyright notice and this permission notice shall be
cannam@41 20 included in all copies or substantial portions of the Software.
cannam@41 21
cannam@41 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@41 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@41 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@41 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@41 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@41 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@41 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@41 29
cannam@41 30 Except as contained in this notice, the names of the Centre for
cannam@41 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@41 32 shall not be used in advertising or otherwise to promote the sale,
cannam@41 33 use or other dealings in this Software without prior written
cannam@41 34 authorization.
cannam@41 35 */
cannam@41 36
cannam@41 37 #include "AmplitudeFollower.h"
cannam@41 38
cannam@41 39 #include <cmath>
cannam@41 40
cannam@41 41 #include <string>
cannam@41 42 #include <vector>
cannam@41 43 #include <iostream>
cannam@41 44
cannam@41 45 using std::string;
cannam@41 46 using std::vector;
cannam@41 47 using std::cerr;
cannam@41 48 using std::endl;
cannam@41 49
cannam@41 50 /*
cannam@41 51 * An implementation of SuperCollider's amplitude-follower algorithm
cannam@41 52 * as a simple Vamp plugin.
cannam@41 53 */
cannam@41 54
cannam@41 55 AmplitudeFollower::AmplitudeFollower(float inputSampleRate) :
cannam@41 56 Plugin(inputSampleRate),
cannam@41 57 m_stepSize(0),
cannam@41 58 m_previn(0.0f),
cannam@41 59 m_clampcoef(0.01f),
cannam@41 60 m_relaxcoef(0.01f)
cannam@41 61 {
cannam@41 62 }
cannam@41 63
cannam@41 64 AmplitudeFollower::~AmplitudeFollower()
cannam@41 65 {
cannam@41 66 }
cannam@41 67
cannam@41 68 string
cannam@41 69 AmplitudeFollower::getName() const
cannam@41 70 {
cannam@41 71 return "amplitudefollower";
cannam@41 72 }
cannam@41 73
cannam@41 74 string
cannam@41 75 AmplitudeFollower::getDescription() const
cannam@41 76 {
cannam@41 77 return "Amplitude Follower";
cannam@41 78 }
cannam@41 79
cannam@41 80 string
cannam@41 81 AmplitudeFollower::getMaker() const
cannam@41 82 {
cannam@41 83 return "Queen Mary, University of London";
cannam@41 84 }
cannam@41 85
cannam@41 86 int
cannam@41 87 AmplitudeFollower::getPluginVersion() const
cannam@41 88 {
cannam@41 89 return 1;
cannam@41 90 }
cannam@41 91
cannam@41 92 string
cannam@41 93 AmplitudeFollower::getCopyright() const
cannam@41 94 {
cannam@41 95 return "Code copyright 2006 Dan Stowell; method from SuperCollider. Freely redistributable (BSD license)";
cannam@41 96 }
cannam@41 97
cannam@41 98 bool
cannam@41 99 AmplitudeFollower::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@41 100 {
cannam@41 101 if (channels < getMinChannelCount() ||
cannam@41 102 channels > getMaxChannelCount()) return false;
cannam@41 103
cannam@41 104 m_stepSize = std::min(stepSize, blockSize);
cannam@41 105
cannam@41 106 // Translate the coefficients
cannam@41 107 // from their "convenient" 60dB convergence-time values
cannam@41 108 // to real coefficients
cannam@41 109 m_clampcoef = m_clampcoef==0.0 ? 0.0 : exp(log(0.1)/(m_clampcoef * m_inputSampleRate));
cannam@41 110 m_relaxcoef = m_relaxcoef==0.0 ? 0.0 : exp(log(0.1)/(m_relaxcoef * m_inputSampleRate));
cannam@41 111
cannam@41 112 return true;
cannam@41 113 }
cannam@41 114
cannam@41 115 void
cannam@41 116 AmplitudeFollower::reset()
cannam@41 117 {
cannam@41 118 m_previn = 0.0f;
cannam@41 119 }
cannam@41 120
cannam@41 121 AmplitudeFollower::OutputList
cannam@41 122 AmplitudeFollower::getOutputDescriptors() const
cannam@41 123 {
cannam@41 124 OutputList list;
cannam@41 125
cannam@41 126 OutputDescriptor sca;
cannam@41 127 sca.name = "amplitude";
cannam@41 128 sca.unit = "V";
cannam@41 129 sca.description = "Amplitude";
cannam@41 130 sca.hasFixedBinCount = true;
cannam@41 131 sca.binCount = 1;
cannam@41 132 sca.hasKnownExtents = false;
cannam@41 133 sca.isQuantized = false;
cannam@41 134 sca.sampleType = OutputDescriptor::OneSamplePerStep;
cannam@41 135 list.push_back(sca);
cannam@41 136
cannam@41 137 return list;
cannam@41 138 }
cannam@41 139
cannam@41 140 AmplitudeFollower::ParameterList
cannam@41 141 AmplitudeFollower::getParameterDescriptors() const
cannam@41 142 {
cannam@41 143 ParameterList list;
cannam@41 144
cannam@41 145 ParameterDescriptor att;
cannam@41 146 att.name = "attack";
cannam@41 147 att.description = "Attack time";
cannam@41 148 att.unit = "s";
cannam@41 149 att.minValue = 0.0f;
cannam@41 150 att.maxValue = 1.f;
cannam@41 151 att.defaultValue = 0.01f;
cannam@41 152 att.isQuantized = false;
cannam@41 153
cannam@41 154 list.push_back(att);
cannam@41 155
cannam@41 156 ParameterDescriptor dec;
cannam@41 157 dec.name = "release";
cannam@41 158 dec.description = "Release time";
cannam@41 159 dec.unit = "s";
cannam@41 160 dec.minValue = 0.0f;
cannam@41 161 dec.maxValue = 1.f;
cannam@41 162 dec.defaultValue = 0.01f;
cannam@41 163 dec.isQuantized = false;
cannam@41 164
cannam@41 165 list.push_back(dec);
cannam@41 166
cannam@41 167 return list;
cannam@41 168 }
cannam@41 169
cannam@41 170 void AmplitudeFollower::setParameter(std::string paramname, float newval)
cannam@41 171 {
cannam@41 172 if (paramname == "attack") {
cannam@41 173 m_clampcoef = newval;
cannam@41 174 } else if (paramname == "release") {
cannam@41 175 m_relaxcoef = newval;
cannam@41 176 }
cannam@41 177 }
cannam@41 178
cannam@41 179 float AmplitudeFollower::getParameter(std::string paramname) const
cannam@41 180 {
cannam@41 181 if (paramname == "attack") {
cannam@41 182 return m_clampcoef;
cannam@41 183 } else if (paramname == "release") {
cannam@41 184 return m_relaxcoef;
cannam@41 185 }
cannam@41 186
cannam@41 187 return 0.0f;
cannam@41 188 }
cannam@41 189
cannam@41 190 AmplitudeFollower::FeatureSet
cannam@41 191 AmplitudeFollower::process(float **inputBuffers, Vamp::RealTime timestamp)
cannam@41 192 {
cannam@41 193 if (m_stepSize == 0) {
cannam@41 194 cerr << "ERROR: AmplitudeFollower::process: "
cannam@41 195 << "AmplitudeFollower has not been initialised"
cannam@41 196 << endl;
cannam@41 197 return FeatureSet();
cannam@41 198 }
cannam@41 199
cannam@41 200 float previn = m_previn;
cannam@41 201
cannam@41 202 FeatureSet returnFeatures;
cannam@41 203
cannam@41 204 float val;
cannam@41 205 float peak = 0.0f;
cannam@41 206
cannam@41 207 for (size_t i = 0; i < m_stepSize; ++i) {
cannam@41 208
cannam@41 209 val = fabs(inputBuffers[0][i]);
cannam@41 210
cannam@41 211 if (val < previn) {
cannam@41 212 val = val + (previn - val) * m_relaxcoef;
cannam@41 213 } else {
cannam@41 214 val = val + (previn - val) * m_clampcoef;
cannam@41 215 }
cannam@41 216
cannam@41 217 if (val > peak) peak = val;
cannam@41 218 previn = val;
cannam@41 219 }
cannam@41 220
cannam@41 221 m_previn = previn;
cannam@41 222
cannam@41 223 // Now store the "feature" (peak amp) for this sample
cannam@41 224 Feature feature;
cannam@41 225 feature.hasTimestamp = false;
cannam@41 226 feature.values.push_back(peak);
cannam@41 227 returnFeatures[0].push_back(feature);
cannam@41 228
cannam@41 229 return returnFeatures;
cannam@41 230 }
cannam@41 231
cannam@41 232 AmplitudeFollower::FeatureSet
cannam@41 233 AmplitudeFollower::getRemainingFeatures()
cannam@41 234 {
cannam@41 235 return FeatureSet();
cannam@41 236 }
cannam@41 237