shithub: leaf

Download patch

ref: 9902cb9f346c78aa0bb4ea27e9c39dcedd60ed80
parent: c84e0daafc6d83b0b44a2575bcafe27cf85f1109
author: mulshine <mulshine@princeton.edu>
date: Thu Jan 24 06:51:22 EST 2019

Looperworking well, have to handle flip and dir different.

--- a/LEAF/Inc/leaf-sample.h
+++ b/LEAF/Inc/leaf-sample.h
@@ -73,11 +73,13 @@
         
         float idx;
         float inc;
-        int dir;
+        int8_t dir;
+        uint8_t flip;
         
         int32_t start;
         int32_t end;
         uint32_t len;
+        uint32_t cnt;
     
         Mode mode;
         
--- a/LEAF/Src/leaf-sample.c
+++ b/LEAF/Src/leaf-sample.c
@@ -119,6 +119,8 @@
     
     p->idx = 0.f;
     p->inc = 1.f;
+    p->dir = 1;
+    p->flip = 0;
     
     p->mode = Normal;
 
@@ -133,52 +135,110 @@
 
 float tSamplePlayer_tick        (tSamplePlayer* const p)
 {
-    if (p->active == 0 || (p->len < 4)) return 0.f;
+    if ((p->mode == Normal) && (p->cnt > 0))    p->active = 0;
     
+    if (p->active == 0 || (p->len < 4))         return 0.f;
+    
     float sample = 0.f;
     
     float* buff = p->samp->buff;
     
     int idx =  (int) p->idx;
-    float alpha = p->idx - idx;
     
-    int i1 = idx-1;
-    int i3 = idx+1;
-    int i4 = idx+2;
+    // Check dir (direction bit) to interpolate properly
+    if (p->dir > 0)
+    {
+        // FORWARD
+        float alpha = p->idx - idx;
+        
+        int i1 = idx-1;
+        int i3 = idx+1;
+        int i4 = idx+2;
+        
+        if (i1 < p->start)  i1 += p->len;
+        if (i3 > p->end)    i3 -= p->len;
+        if (i4 > p->end)    i4 -= p->len;
+        
+        sample =     LEAF_interpolate_hermite (buff[i1],
+                                               buff[idx],
+                                               buff[i3],
+                                               buff[i4],
+                                               alpha);
+    }
+    else
+    {
+        // REVERSE
+        float alpha = 1.0f - (p->idx - idx);
+        
+        int i1 = idx+1;
+        int i3 = idx-1;
+        int i4 = idx-2;
+        
+        if (i1 > p->start)  i1 -= p->len;
+        if (i3 < p->end)    i3 += p->len;
+        if (i4 < p->end)    i4 += p->len;
+        
+        sample =     LEAF_interpolate_hermite (buff[i1],
+                                               buff[idx],
+                                               buff[i3],
+                                               buff[i4],
+                                               alpha);
+    }
     
-    if (i1 < p->start)  i1 += p->len;
-    if (i3 > p->end)    i3 -= p->len;
-    if (i4 > p->end)    i4 -= p->len;
     
-    sample =     LEAF_interpolate_hermite (buff[i1],
-                                           buff[idx],
-                                           buff[i3],
-                                           buff[i4],
-                                           alpha);
-    
     p->idx += (p->dir * p->inc);
     
-    if (p->mode == Normal)
+    if (p->mode != BackAndForth)
     {
-        // FIX THIS: NORMAL MODE NOT IMPLEMENTED YET
-        if ((p->dir == 1) && (idx > p->end))
+        // Check flip bit to change loop test
+        if (p->flip > 0)
         {
-            p->active = 0;
+            if (idx <= p->end)
+            {
+                p->idx += (float)(p->len);
+                p->cnt++;
+            }
+            else if (idx >= p->start)
+            {
+                p->idx -= (float)(p->len);
+                p->cnt++;
+            }
         }
+        else
+        {
+            if (idx <= p->start)
+            {
+                p->idx += (float)(p->len);
+                p->cnt++;
+            }
+            else if (idx >= p->end)
+            {
+                p->idx -= (float)(p->len);
+                p->cnt++;
+            }
+        }
     }
-    else if (p->mode == Loop)
+    else // BackAndForth
     {
-        if (idx < p->start) p->idx += (float)(p->len);
-        
-        if (idx > p->end)   p->idx -= (float)(p->len);
-    }
-    else if (p->mode == BackAndForth)
-    {
-        if ((idx < p->start) || (idx > p->end))
+        if (p->flip > 0)
         {
-            p->dir = -p->dir;
-            p->idx += (2*p->inc);
+            if ((idx < p->start) || (idx > p->end))
+            {
+                p->dir = -p->dir;
+                p->idx += (2*p->inc);
+                p->cnt++;
+            }
         }
+        else
+        {
+            if ((idx > p->start) || (idx < p->end))
+            {
+                p->dir = -p->dir;
+                p->idx += (2*p->inc);
+                p->cnt++;
+            }
+        }
+        
     }
     
     
@@ -198,6 +258,7 @@
 void tSamplePlayer_play         (tSamplePlayer* const p)
 {
     p->active = 1;
+    p->cnt = 0;
 }
 
 void tSamplePlayer_stop         (tSamplePlayer* const p)
@@ -205,6 +266,20 @@
     p->active = 0;
 }
 
+static void handleStartEndChange(tSamplePlayer* const p)
+{
+    if (p->start > p->end)
+    {
+        p->dir = -1;
+        p->flip = 1;
+    }
+    else
+    {
+        p->dir = 1;
+        p->flip = 0;
+    }
+}
+
 void tSamplePlayer_setStart     (tSamplePlayer* const p, int32_t start)
 {
     p->start = LEAF_clipInt(0, start, (p->samp->length - 1));
@@ -211,8 +286,7 @@
     
     p->len = abs(p->end - p->start);
     
-    if (p->start > p->end)  p->dir = -1;
-    else                    p->dir = 1;
+    handleStartEndChange(p);
 }
 
 void tSamplePlayer_setEnd       (tSamplePlayer* const p, int32_t end)
@@ -221,8 +295,7 @@
 
     p->len = abs(p->end - p->start);
     
-    if (p->start > p->end)  p->dir = -1;
-    else                    p->dir = 1;
+    handleStartEndChange(p);
 }
 
 void tSamplePlayer_setRate      (tSamplePlayer* const p, float rate)
--- a/LEAF_JUCEPlugin/Source/LEAFLink.cpp
+++ b/LEAF_JUCEPlugin/Source/LEAFLink.cpp
@@ -15,7 +15,8 @@
 
 std::vector<juce::String> cButtonNames =  std::vector<juce::String>
 {
-    "sample"
+    "sample",
+    "play"
 };
 
 std::vector<juce::String> cSliderNames =  std::vector<juce::String>