/
rtbuf_signal.c
362 lines (324 loc) · 13 KB
/
rtbuf_signal.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
/*
* Copyright 2018 Thomas de Grivel <thoxdg@gmail.com> +33614550127
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <math.h>
#include <stdio.h>
#include <strings.h>
#include "rtbuf.h"
#include "rtbuf_lib.h"
#include "rtbuf_signal.h"
#include "rtbuf_signal_type.h"
const double g_rtbuf_signal_sample_zero = 0.0;
const double g_rtbuf_signal_sample_half = 0.5;
const double g_rtbuf_signal_sample_one = 1.0;
const double g_rtbuf_signal_default_frequency = 220.0;
s_rtbuf_lib_proc_in g_rtbuf_signal_bandpass2_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ "qfactor", RTBUF_SIGNAL_TYPE, 1.0, 0.0, 1000.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_bandpass2_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_delay_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "delay", RTBUF_SIGNAL_TYPE, 0.4, 0.0, RTBUF_SIGNAL_DELAY_MAX },
{ "feedback", RTBUF_SIGNAL_TYPE, 0.5, 0.0, 1.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_delay_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "in", RTBUF_SIGNAL_DELAY_TYPE },
{ "pos", "unsigned int" },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_flanger_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "frequency", RTBUF_SIGNAL_TYPE, 0.5, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ "amplitude", RTBUF_SIGNAL_TYPE, 0.02, 0.0, 1.0 },
{ "delay", RTBUF_SIGNAL_TYPE, 0.0001, 0.0, 1.0 },
{ "feedback", RTBUF_SIGNAL_TYPE, 0.01, 0.0, 1.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_flanger_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "phase", "double" },
{ "in", RTBUF_SIGNAL_FLANGER_TYPE },
{ "pos", "unsigned int" },
{ "ds", "unsigned int" },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_hipass_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 8000.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_hipass_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_hipass2_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 8000.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_hipass2_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_hipass3_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 8000.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_hipass3_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_hipass4_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 8000.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_hipass4_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_hipass5_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_hipass5_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x5", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y5", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_lowpass_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_lowpass2_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass2_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_lowpass3_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass3_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_lowpass4_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass4_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_lowpass5_in[] = {
{ "signal", RTBUF_SIGNAL_TYPE, 0.0, -1.0, 1.0 },
{ "cutoff", RTBUF_SIGNAL_TYPE, 400.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_lowpass5_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "x1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "x5", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y1", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y2", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y3", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y4", RTBUF_SIGNAL_SAMPLE_TYPE },
{ "y5", RTBUF_SIGNAL_SAMPLE_TYPE },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_sinus_in[] = {
{ "frequency", RTBUF_SIGNAL_TYPE, 220.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ "amplitude", RTBUF_SIGNAL_TYPE, 1.0, 0.0, 1.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_sinus_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "phase", "double" },
{ 0, 0 } };
s_rtbuf_lib_proc_in g_rtbuf_signal_square_in[] = {
{ "frequency", RTBUF_SIGNAL_TYPE, 220.0, 0.0, RTBUF_SIGNAL_SAMPLERATE / 2.0 },
{ "amplitude", RTBUF_SIGNAL_TYPE, 1.0, 0.0, 1.0 },
{ "pulse", RTBUF_SIGNAL_TYPE, 0.5, 0.0, 1.0 },
{ 0, 0, 0.0, 0.0, 0.0 } };
s_rtbuf_lib_proc_out g_rtbuf_signal_square_out[] = {
{ "signal", RTBUF_SIGNAL_TYPE },
{ "phase", "double" },
{ 0, 0 } };
const char *rtbuf_lib_name = "signal";
unsigned long rtbuf_lib_ver = RTBUF_LIB_VER;
s_rtbuf_lib_proc rtbuf_lib_proc[] = {
{ "bandpass2", rtbuf_signal_bandpass2, rtbuf_signal_bandpass2_start, 0,
g_rtbuf_signal_bandpass2_in, g_rtbuf_signal_bandpass2_out },
{ "delay", rtbuf_signal_delay, rtbuf_signal_delay_start, 0,
g_rtbuf_signal_delay_in, g_rtbuf_signal_delay_out },
{ "flanger", rtbuf_signal_flanger, rtbuf_signal_flanger_start, 0,
g_rtbuf_signal_flanger_in, g_rtbuf_signal_flanger_out },
{ "hipass", rtbuf_signal_hipass, rtbuf_signal_hipass_start, 0,
g_rtbuf_signal_hipass_in, g_rtbuf_signal_hipass_out },
{ "hipass2", rtbuf_signal_hipass2, rtbuf_signal_hipass2_start, 0,
g_rtbuf_signal_hipass2_in, g_rtbuf_signal_hipass2_out },
{ "hipass3", rtbuf_signal_hipass3, rtbuf_signal_hipass3_start, 0,
g_rtbuf_signal_hipass3_in, g_rtbuf_signal_hipass3_out },
{ "hipass4", rtbuf_signal_hipass4, rtbuf_signal_hipass4_start, 0,
g_rtbuf_signal_hipass4_in, g_rtbuf_signal_hipass4_out },
{ "hipass5", rtbuf_signal_hipass5, rtbuf_signal_hipass5_start, 0,
g_rtbuf_signal_hipass5_in, g_rtbuf_signal_hipass5_out },
{ "lowpass", rtbuf_signal_lowpass, rtbuf_signal_lowpass_start, 0,
g_rtbuf_signal_lowpass_in, g_rtbuf_signal_lowpass_out },
{ "lowpass2", rtbuf_signal_lowpass2, rtbuf_signal_lowpass2_start, 0,
g_rtbuf_signal_lowpass2_in, g_rtbuf_signal_lowpass2_out },
{ "lowpass3", rtbuf_signal_lowpass3, rtbuf_signal_lowpass3_start, 0,
g_rtbuf_signal_lowpass3_in, g_rtbuf_signal_lowpass3_out },
{ "lowpass4", rtbuf_signal_lowpass4, rtbuf_signal_lowpass4_start, 0,
g_rtbuf_signal_lowpass4_in, g_rtbuf_signal_lowpass4_out },
{ "lowpass5", rtbuf_signal_lowpass5, rtbuf_signal_lowpass5_start, 0,
g_rtbuf_signal_lowpass5_in, g_rtbuf_signal_lowpass5_out },
{ "sinus", rtbuf_signal_sinus, rtbuf_signal_sinus_start, 0,
g_rtbuf_signal_sinus_in, g_rtbuf_signal_sinus_out },
{ "square", rtbuf_signal_square, rtbuf_signal_square_start, 0,
g_rtbuf_signal_square_in, g_rtbuf_signal_square_out },
{ 0, 0, 0, 0, 0, 0 } };
void rtbuf_signal_zero (t_rtbuf_signal_sample *signal)
{
assert(signal);
bzero(signal, sizeof(t_rtbuf_signal));
}
t_rtbuf_signal_sample
rtbuf_signal_sample (s_rtbuf *rtb,
unsigned int in,
t_rtbuf_signal_sample default_value)
{
s_rtbuf_binding *v;
assert(rtb);
assert(rtb->proc);
assert(in < rtb->proc->in_n);
v = &rtb->in[in];
if (v->rtb >= 0) {
s_rtbuf *src;
s_rtbuf_proc_out *out;
assert(v->rtb < RTBUF_MAX);
src = &g_rtbuf[v->rtb];
assert(src->data);
assert(src->proc);
assert(v->out < src->proc->out_n);
out = &src->proc->out[v->out];
assert(out->type);
if (out->type->t.bits >= sizeof(t_rtbuf_signal_sample) * 8) {
t_rtbuf_signal_sample *sample = (t_rtbuf_signal_sample*)
(src->data + out->offset);
return *sample;
}
}
return default_value;
}
double rtbuf_signal_sample_from_sample (const double *signal,
unsigned int i)
{
(void) i;
assert(signal);
return *signal;
}
double rtbuf_signal_sample_from_signal (const double *signal,
unsigned int i)
{
assert(signal);
assert(i < RTBUF_SIGNAL_SAMPLES);
return signal[i];
}
void rtbuf_signal_fun (s_rtbuf *rtb,
unsigned int in,
s_rtbuf_signal_fun *rsf)
{
s_rtbuf_binding *v;
const double *default_value;
assert(rtb);
assert(rtb->proc);
assert(in < rtb->proc->in_n);
assert(rsf);
default_value = &rtb->proc->in[in].def;
rsf->signal = default_value;
rsf->sample_fun = rtbuf_signal_sample_from_sample;
v = &rtb->in[in];
if (v->rtb >= 0) {
s_rtbuf *dest;
s_rtbuf_proc_out *out;
assert(v->rtb < RTBUF_MAX);
dest = &g_rtbuf[v->rtb];
assert(dest->data);
assert(dest->proc);
assert(v->out < dest->proc->out_n);
out = &dest->proc->out[v->out];
assert(out->type);
if (out->type->t.bits >= sizeof(t_rtbuf_signal_sample) * 8)
rsf->signal = (double*)(dest->data + out->offset);
if (out->type->t.bits >= sizeof(t_rtbuf_signal) * 8)
rsf->sample_fun = rtbuf_signal_sample_from_signal;
}
}