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