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