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 00045 using std::string; 00046 using std::vector; 00047 using std::cerr; 00048 using std::endl; 00049 00055 AmplitudeFollower::AmplitudeFollower(float inputSampleRate) : 00056 Plugin(inputSampleRate), 00057 m_stepSize(0), 00058 m_previn(0.0f), 00059 m_clampcoef(0.01f), 00060 m_relaxcoef(0.01f) 00061 { 00062 } 00063 00064 AmplitudeFollower::~AmplitudeFollower() 00065 { 00066 } 00067 00068 string 00069 AmplitudeFollower::getIdentifier() const 00070 { 00071 return "amplitudefollower"; 00072 } 00073 00074 string 00075 AmplitudeFollower::getName() const 00076 { 00077 return "Amplitude Follower"; 00078 } 00079 00080 string 00081 AmplitudeFollower::getDescription() const 00082 { 00083 return "Track the amplitude of the audio signal"; 00084 } 00085 00086 string 00087 AmplitudeFollower::getMaker() const 00088 { 00089 return "Vamp SDK Example Plugins"; 00090 } 00091 00092 int 00093 AmplitudeFollower::getPluginVersion() const 00094 { 00095 return 1; 00096 } 00097 00098 string 00099 AmplitudeFollower::getCopyright() const 00100 { 00101 return "Code copyright 2006 Dan Stowell; method from SuperCollider. Freely redistributable (BSD license)"; 00102 } 00103 00104 bool 00105 AmplitudeFollower::initialise(size_t channels, size_t stepSize, size_t blockSize) 00106 { 00107 if (channels < getMinChannelCount() || 00108 channels > getMaxChannelCount()) return false; 00109 00110 m_stepSize = std::min(stepSize, blockSize); 00111 00112 // Translate the coefficients 00113 // from their "convenient" 60dB convergence-time values 00114 // to real coefficients 00115 m_clampcoef = m_clampcoef==0.0 ? 0.0 : exp(log(0.1)/(m_clampcoef * m_inputSampleRate)); 00116 m_relaxcoef = m_relaxcoef==0.0 ? 0.0 : exp(log(0.1)/(m_relaxcoef * m_inputSampleRate)); 00117 00118 return true; 00119 } 00120 00121 void 00122 AmplitudeFollower::reset() 00123 { 00124 m_previn = 0.0f; 00125 } 00126 00127 AmplitudeFollower::OutputList 00128 AmplitudeFollower::getOutputDescriptors() const 00129 { 00130 OutputList list; 00131 00132 OutputDescriptor sca; 00133 sca.identifier = "amplitude"; 00134 sca.name = "Amplitude"; 00135 sca.description = "The peak tracked amplitude for the current processing block"; 00136 sca.unit = "V"; 00137 sca.hasFixedBinCount = true; 00138 sca.binCount = 1; 00139 sca.hasKnownExtents = false; 00140 sca.isQuantized = false; 00141 sca.sampleType = OutputDescriptor::OneSamplePerStep; 00142 list.push_back(sca); 00143 00144 return list; 00145 } 00146 00147 AmplitudeFollower::ParameterList 00148 AmplitudeFollower::getParameterDescriptors() const 00149 { 00150 ParameterList list; 00151 00152 ParameterDescriptor att; 00153 att.identifier = "attack"; 00154 att.name = "Attack time"; 00155 att.description = "The 60dB convergence time for an increase in amplitude"; 00156 att.unit = "s"; 00157 att.minValue = 0.0f; 00158 att.maxValue = 1.f; 00159 att.defaultValue = 0.01f; 00160 att.isQuantized = false; 00161 00162 list.push_back(att); 00163 00164 ParameterDescriptor dec; 00165 dec.identifier = "release"; 00166 dec.name = "Release time"; 00167 dec.description = "The 60dB convergence time for a decrease in amplitude"; 00168 dec.unit = "s"; 00169 dec.minValue = 0.0f; 00170 dec.maxValue = 1.f; 00171 dec.defaultValue = 0.01f; 00172 dec.isQuantized = false; 00173 00174 list.push_back(dec); 00175 00176 return list; 00177 } 00178 00179 void AmplitudeFollower::setParameter(std::string paramid, float newval) 00180 { 00181 if (paramid == "attack") { 00182 m_clampcoef = newval; 00183 } else if (paramid == "release") { 00184 m_relaxcoef = newval; 00185 } 00186 } 00187 00188 float AmplitudeFollower::getParameter(std::string paramid) const 00189 { 00190 if (paramid == "attack") { 00191 return m_clampcoef; 00192 } else if (paramid == "release") { 00193 return m_relaxcoef; 00194 } 00195 00196 return 0.0f; 00197 } 00198 00199 AmplitudeFollower::FeatureSet 00200 AmplitudeFollower::process(const float *const *inputBuffers, 00201 Vamp::RealTime timestamp) 00202 { 00203 if (m_stepSize == 0) { 00204 cerr << "ERROR: AmplitudeFollower::process: " 00205 << "AmplitudeFollower has not been initialised" 00206 << endl; 00207 return FeatureSet(); 00208 } 00209 00210 float previn = m_previn; 00211 00212 FeatureSet returnFeatures; 00213 00214 float val; 00215 float peak = 0.0f; 00216 00217 for (size_t i = 0; i < m_stepSize; ++i) { 00218 00219 val = fabs(inputBuffers[0][i]); 00220 00221 if (val < previn) { 00222 val = val + (previn - val) * m_relaxcoef; 00223 } else { 00224 val = val + (previn - val) * m_clampcoef; 00225 } 00226 00227 if (val > peak) peak = val; 00228 previn = val; 00229 } 00230 00231 m_previn = previn; 00232 00233 // Now store the "feature" (peak amp) for this sample 00234 Feature feature; 00235 feature.hasTimestamp = false; 00236 feature.values.push_back(peak); 00237 returnFeatures[0].push_back(feature); 00238 00239 return returnFeatures; 00240 } 00241 00242 AmplitudeFollower::FeatureSet 00243 AmplitudeFollower::getRemainingFeatures() 00244 { 00245 return FeatureSet(); 00246 } 00247