-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx_saturator.h
118 lines (93 loc) · 3.01 KB
/
px_saturator.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
#include "px_globals.h"
#include "px_memory.h"
#ifndef PX_SATURATOR_H
#define PX_SATURATOR_H
typedef enum
{
ARCTANGENT,
TANGENT
} SATURATION_CURVE;
typedef struct
{
float drive;
SATURATION_CURVE curve;
} px_saturator;
// ----------------------------------------------------------------------------------------------------
static void px_saturator_initialize(px_saturator* saturator, SATURATION_CURVE curve);
static px_saturator* px_saturator_create(SATURATION_CURVE curve);
static void px_saturator_destroy(px_saturator* saturator);
static void px_saturator_set_drive(px_saturator* saturator, float drive);
static void px_saturator_set_curve(px_saturator* saturator, SATURATION_CURVE curve);
static void px_saturator_mono_process(px_saturator* saturator, float* input);
static void px_saturator_stereo_process(px_saturator* saturator, float* input_left, float* input_right);
static inline float px_saturate_arctangent(float input, float drive);
static inline float px_saturate_tangent(float input, float drive);
// ----------------------------------------------------------------------------------------------------
static void px_saturator_initialize(px_saturator* saturator, SATURATION_CURVE curve)
{
assert(saturator);
saturator->drive = 0.f;
saturator->curve = curve;
}
static px_saturator* px_saturator_create(SATURATION_CURVE curve)
{
px_saturator* saturator = (px_saturator*)px_malloc(sizeof(px_saturator));
if (saturator)
{
px_saturator_initialize(saturator, curve);
return saturator;
}
else return NULL;
}
static void px_saturator_destroy(px_saturator* saturator)
{
if (saturator)
px_free(saturator);
}
static void px_saturator_set_drive(px_saturator* saturator, float drive)
{
assert(saturator);
saturator->drive = drive;
}
static void px_saturator_set_curve(px_saturator* saturator, SATURATION_CURVE curve)
{
assert(saturator);
saturator->curve = curve;
}
static void px_saturator_mono_process(px_saturator* saturator, float* input)
{
px_assert(saturator, input);
switch (saturator->curve)
{
case ARCTANGENT:
*input = px_saturate_arctangent(*input, saturator->drive);
break;
case TANGENT:
*input = px_saturate_tangent(*input, saturator->drive);
break;
}
}
static void px_saturator_stereo_process(px_saturator* saturator, float* input_left, float* input_right)
{
px_assert(saturator, input_left, input_right);
switch (saturator->curve)
{
case ARCTANGENT:
*input_left = px_saturate_arctangent(*input_left, saturator->drive);
*input_right = px_saturate_arctangent(*input_right, saturator->drive);
break;
case TANGENT:
*input_left = px_saturate_tangent(*input_left, saturator->drive);
*input_right = px_saturate_tangent(*input_right, saturator->drive);
break;
}
}
static inline float px_saturate_arctangent(float input, float drive)
{
return atan(input * dB2lin(drive));
}
static inline float px_saturate_tangent(float input, float drive)
{
return tanh(input * dB2lin(drive));
}
#endif