~ubuntu-branches/ubuntu/utopic/lmms/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/fix-crash-on-close.patch/plugins/LadspaEffect/tap/tap_vibrato.c

  • Committer: Package Import Robot
  • Author(s): Israel Dahl
  • Date: 2014-04-30 18:49:37 UTC
  • mfrom: (1.1.15)
  • Revision ID: package-import@ubuntu.com-20140430184937-hozuuxonlbshciya
Tags: 1.0.1-src-0ubuntu1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*                                                     -*- linux-c -*-
2
 
    Copyright (C) 2004 Tom Szilagyi
3
 
 
4
 
    This program is free software; you can redistribute it and/or modify
5
 
    it under the terms of the GNU General Public License as published by
6
 
    the Free Software Foundation; either version 2 of the License, or
7
 
    (at your option) any later version.
8
 
 
9
 
    This program is distributed in the hope that it will be useful,
10
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
    GNU General Public License for more details.
13
 
 
14
 
    You should have received a copy of the GNU General Public License
15
 
    along with this program; if not, write to the Free Software
16
 
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
 
 
18
 
    $Id: tap_vibrato.c,v 1.3 2004/02/21 17:33:36 tszilagyi Exp $
19
 
*/
20
 
 
21
 
 
22
 
#include <stdio.h>
23
 
#include <stdlib.h>
24
 
#include <string.h>
25
 
#include <math.h>
26
 
 
27
 
#include <ladspa.h>
28
 
#include "tap_utils.h"
29
 
 
30
 
 
31
 
/* The Unique ID of the plugin: */
32
 
 
33
 
#define ID_MONO         2148
34
 
 
35
 
/* The port numbers for the plugin: */
36
 
 
37
 
#define FREQ            0
38
 
#define DEPTH           1
39
 
#define DRYLEVEL        2
40
 
#define WETLEVEL        3
41
 
#define LATENCY         4
42
 
#define INPUT           5
43
 
#define OUTPUT          6
44
 
 
45
 
/* Total number of ports */
46
 
 
47
 
 
48
 
#define PORTCOUNT_MONO   7
49
 
 
50
 
 
51
 
/*
52
 
 * This has to be bigger than 0.2f * sample_rate / (2*PI) for any sample rate.
53
 
 * At 192 kHz 6238 is needed so this should be enough.
54
 
 */
55
 
#define PM_DEPTH 6300
56
 
 
57
 
 
58
 
#define PM_FREQ 30.0f
59
 
 
60
 
 
61
 
#define COS_TABLE_SIZE 1024
62
 
LADSPA_Data cos_table[COS_TABLE_SIZE];
63
 
 
64
 
 
65
 
/* The structure used to hold port connection information and state */
66
 
 
67
 
typedef struct {
68
 
        LADSPA_Data * depth;
69
 
        LADSPA_Data * freq;
70
 
        LADSPA_Data * drylevel;
71
 
        LADSPA_Data * wetlevel;
72
 
        LADSPA_Data * latency;
73
 
        LADSPA_Data * input;
74
 
        LADSPA_Data * output;
75
 
 
76
 
        LADSPA_Data * ringbuffer;
77
 
        unsigned long buflen;
78
 
        unsigned long pos;
79
 
        LADSPA_Data phase;
80
 
 
81
 
        unsigned long sample_rate;
82
 
        LADSPA_Data run_adding_gain;
83
 
} Vibrato;
84
 
 
85
 
 
86
 
 
87
 
/* Construct a new plugin instance. */
88
 
LADSPA_Handle
89
 
instantiate_Vibrato(const LADSPA_Descriptor * Descriptor,
90
 
                    unsigned long             sample_rate) {
91
 
 
92
 
        LADSPA_Handle * ptr;
93
 
 
94
 
        if ((ptr = malloc(sizeof(Vibrato))) != NULL) {
95
 
                ((Vibrato *)ptr)->sample_rate = sample_rate;
96
 
                ((Vibrato *)ptr)->run_adding_gain = 1.0f;
97
 
 
98
 
                if ((((Vibrato *)ptr)->ringbuffer =
99
 
                     calloc(2 * PM_DEPTH, sizeof(LADSPA_Data))) == NULL)
100
 
                {
101
 
                        free(ptr);
102
 
                        return NULL;
103
 
                }
104
 
                ((Vibrato *)ptr)->buflen = ceil(0.2f * sample_rate / M_PI);
105
 
                ((Vibrato *)ptr)->pos = 0;
106
 
 
107
 
                return ptr;
108
 
        }
109
 
        return NULL;
110
 
}
111
 
 
112
 
 
113
 
void
114
 
activate_Vibrato(LADSPA_Handle Instance) {
115
 
 
116
 
        Vibrato * ptr = (Vibrato *)Instance;
117
 
        unsigned long i;
118
 
 
119
 
        for (i = 0; i < 2 * PM_DEPTH; i++)
120
 
                ptr->ringbuffer[i] = 0.0f;
121
 
 
122
 
        ptr->phase = 0.0f;
123
 
}
124
 
 
125
 
 
126
 
 
127
 
 
128
 
 
129
 
/* Connect a port to a data location. */
130
 
void 
131
 
connect_port_Vibrato(LADSPA_Handle Instance,
132
 
                     unsigned long Port,
133
 
                     LADSPA_Data * DataLocation) {
134
 
        
135
 
        Vibrato * ptr = (Vibrato *)Instance;
136
 
 
137
 
        switch (Port) {
138
 
        case DEPTH:
139
 
                ptr->depth = DataLocation;
140
 
                break;
141
 
        case FREQ:
142
 
                ptr->freq = DataLocation;
143
 
                break;
144
 
        case DRYLEVEL:
145
 
                ptr->drylevel = DataLocation;
146
 
                break;
147
 
        case WETLEVEL:
148
 
                ptr->wetlevel = DataLocation;
149
 
                break;
150
 
        case LATENCY:
151
 
                ptr->latency = DataLocation;
152
 
                *(ptr->latency) = ptr->buflen / 2;  /* IS THIS LEGAL? */
153
 
                break;
154
 
        case INPUT:
155
 
                ptr->input = DataLocation;
156
 
                break;
157
 
        case OUTPUT:
158
 
                ptr->output = DataLocation;
159
 
                break;
160
 
        }
161
 
}
162
 
 
163
 
 
164
 
 
165
 
void 
166
 
run_Vibrato(LADSPA_Handle Instance,
167
 
            unsigned long SampleCount) {
168
 
  
169
 
        Vibrato * ptr = (Vibrato *)Instance;
170
 
 
171
 
        LADSPA_Data freq = LIMIT(*(ptr->freq),0.0f,PM_FREQ);
172
 
        LADSPA_Data depth = 
173
 
                LIMIT(LIMIT(*(ptr->depth),0.0f,20.0f) * ptr->sample_rate / 200.0f / M_PI / freq,
174
 
                      0, ptr->buflen / 2);
175
 
        LADSPA_Data drylevel = db2lin(LIMIT(*(ptr->drylevel),-90.0f,20.0f));
176
 
        LADSPA_Data wetlevel = db2lin(LIMIT(*(ptr->wetlevel),-90.0f,20.0f));
177
 
        LADSPA_Data * input = ptr->input;
178
 
        LADSPA_Data * output = ptr->output;
179
 
 
180
 
        unsigned long sample_index;
181
 
        unsigned long sample_count = SampleCount;
182
 
 
183
 
        LADSPA_Data in = 0.0f;
184
 
        LADSPA_Data phase = 0.0f;
185
 
        LADSPA_Data fpos = 0.0f;
186
 
        LADSPA_Data n = 0.0f;
187
 
        LADSPA_Data rem = 0.0f;
188
 
        LADSPA_Data s_a, s_b;
189
 
 
190
 
 
191
 
        if (freq == 0.0f)
192
 
                depth = 0.0f;
193
 
 
194
 
        for (sample_index = 0; sample_index < sample_count; sample_index++) {
195
 
 
196
 
                in = *(input++);
197
 
 
198
 
                phase = COS_TABLE_SIZE * freq * sample_index / ptr->sample_rate + ptr->phase;
199
 
                while (phase >= COS_TABLE_SIZE)
200
 
                        phase -= COS_TABLE_SIZE;
201
 
 
202
 
                push_buffer(in, ptr->ringbuffer, ptr->buflen, &(ptr->pos));
203
 
 
204
 
                fpos = depth * (1.0f - cos_table[(unsigned long) phase]);
205
 
                n = floorf(fpos);
206
 
                rem = fpos - n;
207
 
 
208
 
                s_a = read_buffer(ptr->ringbuffer, ptr->buflen, ptr->pos, (unsigned long) n);
209
 
                s_b = read_buffer(ptr->ringbuffer, ptr->buflen, ptr->pos, (unsigned long) n + 1);
210
 
 
211
 
                *(output++) = wetlevel * ((1 - rem) * s_a + rem * s_b) +
212
 
                        drylevel * read_buffer(ptr->ringbuffer, ptr->buflen,
213
 
                                               ptr->pos, ptr->buflen / 2);
214
 
 
215
 
        }
216
 
 
217
 
        ptr->phase += COS_TABLE_SIZE * freq * sample_index / ptr->sample_rate;
218
 
        while (ptr->phase >= COS_TABLE_SIZE)
219
 
                ptr->phase -= COS_TABLE_SIZE;
220
 
 
221
 
        *(ptr->latency) = ptr->buflen / 2;
222
 
}
223
 
 
224
 
 
225
 
void
226
 
set_run_adding_gain_Vibrato(LADSPA_Handle Instance, LADSPA_Data gain) {
227
 
 
228
 
        Vibrato * ptr = (Vibrato *)Instance;
229
 
 
230
 
        ptr->run_adding_gain = gain;
231
 
}
232
 
 
233
 
 
234
 
 
235
 
void 
236
 
run_adding_Vibrato(LADSPA_Handle Instance,
237
 
                   unsigned long SampleCount) {
238
 
  
239
 
        Vibrato * ptr = (Vibrato *)Instance;
240
 
 
241
 
        LADSPA_Data freq = LIMIT(*(ptr->freq),0.0f,PM_FREQ);
242
 
        LADSPA_Data depth = 
243
 
                LIMIT(LIMIT(*(ptr->depth),0.0f,20.0f) * ptr->sample_rate / 200.0f / M_PI / freq,
244
 
                      0, ptr->buflen / 2);
245
 
        LADSPA_Data drylevel = db2lin(LIMIT(*(ptr->drylevel),-90.0f,20.0f));
246
 
        LADSPA_Data wetlevel = db2lin(LIMIT(*(ptr->wetlevel),-90.0f,20.0f));
247
 
        LADSPA_Data * input = ptr->input;
248
 
        LADSPA_Data * output = ptr->output;
249
 
 
250
 
        unsigned long sample_index;
251
 
        unsigned long sample_count = SampleCount;
252
 
 
253
 
        LADSPA_Data in = 0.0f;
254
 
        LADSPA_Data phase = 0.0f;
255
 
        LADSPA_Data fpos = 0.0f;
256
 
        LADSPA_Data n = 0.0f;
257
 
        LADSPA_Data rem = 0.0f;
258
 
        LADSPA_Data s_a, s_b;
259
 
 
260
 
 
261
 
        if (freq == 0.0f)
262
 
                depth = 0.0f;
263
 
 
264
 
        for (sample_index = 0; sample_index < sample_count; sample_index++) {
265
 
 
266
 
                in = *(input++);
267
 
 
268
 
                phase = COS_TABLE_SIZE * freq * sample_index / ptr->sample_rate + ptr->phase;
269
 
                while (phase >= COS_TABLE_SIZE)
270
 
                        phase -= COS_TABLE_SIZE;
271
 
 
272
 
                push_buffer(in, ptr->ringbuffer, ptr->buflen, &(ptr->pos));
273
 
 
274
 
                fpos = depth * (1.0f - cos_table[(unsigned long) phase]);
275
 
                n = floorf(fpos);
276
 
                rem = fpos - n;
277
 
 
278
 
                s_a = read_buffer(ptr->ringbuffer, ptr->buflen, ptr->pos, (unsigned long) n);
279
 
                s_b = read_buffer(ptr->ringbuffer, ptr->buflen, ptr->pos, (unsigned long) n + 1);
280
 
 
281
 
                *(output++) += ptr->run_adding_gain * wetlevel * ((1 - rem) * s_a + rem * s_b) +
282
 
                        drylevel * read_buffer(ptr->ringbuffer, ptr->buflen,
283
 
                                               ptr->pos, ptr->buflen / 2);
284
 
 
285
 
        }
286
 
 
287
 
        ptr->phase += COS_TABLE_SIZE * freq * sample_index / ptr->sample_rate;
288
 
        while (ptr->phase >= COS_TABLE_SIZE)
289
 
                ptr->phase -= COS_TABLE_SIZE;
290
 
 
291
 
        *(ptr->latency) = ptr->buflen / 2;
292
 
}
293
 
 
294
 
 
295
 
 
296
 
