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