annotate examples/ZeroCrossing.cpp @ 203:4e90c48a7460

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