annotate src/vamp-plugin-sdk-2.5/examples/AmplitudeFollower.cpp @ 55:284acf908dcd

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