shithub: leaf

Download patch

ref: cb6284e95107db310a8b4193f15fa6e45941a5d7
parent: 1bd52d6f273a6897ad8d9bf56d4dd22034e5ca68
author: mulshine <mulshine@princeton.edu>
date: Tue May 14 10:03:40 EDT 2019

Sampler fixes.

--- a/LEAF/Src/leaf-sample.c
+++ b/LEAF/Src/leaf-sample.c
@@ -162,7 +162,7 @@
     }
     else
     {
-    	idx = (int) (p->idx + 1.f); // we think this is because flooring on int works different when reading backwards
+    	idx = (int) (p->idx+1.f); // we think this is because flooring on int works different when reading backwards
     	alpha = (p->idx+1.f) - idx;
     }
     
@@ -173,51 +173,52 @@
         end = p->start;
     }
     
-    // Check dir (direction) to interpolate properly
-    if (dir > 0)
+    // Check dir (direction) to interpolate properly
+    if (dir > 0)
+    {
+        // FORWARD NORMAL SAMPLE
+        int i1 = idx-1;
+        int i3 = idx+1;
+        int i4 = idx+2;
+
+        sample =     LEAF_interpolate_hermite (buff[i1],
+                                               buff[idx],
+                                               buff[i3],
+                                               buff[i4],
+                                               alpha);
+        
+        // num samples to end of loop
+        numsamps = (idx - start) / p->inc;
+        //numsamps = (dir > 0) ? (end - idx) : (idx - start);
+        //numsamps *= p->iinc;
+        
+        if (p->mode == PlayLoop)
+        {
+            if (numsamps <= p->cfxlen)
+            {
+                // CROSSFADE SAMPLE
+                float idxx =  p->idx - p->len;
+                int cdx = (int)(idxx);
+                
+                i1 = cdx-1;
+                i3 = cdx+1;
+                i4 = cdx+2;
+                
+                cfxsample =     LEAF_interpolate_hermite (buff[i1],
+                                                          buff[cdx],
+                                                          buff[i3],
+                                                          buff[i4],
+                                                          alpha);
+                
+                g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen;
+            }
+        }
+    }
+    else
     {
-        // FORWARD NORMAL SAMPLE
-        int i1 = idx-1;
-        int i3 = idx+1;
-        int i4 = idx+2;
-
-        sample =     LEAF_interpolate_hermite (buff[i1],
-                                               buff[idx],
-                                               buff[i3],
-                                               buff[i4],
-                                               alpha);
-        
-        // num samples to end of loop
-        numsamps = (dir > 0) ? (end - idx) : (idx - start);
-        numsamps *= p->iinc;
-        
-        if (p->mode == PlayLoop)
-        {
-            if (numsamps <= p->cfxlen)
-            {
-                // CROSSFADE SAMPLE
-                float idxx =  p->idx - p->len;
-                int cdx = (int)(idxx);
-                
-                i1 = cdx-1;
-                i3 = cdx+1;
-                i4 = cdx+2;
-                
-                cfxsample =     LEAF_interpolate_hermite (buff[i1],
-                                                          buff[cdx],
-                                                          buff[i3],
-                                                          buff[i4],
-                                                          alpha);
-                
-                g2 = (float) (p->cfxlen - numsamps) / (float) p->cfxlen;
-            }
-        }
-    }
-    else
-    {
-        // REVERSE
-        int i1 = idx+1;
-        int i3 = idx-1;
+        // REVERSE
+        int i1 = idx+1;
+        int i3 = idx-1;
         int i4 = idx-2;
     
         sample =     LEAF_interpolate_hermite (buff[i1],
--- a/LEAF_JUCEPlugin/Source/LEAFLink.cpp
+++ b/LEAF_JUCEPlugin/Source/LEAFLink.cpp
@@ -15,14 +15,13 @@
 
 std::vector<juce::String> cButtonNames =  std::vector<juce::String>
 {
-
+    "record"
 };
 
 std::vector<juce::String> cSliderNames =  std::vector<juce::String>
 {
-    "delay",
-    "feedback",
-    "mix"
+    "mod freq",
+    "mod depth"
 };
 
 std::vector<juce::String> cComboBoxNames =  std::vector<juce::String>
--- a/LEAF_JUCEPlugin/Source/MyTest.cpp
+++ b/LEAF_JUCEPlugin/Source/MyTest.cpp
@@ -21,99 +21,83 @@
 
 #define NUM_GRAINS 20
 
-tBuffer buff;
-tSampler samp[NUM_GRAINS];
 
+
 tTapeDelay delay;
 
 float feedback = 0.f;
 
+tBuffer buff;
+tSampler samp;
+
+tCycle osc;
 
 void    LEAFTest_init            (float sampleRate, int blockSize)
 {
     LEAF_init(sampleRate, blockSize, &getRandomFloat);
     
-    // Init and set record loop
-    tBuffer_init (&buff, leaf.sampleRate * 1.f); // 0.5-second buffers
-    tBuffer_setRecordMode (&buff, RecordLoop);
-    tBuffer_record(&buff);
-    
-    for (int i = 0; i < NUM_GRAINS; i++)
-    {
-        // Init and set play loop
-        tSampler_init (&samp[i], &buff);
-        tSampler_setMode (&samp[i], PlayLoop);
+    // Init and set record
+    tBuffer_init (&buff, leaf.sampleRate * 1.f); // init, 1 second buffer
+    tBuffer_setRecordMode (&buff, RecordOneShot); // RecordOneShot records once through
+    tBuffer_record(&buff); // starts recording
 
-        /*
-        float speed = ((getRandomFloat() < 0.5f) ? 0.5f : 1.f);
-        float dir = (getRandomFloat() < 0.5f) ? -1 : 1;
-        
-        tSampler_setRate(&samp[i], speed * dir);
-         */
-        
-        tSampler_setRate(&samp[i], 1.f);
-        
-        // Record and play
-        tSampler_play(&samp[i]);
-    }
+    // Init and set play
+    tSampler_init (&samp, &buff); // init, give address of record buffer
+    tSampler_setMode (&samp, PlayLoop); //set in Loop Mode
+    tSampler_setRate(&samp, 1.f); // Rate of 1.0
+    tSampler_play(&samp); // start spitting samples out
+    
+    tCycle_init(&osc);
     
-    tTapeDelay_init(&delay, leaf.sampleRate * 0.05f, leaf.sampleRate * 1.f); // 1 second delay, starts out at 50 ms
-    
     leaf_pool_report();
-}
+}
+
+float depth = 1.0f;
 
-int timer = 0;
-
-float lastOut;
-
 float   LEAFTest_tick            (float input)
 {
     float sample = 0.f;
     
-    tBuffer_tick(&buff, input);
-    
-    for (int i = 0; i < NUM_GRAINS; i++)
+    tBuffer_tick(&buff, input); // ticking the buffer records in to buffer
+    
+    tSampler_setRate(&samp, tCycle_tick(&osc) * depth);
+
+    // dont tick sampler if buffer not active (not recording)
+    if (buff.active == 0)
     {
-        sample += tSampler_tick(&samp[i]);
+        sample = tSampler_tick(&samp); // ticking sampler loops sample
     }
     
-    sample /= NUM_GRAINS;
     
-    sample = tTapeDelay_tick(&delay, (1.f - feedback) * sample + feedback * lastOut);
-    
-    lastOut = sample;
-
-    return  (sample * mix) +
-            (input * (1.f - mix));
+    return sample;
 }
 
 bool lastState = false, lastPlayState = false;
 void    LEAFTest_block           (void)
 {
-    float val = getSliderValue("mix");
-    
-    mix = val;
-    
-    val = getSliderValue("delay");
-    
-    tTapeDelay_setDelay(&delay, val * leaf.sampleRate);
-    
-    val = getSliderValue("feedback");
-    
-    feedback = val;
-    
-    for (int i = 0; i < NUM_GRAINS; i++)
-    {
-        if (getRandomFloat() < 0.01f)
-        {
-            tSampler_setStart(&samp[i], leaf.sampleRate * 0.05f + leaf.sampleRate * getRandomFloat() * 0.95f);
-            
-            uint64_t end = (leaf.sampleRate * 0.05f + getRandomFloat() * leaf.sampleRate * 0.45f + samp[i].start);
-            
-            tSampler_setEnd(&samp[i],  end % buff.length); // 10 - 1010 ms
-        }
-    }
-    
+    bool state = getButtonState("record");
+    
+    if (state)
+    {
+        setButtonState("record", false);
+        tBuffer_record(&buff);
+    }
+    
+    float val = getSliderValue("mod freq");
+    
+    float freq = 0.1 + 999.9 * val;
+    
+    tCycle_setFreq(&osc, freq);
+    
+    DBG("mod freq: " + String(freq));
+    
+    val = getSliderValue("mod depth");
+    
+    depth = 0.1 + val * 15.9f;
+    
+    DBG("mod depth: " + String(depth));
+    
+
 }
 
 void    LEAFTest_controllerInput (int cnum, float cval)