History

fixed wavefolder and added samplereducer

spiricom committed 6 years ago

Commit 19a991d1 parent 6e322e97 files · +105 −56Browse files

Unified · Split

7 files
  1. .DS_Store +0 −0
  2. LEAF/.DS_Store +0 −0
  3. LEAF/Inc/.DS_Store +0 −0
  4. LEAF/Inc/leaf-distortion.h +37 −13
  5. LEAF/Src/.DS_Store +0 −0
  6. LEAF/Src/leaf-distortion.c +67 −25
  7. LEAF/Src/leaf-math.c +5 −22
.DS_Store+0−0

Binary file not shown.

LEAF/.DS_Store+0−0

Binary file not shown.

LEAF/Inc/.DS_Store+0−0

Binary file not shown.

LEAF/Inc/leaf-distortion.h+37−13
@@ -25,23 +25,23 @@ extern "C" { · 24 unmodified lines
2525
2626 typedef struct _tLockhartWavefolder
2727 {
28− float Ln1;
29− float Fn1;
28+ double Ln1;
29+ double Fn1;
3030 float xn1;
31−
32− float RL;
33− float R;
34− float VT;
35− float Is;
3631
37− float a;
38− float b;
39− float d;
32+ double RL;
33+ double R;
34+ double VT;
35+ double Is;
36+
37+ double a;
38+ double b;
39+ double d;
4040
4141 // Antialiasing error threshold
42− float thresh;
43− float half_a;
44− float longthing;
42+ double thresh;
43+ double half_a;
44+ double longthing;
4545
4646 } _tLockhartWavefolder;
4747
@@ -54,6 +54,27 @@ extern "C" { · 6 unmodified lines
5454
5555 //==============================================================================
5656
57+
58+
59+ typedef struct _tSampleReducer
60+ {
61+ float invRatio;
62+ float hold;
63+ uint32_t count;
64+ } _tSampleReducer;
65+
66+ typedef _tSampleReducer* tSampleReducer;
67+
68+ void tSampleReducer_init (tSampleReducer* const);
69+ void tSampleReducer_free (tSampleReducer* const);
70+
71+ float tSampleReducer_tick (tSampleReducer* const, float input);
72+
73+ // sampling ratio
74+ void tSampleReducer_setRatio (tSampleReducer* const, float ratio);
75+
76+ //==============================================================================
77+
5778 typedef struct _tCrusher
5879 {
5980 float srr;
@@ -63,6 +84,7 @@ extern "C" { · 3 unmodified lines
6384 uint32_t op; //which bitwise operation (0-7)
6485
6586 float gain;
87+ tSampleReducer sReducer;
6688
6789 } _tCrusher;
6890
@@ -85,7 +107,9 @@ extern "C" { · 16 unmodified lines
85107 // sampling ratio
86108 void tCrusher_setSamplingRatio (tCrusher* const, float ratio);
87109
110+
88111 //==============================================================================
112+
89113
90114 typedef struct _tOversampler
91115 {
LEAF/Src/.DS_Store+0−0

Binary file not shown.

LEAF/Src/leaf-distortion.c+67−25
@@ -16,11 +16,17 @@ · 15 unmodified lines
1616 #include "../Inc/leaf-distortion.h"
1717 #include "../Inc/leaf-tables.h"
1818
19+
1920 #endif
2021
2122 //============================================================================================================
2223 // WAVEFOLDER
2324 //============================================================================================================
25+
26+
27+//from the paper: Virtual Analog Model of the Lockhart Wavefolder
28+//by Fabián Esqueda, Henri Pöntynen, Julian D. Parker and Stefan Bilbao
29+
2430 void tLockhartWavefolder_init(tLockhartWavefolder* const wf)
2531 {
2632 _tLockhartWavefolder* w = *wf = (_tLockhartWavefolder*) leaf_alloc(sizeof(_tLockhartWavefolder));
@@ -37,9 +43,10 @@ void tLockhartWavefolder_init(tLockhartWavefolder* const wf) · 10 unmodified lines
3743 w->a = 2.0*w->RL/w->R;
3844 w->b = (w->R+2.0*w->RL)/(w->VT*w->R);
3945 w->d = (w->RL*w->Is)/w->VT;
40− w->half_a = 0.5f * w->a;
46+ w->half_a = 0.5 * w->a;
4147 w->longthing = (0.5*w->VT/w->b);
4248
49+
4350 // Antialiasing error threshold
4451 w->thresh = 10e-10;
4552 }
@@ -55,28 +62,26 @@ double tLockhartWavefolderLambert(double x, double ln) · 9 unmodified lines
5562 {
5663 double thresh, w, expw, p, r, s, err;
5764 // Error threshold
58− thresh = 10e-6;
65+ thresh = 10e-10;
5966 // Initial guess (use previous value)
6067 w = ln;
6168
6269 // Haley's method (Sec. 4.2 of the paper)
63− for(int i=0; i<100; i+=1) {
70+ for(int i=0; i<1000; i+=1) {
6471
6572 expw = exp(w);
6673
6774 p = w*expw - x;
6875 r = (w+1.0)*expw;
69− s = (w+2.0)/(2.0*(w+1.0)); err = (p/(r-(p*s)));
76+ s = (w+2.0)/(2.0*(w+1.0));
77+ err = (p/(r-(p*s)));
7078
7179 if (fabs(err)<thresh) {
72− break;
73− }
74− if (isnan(err))
75− {
76− break;
80+ break;
7781 }
7882
7983 w = w - err;
84+
8085 }
8186 return w;
8287 }
@@ -86,11 +91,9 @@ float tLockhartWavefolder_tick(tLockhartWavefolder* const wf, float samp) · 3 unmodified lines
8691 _tLockhartWavefolder* w = *wf;
8792
8893 float out = 0.0f;
89− // Constants
90−
9194
9295 // Compute Antiderivative
93− int l = (samp > 0) - (samp < 0);
96+ int l = (samp > 0.0f) - (samp < 0.0f);
9497 double u = w->d*exp(l*w->b*samp);
9598 double Ln = tLockhartWavefolderLambert(u,w->Ln1);
9699 double Fn = w->longthing*(Ln*(Ln + 2.0)) - w->half_a*samp*samp;
@@ -99,24 +102,16 @@ float tLockhartWavefolder_tick(tLockhartWavefolder* const wf, float samp) · 2 unmodified lines
99102 if (fabs(samp-w->xn1)<w->thresh) {
100103
101104 // Compute Averaged Wavefolder Output
102− double xn = 0.5*(samp+w->xn1);
105+ float xn = 0.5f*(samp+w->xn1);
103106 u = w->d*exp(l*w->b*xn);
104107 Ln = tLockhartWavefolderLambert(u,w->Ln1);
105108 out = (float) (l*w->VT*Ln - w->a*xn);
106− if (isnan(out))
107− {
108− ;
109− }
110109
111110 }
112111 else {
113112
114113 // Apply AA Form
115114 out = (float) ((Fn-w->Fn1)/(samp-w->xn1));
116− if (isnan(out))
117− {
118− ;
119− }
120115 }
121116
122117 // Update States
@@ -140,14 +135,14 @@ void tCrusher_init (tCrusher* const cr) · 17 unmodified lines
140135 c->div = SCALAR;
141136 c->rnd = 0.25f;
142137 c->srr = 0.25f;
143−
138+ tSampleReducer_init(&c->sReducer);
144139 c->gain = (c->div / SCALAR) * 0.7f + 0.3f;
145140 }
146141
147142 void tCrusher_free (tCrusher* const cr)
148143 {
149144 _tCrusher* c = *cr;
150−
145+ tSampleReducer_free(&c->sReducer);
151146 leaf_free(c);
152147 }
153148
@@ -169,7 +164,7 @@ float tCrusher_tick (tCrusher* const cr, float input) · 15 unmodified lines
169164
170165 sample = LEAF_round(sample, c->rnd);
171166
172− sample = LEAF_reduct(sample, c->srr);
167+ sample = tSampleReducer_tick(&c->sReducer, sample);
173168
174169 return sample * c->gain;
175170
@@ -204,8 +199,55 @@ void tCrusher_setSamplingRatio (tCrusher* const cr, float ratio) · 28 unmodified lines
204199 {
205200 _tCrusher* c = *cr;
206201 c->srr = ratio;
202+ tSampleReducer_setRatio(&c->sReducer, ratio);
203+
207204 }
208205
206+//============================================================================================================
207+// Sample-Rate reducer
208+//============================================================================================================
209+
210+
211+void tSampleReducer_init(tSampleReducer* const sr)
212+{
213+ _tSampleReducer* s = *sr = (_tSampleReducer*) leaf_alloc(sizeof(_tSampleReducer));
214+
215+
216+
217+ s->invRatio = 1.0f;
218+ s->hold = 0.0f;
219+ s->count = 0;
220+
221+}
222+
223+void tSampleReducer_free (tSampleReducer* const sr)
224+{
225+ _tSampleReducer* s = *sr;
226+
227+ leaf_free(s);
228+}
229+
230+float tSampleReducer_tick(tSampleReducer* const sr, float input)
231+{
232+ _tSampleReducer* s = *sr;
233+ if (s->count > s->invRatio)
234+ {
235+ s->hold = input;
236+ s->count = 0;
237+ }
238+
239+ s->count++;
240+ return s->hold;
241+}
242+
243+
244+void tSampleReducer_setRatio(tSampleReducer* const sr, float ratio)
245+{
246+ _tSampleReducer* s = *sr;
247+ if ((ratio <= 1.0f) && (ratio >= 0.0f))
248+ s->invRatio = 1.0f / ratio;
249+
250+}
209251
210252 //============================================================================================================
211253 // Oversampler
@@ -411,7 +453,7 @@ float tOversampler_downsample(tOversampler *const osr, float* input) · 199 unmodified lines
411453 output = acc0;
412454
413455 /* Processing is complete.
414− Now copy the last numTaps - 1 samples to the satrt of the state buffer.
456+ Now copy the last numTaps - 1 samples to the start of the state buffer.
415457 This prepares the state buffer for the next function call. */
416458
417459 /* Points to the start of the state buffer */
LEAF/Src/leaf-math.c+5−22
@@ -165,23 +165,6 @@ float LEAF_shaper(float input, float m_drive) · 164 unmodified lines
165165 return shaperOut;
166166 }
167167
168−// reduce sample resolution
169−float hold = 0.f;
170−int reduct_count = 0;
171−
172−
173−float LEAF_reduct (float input, float ratio)
174−{
175− reduct_count++;
176− if (reduct_count > 1.f / ratio)
177− {
178− hold = input;
179− reduct_count = 0;
180− }
181−
182− return hold;
183−}
184−
185168 // round input to nearest rnd
186169 float LEAF_round (float input, float rnd)
187170 {
@@ -270,12 +253,12 @@ oBool LEAF_isPrime(uint64_t number ) · 82 unmodified lines
270253 float LEAF_tanh(float x)
271254 {
272255
273− if( x < -3 )
274− return -1;
275− else if( x > 3 )
276− return 1;
256+ if( x < -3.0f )
257+ return -1.0f;
258+ else if( x > 3.0f )
259+ return 1.0f;
277260 else
278− return x * ( 27 + x * x ) / ( 27 + 9 * x * x );
261+ return x * ( 27.0f + x * x ) / ( 27.0f + 9.0f * x * x );
279262 }
280263
281264

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t