Remove no-longer-supported-by-aubio onset detection function
[vamp-aubio-plugins.git] / plugins / Onset.cpp
index 089f5e1..e5875c5 100644 (file)
@@ -14,6 +14,7 @@
 
 */
 
+#include <math.h>
 #include "Onset.h"
 
 using std::string;
@@ -24,40 +25,41 @@ using std::endl;
 Onset::Onset(float inputSampleRate) :
     Plugin(inputSampleRate),
     m_ibuf(0),
-    m_fftgrain(0),
     m_onset(0),
-    m_pv(0),
-    m_peakpick(0),
     m_onsetdet(0),
-    m_onsettype(aubio_onset_mkl),
+    m_onsettype(OnsetComplex),
     m_threshold(0.3),
-    m_silence(-90)
+    m_silence(-70),
+    m_minioi(4)
 {
 }
 
 Onset::~Onset()
 {
-    if (m_onsetdet) aubio_onsetdetection_free(m_onsetdet);
+    if (m_onsetdet) del_aubio_onset(m_onsetdet);
     if (m_ibuf) del_fvec(m_ibuf);
     if (m_onset) del_fvec(m_onset);
-    if (m_fftgrain) del_cvec(m_fftgrain);
-    if (m_pv) del_aubio_pvoc(m_pv);
-    if (m_peakpick) del_aubio_peakpicker(m_peakpick);
 }
 
 string
-Onset::getName() const
+Onset::getIdentifier() const
 {
     return "aubioonset";
 }
 
 string
-Onset::getDescription() const
+Onset::getName() const
 {
     return "Aubio Onset Detector";
 }
 
 string
+Onset::getDescription() const
+{
+    return "Estimate note onset times";
+}
+
+string
 Onset::getMaker() const
 {
     return "Paul Brossier (plugin by Chris Cannam)";
@@ -66,7 +68,7 @@ Onset::getMaker() const
 int
 Onset::getPluginVersion() const
 {
-    return 1;
+    return 2;
 }
 
 string
@@ -78,25 +80,18 @@ Onset::getCopyright() const
 bool
 Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
 {
-    m_channelCount = channels;
+    if (channels != 1) {
+        std::cerr << "Onset::initialise: channels must be 1" << std::endl;
+        return false;
+    }
+
     m_stepSize = stepSize;
     m_blockSize = blockSize;
 
-    size_t processingBlockSize;
-    if (m_onsettype == aubio_onset_energy ||
-        m_onsettype == aubio_onset_hfc) {
-        processingBlockSize = stepSize * 2;
-    } else {
-        processingBlockSize = stepSize * 4;
-    }
-
-    m_ibuf = new_fvec(stepSize, channels);
-    m_onset = new_fvec(1, channels);
-    m_fftgrain = new_cvec(processingBlockSize, channels);
-    m_pv = new_aubio_pvoc(processingBlockSize, stepSize, channels);
-    m_peakpick = new_aubio_peakpicker(m_threshold);
+    m_ibuf = new_fvec(stepSize);
+    m_onset = new_fvec(1);
 
-    m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
+    reset();
 
     return true;
 }
@@ -104,23 +99,34 @@ Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
 void
 Onset::reset()
 {
+    if (m_onsetdet) del_aubio_onset(m_onsetdet);
+
+    m_onsetdet = new_aubio_onset
+        (const_cast<char *>(getAubioNameForOnsetType(m_onsettype)),
+         m_blockSize,
+         m_stepSize,
+         lrintf(m_inputSampleRate));
+    
+    aubio_onset_set_threshold(m_onsetdet, m_threshold);
+    aubio_onset_set_silence(m_onsetdet, m_silence);
+    aubio_onset_set_minioi(m_onsetdet, m_minioi);
+
+    m_delay = Vamp::RealTime::frame2RealTime(4 * m_stepSize,
+                                             lrintf(m_inputSampleRate));
+
+    m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
 }
 
 size_t
 Onset::getPreferredStepSize() const
 {
-    if (m_onsettype == aubio_onset_energy ||
-        m_onsettype == aubio_onset_hfc) {
-        return 512;
-    } else {
-        return 128;
-    }
+    return 512;
 }
 
 size_t
 Onset::getPreferredBlockSize() const
 {
-    return getPreferredStepSize();
+    return 2 * getPreferredStepSize();
 }
 
 Onset::ParameterList
@@ -129,11 +135,11 @@ Onset::getParameterDescriptors() const
     ParameterList list;
     
     ParameterDescriptor desc;
-    desc.name = "onsettype";
-    desc.description = "Onset Detection Function Type";
+    desc.identifier = "onsettype";
+    desc.name = "Onset Detection Function Type";
     desc.minValue = 0;
-    desc.maxValue = 6;
-    desc.defaultValue = (int)aubio_onset_mkl;
+    desc.maxValue = 7;
+    desc.defaultValue = (int)OnsetComplex;
     desc.isQuantized = true;
     desc.quantizeStep = 1;
     desc.valueNames.push_back("Energy Based");
@@ -143,11 +149,12 @@ Onset::getParameterDescriptors() const
     desc.valueNames.push_back("Phase Deviation");
     desc.valueNames.push_back("Kullback-Liebler");
     desc.valueNames.push_back("Modified Kullback-Liebler");
+    desc.valueNames.push_back("Spectral Flux");
     list.push_back(desc);
 
     desc = ParameterDescriptor();
-    desc.name = "peakpickthreshold";
-    desc.description = "Peak Picker Threshold";
+    desc.identifier = "peakpickthreshold";
+    desc.name = "Peak Picker Threshold";
     desc.minValue = 0;
     desc.maxValue = 1;
     desc.defaultValue = 0.3;
@@ -155,15 +162,26 @@ Onset::getParameterDescriptors() const
     list.push_back(desc);
 
     desc = ParameterDescriptor();
-    desc.name = "silencethreshold";
-    desc.description = "Silence Threshold";
+    desc.identifier = "silencethreshold";
+    desc.name = "Silence Threshold";
     desc.minValue = -120;
     desc.maxValue = 0;
-    desc.defaultValue = -90;
+    desc.defaultValue = -70;
     desc.unit = "dB";
     desc.isQuantized = false;
     list.push_back(desc);
 
+    desc = ParameterDescriptor();
+    desc.identifier = "minioi";
+    desc.name = "Minimum Inter-Onset Interval";
+    desc.minValue = 0;
+    desc.maxValue = 40;
+    desc.defaultValue = 4;
+    desc.unit = "ms";
+    desc.isQuantized = true;
+    desc.quantizeStep = 1;
+    list.push_back(desc);
+
     return list;
 }
 
@@ -176,6 +194,8 @@ Onset::getParameter(std::string param) const
         return m_threshold;
     } else if (param == "silencethreshold") {
         return m_silence;
+    } else if (param == "minioi") {
+        return m_minioi;
     } else {
         return 0.0;
     }
@@ -186,18 +206,21 @@ Onset::setParameter(std::string param, float value)
 {
     if (param == "onsettype") {
         switch (lrintf(value)) {
-        case 0: m_onsettype = aubio_onset_energy; break;
-        case 1: m_onsettype = aubio_onset_specdiff; break;
-        case 2: m_onsettype = aubio_onset_hfc; break;
-        case 3: m_onsettype = aubio_onset_complex; break;
-        case 4: m_onsettype = aubio_onset_phase; break;
-        case 5: m_onsettype = aubio_onset_kl; break;
-        case 6: m_onsettype = aubio_onset_mkl; break;
+        case 0: m_onsettype = OnsetEnergy; break;
+        case 1: m_onsettype = OnsetSpecDiff; break;
+        case 2: m_onsettype = OnsetHFC; break;
+        case 3: m_onsettype = OnsetComplex; break;
+        case 4: m_onsettype = OnsetPhase; break;
+        case 5: m_onsettype = OnsetKL; break;
+        case 6: m_onsettype = OnsetMKL; break;
+        case 7: m_onsettype = OnsetSpecFlux; break;
         }
     } else if (param == "peakpickthreshold") {
         m_threshold = value;
     } else if (param == "silencethreshold") {
         m_silence = value;
+    } else if (param == "minioi") {
+        m_minioi = value;
     }
 }
 
@@ -207,41 +230,41 @@ Onset::getOutputDescriptors() const
     OutputList list;
 
     OutputDescriptor d;
-    d.name = "onsets";
+    d.identifier = "onsets";
+    d.name = "Onsets";
     d.unit = "";
-    d.description = "Onsets";
     d.hasFixedBinCount = true;
     d.binCount = 0;
-    d.sampleType = OutputDescriptor::OneSamplePerStep;
+    d.sampleType = OutputDescriptor::VariableSampleRate;
+    d.sampleRate = 0;
     list.push_back(d);
 
     return list;
 }
 
 Onset::FeatureSet
-Onset::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
+Onset::process(const float *const *inputBuffers,
+               Vamp::RealTime timestamp)
 {
     for (size_t i = 0; i < m_stepSize; ++i) {
-        for (size_t j = 0; j < m_channelCount; ++j) {
-            fvec_write_sample(m_ibuf, inputBuffers[j][i], j, i);
-        }
+        fvec_write_sample(m_ibuf, inputBuffers[0][i], i);
     }
 
-    aubio_pvoc_do(m_pv, m_ibuf, m_fftgrain);
-    aubio_onsetdetection(m_onsetdet, m_fftgrain, m_onset);
+    aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
 
-    bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
-
-    if (isonset) {
-        if (aubio_silence_detection(m_ibuf, m_silence)) {
-            isonset = false;
-        }
-    }
+    bool isonset = m_onset->data[0];
 
     FeatureSet returnFeatures;
 
     if (isonset) {
-        returnFeatures[0].push_back(Feature());
+        if (timestamp - m_lastOnset >= m_delay) {
+            Feature onsettime;
+            onsettime.hasTimestamp = true;
+            if (timestamp < m_delay) timestamp = m_delay;
+            onsettime.timestamp = timestamp - m_delay;
+            returnFeatures[0].push_back(onsettime);
+            m_lastOnset = timestamp;
+        }
     }
 
     return returnFeatures;