shithub: leaf

Download patch

ref: 6e8f4634b7cfc75451ab87275c4ce2b90059b193
parent: c27e1979bdab14b50ad69a799bbe5028fb3557c3
author: mulshine <mulshine@princeton.edu>
date: Mon Jan 7 07:18:11 EST 2019

Another gitignore update.

binary files a/.DS_Store b/.DS_Store differ
--- /dev/null
+++ b/.gitignore
@@ -1,0 +1,11 @@
+*.DS_Store
+*.app
+*.a
+*.appex
+*.vst
+*.vst3
+*.component
+*.xcuserstate
+*.zip
+*.swp
+*/Builds
--- a/LEAF/Inc/leaf-math.h
+++ b/LEAF/Inc/leaf-math.h
@@ -76,13 +76,19 @@
 
 float       LEAF_clip               (float min, float val, float max);
 float   	LEAF_softClip						(float val, float thresh);
-oBool       LEAF_isPrime            (uint64_t number );
-float       LEAF_midiToFrequency    (float f);
+oBool       LEAF_isPrime            (uint64_t number );
 
+float       LEAF_midiToFrequency    (float f);
+float       LEAF_frequencyToMidi(float f);
+
+void        LEAF_generate_sine     (float* buffer, int size);
+void        LEAF_generate_sawtooth (float* buffer, float basefreq, int size);
+void        LEAF_generate_triangle (float* buffer, float basefreq, int size);
+void        LEAF_generate_square   (float* buffer, float basefreq, int size);
+
 // dope af
-float LEAF_chebyshevT(float in, int n);
-float LEAF_CompoundChebyshevT(float in, int n, float* amps);
-float LEAF_frequencyToMidi(float f);
+float       LEAF_chebyshevT(float in, int n);
+float       LEAF_CompoundChebyshevT(float in, int n, float* amps);
 
 static inline float interpolate3max(float *buf, const int peakindex)
 {
--- a/LEAF/Inc_cpp/leaf-math.hpp
+++ b/LEAF/Inc_cpp/leaf-math.hpp
@@ -76,13 +76,19 @@
 
 float       LEAF_clip               (float min, float val, float max);
 float   	LEAF_softClip						(float val, float thresh);
-oBool       LEAF_isPrime            (uint64_t number );
-float       LEAF_midiToFrequency    (float f);
+oBool       LEAF_isPrime            (uint64_t number );
 
+float       LEAF_midiToFrequency    (float f);
+float       LEAF_frequencyToMidi(float f);
+
+void        LEAF_generate_sine     (float* buffer, int size);
+void        LEAF_generate_sawtooth (float* buffer, float basefreq, int size);
+void        LEAF_generate_triangle (float* buffer, float basefreq, int size);
+void        LEAF_generate_square   (float* buffer, float basefreq, int size);
+
 // dope af
-float LEAF_chebyshevT(float in, int n);
-float LEAF_CompoundChebyshevT(float in, int n, float* amps);
-float LEAF_frequencyToMidi(float f);
+float       LEAF_chebyshevT(float in, int n);
+float       LEAF_CompoundChebyshevT(float in, int n, float* amps);
 
 static inline float interpolate3max(float *buf, const int peakindex)
 {
--- a/LEAF/Src/leaf-math.c
+++ b/LEAF/Src/leaf-math.c
@@ -136,6 +136,91 @@
     else
         return x * ( 27 + x * x ) / ( 27 + 9 * x * x );
 }
+
+
+void LEAF_generate_sine(float* buffer, int size)
+{
+    float phase;
+    for (int i = 0; i < size; i++)
+    {
+        phase = (float) i / (float) size;
+        buffer[i] = sinf(phase * TWO_PI);
+    }
+}
+
+void LEAF_generate_sawtooth(float* buffer, float basefreq, int size)
+{
+    int harmonic = 1;
+    float phase = 0.0f;
+    float freq = harmonic * basefreq;
+    float amp;
+    
+    while (freq < (leaf.sampleRate * 0.5))
+    {
+        amp = 1.0f / harmonic;
+        for (int i = 0; i < size; i++)
+        {
+            phase = (float) i / (float) size;
+            buffer[i] += (amp * sinf(harmonic * phase * TWO_PI));
+        }
+        
+        harmonic++;
+        freq = harmonic * basefreq;
+    }
+}
+
+
+void LEAF_generate_triangle(float* buffer, float basefreq, int size)
+{
+    int harmonic = 1;
+    float phase = 0.0f;
+    float freq = harmonic * basefreq;
+    float amp = 1.0f;
+    
+    int count = 0;
+    float mult = 1.0f;
+    
+    while (freq < (leaf.sampleRate * 0.5))
+    {
+        amp = 1.0f / (float)(harmonic * harmonic);
+        
+        if (count % 2)  mult = -1.0f;
+        else            mult =  1.0f;
+        
+        for (int i = 0; i < size; i++)
+        {
+            phase = (float) i / (float) size;
+            buffer[i] += (mult * amp * sinf(harmonic * phase * TWO_PI));
+        }
+        
+        count++;
+        harmonic += 2;
+        freq = harmonic * basefreq;
+    }
+}
+
+void LEAF_generate_square(float* buffer, float basefreq, int size)
+{
+    int harmonic = 1;
+    float phase = 0.0f;
+    float freq = harmonic * basefreq;
+    float amp = 1.0f;
+    
+    while (freq < (leaf.sampleRate * 0.5))
+    {
+        amp = 1.0f / (float)(harmonic);
+        
+        for (int i = 0; i < size; i++)
+        {
+            phase = (float) i / (float) size;
+            buffer[i] += (amp * sinf(harmonic * phase * TWO_PI));
+        }
+        
+        harmonic += 2;
+        freq = harmonic * basefreq;
+    }
+}
+
 
 //-----------------------------------------------------------------------------
 // name: mtof()
--- a/LEAF/Src_cpp/leaf-math.cpp
+++ b/LEAF/Src_cpp/leaf-math.cpp
@@ -136,6 +136,91 @@
     else
         return x * ( 27 + x * x ) / ( 27 + 9 * x * x );
 }
