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