/* Throw away a Vibrato effect instance. */
297
 
void 
298
 
cleanup_Vibrato(LADSPA_Handle Instance) {
299
 
 
300
 
        Vibrato * ptr = (Vibrato *)Instance;
301
 
        free(ptr->ringbuffer);
302
 
        free(Instance);
303
 
}
304
 
 
305
 
 
306
 
 
307
 
LADSPA_Descriptor * mono_descriptor = NULL;
308
 
 
309
 
 
310
 
 
311
 
/* __attribute__((constructor)) _init() is called automatically when the plugin library is first
312
 
   loaded. */
313
 
void 
314
 
__attribute__((constructor)) _init() {
315
 
        
316
 
        int i;
317
 
        char ** port_names;
318
 
        LADSPA_PortDescriptor * port_descriptors;
319
 
        LADSPA_PortRangeHint * port_range_hints;
320
 
        
321
 
        if ((mono_descriptor = 
322
 
             (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor))) == NULL)
323
 
                exit(1);
324
 
        
325
 
        for (i = 0; i < COS_TABLE_SIZE; i++)
326
 
                cos_table[i] = cosf(i * 2.0f * M_PI / COS_TABLE_SIZE);
327
 
 
328
 
        mono_descriptor->UniqueID = ID_MONO;
329
 
        mono_descriptor->Label = strdup("tap_vibrato");
330
 
        mono_descriptor->Properties = LADSPA_PROPERTY_HARD_RT_CAPABLE;
331
 
        mono_descriptor->Name = strdup("TAP Vibrato");
332
 
        mono_descriptor->Maker = strdup("Tom Szilagyi");
333
 
        mono_descriptor->Copyright = strdup("GPL");
334
 
        mono_descriptor->PortCount = PORTCOUNT_MONO;
335
 
 
336
 
        if ((port_descriptors =
337
 
             (LADSPA_PortDescriptor *)calloc(PORTCOUNT_MONO, sizeof(LADSPA_PortDescriptor))) == NULL)
338
 
                exit(1);
339
 
 
340
 
        mono_descriptor->PortDescriptors = (const LADSPA_PortDescriptor *)port_descriptors;
341
 
        port_descriptors[DEPTH] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
342
 
        port_descriptors[FREQ] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
343
 
        port_descriptors[DRYLEVEL] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
344
 
        port_descriptors[WETLEVEL] = LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
345
 
        port_descriptors[LATENCY] = LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
346
 
        port_descriptors[INPUT] = LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
347
 
        port_descriptors[OUTPUT] = LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
348
 
 
349
 
        if ((port_names = 
350
 
             (char **)calloc(PORTCOUNT_MONO, sizeof(char *))) == NULL)
351
 
                exit(1);
352
 
 
353
 
        mono_descriptor->PortNames = (const char **)port_names;
354
 
        port_names[FREQ] = strdup("Frequency [Hz]");
355
 
        port_names[DEPTH] = strdup("Depth [%]");
356
 
        port_names[DRYLEVEL] = strdup("Dry Level [dB]");
357
 
        port_names[WETLEVEL] = strdup("Wet Level [dB]");
358
 
        port_names[LATENCY] = strdup("latency");
359
 
        port_names[INPUT] = strdup("Input");
360
 
        port_names[OUTPUT] = strdup("Output");
361
 
 
362
 
        if ((port_range_hints = 
363
 
             ((LADSPA_PortRangeHint *)calloc(PORTCOUNT_MONO, sizeof(LADSPA_PortRangeHint)))) == NULL)
364
 
                exit(1);
365
 
 
366
 
        mono_descriptor->PortRangeHints = (const LADSPA_PortRangeHint *)port_range_hints;
367
 
        port_range_hints[DEPTH].HintDescriptor = 
368
 
                (LADSPA_HINT_BOUNDED_BELOW |
369
 
                 LADSPA_HINT_BOUNDED_ABOVE |
370
 
                 LADSPA_HINT_DEFAULT_0);
