cannam@0: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@0: cannam@0: /* cannam@0: Vamp cannam@0: cannam@0: An API for audio analysis and feature extraction plugins. cannam@0: cannam@0: Centre for Digital Music, Queen Mary, University of London. cannam@0: Copyright 2006 Chris Cannam. cannam@0: cannam@0: Permission is hereby granted, free of charge, to any person cannam@0: obtaining a copy of this software and associated documentation cannam@0: files (the "Software"), to deal in the Software without cannam@0: restriction, including without limitation the rights to use, copy, cannam@0: modify, merge, publish, distribute, sublicense, and/or sell copies cannam@0: of the Software, and to permit persons to whom the Software is cannam@0: furnished to do so, subject to the following conditions: cannam@0: cannam@0: The above copyright notice and this permission notice shall be cannam@0: included in all copies or substantial portions of the Software. cannam@0: cannam@0: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@0: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@0: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@6: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@0: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@0: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@0: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@0: cannam@0: Except as contained in this notice, the names of the Centre for cannam@0: Digital Music; Queen Mary, University of London; and Chris Cannam cannam@0: shall not be used in advertising or otherwise to promote the sale, cannam@0: use or other dealings in this Software without prior written cannam@0: authorization. cannam@0: */ cannam@0: cannam@0: #include "ZeroCrossing.h" cannam@0: cannam@0: using std::string; cannam@0: using std::vector; cannam@0: using std::cerr; cannam@0: using std::endl; cannam@0: cannam@0: cannam@0: ZeroCrossing::ZeroCrossing(float inputSampleRate) : cannam@0: Plugin(inputSampleRate), cannam@0: m_stepSize(0), cannam@0: m_previousSample(0.0f) cannam@0: { cannam@0: } cannam@0: cannam@0: ZeroCrossing::~ZeroCrossing() cannam@0: { cannam@0: } cannam@0: cannam@0: string cannam@0: ZeroCrossing::getName() const cannam@0: { cannam@0: return "zerocrossing"; cannam@0: } cannam@0: cannam@0: string cannam@0: ZeroCrossing::getDescription() const cannam@0: { cannam@0: return "Zero Crossings"; cannam@0: } cannam@0: cannam@0: string cannam@0: ZeroCrossing::getMaker() const cannam@0: { cannam@43: return "Vamp SDK Example Plugins"; cannam@0: } cannam@0: cannam@0: int cannam@0: ZeroCrossing::getPluginVersion() const cannam@0: { cannam@0: return 2; cannam@0: } cannam@0: cannam@0: string cannam@0: ZeroCrossing::getCopyright() const cannam@0: { cannam@7: return "Freely redistributable (BSD license)"; cannam@0: } cannam@0: cannam@0: bool cannam@0: ZeroCrossing::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@0: { cannam@0: if (channels < getMinChannelCount() || cannam@0: channels > getMaxChannelCount()) return false; cannam@0: cannam@0: m_stepSize = std::min(stepSize, blockSize); cannam@0: cannam@0: return true; cannam@0: } cannam@0: cannam@0: void cannam@0: ZeroCrossing::reset() cannam@0: { cannam@0: m_previousSample = 0.0f; cannam@0: } cannam@0: cannam@0: ZeroCrossing::OutputList cannam@0: ZeroCrossing::getOutputDescriptors() const cannam@0: { cannam@0: OutputList list; cannam@0: cannam@0: OutputDescriptor zc; cannam@0: zc.name = "counts"; cannam@0: zc.unit = "crossings"; cannam@0: zc.description = "Zero Crossing Counts"; cannam@9: zc.hasFixedBinCount = true; cannam@9: zc.binCount = 1; cannam@0: zc.hasKnownExtents = false; cannam@0: zc.isQuantized = true; cannam@0: zc.quantizeStep = 1.0; cannam@0: zc.sampleType = OutputDescriptor::OneSamplePerStep; cannam@0: list.push_back(zc); cannam@0: cannam@0: zc.name = "zerocrossings"; cannam@0: zc.unit = ""; cannam@0: zc.description = "Zero Crossings"; cannam@9: zc.hasFixedBinCount = true; cannam@9: zc.binCount = 0; cannam@0: zc.sampleType = OutputDescriptor::VariableSampleRate; cannam@0: zc.sampleRate = m_inputSampleRate; cannam@0: list.push_back(zc); cannam@0: cannam@0: return list; cannam@0: } cannam@0: cannam@0: ZeroCrossing::FeatureSet cannam@0: ZeroCrossing::process(float **inputBuffers, Vamp::RealTime timestamp) cannam@0: { cannam@0: if (m_stepSize == 0) { cannam@0: cerr << "ERROR: ZeroCrossing::process: " cannam@0: << "ZeroCrossing has not been initialised" cannam@0: << endl; cannam@0: return FeatureSet(); cannam@0: } cannam@0: cannam@0: float prev = m_previousSample; cannam@0: size_t count = 0; cannam@0: cannam@0: FeatureSet returnFeatures; cannam@0: cannam@0: for (size_t i = 0; i < m_stepSize; ++i) { cannam@0: cannam@0: float sample = inputBuffers[0][i]; cannam@0: bool crossing = false; cannam@0: cannam@0: if (sample <= 0.0) { cannam@0: if (prev > 0.0) crossing = true; cannam@0: } else if (sample > 0.0) { cannam@0: if (prev <= 0.0) crossing = true; cannam@0: } cannam@0: cannam@0: if (crossing) { cannam@0: ++count; cannam@0: Feature feature; cannam@0: feature.hasTimestamp = true; cannam@0: feature.timestamp = timestamp + cannam@0: Vamp::RealTime::frame2RealTime(i, (size_t)m_inputSampleRate); cannam@0: returnFeatures[1].push_back(feature); cannam@0: } cannam@0: cannam@0: prev = sample; cannam@0: } cannam@0: cannam@0: m_previousSample = prev; cannam@0: cannam@0: Feature feature; cannam@0: feature.hasTimestamp = false; cannam@0: feature.values.push_back(count); cannam@0: cannam@0: returnFeatures[0].push_back(feature); cannam@0: return returnFeatures; cannam@0: } cannam@0: cannam@0: ZeroCrossing::FeatureSet cannam@0: ZeroCrossing::getRemainingFeatures() cannam@0: { cannam@0: return FeatureSet(); cannam@0: } cannam@0: