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 #include <algorithm>
00046 
00047 ZeroCrossing::ZeroCrossing(float inputSampleRate) :
00048     Plugin(inputSampleRate),
00049     m_stepSize(0),
00050     m_previousSample(0.0f)
00051 {
00052 }
00053 
00054 ZeroCrossing::~ZeroCrossing()
00055 {
00056 }
00057 
00058 string
00059 ZeroCrossing::getIdentifier() const
00060 {
00061     return "zerocrossing";
00062 }
00063 
00064 string
00065 ZeroCrossing::getName() const
00066 {
00067     return "Zero Crossings";
00068 }
00069 
00070 string
00071 ZeroCrossing::getDescription() const
00072 {
00073     return "Detect and count zero crossing points";
00074 }
00075 
00076 string
00077 ZeroCrossing::getMaker() const
00078 {
00079     return "Vamp SDK Example Plugins";
00080 }
00081 
00082 int
00083 ZeroCrossing::getPluginVersion() const
00084 {
00085     return 2;
00086 }
00087 
00088 string
00089 ZeroCrossing::getCopyright() const
00090 {
00091     return "Freely redistributable (BSD license)";
00092 }
00093 
00094 bool
00095 ZeroCrossing::initialise(size_t channels, size_t stepSize, size_t blockSize)
00096 {
00097     if (channels < getMinChannelCount() ||
00098         channels > getMaxChannelCount()) return false;
00099 
00100     m_stepSize = std::min(stepSize, blockSize);
00101 
00102     return true;
00103 }
00104 
00105 void
00106 ZeroCrossing::reset()
00107 {
00108     m_previousSample = 0.0f;
00109 }
00110 
00111 ZeroCrossing::OutputList
00112 ZeroCrossing::getOutputDescriptors() const
00113 {
00114     OutputList list;
00115 
00116     OutputDescriptor zc;
00117     zc.identifier = "counts";
00118     zc.name = "Zero Crossing Counts";
00119     zc.description = "The number of zero crossing points per processing block";
00120     zc.unit = "crossings";
00121     zc.hasFixedBinCount = true;
00122     zc.binCount = 1;
00123     zc.hasKnownExtents = false;
00124     zc.isQuantized = true;
00125     zc.quantizeStep = 1.0;
00126     zc.sampleType = OutputDescriptor::OneSamplePerStep;
00127     list.push_back(zc);
00128 
00129     zc.identifier = "zerocrossings";
00130     zc.name = "Zero Crossings";
00131     zc.description = "The locations of zero crossing points";
00132     zc.unit = "";
00133     zc.hasFixedBinCount = true;
00134     zc.binCount = 0;
00135     zc.sampleType = OutputDescriptor::VariableSampleRate;
00136     zc.sampleRate = m_inputSampleRate;
00137     list.push_back(zc);
00138 
00139     return list;
00140 }
00141 
00142 ZeroCrossing::FeatureSet
00143 ZeroCrossing::process(const float *const *inputBuffers,
00144                       Vamp::RealTime timestamp)
00145 {
00146     if (m_stepSize == 0) {
00147         cerr << "ERROR: ZeroCrossing::process: "
00148              << "ZeroCrossing has not been initialised"
00149              << endl;
00150         return FeatureSet();
00151     }
00152 
00153     float prev = m_previousSample;
00154     size_t count = 0;
00155 
00156     FeatureSet returnFeatures;
00157 
00158     for (size_t i = 0; i < m_stepSize; ++i) {
00159 
00160         float sample = inputBuffers[0][i];
00161         bool crossing = false;
00162 
00163         if (sample <= 0.0) {
00164             if (prev > 0.0) crossing = true;
00165         } else if (sample > 0.0) {
00166             if (prev <= 0.0) crossing = true;
00167         }
00168 
00169         if (crossing) {
00170             ++count; 
00171             Feature feature;
00172             feature.hasTimestamp = true;
00173             feature.timestamp = timestamp +
00174                 Vamp::RealTime::frame2RealTime(i, (size_t)m_inputSampleRate);
00175             returnFeatures[1].push_back(feature);
00176         }
00177 
00178         prev = sample;
00179     }
00180 
00181     m_previousSample = prev;
00182 
00183     Feature feature;
00184     feature.hasTimestamp = false;
00185     feature.values.push_back(float(count));
00186 
00187     returnFeatures[0].push_back(feature);
00188     return returnFeatures;
00189 }
00190 
00191 ZeroCrossing::FeatureSet
00192 ZeroCrossing::getRemainingFeatures()
00193 {
00194     return FeatureSet();
00195 }
00196