-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpx_clip.h
143 lines (123 loc) · 2.88 KB
/
px_clip.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
#include "px_globals.h"
#ifndef PX_CLIP_H
#define PX_CLIP_H
typedef enum
{
HARD,
SOFT, // quintic
SMOOTH // arctangent
} CLIP_TYPE;
typedef struct
{
CLIP_TYPE type;
} px_clipper;
// -----------------------------------------------------------------------------------------
// api
static px_clipper* px_clipper_create();
static void px_clipper_destroy(px_clipper* clipper);
static void px_clipper_initialize(px_clipper* clipper);
static void px_clipper_set_type(px_clipper* clipper, CLIP_TYPE in_type);
static void px_clipper_mono_process(px_clipper* clipper, float* input);
static void px_clipper_stereo_process(px_clipper* clipper, float* input_left, float* input_right);
// ---------------------------------------------------------------------------------------------
// inline functions
static inline float hard_clip(float input);
static inline float quintic_clip(float input);
static inline float arctangent_clip(float input);
// ---------------------------------------------------------------------------------------------
static px_clipper* px_clipper_create()
{
px_clipper* clipper = (px_clipper*)malloc(sizeof(px_clipper));
px_clipper_initialize(clipper);
return clipper;
}
static void px_clipper_destroy(px_clipper* clipper)
{
if (clipper)
free(clipper);
}
static void px_clipper_initialize(px_clipper* clipper)
{
assert(clipper);
clipper->type = HARD;
}
static void px_clipper_set_type(px_clipper* clipper, CLIP_TYPE in_type)
{
assert(clipper);
clipper->type = in_type;
}
static void px_clipper_mono_process(px_clipper* clipper, float* input)
{
px_assert(clipper, input);
printf("%d", clipper->type);
switch (clipper->type)
{
case HARD:
{
*input = hard_clip(*input);
break;
}
case SOFT:
{
*input = quintic_clip(*input);
break;
}
case SMOOTH:
{
*input = arctangent_clip(*input);
break;
}
default:
{
printf("clipper uninitialized");
break;
}
}
}
static void px_clipper_stereo_process(px_clipper* clipper, float* input_left, float* input_right)
{
px_assert(clipper, input_left, input_right);
printf("%d", clipper->type);
switch (clipper->type)
{
case HARD:
{
*input_left = hard_clip(*input_left);
*input_right = hard_clip(*input_right);
break;
}
case SOFT:
{
*input_left = quintic_clip(*input_left);
*input_right = quintic_clip(*input_right);
break;
}
case SMOOTH:
{
*input_left = arctangent_clip(*input_left);
*input_right = arctangent_clip(*input_right);
break;
}
default:
{
printf("clipper uninitialized");
break;
}
}
}
static inline float hard_clip(float input)
{
return sgn(input) * fmin(fabs(input), 1.0f);
}
static inline float quintic_clip(float input)
{
if (fabs(input) < 1.25f)
return input - (256.0f / 3125.0f) * powf(input, 5.0f);
else
return sgn(input) * 1.0f;
}
static inline float arctangent_clip(float input)
{
return (2.0f / PI) * atan((1.6f * 0.6f) * input);
}
#endif