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 "PercussionOnsetDetector.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 #include <cmath> cannam@0: 00045 cannam@0: 00046 cannam@0: 00047 PercussionOnsetDetector::PercussionOnsetDetector(float inputSampleRate) : cannam@0: 00048 Plugin(inputSampleRate), cannam@0: 00049 m_stepSize(0), cannam@0: 00050 m_blockSize(0), cannam@0: 00051 m_threshold(3), cannam@0: 00052 m_sensitivity(40), cannam@0: 00053 m_priorMagnitudes(0), cannam@0: 00054 m_dfMinus1(0), cannam@0: 00055 m_dfMinus2(0) cannam@0: 00056 { cannam@0: 00057 } cannam@0: 00058 cannam@0: 00059 PercussionOnsetDetector::~PercussionOnsetDetector() cannam@0: 00060 { cannam@0: 00061 delete[] m_priorMagnitudes; cannam@0: 00062 } cannam@0: 00063 cannam@0: 00064 string cannam@0: 00065 PercussionOnsetDetector::getIdentifier() const cannam@0: 00066 { cannam@0: 00067 return "percussiononsets"; cannam@0: 00068 } cannam@0: 00069 cannam@0: 00070 string cannam@0: 00071 PercussionOnsetDetector::getName() const cannam@0: 00072 { cannam@0: 00073 return "Simple Percussion Onset Detector"; cannam@0: 00074 } cannam@0: 00075 cannam@0: 00076 string cannam@0: 00077 PercussionOnsetDetector::getDescription() const cannam@0: 00078 { cannam@0: 00079 return "Detect percussive note onsets by identifying broadband energy rises"; cannam@0: 00080 } cannam@0: 00081 cannam@0: 00082 string cannam@0: 00083 PercussionOnsetDetector::getMaker() const cannam@0: 00084 { cannam@0: 00085 return "Vamp SDK Example Plugins"; cannam@0: 00086 } cannam@0: 00087 cannam@0: 00088 int cannam@0: 00089 PercussionOnsetDetector::getPluginVersion() const cannam@0: 00090 { cannam@0: 00091 return 2; cannam@0: 00092 } cannam@0: 00093 cannam@0: 00094 string cannam@0: 00095 PercussionOnsetDetector::getCopyright() const cannam@0: 00096 { cannam@0: 00097 return "Code copyright 2006 Queen Mary, University of London, after Dan Barry et al 2005. Freely redistributable (BSD license)"; cannam@0: 00098 } cannam@0: 00099 cannam@0: 00100 size_t cannam@0: 00101 PercussionOnsetDetector::getPreferredStepSize() const cannam@0: 00102 { cannam@0: 00103 return 0; cannam@0: 00104 } cannam@0: 00105 cannam@0: 00106 size_t cannam@0: 00107 PercussionOnsetDetector::getPreferredBlockSize() const cannam@0: 00108 { cannam@0: 00109 return 1024; cannam@0: 00110 } cannam@0: 00111 cannam@0: 00112 bool cannam@0: 00113 PercussionOnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@0: 00114 { cannam@0: 00115 if (channels < getMinChannelCount() || cannam@0: 00116 channels > getMaxChannelCount()) return false; cannam@0: 00117 cannam@0: 00118 m_stepSize = stepSize; cannam@0: 00119 m_blockSize = blockSize; cannam@0: 00120 cannam@0: 00121 m_priorMagnitudes = new float[m_blockSize/2]; cannam@0: 00122 cannam@0: 00123 for (size_t i = 0; i < m_blockSize/2; ++i) { cannam@0: 00124 m_priorMagnitudes[i] = 0.f; cannam@0: 00125 } cannam@0: 00126 cannam@0: 00127 m_dfMinus1 = 0.f; cannam@0: 00128 m_dfMinus2 = 0.f; cannam@0: 00129 cannam@0: 00130 return true; cannam@0: 00131 } cannam@0: 00132 cannam@0: 00133 void cannam@0: 00134 PercussionOnsetDetector::reset() cannam@0: 00135 { cannam@0: 00136 for (size_t i = 0; i < m_blockSize/2; ++i) { cannam@0: 00137 m_priorMagnitudes[i] = 0.f; cannam@0: 00138 } cannam@0: 00139 cannam@0: 00140 m_dfMinus1 = 0.f; cannam@0: 00141 m_dfMinus2 = 0.f; cannam@0: 00142 } cannam@0: 00143 cannam@0: 00144 PercussionOnsetDetector::ParameterList cannam@0: 00145 PercussionOnsetDetector::getParameterDescriptors() const cannam@0: 00146 { cannam@0: 00147 ParameterList list; cannam@0: 00148 cannam@0: 00149 ParameterDescriptor d; cannam@35: 00150 d.identifier = "threshold"; cannam@35: 00151 d.name = "Energy rise threshold"; cannam@35: 00152 d.description = "Energy rise within a frequency bin necessary to count toward broadband total"; cannam@35: 00153 d.unit = "dB"; cannam@35: 00154 d.minValue = 0; cannam@35: 00155 d.maxValue = 20; cannam@35: 00156 d.defaultValue = 3; cannam@35: 00157 d.isQuantized = false; cannam@0: 00158 list.push_back(d); cannam@0: 00159 cannam@35: 00160 d.identifier = "sensitivity"; cannam@35: 00161 d.name = "Sensitivity"; cannam@35: 00162 d.description = "Sensitivity of peak detector applied to broadband detection function"; cannam@35: 00163 d.unit = "%"; cannam@35: 00164 d.minValue = 0; cannam@35: 00165 d.maxValue = 100; cannam@35: 00166 d.defaultValue = 40; cannam@35: 00167 d.isQuantized = false; cannam@0: 00168 list.push_back(d); cannam@0: 00169 cannam@0: 00170 return list; cannam@0: 00171 } cannam@0: 00172 cannam@0: 00173 float cannam@0: 00174 PercussionOnsetDetector::getParameter(std::string id) const cannam@0: 00175 { cannam@0: 00176 if (id == "threshold") return m_threshold; cannam@0: 00177 if (id == "sensitivity") return m_sensitivity; cannam@0: 00178 return 0.f; cannam@0: 00179 } cannam@0: 00180 cannam@0: 00181 void cannam@0: 00182 PercussionOnsetDetector::setParameter(std::string id, float value) cannam@0: 00183 { cannam@0: 00184 if (id == "threshold") { cannam@0: 00185 if (value < 0) value = 0; cannam@0: 00186 if (value > 20) value = 20; cannam@0: 00187 m_threshold = value; cannam@0: 00188 } else if (id == "sensitivity") { cannam@0: 00189 if (value < 0) value = 0; cannam@0: 00190 if (value > 100) value = 100; cannam@0: 00191 m_sensitivity = value; cannam@0: 00192 } cannam@0: 00193 } cannam@0: 00194 cannam@0: 00195 PercussionOnsetDetector::OutputList cannam@0: 00196 PercussionOnsetDetector::getOutputDescriptors() const cannam@0: 00197 { cannam@0: 00198 OutputList list; cannam@0: 00199 cannam@0: 00200 OutputDescriptor d; cannam@35: 00201 d.identifier = "onsets"; cannam@35: 00202 d.name = "Onsets"; cannam@35: 00203 d.description = "Percussive note onset locations"; cannam@35: 00204 d.unit = ""; cannam@35: 00205 d.hasFixedBinCount = true; cannam@35: 00206 d.binCount = 0; cannam@35: 00207 d.hasKnownExtents = false; cannam@35: 00208 d.isQuantized = false; cannam@35: 00209 d.sampleType = OutputDescriptor::VariableSampleRate; cannam@35: 00210 d.sampleRate = m_inputSampleRate; cannam@0: 00211 list.push_back(d); cannam@0: 00212 cannam@35: 00213 d.identifier = "detectionfunction"; cannam@35: 00214 d.name = "Detection Function"; cannam@35: 00215 d.description = "Broadband energy rise detection function"; cannam@35: 00216 d.binCount = 1; cannam@35: 00217 d.isQuantized = true; cannam@35: 00218 d.quantizeStep = 1.0; cannam@35: 00219 d.sampleType = OutputDescriptor::OneSamplePerStep; cannam@0: 00220 list.push_back(d); cannam@0: 00221 cannam@0: 00222 return list; cannam@0: 00223 } cannam@0: 00224 cannam@0: 00225 PercussionOnsetDetector::FeatureSet cannam@0: 00226 PercussionOnsetDetector::process(const float *const *inputBuffers, cannam@0: 00227 Vamp::RealTime ts) cannam@0: 00228 { cannam@0: 00229 if (m_stepSize == 0) { cannam@0: 00230 cerr << "ERROR: PercussionOnsetDetector::process: " cannam@0: 00231 << "PercussionOnsetDetector has not been initialised" cannam@0: 00232 << endl; cannam@0: 00233 return FeatureSet(); cannam@0: 00234 } cannam@0: 00235 cannam@0: 00236 int count = 0; cannam@0: 00237 cannam@0: 00238 for (size_t i = 1; i < m_blockSize/2; ++i) { cannam@0: 00239 cannam@0: 00240 float real = inputBuffers[0][i*2]; cannam@0: 00241 float imag = inputBuffers[0][i*2 + 1]; cannam@0: 00242 cannam@0: 00243 float sqrmag = real * real + imag * imag; cannam@0: 00244 cannam@0: 00245 if (m_priorMagnitudes[i] > 0.f) { cannam@0: 00246 float diff = 10.f * log10f(sqrmag / m_priorMagnitudes[i]); cannam@0: 00247 cannam@21: 00248 // std::cout << "i=" << i << ", sqrmag=" << sqrmag << ", prior=" << m_priorMagnitudes[i] << ", diff=" << diff << ", threshold=" << m_threshold << " " << (diff >= m_threshold ? "[*]" : "") << std::endl; cannam@0: 00249 cannam@0: 00250 if (diff >= m_threshold) ++count; cannam@0: 00251 } cannam@0: 00252 cannam@0: 00253 m_priorMagnitudes[i] = sqrmag; cannam@0: 00254 } cannam@0: 00255 cannam@0: 00256 FeatureSet returnFeatures; cannam@0: 00257 cannam@0: 00258 Feature detectionFunction; cannam@35: 00259 detectionFunction.hasTimestamp = false; cannam@35: 00260 detectionFunction.values.push_back(count); cannam@0: 00261 returnFeatures[1].push_back(detectionFunction); cannam@0: 00262 cannam@0: 00263 if (m_dfMinus2 < m_dfMinus1 && cannam@0: 00264 m_dfMinus1 >= count && cannam@0: 00265 m_dfMinus1 > ((100 - m_sensitivity) * m_blockSize) / 200) { cannam@0: 00266 cannam@21: 00267 //std::cout << "result at " << ts << "! (count == " << count << ", prev == " << m_dfMinus1 << ")" << std::endl; cannam@21: 00268 cannam@21: 00269 Feature onset; cannam@35: 00270 onset.hasTimestamp = true; cannam@35: 00271 onset.timestamp = ts - Vamp::RealTime::frame2RealTime cannam@21: 00272 (m_stepSize, int(m_inputSampleRate + 0.5)); cannam@21: 00273 returnFeatures[0].push_back(onset); cannam@21: 00274 } cannam@21: 00275 cannam@21: 00276 m_dfMinus2 = m_dfMinus1; cannam@21: 00277 m_dfMinus1 = count; cannam@21: 00278 cannam@21: 00279 return returnFeatures; cannam@21: 00280 } cannam@21: 00281 cannam@21: 00282 PercussionOnsetDetector::FeatureSet cannam@21: 00283 PercussionOnsetDetector::getRemainingFeatures() cannam@21: 00284 { cannam@21: 00285 return FeatureSet(); cannam@21: 00286 } cannam@21: 00287 cannam@0: