-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsflib.c
493 lines (383 loc) · 11.4 KB
/
dsflib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/**
\file dsflib.c
\author Sebastian Zimmermann
\date September 2020
\brief contains the main functionality of the dsf synthesis.
\see James A. Moorer, "The Synthesis of Complex Audio Spectra by Means of Discrete Summation Formulas", 1976
\see https://ccrma.stanford.edu/files/papers/stanm5.pdf
\see Tim Stilson, Julius Smith, "Alias-Free Digital Synthesis of Classic Analog Waveforms", 1996
\see https://ccrma.stanford.edu/~stilti/papers/blit.pdf
*/
#include "dsflib.h"
/**
\brief initialize a complex_nr
Allocates necessary memory and sets the
complex number to a degree so that it could
be used as an increment to generate an oscillation
at 440 Hz. This enables fast sanity checking.
\return Pointer to initialized complex_nr
*/
complex_nr *new_complex_nr()
{
complex_nr *new_nr = (complex_nr *)malloc(sizeof(complex_nr));
new_nr->im = 0.03141;
new_nr->re = 0.999506;
adjust_phasor(new_nr);
return new_nr;
}
/**
\brief initialize dsf memory
The dsf struct holds all variables necessary for
calculating the dsf.
\return Pointer to allocated and initialized memory.
\see dsf
*/
dsf *dsf_new()
{
dsf *x = (dsf *)malloc(sizeof(dsf));
x->phasor_a = new_complex_nr();
x->increment_a = new_complex_nr();
x->phasor_b = new_complex_nr();
x->increment_b = new_complex_nr();
x->weight = 0.5;
x->usr_num_of_sines = 90;
x->num_of_sines = 90;
x->frequency = 220;
x->distance = 220;
return x;
}
/**
\brief free dsf struct memory
*/
void dsf_free(dsf *x)
{
free(x->increment_a);
free(x->phasor_a);
free(x->increment_b);
free(x->phasor_b);
free(x);
}
/**
\brief get the smaller value of two integers
\param a first value
\param b second value
\return smaller of a and b
*/
int min(int a, int b)
{
int x = a < b ? a : b;
return x;
}
/**
\brief clip float value between a min and a max value
\param min Minimal value
\param max Maximal value
\param signal Signal to be clipped
\return clipped signal
*/
float clip(float min, float max, float signal)
{
float out = signal;
if(signal < min)
{
out = min;
}
if(signal > max)
{
out = max;
}
return out;
}
/**
\brief convert midi note values to frequencies
\param note midi note value
\return corresponding frequency
*/
float mtof(float note)
{
float frequency = pow(2.0, (note - 69.0) / 12.0) * 440;
return frequency;
}
/**
\brief set angle of increment to frequency
Rotating phasors are being rotated by multiplications with
a complex number called increment. set_increment_to_freq()
sets the angle of
an increment with respect to the sample frequency
so that those multiplications produce
the correct oscillation frequency of the rotating phasor.
\param increment The complex number that is to be adjusted
for the given frequency
\param freq The frequency to which the increment is to be set
\param sr_inv The inverted sample frequency
*/
void set_increment_to_freq(complex_nr* increment, INPRECISION freq, INPRECISION sr_inv)
{
// multiply by 2.0 to get fraction of Nyquist frequency
double argument = (freq * sr_inv * 2.0) * PI;
set_phasor_to_argument(increment, argument);
}
/**
\brief set angle of a phasor
\param phasor The phasor whose angle is to be set
\param argument The angle to which to set the phasor to.
The range \f$[0 \dots 2\pi]\f$ describes a full rotation.
*/
void set_phasor_to_argument(complex_nr *phasor, double argument)
{
phasor->im = sin(argument);
phasor->re = cos(argument);
}
/**
\brief multiplying two complex numbers
\param a Factor a
\param b Factor b
\param result Memory to which the result is being written
*/
void multiply_complex(complex_nr *a, complex_nr *b, complex_nr *result)
{
INPRECISION re = (a->re * b->re) - (a->im * b->im);
INPRECISION im = (a->re * b->im) + (a->im * b->re);
result->re = re;
result->im = im;
}
/**
\brief divide complex numbers
\param numerator Numerator of division
\param denominator Denominator of division
\param result Pointer to memory where result of division is written to
*/
void divide_complex(complex_nr *numerator, complex_nr *denominator, complex_nr *result)
{
complex_nr conjugate;
conjugate.im = -denominator->im;
conjugate.re = denominator->re;
complex_nr expanded_num;
multiply_complex(numerator, &conjugate, &expanded_num);
complex_nr expanded_denom;
multiply_complex(denominator, &conjugate, &expanded_denom);
result->im = expanded_num.im / expanded_denom.re;
result->re = expanded_num.re / expanded_denom.re;
}
/**
\brief adjust phasor degeneration caused by digital noise
Due to finite machine precision, over time phasors might
diverge from the unit circle. In certain intervals
adjust_phasor(complex_nr *phasor) is being called to
re-adjust the length of a phasor to 1.
\param phasor Phasor to be adjusted.
*/
void adjust_phasor(complex_nr *phasor)
{
double length = sqrt(phasor->im * phasor->im + phasor->re * phasor->re);
double adjustment = 1.0 / length;
phasor->im *= adjustment;
phasor->re *= adjustment;
}
/**
\brief calculate power of complex number
\param x Base
\param power Exponent
\param result Pointer to memory where to write the result to
*/
void power_complex(complex_nr *x, int power, complex_nr *result)
{
// stop recursion:
if(power <= 1)
{
result->im = x->im;
result->re = x->re;
}
else
{
complex_nr intermediate;
power_complex(x, power/2, &intermediate);
multiply_complex(&intermediate, &intermediate, result);
if(power % 2)
{
// uneven exponent: multiply result one more time with x
multiply_complex(x, result, result);
}
}
}
/**
\brief calculate power of complex number
This function is computationally very expensive.
\param x Base
\param power Exponent
\param result Pointer to memory where to write the result to
*/
void power_complex_naiv(complex_nr *x, int power, complex_nr *result)
{
result->im = x->im;
result->re = x->re;
for(int i = 1; i < power; i++)
{
multiply_complex(x, result, result);
}
}
/**
\brief calculate geometric series by addition
This function performs the actual addition of
the geometric series and is thus computationally
expensive.
\param x dsf variables
\param result Pointer to memory where to write the sum
of the geometric series to
\param norm_factor Pointer to memory where to store the
maximal possible value of the sum to. This can be used to
normalize the signal
*/
void calculate_series(dsf *x, complex_nr *result, double *norm_factor)
{
INPRECISION powered_weight;
complex_nr running_sum;
complex_nr powered_b;
// w^0 * b^0
powered_b.im = 0.0;
powered_b.re = 1.0;
running_sum.im = 0.0;
running_sum.re = 1.0;
powered_weight = 1.0;
*norm_factor = 1.0;
// increasing powers
for(int i = 1; i < x->num_of_sines; i++)
{
// b^i
multiply_complex(x->phasor_b, &powered_b, &powered_b);
// w^i
powered_weight *= x->weight;
// track norm factor
*norm_factor += powered_weight;
// add w^i * b^i to sum
running_sum.im += powered_weight * powered_b.im;
running_sum.re += powered_weight * powered_b.re;
}
multiply_complex(&running_sum, x->phasor_a, result);
// track length for phasor a:
*norm_factor += 1.0;
}
/**
\brief calculate \f$ 1 - x, x \in \mathbb{C} \f$
\param x Number to substract from 1
\param result Pointer to memory where to write the result to
*/
void one_minus_complex(complex_nr *x, complex_nr *result)
{
result->im = 0.0 - x->im;
result->re = 1.0 - x->re;
}
/**
\brief calculate \f$ a \cdot x, x \in \mathbb{C} \f$
\param x Complex number to scale
\param scalar Scalar to scale x with
\param result Pointer to memory where to write the result to
*/
void scale_complex(complex_nr *x, double scalar, complex_nr *result)
{
result->im = scalar * x->im;
result->re = scalar * x->re;
}
/**
\brief calculate denominator of closed form of geometric series
Closed form of geometric series is
\f$ a \cdot \frac{1 - (wb)^n}{1 - wb} \f$.
This function calculates the denominator \f$ 1 - wb \f$.
\param x dsf variables
\param result Pointer to memory where to write the result to
*/
void geometric_series_denominator(dsf *x, complex_nr *result)
{
// calculating (1 - w * b)
// where w is x->weight
// and b is current position of distance phasor x->phasor_b
double w = x->weight;
complex_nr wb;
scale_complex(x->phasor_b, w, &wb);
one_minus_complex(&wb, result);
}
/**
\brief calculate numerator of closed form of geometric series
Closed form of geometric series is
\f$ a \cdot \frac{1 - (wb)^n}{1 - wb} \f$.
This function calculates the numerator \f$ 1 - (wb)^n \f$.
\param x dsf variables
\param result Pointer to memory where to write the result to
*/
void geometric_series_numerator(dsf *x, complex_nr *result)
{
// calculating (1 - (w * b)^n) = (1 - (w^n * b^n))
// where w is x->weight,
// b is current position of distance phasor x->phasor_b,
// and n is number of sines (or number of sines + 1?)
int n = x->num_of_sines;
double wn = pow(x->weight, n);
complex_nr bn;
power_complex(x->phasor_b, n, &bn);
complex_nr wnbn;
scale_complex(&bn, wn, &wnbn);
one_minus_complex(&wnbn, result);
}
/**
\brief calculate factor of closed form of geometric series
Closed form of geometric series is
\f$ a \cdot \frac{1 - (wb)^n}{1 - wb} \f$.
This function calculates the factor \f$ \frac{1 - (wb)^n}{1 - wb} \f$.
\param x dsf variables
\param result Pointer to memory where to write the result to
*/
void geometric_series_factor(dsf *x, complex_nr *result)
{
// calculating (1 - (wb)^k) / (1 - wb)
complex_nr numerator;
geometric_series_numerator(x, &numerator);
complex_nr denominator;
geometric_series_denominator(x, &denominator);
divide_complex(&numerator, &denominator, result);
}
/**
\brief calculate closed form of geometric series
Closed form of geometric series is
\f$ a \cdot \frac{1 - (wb)^n}{1 - wb} \f$.
\param x dsf variables
\param result Pointer to memory where to write the result to
*/
void geometric_series(dsf *x, complex_nr *result)
{
complex_nr factor;
if(x->phasor_b->re == 1 && x->weight == 1)
{
// following rule of l'hopital for "0/0":
factor.im = 0;
factor.re = 1;
}
else
{
geometric_series_factor(x, &factor);
}
multiply_complex(x->phasor_a, &factor, result);
}
/**
\brief calculate maximal value of geometric series
use the result to normalise a signal that was calculated
with the geometric series.
\param len This is the weight factor of the dsf synthesis
\param num_of_sines Number of partials in synthesis
\returns A scalar x that normalizes the signal s generated
by the geometric series when applied like so:
\f$ s_{norm} = s \cdot x\f$.
*/
INPRECISION norm_factor(INPRECISION len, int num_of_sines)
{
INPRECISION norm_factor = 0;
if(len == 1.0)
{
// l'hopital "0/0":
norm_factor = 1.0 / num_of_sines;
}
else
{
norm_factor = (1.0 - len) / (1.0 - pow(len, num_of_sines));
}
return norm_factor;
}