annotate src/vamp-plugin-sdk-2.5/examples/ZeroCrossing.cpp @ 23:619f715526df sv_v2.1

Update Vamp plugin SDK to 2.5
author Chris Cannam
date Thu, 09 May 2013 10:52:46 +0100
parents
children
rev   line source
Chris@23 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@23 2
Chris@23 3 /*
Chris@23 4 Vamp
Chris@23 5
Chris@23 6 An API for audio analysis and feature extraction plugins.
Chris@23 7
Chris@23 8 Centre for Digital Music, Queen Mary, University of London.
Chris@23 9 Copyright 2006 Chris Cannam.
Chris@23 10
Chris@23 11 Permission is hereby granted, free of charge, to any person
Chris@23 12 obtaining a copy of this software and associated documentation
Chris@23 13 files (the "Software"), to deal in the Software without
Chris@23 14 restriction, including without limitation the rights to use, copy,
Chris@23 15 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@23 16 of the Software, and to permit persons to whom the Software is
Chris@23 17 furnished to do so, subject to the following conditions:
Chris@23 18
Chris@23 19 The above copyright notice and this permission notice shall be
Chris@23 20 included in all copies or substantial portions of the Software.
Chris@23 21
Chris@23 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@23 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@23 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@23 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
Chris@23 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@23 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@23 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@23 29
Chris@23 30 Except as contained in this notice, the names of the Centre for
Chris@23 31 Digital Music; Queen Mary, University of London; and Chris Cannam
Chris@23 32 shall not be used in advertising or otherwise to promote the sale,
Chris@23 33 use or other dealings in this Software without prior written
Chris@23 34 authorization.
Chris@23 35 */
Chris@23 36
Chris@23 37 #include "ZeroCrossing.h"
Chris@23 38
Chris@23 39 using std::string;
Chris@23 40 using std::vector;
Chris@23 41 using std::cerr;
Chris@23 42 using std::endl;
Chris@23 43
Chris@23 44 #include <cmath>
Chris@23 45
Chris@23 46 ZeroCrossing::ZeroCrossing(float inputSampleRate) :
Chris@23 47 Plugin(inputSampleRate),
Chris@23 48 m_stepSize(0),
Chris@23 49 m_previousSample(0.0f)
Chris@23 50 {
Chris@23 51 }
Chris@23 52
Chris@23 53 ZeroCrossing::~ZeroCrossing()
Chris@23 54 {
Chris@23 55 }
Chris@23 56
Chris@23 57 string
Chris@23 58 ZeroCrossing::getIdentifier() const
Chris@23 59 {
Chris@23 60 return "zerocrossing";
Chris@23 61 }
Chris@23 62
Chris@23 63 string
Chris@23 64 ZeroCrossing::getName() const
Chris@23 65 {
Chris@23 66 return "Zero Crossings";
Chris@23 67 }
Chris@23 68
Chris@23 69 string
Chris@23 70 ZeroCrossing::getDescription() const
Chris@23 71 {
Chris@23 72 return "Detect and count zero crossing points";
Chris@23 73 }
Chris@23 74
Chris@23 75 string
Chris@23 76 ZeroCrossing::getMaker() const
Chris@23 77 {
Chris@23 78 return "Vamp SDK Example Plugins";
Chris@23 79 }
Chris@23 80
Chris@23 81 int
Chris@23 82 ZeroCrossing::getPluginVersion() const
Chris@23 83 {
Chris@23 84 return 2;
Chris@23 85 }
Chris@23 86
Chris@23 87 string
Chris@23 88 ZeroCrossing::getCopyright() const
Chris@23 89 {
Chris@23 90 return "Freely redistributable (BSD license)";
Chris@23 91 }
Chris@23 92
Chris@23 93 bool
Chris@23 94 ZeroCrossing::initialise(size_t channels, size_t stepSize, size_t blockSize)
Chris@23 95 {
Chris@23 96 if (channels < getMinChannelCount() ||
Chris@23 97 channels > getMaxChannelCount()) return false;
Chris@23 98
Chris@23 99 m_stepSize = std::min(stepSize, blockSize);
Chris@23 100
Chris@23 101 return true;
Chris@23 102 }
Chris@23 103
Chris@23 104 void
Chris@23 105 ZeroCrossing::reset()
Chris@23 106 {
Chris@23 107 m_previousSample = 0.0f;
Chris@23 108 }
Chris@23 109
Chris@23 110 ZeroCrossing::OutputList
Chris@23 111 ZeroCrossing::getOutputDescriptors() const
Chris@23 112 {
Chris@23 113 OutputList list;
Chris@23 114
Chris@23 115 OutputDescriptor zc;
Chris@23 116 zc.identifier = "counts";
Chris@23 117 zc.name = "Zero Crossing Counts";
Chris@23 118 zc.description = "The number of zero crossing points per processing block";
Chris@23 119 zc.unit = "crossings";
Chris@23 120 zc.hasFixedBinCount = true;
Chris@23 121 zc.binCount = 1;
Chris@23 122 zc.hasKnownExtents = false;
Chris@23 123 zc.isQuantized = true;
Chris@23 124 zc.quantizeStep = 1.0;
Chris@23 125 zc.sampleType = OutputDescriptor::OneSamplePerStep;
Chris@23 126 list.push_back(zc);
Chris@23 127
Chris@23 128 zc.identifier = "zerocrossings";
Chris@23 129 zc.name = "Zero Crossings";
Chris@23 130 zc.description = "The locations of zero crossing points";
Chris@23 131 zc.unit = "";
Chris@23 132 zc.hasFixedBinCount = true;
Chris@23 133 zc.binCount = 0;
Chris@23 134 zc.sampleType = OutputDescriptor::VariableSampleRate;
Chris@23 135 zc.sampleRate = m_inputSampleRate;
Chris@23 136 list.push_back(zc);
Chris@23 137
Chris@23 138 return list;
Chris@23 139 }
Chris@23 140
Chris@23 141 ZeroCrossing::FeatureSet
Chris@23 142 ZeroCrossing::process(const float *const *inputBuffers,
Chris@23 143 Vamp::RealTime timestamp)
Chris@23 144 {
Chris@23 145 if (m_stepSize == 0) {
Chris@23 146 cerr << "ERROR: ZeroCrossing::process: "
Chris@23 147 << "ZeroCrossing has not been initialised"
Chris@23 148 << endl;
Chris@23 149 return FeatureSet();
Chris@23 150 }
Chris@23 151
Chris@23 152 float prev = m_previousSample;
Chris@23 153 size_t count = 0;
Chris@23 154
Chris@23 155 FeatureSet returnFeatures;
Chris@23 156
Chris@23 157 for (size_t i = 0; i < m_stepSize; ++i) {
Chris@23 158
Chris@23 159 float sample = inputBuffers[0][i];
Chris@23 160 bool crossing = false;
Chris@23 161
Chris@23 162 if (sample <= 0.0) {
Chris@23 163 if (prev > 0.0) crossing = true;
Chris@23 164 } else if (sample > 0.0) {
Chris@23 165 if (prev <= 0.0) crossing = true;
Chris@23 166 }
Chris@23 167
Chris@23 168 if (crossing) {
Chris@23 169 ++count;
Chris@23 170 Feature feature;
Chris@23 171 feature.hasTimestamp = true;
Chris@23 172 feature.timestamp = timestamp +
Chris@23 173 Vamp::RealTime::frame2RealTime(i, (size_t)m_inputSampleRate);
Chris@23 174 returnFeatures[1].push_back(feature);
Chris@23 175 }
Chris@23 176
Chris@23 177 prev = sample;
Chris@23 178 }
Chris@23 179
Chris@23 180 m_previousSample = prev;
Chris@23 181
Chris@23 182 Feature feature;
Chris@23 183 feature.hasTimestamp = false;
Chris@23 184 feature.values.push_back(float(count));
Chris@23 185
Chris@23 186 returnFeatures[0].push_back(feature);
Chris@23 187 return returnFeatures;
Chris@23 188 }
Chris@23 189
Chris@23 190 ZeroCrossing::FeatureSet
Chris@23 191 ZeroCrossing::getRemainingFeatures()
Chris@23 192 {
Chris@23 193 return FeatureSet();
Chris@23 194 }
Chris@23 195