AmplitudeFollower.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Vamp
00005 
00006     An API for audio analysis and feature extraction plugins.
00007 
00008     Centre for Digital Music, Queen Mary, University of London.
00009     This file copyright 2006 Dan Stowell.
00010   
00011     Permission is hereby granted, free of charge, to any person
00012     obtaining a copy of this software and associated documentation
00013     files (the "Software"), to deal in the Software without
00014     restriction, including without limitation the rights to use, copy,
00015     modify, merge, publish, distribute, sublicense, and/or sell copies
00016     of the Software, and to permit persons to whom the Software is
00017     furnished to do so, subject to the following conditions:
00018 
00019     The above copyright notice and this permission notice shall be
00020     included in all copies or substantial portions of the Software.
00021 
00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00029 
00030     Except as contained in this notice, the names of the Centre for
00031     Digital Music; Queen Mary, University of London; and Chris Cannam
00032     shall not be used in advertising or otherwise to promote the sale,
00033     use or other dealings in this Software without prior written
00034     authorization.
00035 */
00036 
00037 #include "AmplitudeFollower.h"
00038 
00039 #include <cmath>
00040 
00041 #include <string>
00042 #include <vector>
00043 #include <iostream>
00044 #include <algorithm>
00045 
00046 using std::string;
00047 using std::vector;
00048 using std::cerr;
00049 using std::endl;
00050 
00056 AmplitudeFollower::AmplitudeFollower(float inputSampleRate) :
00057     Plugin(inputSampleRate),
00058     m_stepSize(0),
00059     m_previn(0.0f),
00060     m_clampcoef(0.01f),
00061     m_relaxcoef(0.01f)
00062 {
00063 }
00064 
00065 AmplitudeFollower::~AmplitudeFollower()
00066 {
00067 }
00068 
00069 string
00070 AmplitudeFollower::getIdentifier() const
00071 {
00072     return "amplitudefollower";
00073 }
00074 
00075 string
00076 AmplitudeFollower::getName() const
00077 {
00078     return "Amplitude Follower";
00079 }
00080 
00081 string
00082 AmplitudeFollower::getDescription() const
00083 {
00084     return "Track the amplitude of the audio signal";
00085 }
00086 
00087 string
00088 AmplitudeFollower::getMaker() const
00089 {
00090     return "Vamp SDK Example Plugins";
00091 }
00092 
00093 int
00094 AmplitudeFollower::getPluginVersion() const
00095 {
00096     return 1;
00097 }
00098 
00099 string
00100 AmplitudeFollower::getCopyright() const
00101 {
00102     return "Code copyright 2006 Dan Stowell; method from SuperCollider.  Freely redistributable (BSD license)";
00103 }
00104 
00105 bool
00106 AmplitudeFollower::initialise(size_t channels, size_t stepSize, size_t blockSize)
00107 {
00108     if (channels < getMinChannelCount() ||
00109         channels > getMaxChannelCount()) return false;
00110 
00111     m_stepSize = std::min(stepSize, blockSize);
00112         
00113     // Translate the coefficients 
00114     // from their "convenient" 60dB convergence-time values
00115     // to real coefficients
00116     m_clampcoef = m_clampcoef==0.0 ? 0.0 : exp(log(0.1)/(m_clampcoef * m_inputSampleRate));
00117     m_relaxcoef = m_relaxcoef==0.0 ? 0.0 : exp(log(0.1)/(m_relaxcoef * m_inputSampleRate));
00118 
00119     return true;
00120 }
00121 
00122 void
00123 AmplitudeFollower::reset()
00124 {
00125     m_previn = 0.0f;
00126 }
00127 
00128 AmplitudeFollower::OutputList
00129 AmplitudeFollower::getOutputDescriptors() const
00130 {
00131     OutputList list;
00132 
00133     OutputDescriptor sca;
00134     sca.identifier = "amplitude";
00135     sca.name = "Amplitude";
00136     sca.description = "The peak tracked amplitude for the current processing block";
00137     sca.unit = "V";
00138     sca.hasFixedBinCount = true;
00139     sca.binCount = 1;
00140     sca.hasKnownExtents = false;
00141     sca.isQuantized = false;
00142     sca.sampleType = OutputDescriptor::OneSamplePerStep;
00143     list.push_back(sca);
00144 
00145     return list;
00146 }
00147 
00148 AmplitudeFollower::ParameterList
00149 AmplitudeFollower::getParameterDescriptors() const
00150 {
00151     ParameterList list;
00152         
00153     ParameterDescriptor att;
00154     att.identifier = "attack";
00155     att.name = "Attack time";
00156     att.description = "The 60dB convergence time for an increase in amplitude";
00157     att.unit = "s";
00158     att.minValue = 0.0f;
00159     att.maxValue = 1.f;
00160     att.defaultValue = 0.01f;
00161     att.isQuantized = false;
00162     
00163     list.push_back(att);
00164     
00165     ParameterDescriptor dec;
00166     dec.identifier = "release";
00167     dec.name = "Release time";
00168     dec.description = "The 60dB convergence time for a decrease in amplitude";
00169     dec.unit = "s";
00170     dec.minValue = 0.0f;
00171     dec.maxValue = 1.f;
00172     dec.defaultValue = 0.01f;
00173     dec.isQuantized = false;
00174     
00175     list.push_back(dec);
00176     
00177     return list;
00178 }
00179 
00180 void AmplitudeFollower::setParameter(std::string paramid, float newval)
00181 {
00182     if (paramid == "attack") {
00183         m_clampcoef = newval;
00184     } else if (paramid == "release") {
00185         m_relaxcoef = newval;
00186     }
00187 }
00188 
00189 float AmplitudeFollower::getParameter(std::string paramid) const
00190 {
00191     if (paramid == "attack") {
00192         return m_clampcoef;
00193     } else if (paramid == "release") {
00194         return m_relaxcoef;
00195     }
00196 
00197     return 0.0f;
00198 }
00199 
00200 AmplitudeFollower::FeatureSet
00201 AmplitudeFollower::process(const float *const *inputBuffers,
00202                            Vamp::RealTime /* timestamp */)
00203 {
00204     if (m_stepSize == 0) {
00205         cerr << "ERROR: AmplitudeFollower::process: "
00206              << "AmplitudeFollower has not been initialised"
00207              << endl;
00208         return FeatureSet();
00209     }
00210 
00211     float previn = m_previn;
00212 
00213     FeatureSet returnFeatures;
00214         
00215     float val;
00216     float peak = 0.0f;
00217 
00218     for (size_t i = 0; i < m_stepSize; ++i) {
00219 
00220         val = fabs(inputBuffers[0][i]);
00221                 
00222         if (val < previn) {
00223             val = val + (previn - val) * m_relaxcoef;
00224         } else {
00225             val = val + (previn - val) * m_clampcoef;
00226         }
00227 
00228         if (val > peak) peak = val;
00229         previn = val;
00230     }
00231 
00232     m_previn = previn;
00233 
00234     // Now store the "feature" (peak amp) for this sample
00235     Feature feature;
00236     feature.hasTimestamp = false;
00237     feature.values.push_back(peak);
00238     returnFeatures[0].push_back(feature);
00239 
00240     return returnFeatures;
00241 }
00242 
00243 AmplitudeFollower::FeatureSet
00244 AmplitudeFollower::getRemainingFeatures()
00245 {
00246     return FeatureSet();
00247 }
00248