-
Notifications
You must be signed in to change notification settings - Fork 0
/
phaser.c
194 lines (179 loc) · 4.49 KB
/
phaser.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
#include <ladspa.h>
#define _GNU_SOURCE
#include <math.h>
#include <complex.h>
#include <stdlib.h>
#define N_FILTERS 8
enum {
PORT_IN,
PORT_OUT,
PORT_WET,
PORT_FREQUENCY,
PORT_NFILTERS,
PORT_LFO_FREQUENCY,
PORT_LFO_AMOUNT,
PORT_NPORTS
};
typedef struct
{
LADSPA_Data m_e_c;
LADSPA_Data m_x_last;
LADSPA_Data m_alpha;
} Filter;
static void FilterCoefficients( Filter *p_filter, LADSPA_Data p_frequency, unsigned long p_sample_rate )
{
LADSPA_Data l_T = (LADSPA_Data)1.0f/p_sample_rate;
LADSPA_Data l_tau = 1.0f/(2.0f*M_PIf*p_frequency);
p_filter->m_alpha = l_tau/(l_T + l_tau);
}
static void FilterInit( Filter *p_filter )
{
p_filter->m_e_c = 0.0;
p_filter->m_x_last = 0.0;
}
static LADSPA_Data FilterEvaluate( Filter *p_filter, LADSPA_Data p_in )
{
p_filter->m_e_c = p_filter->m_alpha*( p_filter->m_e_c + p_in - p_filter->m_x_last );
LADSPA_Data l_v_o = 2.0f*p_filter->m_e_c - p_in;
p_filter->m_x_last = p_in;
return -l_v_o;
}
typedef struct {
LADSPA_Data m_sample_rate;
LADSPA_Data *m_pport[PORT_NPORTS];
Filter m_filters[N_FILTERS];
LADSPA_Data m_lfo_theta;
} Phaser_Data;
static LADSPA_Handle Phaser_instantiate(
const struct _LADSPA_Descriptor *p_pDescriptor,
unsigned long p_sample_rate )
{
Phaser_Data *l_pPhaser = malloc( sizeof(Phaser_Data) );
if(l_pPhaser){
l_pPhaser->m_sample_rate = (float)p_sample_rate;
l_pPhaser->m_lfo_theta = 0.0f;
unsigned long l_f;
for( l_f=0;l_f<N_FILTERS;l_f++){
FilterInit( &l_pPhaser->m_filters[l_f] );
}
}
return (LADSPA_Handle)l_pPhaser;
}
static void Phaser_connect_port(
LADSPA_Handle p_instance,
unsigned long p_port,
LADSPA_Data *p_pdata )
{
Phaser_Data *l_pPhaser = (Phaser_Data*)p_instance;
l_pPhaser->m_pport[p_port] = p_pdata;
}
static void Phaser_run( LADSPA_Handle p_instance, unsigned long p_sample_count )
{
Phaser_Data *l_pPhaser = (Phaser_Data*)p_instance;
unsigned long l_f;
LADSPA_Data l_wet_gain = *l_pPhaser->m_pport[PORT_WET];
LADSPA_Data *l_psrc = l_pPhaser->m_pport[PORT_IN];
LADSPA_Data *l_pdst = l_pPhaser->m_pport[PORT_OUT];
unsigned long l_nfilters = (unsigned long)floorf(*l_pPhaser->m_pport[PORT_NFILTERS]);
unsigned long l_sample;
LADSPA_Data l_freq0 = *l_pPhaser->m_pport[PORT_FREQUENCY];
LADSPA_Data l_lfo_amount = *l_pPhaser->m_pport[PORT_LFO_AMOUNT];
LADSPA_Data l_dtheta = 2.0f*M_PIf* *l_pPhaser->m_pport[PORT_LFO_FREQUENCY] / l_pPhaser->m_sample_rate;
for( l_sample=0;l_sample<p_sample_count;l_sample++){
LADSPA_Data l_out = *l_psrc;
Filter *l_pFilter = l_pPhaser->m_filters;
LADSPA_Data l_lfo_freq = l_freq0 + (0.5f+0.5f*sinf(l_pPhaser->m_lfo_theta))*l_lfo_amount;
for(l_f=0;l_f<l_nfilters;l_f++,l_pFilter++){
FilterCoefficients(
l_pFilter,
l_lfo_freq,
l_pPhaser->m_sample_rate);
l_out = FilterEvaluate( l_pFilter, l_out );
}
*l_pdst = (l_out*l_wet_gain + *l_psrc)*0.5f;
l_pdst++;
l_psrc++;
l_pPhaser->m_lfo_theta += l_dtheta;
if(l_pPhaser->m_lfo_theta >= 2.0f*M_PIf)
l_pPhaser->m_lfo_theta-=2.0f*M_PIf;
}
}
static void Phaser_cleanup( LADSPA_Handle p_instance )
{
free( p_instance );
}
static LADSPA_PortDescriptor Phaser_PortDescriptors[]=
{
LADSPA_PORT_INPUT|LADSPA_PORT_AUDIO,
LADSPA_PORT_OUTPUT|LADSPA_PORT_AUDIO,
LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL,
LADSPA_PORT_INPUT|LADSPA_PORT_CONTROL
};
static const char* Phaser_PortNames[]=
{
"Input",
"Output",
"Wet",
"Frequency",
"N stages",
"LFO Frequency",
"LFO Amount"
};
static LADSPA_PortRangeHint Phaser_PortRangeHints[]=
{
{0, 0.0, 0.0},
{0, 0.0, 0.0},
{ LADSPA_HINT_BOUNDED_BELOW|
LADSPA_HINT_BOUNDED_ABOVE|
LADSPA_HINT_DEFAULT_MAXIMUM,
-1.0,1.0
},
{ LADSPA_HINT_BOUNDED_BELOW|
LADSPA_HINT_BOUNDED_ABOVE|
LADSPA_HINT_LOGARITHMIC|
LADSPA_HINT_DEFAULT_LOW,
10.0, 5000.0
},
{ LADSPA_HINT_BOUNDED_BELOW|
LADSPA_HINT_BOUNDED_ABOVE|
LADSPA_HINT_INTEGER|
LADSPA_HINT_DEFAULT_LOW,
1.0,N_FILTERS
},
{ LADSPA_HINT_BOUNDED_BELOW|
LADSPA_HINT_BOUNDED_ABOVE|
LADSPA_HINT_LOGARITHMIC|
LADSPA_HINT_DEFAULT_MIDDLE,
0.001, 10.0
},
{ LADSPA_HINT_BOUNDED_BELOW|
LADSPA_HINT_BOUNDED_ABOVE|
LADSPA_HINT_DEFAULT_LOW,
0.0, 5000.0
}
};
LADSPA_Descriptor Phaser_Descriptor =
{
5813,
"phaser",
LADSPA_PROPERTY_HARD_RT_CAPABLE,
"Phaser circuit simulator",
"Timothy William Krause",
"None",
PORT_NPORTS,
Phaser_PortDescriptors,
Phaser_PortNames,
Phaser_PortRangeHints,
NULL,
Phaser_instantiate,
Phaser_connect_port,
NULL,
Phaser_run,
NULL,
NULL,
NULL,
Phaser_cleanup
};