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