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