annotate src/vamp-plugin-sdk-2.4/examples/ZeroCrossing.cpp @ 169:223a55898ab9 tip default

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