371
 
        port_range_hints[FREQ].HintDescriptor = 
372
 
                (LADSPA_HINT_BOUNDED_BELOW |
373
 
                 LADSPA_HINT_BOUNDED_ABOVE |
374
 
                 LADSPA_HINT_DEFAULT_0);
375
 
        port_range_hints[DRYLEVEL].HintDescriptor = 
376
 
                (LADSPA_HINT_BOUNDED_BELOW |
377
 
                 LADSPA_HINT_BOUNDED_ABOVE |
378
 
                 LADSPA_HINT_DEFAULT_MINIMUM);
379
 
        port_range_hints[WETLEVEL].HintDescriptor = 
380
 
                (LADSPA_HINT_BOUNDED_BELOW |
381
 
                 LADSPA_HINT_BOUNDED_ABOVE |
382
 
                 LADSPA_HINT_DEFAULT_0);
383
 
        port_range_hints[LATENCY].HintDescriptor = 
384
 
                (LADSPA_HINT_BOUNDED_BELOW |
385
 
                 LADSPA_HINT_BOUNDED_ABOVE |
386
 
                 LADSPA_HINT_DEFAULT_MAXIMUM);
387
 
        port_range_hints[DEPTH].LowerBound = 0;
388
 
        port_range_hints[DEPTH].UpperBound = 20.0f;
389
 
        port_range_hints[FREQ].LowerBound = 0;
390
 
        port_range_hints[FREQ].UpperBound = PM_FREQ;
391
 
        port_range_hints[DRYLEVEL].LowerBound = -90.0f;
392
 
        port_range_hints[DRYLEVEL].UpperBound = +20.0f;
393
 
        port_range_hints[WETLEVEL].LowerBound = -90.0f;
394
 
        port_range_hints[WETLEVEL].UpperBound = +20.0f;
395
 
        port_range_hints[LATENCY].LowerBound = 0;
396
 
        port_range_hints[LATENCY].UpperBound = PM_DEPTH;
397
 
        port_range_hints[INPUT].HintDescriptor = 0;
398
 
        port_range_hints[OUTPUT].HintDescriptor = 0;
399
 
        mono_descriptor->instantiate = instantiate_Vibrato;
400
 
        mono_descriptor->connect_port = connect_port_Vibrato;
401
 
        mono_descriptor->activate = activate_Vibrato;
402
 
        mono_descriptor->run = run_Vibrato;
403
 
        mono_descriptor->run_adding = run_adding_Vibrato;
404
 
        mono_descriptor->set_run_adding_gain = set_run_adding_gain_Vibrato;
405
 
        mono_descriptor->deactivate = NULL;
406
 
        mono_descriptor->cleanup = cleanup_Vibrato;
407
 
}
408
 
 
409
 
 
410
 
void
411
 
delete_descriptor(LADSPA_Descriptor * descriptor) {
412
 
        unsigned long index;
413
 
        if (descriptor) {
414
 
                free((char *)descriptor->Label);
415
 
                free((char *)descriptor->Name);
416
 
                free((char *)descriptor->Maker);
417
 
                free((char *)descriptor->Copyright);
418
 
                free((LADSPA_PortDescriptor *)descriptor->PortDescriptors);
419
 
                for (index = 0; index < descriptor->PortCount; index++)
420
 
                        free((char *)(descriptor->PortNames[index]));
421
 
                free((char **)descriptor->PortNames);
422
 
                free((LADSPA_PortRangeHint *)descriptor->PortRangeHints);
423
 
                free(descriptor);
424
 
        }
425
 
}
426
 
 
427
 
 
428
 
/* __attribute__((destructor)) _fini() is called automatically when the library is unloaded. */
429
 
void
430
 
__attribute__((destructor)) _fini() {
431
 
        delete_descriptor(mono_descriptor);
432
 
}
433
 
 
434
 
 
435
 
/* Return a descriptor of the requested plugin type. */
436
 
const LADSPA_Descriptor * 
437
 
ladspa_descriptor(unsigned long Index) {
438
 
 
439
 
        switch (Index) {
440
 
        case 0:
441
 
                return mono_descriptor;
442
 
        default:
443
 
                return NULL;
444
 
        }
445
 
}