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