cannam@41: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@41: cannam@41: /* cannam@41: Vamp cannam@41: cannam@41: An API for audio analysis and feature extraction plugins. cannam@41: cannam@41: Centre for Digital Music, Queen Mary, University of London. cannam@41: This file copyright 2006 Dan Stowell. cannam@41: cannam@41: Permission is hereby granted, free of charge, to any person cannam@41: obtaining a copy of this software and associated documentation cannam@41: files (the "Software"), to deal in the Software without cannam@41: restriction, including without limitation the rights to use, copy, cannam@41: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@41: of the Software, and to permit persons to whom the Software is cannam@41: furnished to do so, subject to the following conditions: cannam@41: cannam@41: The above copyright notice and this permission notice shall be cannam@41: included in all copies or substantial portions of the Software. cannam@41: cannam@41: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@41: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@41: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@41: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@41: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@41: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@41: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@41: cannam@41: Except as contained in this notice, the names of the Centre for cannam@41: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@41: shall not be used in advertising or otherwise to promote the sale, cannam@41: use or other dealings in this Software without prior written cannam@41: authorization. cannam@41: */ cannam@41: cannam@41: #include "AmplitudeFollower.h" cannam@41: cannam@41: #include cannam@41: cannam@41: #include cannam@41: #include cannam@41: #include Chris@394: #include cannam@41: cannam@41: using std::string; cannam@41: using std::vector; cannam@41: using std::cerr; cannam@41: using std::endl; cannam@41: cannam@54: /** cannam@41: * An implementation of SuperCollider's amplitude-follower algorithm cannam@41: * as a simple Vamp plugin. cannam@41: */ cannam@41: cannam@41: AmplitudeFollower::AmplitudeFollower(float inputSampleRate) : cannam@41: Plugin(inputSampleRate), cannam@41: m_stepSize(0), cannam@41: m_previn(0.0f), cannam@41: m_clampcoef(0.01f), cannam@41: m_relaxcoef(0.01f) cannam@41: { cannam@41: } cannam@41: cannam@41: AmplitudeFollower::~AmplitudeFollower() cannam@41: { cannam@41: } cannam@41: cannam@41: string cannam@49: AmplitudeFollower::getIdentifier() const cannam@41: { cannam@41: return "amplitudefollower"; cannam@41: } cannam@41: cannam@41: string cannam@49: AmplitudeFollower::getName() const cannam@49: { cannam@49: return "Amplitude Follower"; cannam@49: } cannam@49: cannam@49: string cannam@41: AmplitudeFollower::getDescription() const cannam@41: { cannam@49: return "Track the amplitude of the audio signal"; cannam@41: } cannam@41: cannam@41: string cannam@41: AmplitudeFollower::getMaker() const cannam@41: { cannam@43: return "Vamp SDK Example Plugins"; cannam@41: } cannam@41: cannam@41: int cannam@41: AmplitudeFollower::getPluginVersion() const cannam@41: { cannam@41: return 1; cannam@41: } cannam@41: cannam@41: string cannam@41: AmplitudeFollower::getCopyright() const cannam@41: { cannam@41: return "Code copyright 2006 Dan Stowell; method from SuperCollider. Freely redistributable (BSD license)"; cannam@41: } cannam@41: cannam@41: bool cannam@41: AmplitudeFollower::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@41: { cannam@41: if (channels < getMinChannelCount() || Chris@501: channels > getMaxChannelCount()) { Chris@501: cerr << "ERROR: AmplitudeFollower::initialise: " Chris@501: << "channel count " << channels << " out of supported range" Chris@501: << endl; Chris@501: return false; Chris@501: } cannam@41: cannam@41: m_stepSize = std::min(stepSize, blockSize); Chris@501: cannam@41: // Translate the coefficients cannam@41: // from their "convenient" 60dB convergence-time values cannam@41: // to real coefficients cannam@41: m_clampcoef = m_clampcoef==0.0 ? 0.0 : exp(log(0.1)/(m_clampcoef * m_inputSampleRate)); cannam@41: m_relaxcoef = m_relaxcoef==0.0 ? 0.0 : exp(log(0.1)/(m_relaxcoef * m_inputSampleRate)); cannam@41: cannam@41: return true; cannam@41: } cannam@41: cannam@41: void cannam@41: AmplitudeFollower::reset() cannam@41: { cannam@41: m_previn = 0.0f; cannam@41: } cannam@41: cannam@41: AmplitudeFollower::OutputList cannam@41: AmplitudeFollower::getOutputDescriptors() const cannam@41: { cannam@41: OutputList list; cannam@41: cannam@41: OutputDescriptor sca; cannam@49: sca.identifier = "amplitude"; cannam@49: sca.name = "Amplitude"; cannam@281: sca.description = "The peak tracked amplitude for the current processing block"; cannam@41: sca.unit = "V"; cannam@41: sca.hasFixedBinCount = true; cannam@41: sca.binCount = 1; cannam@41: sca.hasKnownExtents = false; cannam@41: sca.isQuantized = false; cannam@41: sca.sampleType = OutputDescriptor::OneSamplePerStep; cannam@41: list.push_back(sca); cannam@41: cannam@41: return list; cannam@41: } cannam@41: cannam@41: AmplitudeFollower::ParameterList cannam@41: AmplitudeFollower::getParameterDescriptors() const cannam@41: { cannam@41: ParameterList list; cannam@41: cannam@41: ParameterDescriptor att; cannam@49: att.identifier = "attack"; cannam@49: att.name = "Attack time"; cannam@281: att.description = "The 60dB convergence time for an increase in amplitude"; cannam@41: att.unit = "s"; cannam@41: att.minValue = 0.0f; cannam@41: att.maxValue = 1.f; cannam@41: att.defaultValue = 0.01f; cannam@41: att.isQuantized = false; cannam@41: cannam@41: list.push_back(att); cannam@41: cannam@41: ParameterDescriptor dec; cannam@49: dec.identifier = "release"; cannam@49: dec.name = "Release time"; cannam@281: dec.description = "The 60dB convergence time for a decrease in amplitude"; cannam@41: dec.unit = "s"; cannam@41: dec.minValue = 0.0f; cannam@41: dec.maxValue = 1.f; cannam@41: dec.defaultValue = 0.01f; cannam@41: dec.isQuantized = false; cannam@41: cannam@41: list.push_back(dec); cannam@41: cannam@41: return list; cannam@41: } cannam@41: cannam@49: void AmplitudeFollower::setParameter(std::string paramid, float newval) cannam@41: { cannam@49: if (paramid == "attack") { cannam@41: m_clampcoef = newval; cannam@49: } else if (paramid == "release") { cannam@41: m_relaxcoef = newval; cannam@41: } cannam@41: } cannam@41: cannam@49: float AmplitudeFollower::getParameter(std::string paramid) const cannam@41: { cannam@49: if (paramid == "attack") { cannam@41: return m_clampcoef; cannam@49: } else if (paramid == "release") { cannam@41: return m_relaxcoef; cannam@41: } cannam@41: cannam@41: return 0.0f; cannam@41: } cannam@41: cannam@41: AmplitudeFollower::FeatureSet cannam@47: AmplitudeFollower::process(const float *const *inputBuffers, Chris@437: Vamp::RealTime /* timestamp */) cannam@41: { cannam@41: if (m_stepSize == 0) { cannam@41: cerr << "ERROR: AmplitudeFollower::process: " cannam@41: << "AmplitudeFollower has not been initialised" cannam@41: << endl; cannam@41: return FeatureSet(); cannam@41: } cannam@41: cannam@41: float previn = m_previn; cannam@41: cannam@41: FeatureSet returnFeatures; cannam@41: cannam@41: float val; cannam@41: float peak = 0.0f; cannam@41: cannam@41: for (size_t i = 0; i < m_stepSize; ++i) { cannam@41: cannam@41: val = fabs(inputBuffers[0][i]); cannam@41: cannam@41: if (val < previn) { cannam@41: val = val + (previn - val) * m_relaxcoef; cannam@41: } else { cannam@41: val = val + (previn - val) * m_clampcoef; cannam@41: } cannam@41: cannam@41: if (val > peak) peak = val; cannam@41: previn = val; cannam@41: } cannam@41: cannam@41: m_previn = previn; cannam@41: cannam@41: // Now store the "feature" (peak amp) for this sample cannam@41: Feature feature; cannam@41: feature.hasTimestamp = false; cannam@41: feature.values.push_back(peak); cannam@41: returnFeatures[0].push_back(feature); cannam@41: cannam@41: return returnFeatures; cannam@41: } cannam@41: cannam@41: AmplitudeFollower::FeatureSet cannam@41: AmplitudeFollower::getRemainingFeatures() cannam@41: { cannam@41: return FeatureSet(); cannam@41: } cannam@41: