plugins/Onset.cpp: improve parameters handling, use same defaults as aubioonset
[vamp-aubio-plugins.git] / plugins / Onset.cpp
index 68322d8..e531270 100644 (file)
@@ -6,14 +6,24 @@
     Centre for Digital Music, Queen Mary, University of London.
     This file copyright 2006 Chris Cannam.
     
-    This program is free software; you can redistribute it and/or
-    modify it under the terms of the GNU General Public License as
-    published by the Free Software Foundation; either version 2 of the
-    License, or (at your option) any later version.  See the file
-    COPYING included with this distribution for more information.
+    This file is part of vamp-aubio-plugins.
+
+    vamp-aubio is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    vamp-aubio is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with aubio.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 
+#include <math.h>
 #include "Onset.h"
 
 using std::string;
@@ -24,41 +34,42 @@ 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(OnsetDefault),
     m_threshold(0.3),
     m_silence(-90),
-    m_channelCount(1)
+    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)";
@@ -67,7 +78,7 @@ Onset::getMaker() const
 int
 Onset::getPluginVersion() const
 {
-    return 1;
+    return 2;
 }
 
 string
@@ -79,25 +90,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;
 }
@@ -105,23 +109,30 @@ 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);
+
 }
 
 size_t
 Onset::getPreferredStepSize() const
 {
-    if (m_onsettype == aubio_onset_energy ||
-        m_onsettype == aubio_onset_hfc) {
-        return 512;
-    } else {
-        return 128;
-    }
+    return 256;
 }
 
 size_t
 Onset::getPreferredBlockSize() const
 {
-    return getPreferredStepSize();
+    return 2 * getPreferredStepSize();
 }
 
 Onset::ParameterList
@@ -130,11 +141,12 @@ 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.description = "Type of onset detection function to use";
     desc.minValue = 0;
-    desc.maxValue = 6;
-    desc.defaultValue = (int)aubio_onset_mkl;
+    desc.maxValue = 7;
+    desc.defaultValue = (int)OnsetDefault;
     desc.isQuantized = true;
     desc.quantizeStep = 1;
     desc.valueNames.push_back("Energy Based");
@@ -144,11 +156,14 @@ 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");
+    desc.valueNames.push_back("Default");
     list.push_back(desc);
 
     desc = ParameterDescriptor();
-    desc.name = "peakpickthreshold";
-    desc.description = "Peak Picker Threshold";
+    desc.identifier = "peakpickthreshold";
+    desc.name = "Peak Picker Threshold";
+    desc.description = "Threshold used for peak picking, the higher the more detections";
     desc.minValue = 0;
     desc.maxValue = 1;
     desc.defaultValue = 0.3;
@@ -156,8 +171,9 @@ Onset::getParameterDescriptors() const
     list.push_back(desc);
 
     desc = ParameterDescriptor();
-    desc.name = "silencethreshold";
-    desc.description = "Silence Threshold";
+    desc.identifier = "silencethreshold";
+    desc.name = "Silence Threshold";
+    desc.description = "Silence threshold, the higher the least detection";
     desc.minValue = -120;
     desc.maxValue = 0;
     desc.defaultValue = -90;
@@ -165,6 +181,18 @@ Onset::getParameterDescriptors() const
     desc.isQuantized = false;
     list.push_back(desc);
 
+    desc = ParameterDescriptor();
+    desc.identifier = "minioi";
+    desc.name = "Minimum Inter-Onset Interval";
+    desc.description = "Time interval below which two consecutive onsets should be merged";
+    desc.minValue = 0;
+    desc.maxValue = 40;
+    desc.defaultValue = 4;
+    desc.unit = "ms";
+    desc.isQuantized = true;
+    desc.quantizeStep = 1;
+    list.push_back(desc);
+
     return list;
 }
 
