forked from pingdynasty/OwlPatches
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalfprimitives.h
598 lines (546 loc) · 15.9 KB
/
calfprimitives.h
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/* Calf DSP Library
* DSP primitives.
*
* Copyright (C) 2001-2007 Krzysztof Foltman
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02111-1307, USA.
*/
#ifndef __CALF_PRIMITIVES_H
#define __CALF_PRIMITIVES_H
#ifdef min
#undef min
#endif
#ifdef max
#undef max
#endif
#ifdef abs
#undef abs
#endif
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <cstdlib>
#include <map>
namespace dsp {
/// Set a float to zero
inline void zero(float &v) {
v = 0.f;
};
/// Set a double to zero
inline void zero(double &v) {
v = 0.0;
};
/// Set 64-bit unsigned integer value to zero
inline void zero(uint64_t &v) { v = 0; };
/// Set 32-bit unsigned integer value to zero
inline void zero(uint32_t &v) { v = 0; };
/// Set 16-bit unsigned integer value to zero
inline void zero(uint16_t &v) { v = 0; };
/// Set 8-bit unsigned integer value to zero
inline void zero(uint8_t &v) { v = 0; };
/// Set 64-bit signed integer value to zero
inline void zero(int64_t &v) { v = 0; };
/// Set 32-bit signed integer value to zero
inline void zero(int32_t &v) { v = 0; };
/// Set 16-bit signed integer value to zero
inline void zero(int16_t &v) { v = 0; };
/// Set 8-bit signed integer value to zero
inline void zero(int8_t &v) { v = 0; };
/// Set array (buffer or anything similar) to vector of zeroes
template<class T>
void zero(T *data, unsigned int size) {
T value;
dsp::zero(value);
for (unsigned int i=0; i<size; i++)
*data++ = value;
}
/// Set array (buffer or anything similar) to vector of values
template<class T>
void fill(T *data, T value, unsigned int size) {
for (unsigned int i=0; i<size; i++)
*data++ = value;
}
template<class T = float>struct stereo_sample {
T left;
T right;
/// default constructor - preserves T's semantics (ie. no implicit initialization to 0)
inline stereo_sample() {
}
inline stereo_sample(T _left, T _right) {
left = _left;
right = _right;
}
inline stereo_sample(T _both) {
left = right = _both;
}
template<typename U>
inline stereo_sample(const stereo_sample<U> &value) {
left = value.left;
right = value.right;
}
inline stereo_sample& operator=(const T &value) {
left = right = value;
return *this;
}
template<typename U>
inline stereo_sample& operator=(const stereo_sample<U> &value) {
left = value.left;
right = value.right;
return *this;
}
/*
inline operator T() const {
return (left+right)/2;
}
*/
inline stereo_sample& operator*=(const T &multiplier) {
left *= multiplier;
right *= multiplier;
return *this;
}
inline stereo_sample& operator+=(const stereo_sample<T> &value) {
left += value.left;
right += value.right;
return *this;
}
inline stereo_sample& operator-=(const stereo_sample<T> &value) {
left -= value.left;
right -= value.right;
return *this;
}
template<typename U> inline stereo_sample<U> operator*(const U &value) const {
return stereo_sample<U>(left*value, right*value);
}
/*inline stereo_sample<float> operator*(float value) const {
return stereo_sample<float>(left*value, right*value);
}
inline stereo_sample<double> operator*(double value) const {
return stereo_sample<double>(left*value, right*value);
}*/
inline stereo_sample<T> operator+(const stereo_sample<T> &value) {
return stereo_sample(left+value.left, right+value.right);
}
inline stereo_sample<T> operator-(const stereo_sample<T> &value) {
return stereo_sample(left-value.left, right-value.right);
}
inline stereo_sample<T> operator+(const T &value) {
return stereo_sample(left+value, right+value);
}
inline stereo_sample<T> operator-(const T &value) {
return stereo_sample(left-value, right-value);
}
inline stereo_sample<float> operator+(float value) {
return stereo_sample<float>(left+value, right+value);
}
inline stereo_sample<float> operator-(float value) {
return stereo_sample<float>(left-value, right-value);
}
inline stereo_sample<double> operator+(double value) {
return stereo_sample<double>(left+value, right+value);
}
inline stereo_sample<double> operator-(double value) {
return stereo_sample<double>(left-value, right-value);
}
};
/// Multiply constant by stereo_value
template<class T>
inline stereo_sample<T> operator*(const T &value, const stereo_sample<T> &value2) {
return stereo_sample<T>(value2.left*value, value2.right*value);
}
/// Add constant to stereo_value
template<class T>
inline stereo_sample<T> operator+(const T &value, const stereo_sample<T> &value2) {
return stereo_sample<T>(value2.left+value, value2.right+value);
}
/// Subtract stereo_value from constant (yields stereo_value of course)
template<class T>
inline stereo_sample<T> operator-(const T &value, const stereo_sample<T> &value2) {
return stereo_sample<T>(value-value2.left, value-value2.right);
}
/// Shift value right by 'bits' bits (multiply by 2^-bits)
template<typename T>
inline stereo_sample<T> shr(stereo_sample<T> v, int bits = 1) {
v.left = shr(v.left, bits);
v.right = shr(v.right, bits);
return v;
}
/// Set a stereo_sample<T> value to zero
template<typename T>
inline void zero(stereo_sample<T> &v) {
dsp::zero(v.left);
dsp::zero(v.right);
}
/// 'Small value' for integer and other types
template<typename T>
inline T small_value() {
return 0;
}
/// 'Small value' for floats (2^-24) - used for primitive underrun prevention. The value is pretty much arbitrary (allowing for 24-bit signals normalized to 1.0).
template<>
inline float small_value<float>() {
return (1.0/16777216.0); // allows for 2^-24, should be enough for 24-bit DACs at least :)
}
/// 'Small value' for doubles (2^-24) - used for primitive underrun prevention. The value is pretty much arbitrary.
template<>
inline double small_value<double>() {
return (1.0/16777216.0);
}
/// Convert a single value to single value = do nothing :) (but it's a generic with specialisation for stereo_sample)
template<typename T>
inline float mono(T v) {
return v;
}
/// Convert a stereo_sample to single value by averaging two channels
template<typename T>
inline T mono(stereo_sample<T> v) {
return shr(v.left+v.right);
}
/// Clip a value to [min, max]
template<typename T>
inline T clip(T value, T min, T max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
/// Clip a double to [-1.0, +1.0]
inline double clip11(double value) {
double a = fabs(value);
if (a<=1) return value;
return (value<0) ? -1.0 : 1.0;
}
/// Clip a float to [-1.0f, +1.0f]
inline float clip11(float value) {
float a = fabsf(value);
if (a<=1) return value;
return (value<0) ? -1.0f : 1.0f;
}
/// Clip a double to [0.0, +1.0]
inline double clip01(double value) {
double a = fabs(value-0.5);
if (a<=0.5) return value;
return (a<0) ? -0.0 : 1.0;
}
/// Clip a float to [0.0f, +1.0f]
inline float clip01(float value) {
float a = fabsf(value-0.5f);
if (a<=0.5f) return value;
return (value < 0) ? -0.0f : 1.0f;
}
// Linear interpolation (mix-way between v1 and v2).
template<typename T, typename U>
inline T lerp(T v1, T v2, U mix) {
return v1+(v2-v1)*mix;
}
// Linear interpolation for stereo values (mix-way between v1 and v2).
template<typename T>
inline stereo_sample<T> lerp(stereo_sample<T> &v1, stereo_sample<T> &v2, float mix) {
return stereo_sample<T>(v1.left+(v2.left-v1.left)*mix, v1.right+(v2.right-v1.right)*mix);
}
/**
* decay-only envelope (linear or exponential); deactivates itself when it goes below a set point (epsilon)
*/
class decay
{
double value, initial;
unsigned int age, mask;
bool active;
public:
decay() {
active = false;
mask = 127;
initial = value = 0.0;
}
inline bool get_active() {
return active;
}
inline double get() {
return active ? value : 0.0;
}
inline void set(double v) {
initial = value = v;
active = true;
age = 0;
}
/// reinitialise envelope (must be called if shape changes from linear to exponential or vice versa in the middle of envelope)
inline void reinit()
{
initial = value;
age = 1;
}
inline void add(double v) {
if (active)
value += v;
else
value = v;
initial = value;
age = 0;
active = true;
}
static inline double calc_exp_constant(double times, double cycles)
{
if (cycles < 1.0)
cycles = 1.0;
return pow(times, 1.0 / cycles);
}
inline void age_exp(double constant, double epsilon) {
if (active) {
if (!(age & mask))
value = initial * pow(constant, (double)age);
else
value *= constant;
if (value < epsilon)
active = false;
age++;
}
}
inline void age_lin(double constant, double epsilon) {
if (active) {
if (!(age & mask))
value = initial - constant * age;
else
value -= constant;
if (value < epsilon)
active = false;
age++;
}
}
inline void deactivate() {
active = false;
value = 0;
}
};
class scheduler;
class task {
public:
virtual void execute(scheduler *s)=0;
virtual void dispose() { delete this; }
virtual ~task() {}
};
/// this scheduler is based on std::multimap, so it isn't very fast, I guess
/// maybe some day it should be rewritten to use heapsort or something
/// work in progress, don't use!
class scheduler {
std::multimap<unsigned int, task *> timeline;
unsigned int time, next_task;
bool eob;
class end_buf_task: public task {
public:
scheduler *p;
end_buf_task(scheduler *_p) : p(_p) {}
virtual void execute(scheduler *s) { p->eob = true; }
virtual void dispose() { }
} eobt;
public:
scheduler()
: time(0)
, next_task((unsigned)-1)
, eob(true)
, eobt (this)
{
time = 0;
next_task = (unsigned)-1;
eob = false;
}
inline bool is_next_tick() {
if (time < next_task)
return true;
do_tasks();
}
inline void next_tick() {
time++;
}
void set(int pos, task *t) {
timeline.insert(std::pair<unsigned int, task *>(time+pos, t));
next_task = timeline.begin()->first;
}
void do_tasks() {
std::multimap<unsigned int, task *>::iterator i = timeline.begin();
while(i != timeline.end() && i->first == time) {
i->second->execute(this);
i->second->dispose();
timeline.erase(i);
}
}
bool is_eob() {
return eob;
}
void set_buffer_size(int count) {
set(count, &eobt);
}
};
/**
* Force "small enough" float value to zero
*/
inline void sanitize(float &value)
{
// real number?
if (std::abs(value) < small_value<float>())
value = 0.f;
// close to 0?
const int val = *reinterpret_cast <const int *> (&value);
if ((val & 0x7F800000) == 0 && (val & 0x007FFFFF) != 0)
value = 0.f;
}
inline float _sanitize(float value)
{
if (std::abs(value) < small_value<float>())
return 0.f;
return value;
}
/**
* Force already-denormal float value to zero
*/
inline void sanitize_denormal(float& value)
{
if (!std::isnormal(value))
value = 0.f;
}
/**
* Force already-denormal float value to zero
*/
inline void sanitize_denormal(double & value)
{
if (!std::isnormal(value))
value = 0.f;
}
/**
* Force "small enough" double value to zero
*/
inline void sanitize(double &value)
{
if (std::abs(value) < small_value<double>())
value = 0.0;
}
inline double _sanitize(double value)
{
if (std::abs(value) < small_value<double>())
return 0.0;
return value;
}
/**
* Force "small enough" stereo value to zero
*/
template<class T>
inline void sanitize(stereo_sample<T> &value)
{
sanitize(value.left);
sanitize(value.right);
}
inline float fract16(unsigned int value)
{
return (value & 0xFFFF) * (1.0 / 65536.0);
}
/**
* typical precalculated sine table
*/
template<class T, int N, int Multiplier>
class sine_table
{
public:
static bool initialized;
static T data[N+1];
sine_table() {
if (initialized)
return;
initialized = true;
for (int i=0; i<N+1; i++)
data[i] = (T)(Multiplier*sin(i*2*M_PI*(1.0/N)));
}
};
template<class T, int N, int Multiplier>
bool sine_table<T,N,Multiplier>::initialized = false;
template<class T, int N, int Multiplier>
T sine_table<T,N,Multiplier>::data[N+1];
/// fast float to int conversion using default rounding mode
inline int fastf2i_drm(float f)
{
#ifdef __X86__
volatile int v;
__asm ( "flds %1; fistpl %0" : "=m"(v) : "m"(f));
return v;
#else
return (int)nearbyintf(f);
#endif
}
/// Convert MIDI note to frequency in Hz.
inline float note_to_hz(double note, double detune_cents = 0.0)
{
return 440 * pow(2.0, (note - 69 + detune_cents/100.0) / 12.0);
}
/// Hermite interpolation between two points and slopes in normalized range (written after Wikipedia article)
/// @arg t normalized x coordinate (0-1 over the interval in question)
/// @arg p0 first point
/// @arg p1 second point
/// @arg m0 first slope (multiply by interval width when using over non-1-wide interval)
/// @arg m1 second slope (multiply by interval width when using over non-1-wide interval)
inline float normalized_hermite(float t, float p0, float p1, float m0, float m1)
{
float t2 = t*t;
float t3 = t2*t;
return (2*t3 - 3*t2 + 1) * p0 + (t3 - 2*t2 + t) * m0 + (-2*t3 + 3*t2) * p1 + (t3-t2) * m1;
}
/// Hermite interpolation between two points and slopes
/// @arg x point within interval (x0 <= x <= x1)
/// @arg x0 interval start
/// @arg x1 interval end
/// @arg p0 value at x0
/// @arg p1 value at x1
/// @arg m0 slope (steepness, tangent) at x0
/// @arg m1 slope at x1
inline float hermite_interpolation(float x, float x0, float x1, float p0, float p1, float m0, float m1)
{
float width = x1 - x0;
float t = (x - x0) / width;
m0 *= width;
m1 *= width;
float t2 = t*t;
float t3 = t2*t;
float ct0 = p0;
float ct1 = m0;
float ct2 = -3 * p0 - 2 * m0 + 3 * p1 - m1;
float ct3 = 2 * p0 + m0 - 2 * p1 + m1;
return ct3 * t3 + ct2 * t2 + ct1 * t + ct0;
//return (2*t3 - 3*t2 + 1) * p0 + (t3 - 2*t2 + t) * m0 + (-2*t3 + 3*t2) * p1 + (t3-t2) * m1;
}
/// convert amplitude value to dB
inline float amp2dB(float amp)
{
return 20 * log10(amp);
}
/// convert dB to amplitude value
inline float dB2amp(float db)
{
return exp((db / 20.0) * log(10.0));
}
/// print binary of any data type
/// assumes little endian
inline void print_bits(size_t const size, void const * const ptr)
{
unsigned char *b = (unsigned char*) ptr;
unsigned char byte;
for (int i = size - 1; i >=0 ; i--) {
for (int j = 7; j >= 0; j--) {
byte = b[i] & (1<<j);
byte >>= j;
printf("%u", byte);
}
}
puts("");
}
};
#endif