+
+
+void LEAF_generate_sine(float* buffer, int size)
+{
+    float phase;
+    for (int i = 0; i < size; i++)
+    {
+        phase = (float) i / (float) size;
+        buffer[i] = sinf(phase * TWO_PI);
+    }
+}
+
+void LEAF_generate_sawtooth(float* buffer, float basefreq, int size)
+{
+    int harmonic = 1;
+    float phase = 0.0f;
+    float freq = harmonic * basefreq;
+    float amp;
+    
+    while (freq < (leaf.sampleRate * 0.5))
+    {
+        amp = 1.0f / harmonic;
+        for (int i = 0; i < size; i++)
+        {
+            phase = (float) i / (float) size;
+            buffer[i] += (amp * sinf(harmonic * phase * TWO_PI));
+        }
+        
+        harmonic++;
+        freq = harmonic * basefreq;
+    }
+}
+
+
+void LEAF_generate_triangle(float* buffer, float basefreq, int size)
+{
+    int harmonic = 1;
+    float phase = 0.0f;
+    float freq = harmonic * basefreq;
+    float amp = 1.0f;
+    
+    int count = 0;
+    float mult = 1.0f;
+    
+    while (freq < (leaf.sampleRate * 0.5))
+    {
+        amp = 1.0f / (float)(harmonic * harmonic);
+        
+        if (count % 2)  mult = -1.0f;
+        else            mult =  1.0f;
+        
+        for (int i = 0; i < size; i++)
+        {
+            phase = (float) i / (float) size;
+            buffer[i] += (mult * amp * sinf(harmonic * phase * TWO_PI));
+        }
+        
+        count++;
+        harmonic += 2;
+        freq = harmonic * basefreq;
+    }
+}
+
+void LEAF_generate_square(float* buffer, float basefreq, int size)
+{
+    int harmonic = 1;
+    float phase = 0.0f;
+    float freq = harmonic * basefreq;
+    float amp = 1.0f;
+    
+    while (freq < (leaf.sampleRate * 0.5))
+    {
+        amp = 1.0f / (float)(harmonic);
+        
+        for (int i = 0; i < size; i++)
+        {
+            phase = (float) i / (float) size;
+            buffer[i] += (amp * sinf(harmonic * phase * TWO_PI));
+        }
+        
+        harmonic += 2;
+        freq = harmonic * basefreq;
+    }
+}
+
 
 //-----------------------------------------------------------------------------
 // name: mtof()