cannam@50: cannam@50: cannam@50:
cannam@50: cannam@50:
cannam@50: VampPluginSDK
cannam@50: 2.1
cannam@50:
cannam@50:
cannam@50: |
cannam@50:
cannam@50:
cannam@50:
cannam@50:
00001 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ cannam@50: 00002 cannam@50: 00003 /* cannam@50: 00004 Vamp cannam@50: 00005 cannam@50: 00006 An API for audio analysis and feature extraction plugins. cannam@50: 00007 cannam@50: 00008 Centre for Digital Music, Queen Mary, University of London. cannam@50: 00009 Copyright 2006 Chris Cannam. cannam@50: 00010 cannam@50: 00011 Permission is hereby granted, free of charge, to any person cannam@50: 00012 obtaining a copy of this software and associated documentation cannam@50: 00013 files (the "Software"), to deal in the Software without cannam@50: 00014 restriction, including without limitation the rights to use, copy, cannam@50: 00015 modify, merge, publish, distribute, sublicense, and/or sell copies cannam@50: 00016 of the Software, and to permit persons to whom the Software is cannam@50: 00017 furnished to do so, subject to the following conditions: cannam@50: 00018 cannam@50: 00019 The above copyright notice and this permission notice shall be cannam@50: 00020 included in all copies or substantial portions of the Software. cannam@50: 00021 cannam@50: 00022 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, cannam@50: 00023 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF cannam@50: 00024 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND cannam@50: 00025 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR cannam@50: 00026 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF cannam@50: 00027 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION cannam@50: 00028 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. cannam@50: 00029 cannam@50: 00030 Except as contained in this notice, the names of the Centre for cannam@50: 00031 Digital Music; Queen Mary, University of London; and Chris Cannam cannam@50: 00032 shall not be used in advertising or otherwise to promote the sale, cannam@50: 00033 use or other dealings in this Software without prior written cannam@50: 00034 authorization. cannam@50: 00035 */ cannam@50: 00036 cannam@50: 00037 #include "PercussionOnsetDetector.h" cannam@50: 00038 cannam@50: 00039 using std::string; cannam@50: 00040 using std::vector; cannam@50: 00041 using std::cerr; cannam@50: 00042 using std::endl; cannam@50: 00043 cannam@50: 00044 #include <cmath> cannam@50: 00045 cannam@50: 00046 cannam@50: 00047 PercussionOnsetDetector::PercussionOnsetDetector(float inputSampleRate) : cannam@50: 00048 Plugin(inputSampleRate), cannam@50: 00049 m_stepSize(0), cannam@50: 00050 m_blockSize(0), cannam@50: 00051 m_threshold(3), cannam@50: 00052 m_sensitivity(40), cannam@50: 00053 m_priorMagnitudes(0), cannam@50: 00054 m_dfMinus1(0), cannam@50: 00055 m_dfMinus2(0) cannam@50: 00056 { cannam@50: 00057 } cannam@50: 00058 cannam@50: 00059 PercussionOnsetDetector::~PercussionOnsetDetector() cannam@50: 00060 { cannam@50: 00061 delete[] m_priorMagnitudes; cannam@50: 00062 } cannam@50: 00063 cannam@50: 00064 string cannam@50: 00065 PercussionOnsetDetector::getIdentifier() const cannam@50: 00066 { cannam@50: 00067 return "percussiononsets"; cannam@50: 00068 } cannam@50: 00069 cannam@50: 00070 string cannam@50: 00071 PercussionOnsetDetector::getName() const cannam@50: 00072 { cannam@50: 00073 return "Simple Percussion Onset Detector"; cannam@50: 00074 } cannam@50: 00075 cannam@50: 00076 string cannam@50: 00077 PercussionOnsetDetector::getDescription() const cannam@50: 00078 { cannam@50: 00079 return "Detect percussive note onsets by identifying broadband energy rises"; cannam@50: 00080 } cannam@50: 00081 cannam@50: 00082 string cannam@50: 00083 PercussionOnsetDetector::getMaker() const cannam@50: 00084 { cannam@50: 00085 return "Vamp SDK Example Plugins"; cannam@50: 00086 } cannam@50: 00087 cannam@50: 00088 int cannam@50: 00089 PercussionOnsetDetector::getPluginVersion() const cannam@50: 00090 { cannam@50: 00091 return 2; cannam@50: 00092 } cannam@50: 00093 cannam@50: 00094 string cannam@50: 00095 PercussionOnsetDetector::getCopyright() const cannam@50: 00096 { cannam@50: 00097 return "Code copyright 2006 Queen Mary, University of London, after Dan Barry et al 2005. Freely redistributable (BSD license)"; cannam@50: 00098 } cannam@50: 00099 cannam@50: 00100 size_t cannam@50: 00101 PercussionOnsetDetector::getPreferredStepSize() const cannam@50: 00102 { cannam@50: 00103 return 0; cannam@50: 00104 } cannam@50: 00105 cannam@50: 00106 size_t cannam@50: 00107 PercussionOnsetDetector::getPreferredBlockSize() const cannam@50: 00108 { cannam@50: 00109 return 1024; cannam@50: 00110 } cannam@50: 00111 cannam@50: 00112 bool cannam@50: 00113 PercussionOnsetDetector::initialise(size_t channels, size_t stepSize, size_t blockSize) cannam@50: 00114 { cannam@50: 00115 if (channels < getMinChannelCount() || cannam@50: 00116 channels > getMaxChannelCount()) return false; cannam@50: 00117 cannam@50: 00118 m_stepSize = stepSize; cannam@50: 00119 m_blockSize = blockSize; cannam@50: 00120 cannam@50: 00121 m_priorMagnitudes = new float[m_blockSize/2]; cannam@50: 00122 cannam@50: 00123 for (size_t i = 0; i < m_blockSize/2; ++i) { cannam@50: 00124 m_priorMagnitudes[i] = 0.f; cannam@50: 00125 } cannam@50: 00126 cannam@50: 00127 m_dfMinus1 = 0.f; cannam@50: 00128 m_dfMinus2 = 0.f; cannam@50: 00129 cannam@50: 00130 return true; cannam@50: 00131 } cannam@50: 00132 cannam@50: 00133 void cannam@50: 00134 PercussionOnsetDetector::reset() cannam@50: 00135 { cannam@50: 00136 for (size_t i = 0; i < m_blockSize/2; ++i) { cannam@50: 00137 m_priorMagnitudes[i] = 0.f; cannam@50: 00138 } cannam@50: 00139 cannam@50: 00140 m_dfMinus1 = 0.f; cannam@50: 00141 m_dfMinus2 = 0.f; cannam@50: 00142 } cannam@50: 00143 cannam@50: 00144 PercussionOnsetDetector::ParameterList cannam@50: 00145 PercussionOnsetDetector::getParameterDescriptors() const cannam@50: 00146 { cannam@50: 00147 ParameterList list; cannam@50: 00148 cannam@50: 00149 ParameterDescriptor d; cannam@50: 00150 d.identifier = "threshold"; cannam@50: 00151 d.name = "Energy rise threshold"; cannam@50: 00152 d.description = "Energy rise within a frequency bin necessary to count toward broadband total"; cannam@50: 00153 d.unit = "dB"; cannam@50: 00154 d.minValue = 0; cannam@50: 00155 d.maxValue = 20; cannam@50: 00156 d.defaultValue = 3; cannam@50: 00157 d.isQuantized = false; cannam@50: 00158 list.push_back(d); cannam@50: 00159 cannam@50: 00160 d.identifier = "sensitivity"; cannam@50: 00161 d.name = "Sensitivity"; cannam@50: 00162 d.description = "Sensitivity of peak detector applied to broadband detection function"; cannam@50: 00163 d.unit = "%"; cannam@50: 00164 d.minValue = 0; cannam@50: 00165 d.maxValue = 100; cannam@50: 00166 d.defaultValue = 40; cannam@50: 00167 d.isQuantized = false; cannam@50: 00168 list.push_back(d); cannam@50: 00169 cannam@50: 00170 return list; cannam@50: 00171 } cannam@50: 00172 cannam@50: 00173 float cannam@50: 00174 PercussionOnsetDetector::getParameter(std::string id) const cannam@50: 00175 { cannam@50: 00176 if (id == "threshold") return m_threshold; cannam@50: 00177 if (id == "sensitivity") return m_sensitivity; cannam@50: 00178 return 0.f; cannam@50: 00179 } cannam@50: 00180 cannam@50: 00181 void cannam@50: 00182 PercussionOnsetDetector::setParameter(std::string id, float value) cannam@50: 00183 { cannam@50: 00184 if (id == "threshold") { cannam@50: 00185 if (value < 0) value = 0; cannam@50: 00186 if (value > 20) value = 20; cannam@50: 00187 m_threshold = value; cannam@50: 00188 } else if (id == "sensitivity") { cannam@50: 00189 if (value < 0) value = 0; cannam@50: 00190 if (value > 100) value = 100; cannam@50: 00191 m_sensitivity = value; cannam@50: 00192 } cannam@50: 00193 } cannam@50: 00194 cannam@50: 00195 PercussionOnsetDetector::OutputList cannam@50: 00196 PercussionOnsetDetector::getOutputDescriptors() const cannam@50: 00197 { cannam@50: 00198 OutputList list; cannam@50: 00199 cannam@50: 00200 OutputDescriptor d; cannam@50: 00201 d.identifier = "onsets"; cannam@50: 00202 d.name = "Onsets"; cannam@50: 00203 d.description = "Percussive note onset locations"; cannam@50: 00204 d.unit = ""; cannam@50: 00205 d.hasFixedBinCount = true; cannam@50: 00206 d.binCount = 0; cannam@50: 00207 d.hasKnownExtents = false; cannam@50: 00208 d.isQuantized = false; cannam@50: 00209 d.sampleType = OutputDescriptor::VariableSampleRate; cannam@50: 00210 d.sampleRate = m_inputSampleRate; cannam@50: 00211 list.push_back(d); cannam@50: 00212 cannam@50: 00213 d.identifier = "detectionfunction"; cannam@50: 00214 d.name = "Detection Function"; cannam@50: 00215 d.description = "Broadband energy rise detection function"; cannam@50: 00216 d.binCount = 1; cannam@50: 00217 d.isQuantized = true; cannam@50: 00218 d.quantizeStep = 1.0; cannam@50: 00219 d.sampleType = OutputDescriptor::OneSamplePerStep; cannam@50: 00220 list.push_back(d); cannam@50: 00221 cannam@50: 00222 return list; cannam@50: 00223 } cannam@50: 00224 cannam@50: 00225 PercussionOnsetDetector::FeatureSet cannam@50: 00226 PercussionOnsetDetector::process(const float *const *inputBuffers, cannam@50: 00227 Vamp::RealTime ts) cannam@50: 00228 { cannam@50: 00229 if (m_stepSize == 0) { cannam@50: 00230 cerr << "ERROR: PercussionOnsetDetector::process: " cannam@50: 00231 << "PercussionOnsetDetector has not been initialised" cannam@50: 00232 << endl; cannam@50: 00233 return FeatureSet(); cannam@50: 00234 } cannam@50: 00235 cannam@50: 00236 int count = 0; cannam@50: 00237 cannam@50: 00238 for (size_t i = 1; i < m_blockSize/2; ++i) { cannam@50: 00239 cannam@50: 00240 float real = inputBuffers[0][i*2]; cannam@50: 00241 float imag = inputBuffers[0][i*2 + 1]; cannam@50: 00242 cannam@50: 00243 float sqrmag = real * real + imag * imag; cannam@50: 00244 cannam@50: 00245 if (m_priorMagnitudes[i] > 0.f) { cannam@50: 00246 float diff = 10.f * log10f(sqrmag / m_priorMagnitudes[i]); cannam@50: 00247 cannam@50: 00248 // std::cout << "i=" << i << ", sqrmag=" << sqrmag << ", prior=" << m_priorMagnitudes[i] << ", diff=" << diff << ", threshold=" << m_threshold << " " << (diff >= m_threshold ? "[*]" : "") << std::endl; cannam@50: 00249 cannam@50: 00250 if (diff >= m_threshold) ++count; cannam@50: 00251 } cannam@50: 00252 cannam@50: 00253 m_priorMagnitudes[i] = sqrmag; cannam@50: 00254 } cannam@50: 00255 cannam@50: 00256 FeatureSet returnFeatures; cannam@50: 00257 cannam@50: 00258 Feature detectionFunction; cannam@50: 00259 detectionFunction.hasTimestamp = false; cannam@50: 00260 detectionFunction.values.push_back(count); cannam@50: 00261 returnFeatures[1].push_back(detectionFunction); cannam@50: 00262 cannam@50: 00263 if (m_dfMinus2 < m_dfMinus1 && cannam@50: 00264 m_dfMinus1 >= count && cannam@50: 00265 m_dfMinus1 > ((100 - m_sensitivity) * m_blockSize) / 200) { cannam@50: 00266 cannam@50: 00267 //std::cout << "result at " << ts << "! (count == " << count << ", prev == " << m_dfMinus1 << ")" << std::endl; cannam@50: 00268 cannam@50: 00269 Feature onset; cannam@50: 00270 onset.hasTimestamp = true; cannam@50: 00271 onset.timestamp = ts - Vamp::RealTime::frame2RealTime cannam@50: 00272 (m_stepSize, int(m_inputSampleRate + 0.5)); cannam@50: 00273 returnFeatures[0].push_back(onset); cannam@50: 00274 } cannam@50: 00275 cannam@50: 00276 m_dfMinus2 = m_dfMinus1; cannam@50: 00277 m_dfMinus1 = count; cannam@50: 00278 cannam@50: 00279 return returnFeatures; cannam@50: 00280 } cannam@50: 00281 cannam@50: 00282 PercussionOnsetDetector::FeatureSet cannam@50: 00283 PercussionOnsetDetector::getRemainingFeatures() cannam@50: 00284 { cannam@50: 00285 return FeatureSet(); cannam@50: 00286 } cannam@50: 00287 cannam@50: