libmain.cpp, plugins/: update to GPLv3
[vamp-aubio-plugins.git] / plugins / Onset.cpp
index 3e3f8d9..cb93366 100644 (file)
@@ -6,11 +6,20 @@
     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/>.
 
 */
 
@@ -25,25 +34,20 @@ 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_complex),
+    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
@@ -95,16 +99,8 @@ Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
 
     m_ibuf = new_fvec(stepSize);
     m_onset = new_fvec(1);
-    m_fftgrain = new_cvec(blockSize);
-    m_pv = new_aubio_pvoc(blockSize, stepSize);
-    m_peakpick = new_aubio_peakpicker(m_threshold);
 
-    m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize);
-    
-    m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
-                                             lrintf(m_inputSampleRate));
-
-    m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
+    reset();
 
     return true;
 }
@@ -112,6 +108,22 @@ 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
@@ -134,9 +146,10 @@ Onset::getParameterDescriptors() const
     ParameterDescriptor desc;
     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_complex;
+    desc.maxValue = 7;
+    desc.defaultValue = (int)OnsetComplex;
     desc.isQuantized = true;
     desc.quantizeStep = 1;
     desc.valueNames.push_back("Energy Based");
@@ -146,11 +159,13 @@ 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.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;
@@ -160,13 +175,26 @@ Onset::getParameterDescriptors() const
     desc = ParameterDescriptor();
     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;
+    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.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;
 }
 
@@ -179,6 +207,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;
     }
@@ -189,18 +219,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;
     }
 }
 
@@ -212,6 +245,7 @@ Onset::getOutputDescriptors() const
     OutputDescriptor d;
     d.identifier = "onsets";
     d.name = "Onsets";
+    d.description = "List of times at which a note onset was detected";
     d.unit = "";
     d.hasFixedBinCount = true;
     d.binCount = 0;
@@ -219,14 +253,21 @@ Onset::getOutputDescriptors() const
     d.sampleRate = 0;
     list.push_back(d);
 
-    d = OutputDescriptor();
-    d.identifier = "detectionfunction";
-    d.name = "Onset Detection Function";
-    d.unit = "";
-    d.hasFixedBinCount = true;
+    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.identifier = "todf";
+    d.name = "Thresholded Onset detection function";
+    d.description = "Output of the thresholded onset detection function";
     d.binCount = 1;
-    d.hasKnownExtents = false;
-    d.isQuantized = false;
+    d.isQuantized = true;
+    d.quantizeStep = 1.0;
     d.sampleType = OutputDescriptor::OneSamplePerStep;
     list.push_back(d);
 
@@ -238,21 +279,12 @@ 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);
-
-    bool isonset = aubio_peakpick_pimrt(m_onset, m_peakpick);
+    aubio_onset_do(m_onsetdet, m_ibuf, m_onset);
 
-    if (isonset) {
-        if (aubio_silence_detection(m_ibuf, m_silence)) {
-            isonset = false;
-        }
-    }
+    bool isonset = m_onset->data[0];
 
     FeatureSet returnFeatures;
 
@@ -266,11 +298,16 @@ Onset::process(const float *const *inputBuffers,
             m_lastOnset = timestamp;
         }
     }
-    Feature feature;
-    for (size_t j = 0; j < m_channelCount; ++j) {
-        feature.values.push_back(m_onset->data[j][0]);
-    }
-    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;
 }