shithub: leaf

Download patch

ref: d634fb3b428b1cc7bc1f06b39a2ef43f8cc17f2c
parent: 45af5ea9126eb580fcf4916bed29d02995c745b9
author: Matthew Wang <mjw7@princeton.edu>
date: Wed Aug 5 09:09:16 EDT 2020

more pitch detection testing; using both algs and only setting pitch when they match within some error works decently but at the cost of running both; at least suggests that they tend to make different errors

--- a/TestPlugin/Source/MyTest.cpp
+++ b/TestPlugin/Source/MyTest.cpp
@@ -40,6 +40,8 @@
 tZeroCrossingCounter zc;
 tEnvelopeFollower ef;
 
+tCycle sine;
+
 float gain;
 float dtime;
 bool buttonState;
@@ -71,6 +73,9 @@
     
     tZeroCrossingCounter_init(&zc, 128);
     tEnvelopeFollower_init(&ef, 0.02f, 0.9999f);
+    
+    tCycle_init(&sine);
+    tCycle_setFreq(&sine, 100);
 }
 
 inline double getSawFall(double angle) {
@@ -99,17 +104,12 @@
     
     float freq = 1.0f/tPeriodDetection_tick(&pd, input) * leaf.sampleRate;
     tPitchDetector_tick(&detector, input);
+    float altFreq = tPitchDetector_getFrequency(&detector);
     
-    if (tPitchDetector_getPeriodicity(&detector) > 0.0)
-    {
-        float altFreq = tPitchDetector_getFrequency(&detector);
-//        if (fabsf(lastFreq - freq) > fabsf(lastFreq - altFreq))
-            freq = altFreq;
-    }
-    
+    if (fabsf(1.0f - (freq / altFreq)) < 0.05f)
 //    if (tZeroCrossingCounter_tick(&zc, input) < 0.05 && freq > 0.0f)
     {
-        tMBTriangle_setFreq(&btri, freq);
+        tCycle_setFreq(&sine, altFreq);
     }
 
     float g = tEnvelopeFollower_tick(&ef, input);
@@ -116,7 +116,7 @@
     
     lastFreq = freq;
     
-    return input + tMBTriangle_tick(&btri) * g * 10.0f;
+    return tCycle_tick(&sine) * g * 2.0f + input;
 }
 
 int firstFrame = 1;
@@ -123,8 +123,12 @@
 bool lastState = false, lastPlayState = false;
 void    LEAFTest_block           (void)
 {
-    DBG(tPitchDetector_getFrequency(&detector));
-    DBG(tPitchDetector_getPeriodicity(&detector));
+    float periodicity = tPitchDetector_getPeriodicity(&detector);
+    if (periodicity > 0.99f)
+    {
+        DBG(tPitchDetector_getFrequency(&detector));
+        DBG(tPitchDetector_getPeriodicity(&detector));
+    }
     //    if (firstFrame == 1)
     //    {
     //        tBuffer_record(&buff); // starts recording
--- a/leaf/Inc/leaf-analysis.h
+++ b/leaf/Inc/leaf-analysis.h
@@ -713,8 +713,8 @@
      @} */
     
 #define PULSE_THRESHOLD 0.6f
-#define HARMONIC_PERIODICITY_FACTOR 16
-#define PERIODICITY_DIFF_FACTOR 0.008f
+#define HARMONIC_PERIODICITY_FACTOR 16 //16
+#define PERIODICITY_DIFF_FACTOR 0.008f //0.008f
     
     typedef struct _auto_correlation_info
     {
--- a/leaf/Src/leaf-oscillators.c
+++ b/leaf/Src/leaf-oscillators.c
@@ -45,6 +45,8 @@
 {
     _tTable* c = *cy;
     
+    if (!isfinite(freq)) return;
+    
     c->freq = freq;
     c->inc = freq * leaf.invSampleRate;
 }
@@ -109,6 +111,8 @@
 void     tCycle_setFreq(tCycle* const cy, float freq)
 {
     _tCycle* c = *cy;
+    
+    if (!isfinite(freq)) return;
     
     c->freq  = freq;