change lower pitch limit to D#1
[vamp-aubio-plugins.git] / plugins / Onset.cpp
index 68322d8..3771af1 100644 (file)
@@ -14,6 +14,7 @@
 
 */
 
+#include <math.h>
 #include "Onset.h"
 
 using std::string;
@@ -29,7 +30,7 @@ Onset::Onset(float inputSampleRate) :
     m_pv(0),
     m_peakpick(0),
     m_onsetdet(0),
-    m_onsettype(aubio_onset_mkl),
+    m_onsettype(aubio_onset_complex),
     m_threshold(0.3),
     m_silence(-90),
     m_channelCount(1)
@@ -83,21 +84,18 @@ Onset::initialise(size_t channels, size_t stepSize, size_t blockSize)
     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_fftgrain = new_cvec(blockSize, channels);
+    m_pv = new_aubio_pvoc(blockSize, stepSize, channels);
     m_peakpick = new_aubio_peakpicker(m_threshold);
 
-    m_onsetdet = new_aubio_onsetdetection(m_onsettype, processingBlockSize, channels);
+    m_onsetdet = new_aubio_onsetdetection(m_onsettype, blockSize, channels);
+    
+    m_delay = Vamp::RealTime::frame2RealTime(4 * stepSize,
+                                             lrintf(m_inputSampleRate));
+
+    m_lastOnset = Vamp::RealTime::zeroTime - m_delay - m_delay;
 
     return true;
 }
@@ -110,18 +108,13 @@ Onset::reset()
 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
@@ -134,7 +127,7 @@ Onset::getParameterDescriptors() const
     desc.description = "Onset Detection Function Type";
     desc.minValue = 0;
     desc.maxValue = 6;
-    desc.defaultValue = (int)aubio_onset_mkl;
+    desc.defaultValue = (int)aubio_onset_complex;
     desc.isQuantized = true;
     desc.quantizeStep = 1;
     desc.valueNames.push_back("Energy Based");
@@ -213,7 +206,8 @@ Onset::getOutputDescriptors() const
     d.description = "Onsets";
     d.hasFixedBinCount = true;
     d.binCount = 0;
-    d.sampleType = OutputDescriptor::OneSamplePerStep;
+    d.sampleType = OutputDescriptor::VariableSampleRate;
+    d.sampleRate = 0;
     list.push_back(d);
 
     d = OutputDescriptor();
@@ -231,7 +225,7 @@ Onset::getOutputDescriptors() const
 }
 
 Onset::FeatureSet
-Onset::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
+Onset::process(float **inputBuffers, Vamp::RealTime timestamp)
 {
     for (size_t i = 0; i < m_stepSize; ++i) {
         for (size_t j = 0; j < m_channelCount; ++j) {
@@ -253,7 +247,14 @@ Onset::process(float **inputBuffers, Vamp::RealTime /* timestamp */)
     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;
+        }
     }
     Feature feature;
     for (size_t j = 0; j < m_channelCount; ++j) {