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