cannam@35: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@35: cannam@35: /* cannam@35: Vamp cannam@35: cannam@35: An API for audio analysis and feature extraction plugins. cannam@35: cannam@35: Centre for Digital Music, Queen Mary, University of London. cannam@35: Copyright 2006 Chris Cannam. cannam@35: cannam@35: Permission is hereby granted, free of charge, to any person cannam@35: obtaining a copy of this software and associated documentation cannam@35: files (the "Software"), to deal in the Software without cannam@35: restriction, including without limitation the rights to use, copy, cannam@35: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@35: of the Software, and to permit persons to whom the Software is cannam@35: furnished to do so, subject to the following conditions: cannam@35: cannam@35: The above copyright notice and this permission notice shall be cannam@35: included in all copies or substantial portions of the Software. cannam@35: cannam@35: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@35: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@35: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@35: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@35: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@35: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@35: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@35: cannam@35: Except as contained in this notice, the names of the Centre for cannam@35: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@35: shall not be used in advertising or otherwise to promote the sale, cannam@35: use or other dealings in this Software without prior written cannam@35: authorization. cannam@35: */ cannam@35: cannam@35: #include "PercussionOnsetDetector.h" cannam@35: cannam@35: using std::string; cannam@35: using std::vector; cannam@35: using std::cerr; cannam@35: using std::endl; cannam@35: cannam@35: #include cannam@35: cannam@35: cannam@35: PercussionOnsetDetector::PercussionOnsetDetector(float inputSampleRate) : cannam@35: Plugin(inputSampleRate), cannam@35: m_stepSize(0), cannam@35: m_blockSize(0), cannam@35: m_threshold(3), cannam@35: m_sensitivity(40), cannam@35: m_priorMagnitudes(0), cannam@35: m_dfMinus1(0), cannam@35: m_dfMinus2(0) cannam@35: { cannam@35: } cannam@35: cannam@35: PercussionOnsetDetector::~PercussionOnsetDetector() cannam@35: { cannam@35: delete[] m_priorMagnitudes; cannam@35: } cannam@35: cannam@35: string cannam@49: PercussionOnsetDetector::getIdentifier() const cannam@35: { cannam@35: return "percussiononsets"; cannam@35: } cannam@35: cannam@35: string cannam@49: PercussionOnsetDetector::getName() const cannam@49: { cannam@49: return "Simple Percussion Onset Detector"; cannam@49: } cannam@49: cannam@49: string cannam@35: PercussionOnsetDetector::getDescription() const cannam@35: { cannam@49: return "Detect percussive note onsets by identifying broadband energy rises"; cannam@35: } cannam@35: cannam@35: string cannam@35: PercussionOnsetDetector::getMaker() const cannam@35: { cannam@43: return "Vamp SDK Example Plugins"; cannam@35: } cannam@35: cannam@35: int cannam@35: PercussionOnsetDetector::getPluginVersion() const cannam@35: { cannam@35: return 2; cannam@35: } cannam@35: cannam@35: string cannam@35: PercussionOnsetDetector::getCopyright() const cannam@35: { cannam@35: return "Code copyright 2006 Queen Mary, University of London, after Dan Barry et al 2005. Freely redistributable (BSD license)"; cannam@35: } cannam@35: cannam@35: size_t cannam@35: PercussionOnsetDetector::getPreferredStepSize() const cannam@35: { cannam@35: return 0; cannam@35: } cannam@35: cannam@35: size_t cannam@35: PercussionOnsetDetector::getPreferredBlockSize() const cannam@35: { cannam@35: return 1024; cannam@35: } cannam@35: cannam@35: bool cannam@35: PercussionOnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@35: { cannam@35: if (channels < getMinChannelCount() || cannam@35: channels > getMaxChannelCount()) return false; cannam@35: cannam@35: m_stepSize = stepSize; cannam@35: m_blockSize = blockSize; cannam@35: cannam@35: m_priorMagnitudes = new float[m_blockSize/2]; cannam@35: cannam@35: for (size_t i = 0; i < m_blockSize/2; ++i) { cannam@35: m_priorMagnitudes[i] = 0.f; cannam@35: } cannam@35: cannam@35: m_dfMinus1 = 0.f; cannam@35: m_dfMinus2 = 0.f; cannam@35: cannam@35: return true; cannam@35: } cannam@35: cannam@35: void cannam@35: PercussionOnsetDetector::reset() cannam@35: { cannam@35: for (size_t i = 0; i < m_blockSize/2; ++i) { cannam@35: m_priorMagnitudes[i] = 0.f; cannam@35: } cannam@35: cannam@35: m_dfMinus1 = 0.f; cannam@35: m_dfMinus2 = 0.f; cannam@35: } cannam@35: cannam@35: PercussionOnsetDetector::ParameterList cannam@35: PercussionOnsetDetector::getParameterDescriptors() const cannam@35: { cannam@35: ParameterList list; cannam@35: cannam@35: ParameterDescriptor d; cannam@49: d.identifier = "threshold"; cannam@49: d.name = "Energy rise threshold"; cannam@49: d.description = "Energy rise within a frequency bin necessary to count toward broadband total"; cannam@35: d.unit = "dB"; cannam@35: d.minValue = 0; cannam@35: d.maxValue = 20; cannam@35: d.defaultValue = 3; cannam@35: d.isQuantized = false; cannam@35: list.push_back(d); cannam@35: cannam@49: d.identifier = "sensitivity"; cannam@49: d.name = "Sensitivity"; cannam@49: d.description = "Sensitivity of peak detector applied to broadband detection function"; cannam@35: d.unit = "%"; cannam@35: d.minValue = 0; cannam@35: d.maxValue = 100; cannam@35: d.defaultValue = 40; cannam@35: d.isQuantized = false; cannam@35: list.push_back(d); cannam@35: cannam@35: return list; cannam@35: } cannam@35: cannam@35: float cannam@49: PercussionOnsetDetector::getParameter(std::string id) const cannam@35: { cannam@49: if (id == "threshold") return m_threshold; cannam@49: if (id == "sensitivity") return m_sensitivity; cannam@35: return 0.f; cannam@35: } cannam@35: cannam@35: void cannam@49: PercussionOnsetDetector::setParameter(std::string id, float value) cannam@35: { cannam@49: if (id == "threshold") { cannam@35: if (value < 0) value = 0; cannam@35: if (value > 20) value = 20; cannam@35: m_threshold = value; cannam@49: } else if (id == "sensitivity") { cannam@35: if (value < 0) value = 0; cannam@35: if (value > 100) value = 100; cannam@35: m_sensitivity = value; cannam@35: } cannam@35: } cannam@35: cannam@35: PercussionOnsetDetector::OutputList cannam@35: PercussionOnsetDetector::getOutputDescriptors() const cannam@35: { cannam@35: OutputList list; cannam@35: cannam@35: OutputDescriptor d; cannam@49: d.identifier = "onsets"; cannam@49: d.name = "Onsets"; cannam@49: d.description = "Percussive note onset locations"; cannam@35: d.unit = ""; cannam@35: d.hasFixedBinCount = true; cannam@35: d.binCount = 0; cannam@35: d.hasKnownExtents = false; cannam@35: d.isQuantized = false; cannam@35: d.sampleType = OutputDescriptor::VariableSampleRate; cannam@35: d.sampleRate = m_inputSampleRate; cannam@35: list.push_back(d); cannam@35: cannam@49: d.identifier = "detectionfunction"; cannam@49: d.name = "Detection Function"; cannam@49: d.description = "Broadband energy rise detection function"; cannam@35: d.binCount = 1; cannam@35: d.isQuantized = true; cannam@35: d.quantizeStep = 1.0; cannam@35: d.sampleType = OutputDescriptor::OneSamplePerStep; cannam@35: list.push_back(d); cannam@35: cannam@35: return list; cannam@35: } cannam@35: cannam@35: PercussionOnsetDetector::FeatureSet cannam@47: PercussionOnsetDetector::process(const float *const *inputBuffers, cannam@47: Vamp::RealTime ts) cannam@35: { cannam@35: if (m_stepSize == 0) { cannam@35: cerr << "ERROR: PercussionOnsetDetector::process: " cannam@35: << "PercussionOnsetDetector has not been initialised" cannam@35: << endl; cannam@35: return FeatureSet(); cannam@35: } cannam@35: cannam@35: int count = 0; cannam@35: cannam@35: for (size_t i = 1; i < m_blockSize/2; ++i) { cannam@35: cannam@35: float real = inputBuffers[0][i*2]; cannam@35: float imag = inputBuffers[0][i*2 + 1]; cannam@82: cannam@35: float sqrmag = real * real + imag * imag; cannam@35: cannam@35: if (m_priorMagnitudes[i] > 0.f) { cannam@35: float diff = 10.f * log10f(sqrmag / m_priorMagnitudes[i]); cannam@35: cannam@35: // std::cout << "i=" << i << ", mag=" << mag << ", prior=" << m_priorMagnitudes[i] << ", diff=" << diff << ", threshold=" << m_threshold << std::endl; cannam@35: cannam@35: if (diff >= m_threshold) ++count; cannam@35: } cannam@35: cannam@35: m_priorMagnitudes[i] = sqrmag; cannam@35: } cannam@35: cannam@35: FeatureSet returnFeatures; cannam@35: cannam@35: Feature detectionFunction; cannam@35: detectionFunction.hasTimestamp = false; cannam@35: detectionFunction.values.push_back(count); cannam@35: returnFeatures[1].push_back(detectionFunction); cannam@35: cannam@35: if (m_dfMinus2 < m_dfMinus1 && cannam@35: m_dfMinus1 >= count && cannam@37: m_dfMinus1 > ((100 - m_sensitivity) * m_blockSize) / 200) { cannam@35: cannam@35: Feature onset; cannam@35: onset.hasTimestamp = true; cannam@35: onset.timestamp = ts - Vamp::RealTime::frame2RealTime cannam@210: (m_stepSize, int(m_inputSampleRate + 0.5)); cannam@35: returnFeatures[0].push_back(onset); cannam@35: } cannam@35: cannam@35: m_dfMinus2 = m_dfMinus1; cannam@35: m_dfMinus1 = count; cannam@35: cannam@35: return returnFeatures; cannam@35: } cannam@35: cannam@35: PercussionOnsetDetector::FeatureSet cannam@35: PercussionOnsetDetector::getRemainingFeatures() cannam@35: { cannam@35: return FeatureSet(); cannam@35: } cannam@35: