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