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