@@ -174,9 +202,23 @@ Onset::getParameter(std::string param) const
     if (param == "onsettype") {
         return m_onsettype;
     } else if (param == "peakpickthreshold") {
-        return m_threshold;
+        if (m_onsetdet) {
+            return aubio_onset_get_threshold(m_onsetdet);
+        } else {
+            return m_threshold;
+        }
     } else if (param == "silencethreshold") {
-        return m_silence;
+        if (m_onsetdet) {
+            return aubio_onset_get_silence(m_onsetdet);
+        } else {
+            return m_silence;
+        }
+    } else if (param == "minioi") {
+        if (m_onsetdet) {
+            return aubio_onset_get_minioi(m_onsetdet);
+        } else {
+            return m_minioi;
+        }
     } else {
         return 0.0;
     }
@@ -187,18 +229,29 @@ 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;
+        case 8: m_onsettype = OnsetDefault; break;
         }
+        if (!m_onsetdet) initialise(1, 256, 512);
     } else if (param == "peakpickthreshold") {
         m_threshold = value;
+        if (m_onsetdet)
+            aubio_onset_set_threshold(m_onsetdet, m_threshold);
     } else if (param == "silencethreshold") {
         m_silence = value;
+        if (m_onsetdet)
+            aubio_onset_set_silence(m_onsetdet, m_silence);
+    } else if (param == "minioi") {
+        m_minioi = value;
+        if (m_onsetdet)
+            aubio_onset_set_minioi(m_onsetdet, m_minioi);
     }
 }
 
@@ -208,22 +261,31 @@ Onset::getOutputDescriptors() const
     OutputList list;
 
     OutputDescriptor d;
-    d.name = "onsets";
+    d.identifier = "onsets";
+    d.name = "Onsets";
+    d.description = "List of times at which a note onset was detected";
     d.unit = "";
-    d.description = "Onsets";
     d.hasFixedBinCount = true;
     d.binCount = 0;
+    d.sampleType = OutputDescriptor::VariableSampleRate;
+    d.sampleRate = 0;
+    list.push_back(d);
+
+    d.identifier = "odf";
+    d.name = "Onset detection function";
+    d.description = "Output of the onset detection function";
+    d.binCount = 1;
+    d.isQuantized = true;
+    d.quantizeStep = 1.0;
     d.sampleType = OutputDescriptor::OneSamplePerStep;
     list.push_back(d);
 
-    d = OutputDescriptor();
-    d.name = "detectionfunction";
-    d.unit = "";
-    d.description = "Onset Detection Function";
-    d.hasFixedBinCount = true;
-    d.binCount = m_channelCount;
-    d.hasKnownExtents = false;
-    d.isQuantized = false;
+    d.identifier = "todf";
+    d.name = "Thresholded Onset detection function";
+    d.description = "Output of the thresholded onset detection function";
+    d.binCount = 1;
+    d.isQuantized = true;
+    d.quantizeStep = 1.0;
     d.sampleType = OutputDescriptor::OneSamplePerStep;
     list.push_back(d);
 
@@ -231,35 +293,35 @@ Onset::getOutputDescriptors() const
 }
 
 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_set_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;
-        }
-    }
+    smpl_t isonset = m_onset->data[0];
 
     FeatureSet returnFeatures;
 
     if (isonset) {
-        returnFeatures[0].push_back(Feature());
-    }
-    Feature feature;
-    for (size_t j = 0; j < m_channelCount; ++j) {
-        feature.values.push_back(m_onset->data[j][0]);
+        Feature onsettime;
+        onsettime.hasTimestamp = true;
+        onsettime.timestamp = Vamp::RealTime::fromSeconds(aubio_onset_get_last_s(m_onsetdet));
+        returnFeatures[0].push_back(onsettime);
     }
-    returnFeatures[1].push_back(feature);
+
+    Feature odf;
+    odf.hasTimestamp = false;
+    odf.values.push_back(aubio_onset_get_descriptor(m_onsetdet));
+    returnFeatures[1].push_back(odf);
+
+    Feature todf;
+    todf.hasTimestamp = false;
+    todf.values.push_back(aubio_onset_get_thresholded_descriptor(m_onsetdet));
+    returnFeatures[2].push_back(todf);
 
     return returnFeatures;
 }