Showing the changes to LEAF/Src_cpp/leaf-math.cpp (1 of 11 files). Show all
Unified · Split
| @@ -18,13 +18,98 @@ · 17 unmodified lines | |||
| 18 | #include "../Inc/leaf-math.h" | 18 | #include "../Inc/leaf-math.h" |
| 19 | #include "../Inc/leaf-wavetables.h" | 19 | #include "../Inc/leaf-wavetables.h" |
| 20 | 20 | ||
| 21 | #endif | 21 | #endif |
| 22 | |||
| 23 | //#include "../JuceLibraryCode/JuceHeader.h" | ||
| 24 | |||
| 22 | 25 | ||
| 23 | // The C-embedded Audio Library. | 26 | // The C-embedded Audio Library. |
| 24 | #define TWO_TO_16 65536.f | 27 | #define TWO_TO_16 65536.f |
| 25 | 28 | ||
| 26 | #define EXPONENTIAL_TABLE_SIZE 65536 | 29 | #define EXPONENTIAL_TABLE_SIZE 65536 |
| 27 | 30 | ||
| 31 | void LEAF_generate_sine(float* buffer, int size) | ||
| 32 | { | ||
| 33 | float phase; | ||
| 34 | for (int i = 0; i < size; i++) | ||
| 35 | { | ||
| 36 | phase = (float) i / (float) size; | ||
| 37 | buffer[i] = sinf(phase * TWO_PI); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | void LEAF_generate_sawtooth(float* buffer, float basefreq, int size) | ||
| 42 | { | ||
| 43 | int harmonic = 1; | ||
| 44 | float phase = 0.0f; | ||
| 45 | float freq = harmonic * basefreq; | ||
| 46 | float amp; | ||
| 47 | |||
| 48 | while (freq < (leaf.sampleRate * 0.5)) | ||
| 49 | { | ||
| 50 | amp = 1.0f / harmonic; | ||
| 51 | for (int i = 0; i < size; i++) | ||
| 52 | { | ||
| 53 | phase = (float) i / (float) size; | ||
| 54 | buffer[i] += (amp * sinf(harmonic * phase * TWO_PI)); | ||
| 55 | } | ||
| 56 | |||
| 57 | harmonic++; | ||
| 58 | freq = harmonic * basefreq; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | void LEAF_generate_triangle(float* buffer, float basefreq, int size) | ||
| 64 | { | ||
| 65 | int harmonic = 1; | ||
| 66 | float phase = 0.0f; | ||
| 67 | float freq = harmonic * basefreq; | ||
| 68 | float amp = 1.0f; | ||
| 69 | |||
| 70 | int count = 0; | ||
| 71 | float mult = 1.0f; | ||
| 72 | |||
| 73 | while (freq < (leaf.sampleRate * 0.5)) | ||
| 74 | { | ||
| 75 | amp = 1.0f / (float)(harmonic * harmonic); | ||
| 76 | |||
| 77 | if (count % 2) mult = -1.0f; | ||
| 78 | else mult = 1.0f; | ||
| 79 | |||
| 80 | for (int i = 0; i < size; i++) | ||
| 81 | { | ||
| 82 | phase = (float) i / (float) size; | ||
| 83 | buffer[i] += (mult * amp * sinf(harmonic * phase * TWO_PI)); | ||
| 84 | } | ||
| 85 | |||
| 86 | count++; | ||
| 87 | harmonic += 2; | ||
| 88 | freq = harmonic * basefreq; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | void LEAF_generate_square(float* buffer, float basefreq, int size) | ||
| 93 | { | ||
| 94 | int harmonic = 1; | ||
| 95 | float phase = 0.0f; | ||
| 96 | float freq = harmonic * basefreq; | ||
| 97 | float amp = 1.0f; | ||
| 98 | |||
| 99 | while (freq < (leaf.sampleRate * 0.5)) | ||
| 100 | { | ||
| 101 | amp = 1.0f / (float)(harmonic); | ||
| 102 | |||
| 103 | for (int i = 0; i < size; i++) | ||
| 104 | { | ||
| 105 | phase = (float) i / (float) size; | ||
| 106 | buffer[i] += (amp * sinf(harmonic * phase * TWO_PI)); | ||
| 107 | } | ||
| 108 | |||
| 109 | harmonic += 2; | ||
| 110 | freq = harmonic * basefreq; | ||
| 111 | } | ||
| 112 | } | ||
| 28 | 113 | ||
| 29 | 114 | ||
| 30 | 115 | ||