VampPluginSDK  2.1
ZeroCrossing.cpp
Go to the documentation of this file.
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */
00002 
00003 /*
00004     Vamp
00005 
00006     An API for audio analysis and feature extraction plugins.
00007 
00008     Centre for Digital Music, Queen Mary, University of London.
00009     Copyright 2006 Chris Cannam.
00010   
00011     Permission is hereby granted, free of charge, to any person
00012     obtaining a copy of this software and associated documentation
00013     files (the "Software"), to deal in the Software without
00014     restriction, including without limitation the rights to use, copy,
00015     modify, merge, publish, distribute, sublicense, and/or sell copies
00016     of the Software, and to permit persons to whom the Software is
00017     furnished to do so, subject to the following conditions:
00018 
00019     The above copyright notice and this permission notice shall be
00020     included in all copies or substantial portions of the Software.
00021 
00022     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00023     EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00024     MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
00026     ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00027     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00028     WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00029 
00030     Except as contained in this notice, the names of the Centre for
00031     Digital Music; Queen Mary, University of London; and Chris Cannam
00032     shall not be used in advertising or otherwise to promote the sale,
00033     use or other dealings in this Software without prior written
00034     authorization.
00035 */
00036 
00037 #include "ZeroCrossing.h"
00038 
00039 using std::string;
00040 using std::vector;
00041 using std::cerr;
00042 using std::endl;
00043 
00044 #include <cmath>
00045 
00046 ZeroCrossing::ZeroCrossing(float inputSampleRate) :
00047     Plugin(inputSampleRate),
00048     m_stepSize(0),
00049     m_previousSample(0.0f)
00050 {
00051 }
00052 
00053 ZeroCrossing::~ZeroCrossing()
00054 {
00055 }
00056 
00057 string
00058 ZeroCrossing::getIdentifier() const
00059 {
00060     return "zerocrossing";
00061 }
00062 
00063 string
00064 ZeroCrossing::getName() const
00065 {
00066     return "Zero Crossings";
00067 }
00068 
00069 string
00070 ZeroCrossing::getDescription() const
00071 {
00072     return "Detect and count zero crossing points";
00073 }
00074 
00075 string
00076 ZeroCrossing::getMaker() const
00077 {
00078     return "Vamp SDK Example Plugins";
00079 }
00080 
00081 int
00082 ZeroCrossing::getPluginVersion() const
00083 {
00084     return 2;
00085 }
00086 
00087 string
00088 ZeroCrossing::getCopyright() const
00089 {
00090     return "Freely redistributable (BSD license)";
00091 }
00092 
00093 bool
00094 ZeroCrossing::initialise(size_t channels, size_t stepSize, size_t blockSize)
00095 {
00096     if (channels < getMinChannelCount() ||
00097         channels > getMaxChannelCount()) return false;
00098 
00099     m_stepSize = std::min(stepSize, blockSize);
00100 
00101     return true;
00102 }
00103 
00104 void
00105 ZeroCrossing::reset()
00106 {
00107     m_previousSample = 0.0f;
00108 }
00109 
00110 ZeroCrossing::OutputList
00111 ZeroCrossing::getOutputDescriptors() const
00112 {
00113     OutputList list;
00114 
00115     OutputDescriptor zc;
00116     zc.identifier = "counts";
00117     zc.name = "Zero Crossing Counts";
00118     zc.description = "The number of zero crossing points per processing block";
00119     zc.unit = "crossings";
00120     zc.hasFixedBinCount = true;
00121     zc.binCount = 1;
00122     zc.hasKnownExtents = false;
00123     zc.isQuantized = true;
00124     zc.quantizeStep = 1.0;
00125     zc.sampleType = OutputDescriptor::OneSamplePerStep;
00126     list.push_back(zc);
00127 
00128     zc.identifier = "zerocrossings";
00129     zc.name = "Zero Crossings";
00130     zc.description = "The locations of zero crossing points";
00131     zc.unit = "";
00132     zc.hasFixedBinCount = true;
00133     zc.binCount = 0;
00134     zc.sampleType = OutputDescriptor::VariableSampleRate;
00135     zc.sampleRate = m_inputSampleRate;
00136     list.push_back(zc);
00137 
00138     return list;
00139 }
00140 
00141 ZeroCrossing::FeatureSet
00142 ZeroCrossing::process(const float *const *inputBuffers,
00143                       Vamp::RealTime timestamp)
00144 {
00145     if (m_stepSize == 0) {
00146         cerr << "ERROR: ZeroCrossing::process: "
00147              << "ZeroCrossing has not been initialised"
00148              << endl;
00149         return FeatureSet();
00150     }
00151 
00152     float prev = m_previousSample;
00153     size_t count = 0;
00154 
00155     FeatureSet returnFeatures;
00156 
00157     for (size_t i = 0; i < m_stepSize; ++i) {
00158 
00159         float sample = inputBuffers[0][i];
00160         bool crossing = false;
00161 
00162         if (sample <= 0.0) {
00163             if (prev > 0.0) crossing = true;
00164         } else if (sample > 0.0) {
00165             if (prev <= 0.0) crossing = true;
00166         }
00167 
00168         if (crossing) {
00169             ++count; 
00170             Feature feature;
00171             feature.hasTimestamp = true;
00172             feature.timestamp = timestamp +
00173                 Vamp::RealTime::frame2RealTime(i, (size_t)m_inputSampleRate);
00174             returnFeatures[1].push_back(feature);
00175         }
00176 
00177         prev = sample;
00178     }
00179 
00180     m_previousSample = prev;
00181 
00182     Feature feature;
00183     feature.hasTimestamp = false;
00184     feature.values.push_back(float(count));
00185 
00186     returnFeatures[0].push_back(feature);
00187     return returnFeatures;
00188 }
00189 
00190 ZeroCrossing::FeatureSet
00191 ZeroCrossing::getRemainingFeatures()
00192 {
00193     return FeatureSet();
00194 }
00195