-
Notifications
You must be signed in to change notification settings - Fork 0
/
tap_tubewarmth.c
489 lines (378 loc) · 11.8 KB
/
tap_tubewarmth.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
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
/* -*- linux-c -*-
Copyright (C) 2004 Tom Szilagyi
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ladspa.h>
#include "tap_platform.h"
#include "tap_utils.h"
/* The Unique ID of the plugin: */
#define ID_MONO 2158
/* The port numbers for the plugin: */
#define DRIVE 0
#define BLEND 1
#define INPUT 2
#define OUTPUT 3
/* Total number of ports */
#define PORTCOUNT_MONO 4
/* The structure used to hold port connection information and state */
typedef struct {
LADSPA_Data * drive;
LADSPA_Data * blend;
LADSPA_Data * input;
LADSPA_Data * output;
LADSPA_Data prev_med;
LADSPA_Data prev_out;
LADSPA_Data rdrive;
LADSPA_Data rbdr;
LADSPA_Data kpa;
LADSPA_Data kpb;
LADSPA_Data kna;
LADSPA_Data knb;
LADSPA_Data ap;
LADSPA_Data an;
LADSPA_Data imr;
LADSPA_Data kc;
LADSPA_Data srct;
LADSPA_Data sq;
LADSPA_Data pwrq;
LADSPA_Data prev_drive;
LADSPA_Data prev_blend;
unsigned long sample_rate;
LADSPA_Data run_adding_gain;
} TubeWarmth;
/* Construct a new plugin instance. */
LADSPA_Handle
instantiate_TubeWarmth(const LADSPA_Descriptor * Descriptor,
unsigned long sample_rate) {
LADSPA_Handle * ptr;
if ((ptr = malloc(sizeof(TubeWarmth))) != NULL) {
((TubeWarmth *)ptr)->sample_rate = sample_rate;
((TubeWarmth *)ptr)->run_adding_gain = 1.0f;
((TubeWarmth *)ptr)->prev_med = 0.0f;
((TubeWarmth *)ptr)->prev_out = 0.0f;
((TubeWarmth *)ptr)->rdrive = 0.0f;
((TubeWarmth *)ptr)->rbdr = 0.0f;
((TubeWarmth *)ptr)->kpa = 0.0f;
((TubeWarmth *)ptr)->kpb = 0.0f;
((TubeWarmth *)ptr)->kna = 0.0f;
((TubeWarmth *)ptr)->knb = 0.0f;
((TubeWarmth *)ptr)->ap = 0.0f;
((TubeWarmth *)ptr)->an = 0.0f;
((TubeWarmth *)ptr)->imr = 0.0f;
((TubeWarmth *)ptr)->kc = 0.0f;
((TubeWarmth *)ptr)->srct = 0.0f;
((TubeWarmth *)ptr)->sq = 0.0f;
((TubeWarmth *)ptr)->pwrq = 0.0f;
/* These are out of band to force param recalc upon first run() */
((TubeWarmth *)ptr)->prev_drive = -1.0f;
((TubeWarmth *)ptr)->prev_blend = -11.0f;
return ptr;
}
return NULL;
}
/* Connect a port to a data location. */
void
connect_port_TubeWarmth(LADSPA_Handle Instance,
unsigned long Port,
LADSPA_Data * DataLocation) {
TubeWarmth * ptr = (TubeWarmth *)Instance;
switch (Port) {
case DRIVE:
ptr->drive = DataLocation;
break;
case BLEND:
ptr->blend = DataLocation;
break;
case INPUT:
ptr->input = DataLocation;
break;
case OUTPUT:
ptr->output = DataLocation;
break;
}
}
#define EPS 0.000000001f
static inline float
M(float x) {
if ((x > EPS) || (x < -EPS))
return x;
else
return 0.0f;
}
static inline float
D(float x) {
if (x > EPS)
return sqrt(x);
else if (x < -EPS)
return sqrt(-x);
else
return 0.0f;
}
void
run_TubeWarmth(LADSPA_Handle Instance,
unsigned long SampleCount) {
TubeWarmth * ptr = (TubeWarmth *)Instance;
LADSPA_Data * input = ptr->input;
LADSPA_Data * output = ptr->output;
LADSPA_Data drive = LIMIT(*(ptr->drive),0.1f,10.0f);
LADSPA_Data blend = LIMIT(*(ptr->blend),-10.0f,10.0f);
unsigned long sample_index;
unsigned long sample_count = SampleCount;
unsigned long sample_rate = ptr->sample_rate;
LADSPA_Data rdrive = ptr->rdrive;
LADSPA_Data rbdr = ptr->rbdr;
LADSPA_Data kpa = ptr->kpa;
LADSPA_Data kpb = ptr->kpb;
LADSPA_Data kna = ptr->kna;
LADSPA_Data knb = ptr->knb;
LADSPA_Data ap = ptr->ap;
LADSPA_Data an = ptr->an;
LADSPA_Data imr = ptr->imr;
LADSPA_Data kc = ptr->kc;
LADSPA_Data srct = ptr->srct;
LADSPA_Data sq = ptr->sq;
LADSPA_Data pwrq = ptr->pwrq;
LADSPA_Data prev_med;
LADSPA_Data prev_out;
LADSPA_Data in;
LADSPA_Data med;
LADSPA_Data out;
if ((ptr->prev_drive != drive) || (ptr->prev_blend != blend)) {
rdrive = 12.0f / drive;
rbdr = rdrive / (10.5f - blend) * 780.0f / 33.0f;
kpa = D(2.0f * (rdrive*rdrive) - 1.0f) + 1.0f;
kpb = (2.0f - kpa) / 2.0f;
ap = ((rdrive*rdrive) - kpa + 1.0f) / 2.0f;
kc = kpa / D(2.0f * D(2.0f * (rdrive*rdrive) - 1.0f) - 2.0f * rdrive*rdrive);
srct = (0.1f * sample_rate) / (0.1f * sample_rate + 1.0f);
sq = kc*kc + 1.0f;
knb = -1.0f * rbdr / D(sq);
kna = 2.0f * kc * rbdr / D(sq);
an = rbdr*rbdr / sq;
imr = 2.0f * knb + D(2.0f * kna + 4.0f * an - 1.0f);
pwrq = 2.0f / (imr + 1.0f);
ptr->prev_drive = drive;
ptr->prev_blend = blend;
}
for (sample_index = 0; sample_index < sample_count; sample_index++) {
in = *(input++);
prev_med = ptr->prev_med;
prev_out = ptr->prev_out;
if (in >= 0.0f) {
med = (D(ap + in * (kpa - in)) + kpb) * pwrq;
} else {
med = (D(an - in * (kna + in)) + knb) * pwrq * -1.0f;
}
out = srct * (med - prev_med + prev_out);
if (out < -1.0f)
out = -1.0f;
*(output++) = out;
ptr->prev_med = M(med);
ptr->prev_out = M(out);
}
ptr->rdrive = rdrive;
ptr->rbdr = rbdr;
ptr->kpa = kpa;
ptr->kpb = kpb;
ptr->kna = kna;
ptr->knb = knb;
ptr->ap = ap;
ptr->an = an;
ptr->imr = imr;
ptr->kc = kc;
ptr->srct = srct;
ptr->sq = sq;
ptr->pwrq = pwrq;
}
void
set_run_adding_gain_TubeWarmth(LADSPA_Handle Instance, LADSPA_Data gain) {
TubeWarmth * ptr = (TubeWarmth *)Instance;
ptr->run_adding_gain = gain;
}
void
run_adding_TubeWarmth(LADSPA_Handle Instance,
unsigned long SampleCount) {
TubeWarmth * ptr = (TubeWarmth *)Instance;
LADSPA_Data * input = ptr->input;
LADSPA_Data * output = ptr->output;
LADSPA_Data drive = LIMIT(*(ptr->drive),0.1f,10.0f);
LADSPA_Data blend = LIMIT(*(ptr->blend),-10.0f,10.0f);
unsigned long sample_index;
unsigned long sample_count = SampleCount;
unsigned long sample_rate = ptr->sample_rate;
LADSPA_Data rdrive = ptr->rdrive;
LADSPA_Data rbdr = ptr->rbdr;
LADSPA_Data kpa = ptr->kpa;
LADSPA_Data kpb = ptr->kpb;
LADSPA_Data kna = ptr->kna;
LADSPA_Data knb = ptr->knb;
LADSPA_Data ap = ptr->ap;
LADSPA_Data an = ptr->an;
LADSPA_Data imr = ptr->imr;
LADSPA_Data kc = ptr->kc;
LADSPA_Data srct = ptr->srct;
LADSPA_Data sq = ptr->sq;
LADSPA_Data pwrq = ptr->pwrq;
LADSPA_Data prev_med;
LADSPA_Data prev_out;
LADSPA_Data in;
LADSPA_Data med;
LADSPA_Data out;
if ((ptr->prev_drive != drive) || (ptr->prev_blend != blend)) {
rdrive = 12.0f / drive;
rbdr = rdrive / (10.5f - blend) * 780.0f / 33.0f;
kpa = D(2.0f * (rdrive*rdrive) - 1.0f) + 1.0f;
kpb = (2.0f - kpa) / 2.0f;
ap = ((rdrive*rdrive) - kpa + 1.0f) / 2.0f;
kc = kpa / D(2.0f * D(2.0f * (rdrive*rdrive) - 1.0f) - 2.0f * rdrive*rdrive);
srct = (0.1f * sample_rate) / (0.1f * sample_rate + 1.0f);
sq = kc*kc + 1.0f;
knb = -1.0f * rbdr / D(sq);
kna = 2.0f * kc * rbdr / D(sq);
an = rbdr*rbdr / sq;
imr = 2.0f * knb + D(2.0f * kna + 4.0f * an - 1.0f);
pwrq = 2.0f / (imr + 1.0f);
ptr->prev_drive = drive;
ptr->prev_blend = blend;
}
for (sample_index = 0; sample_index < sample_count; sample_index++) {
in = *(input++);
prev_med = ptr->prev_med;
prev_out = ptr->prev_out;
if (in >= 0.0f) {
med = (D(ap + in * (kpa - in)) + kpb) * pwrq;
} else {
med = (D(an - in * (kna + in)) + knb) * pwrq * -1.0f;
}
out = srct * (med - prev_med + prev_out);
if (out < -1.0f)
out = -1.0f;
*(output++) += out * ptr->run_adding_gain;
ptr->prev_med = M(med);
ptr->prev_out = M(out);
}
ptr->rdrive = rdrive;
ptr->rbdr = rbdr;
ptr->kpa = kpa;
ptr->kpb = kpb;
ptr->kna = kna;
ptr->knb = knb;
ptr->ap = ap;
ptr->an = an;
ptr->imr = imr;
ptr->kc = kc;
ptr->srct = srct;
ptr->sq = sq;
ptr->pwrq = pwrq;
}
/* Throw away a TubeWarmth effect instance. */
void
cleanup_TubeWarmth(LADSPA_Handle Instance) {
free(Instance);
}
LADSPA_Descriptor * mono_descriptor = NULL;
/* tap_init() is called automatically when the plugin library is first
loaded. */
void
__CONSTRUCTOR tap_init() {
char ** port_names;
LADSPA_PortDescriptor * port_descriptors;
LADSPA_PortRangeHint * port_range_hints;
if ((mono_descriptor =
(LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor))) == NULL)
exit(1);
mono_descriptor->UniqueID = ID_MONO;
mono_descriptor->Label = strdup("tap_tubewarmth");
mono_descriptor->Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE;
mono_descriptor->Name = strdup("TAP TubeWarmth");
mono_descriptor->Maker = strdup("Tom Szilagyi");
mono_descriptor->Copyright = strdup("GPL");
mono_descriptor->PortCount = PORTCOUNT_MONO;
if ((port_descriptors =
(LADSPA_PortDescriptor *)calloc(PORTCOUNT_MONO, sizeof(LADSPA_PortDescriptor))) == NULL)
exit(1);
mono_descriptor->PortDescriptors = (const LADSPA_PortDescriptor *)port_descriptors;
port_descriptors[DRIVE] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
port_descriptors[BLEND] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
port_descriptors[INPUT] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
port_descriptors[OUTPUT] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
if ((port_names =
(char **)calloc(PORTCOUNT_MONO, sizeof(char *))) == NULL)
exit(1);
mono_descriptor->PortNames = (const char **)port_names;
port_names[DRIVE] = strdup("Drive");
port_names[BLEND] = strdup("Tape--Tube Blend");
port_names[INPUT] = strdup("Input");
port_names[OUTPUT] = strdup("Output");
if ((port_range_hints =
((LADSPA_PortRangeHint *)calloc(PORTCOUNT_MONO, sizeof(LADSPA_PortRangeHint)))) == NULL)
exit(1);
mono_descriptor->PortRangeHints = (const LADSPA_PortRangeHint *)port_range_hints;
port_range_hints[DRIVE].HintDescriptor =
(LADSPA_HINT_BOUNDED_BELOW |
LADSPA_HINT_BOUNDED_ABOVE |
LADSPA_HINT_DEFAULT_LOW);
port_range_hints[BLEND].HintDescriptor =
(LADSPA_HINT_BOUNDED_BELOW |
LADSPA_HINT_BOUNDED_ABOVE |
LADSPA_HINT_DEFAULT_MAXIMUM);
port_range_hints[DRIVE].LowerBound = 0.1f;
port_range_hints[DRIVE].UpperBound = 10.0f;
port_range_hints[BLEND].LowerBound = -10.0f;
port_range_hints[BLEND].UpperBound = 10.0f;
port_range_hints[INPUT].HintDescriptor = 0;
port_range_hints[OUTPUT].HintDescriptor = 0;
mono_descriptor->instantiate = instantiate_TubeWarmth;
mono_descriptor->connect_port = connect_port_TubeWarmth;
mono_descriptor->activate = NULL;
mono_descriptor->run = run_TubeWarmth;
mono_descriptor->run_adding = run_adding_TubeWarmth;
mono_descriptor->set_run_adding_gain = set_run_adding_gain_TubeWarmth;
mono_descriptor->deactivate = NULL;
mono_descriptor->cleanup = cleanup_TubeWarmth;
}
void
delete_descriptor(LADSPA_Descriptor * descriptor) {
unsigned long index;
if (descriptor) {
free((char *)descriptor->Label);
free((char *)descriptor->Name);
free((char *)descriptor->Maker);
free((char *)descriptor->Copyright);
free((LADSPA_PortDescriptor *)descriptor->PortDescriptors);
for (index = 0; index < descriptor->PortCount; index++)
free((char *)(descriptor->PortNames[index]));
free((char **)descriptor->PortNames);
free((LADSPA_PortRangeHint *)descriptor->PortRangeHints);
free(descriptor);
}
}
/* tap_fini() is called automatically when the library is unloaded. */
void
__DESTRUCTOR tap_fini() {
delete_descriptor(mono_descriptor);
}
/* Return a descriptor of the requested plugin type. */
const LADSPA_Descriptor *
ladspa_descriptor(unsigned long Index) {
switch (Index) {
case 0:
return mono_descriptor;
default:
return NULL;
}
}
__INIT_FINI(tap_init, tap_fini);