~ubuntu-branches/ubuntu/raring/lmms/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/ladspa_effect/swh/multivoice_chorus_1201.c

  • Committer: Charlie Smotherman
  • Date: 2012-12-05 22:08:38 UTC
  • mfrom: (33.1.7 lmms_0.4.13)
  • Revision ID: cjsmo@cableone.net-20121205220838-09pjfzew9m5023hr
* New  Upstream release.
  - Minor tweaking to ZynAddSubFX, CALF, SWH plugins  and Stefan Fendt's RC
    filters.
  - Added UI fixes: Magnentic effect of knobs and Piano-roll fixes
  - Updated German localization and copyright year
* debian/lmms-common.install:
  - added /usr/share/applications so the lmms.desktop file will correctly
    install (LP: #863366)
  - This should also fix the Software Center not displaying lmms in sound
    and video (LP: #824231)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdlib.h>
 
2
#include <string.h>
 
3
#ifndef WIN32
 
4
#include "config.h"
 
5
#endif
 
6
 
 
7
#ifdef ENABLE_NLS
 
8
#include <libintl.h>
 
9
#endif
 
10
 
 
11
#define         _ISOC9X_SOURCE  1
 
12
#define         _ISOC99_SOURCE  1
 
13
#define         __USE_ISOC99    1
 
14
#define         __USE_ISOC9X    1
 
15
 
 
16
#include <math.h>
 
17
 
 
18
#include "ladspa.h"
 
19
 
 
20
#ifdef WIN32
 
21
#define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
 
22
int bIsFirstTime = 1; 
 
23
void _init(); // forward declaration
 
24
#else
 
25
#define _WINDOWS_DLL_EXPORT_ 
 
26
#endif
 
27
 
 
28
#line 9 "multivoice_chorus_1201.xml"
 
29
 
 
30
#include "ladspa-util.h"
 
31
#define MAX_LAWS 7
 
32
 
 
33
#define MULTIVOICECHORUS_VOICES        0
 
34
#define MULTIVOICECHORUS_DELAY_BASE    1
 
35
#define MULTIVOICECHORUS_VOICE_SPREAD  2
 
36
#define MULTIVOICECHORUS_DETUNE        3
 
37
#define MULTIVOICECHORUS_LAW_FREQ      4
 
38
#define MULTIVOICECHORUS_ATTENDB       5
 
39
#define MULTIVOICECHORUS_INPUT         6
 
40
#define MULTIVOICECHORUS_OUTPUT        7
 
41
 
 
42
static LADSPA_Descriptor *multivoiceChorusDescriptor = NULL;
 
43
 
 
44
typedef struct {
 
45
        LADSPA_Data *voices;
 
46
        LADSPA_Data *delay_base;
 
47
        LADSPA_Data *voice_spread;
 
48
        LADSPA_Data *detune;
 
49
        LADSPA_Data *law_freq;
 
50
        LADSPA_Data *attendb;
 
51
        LADSPA_Data *input;
 
52
        LADSPA_Data *output;
 
53
        long         count;
 
54
        unsigned int delay_mask;
 
55
        unsigned int delay_pos;
 
56
        unsigned int delay_size;
 
57
        float *      delay_tbl;
 
58
        float *      dp_curr;
 
59
        float *      dp_targ;
 
60
        int          last_law_p;
 
61
        int          law_pos;
 
62
        int          law_roll;
 
63
        int          max_law_p;
 
64
        float *      next_peak_amp;
 
65
        unsigned int *next_peak_pos;
 
66
        float *      prev_peak_amp;
 
67
        unsigned int *prev_peak_pos;
 
68
        long         sample_rate;
 
69
        LADSPA_Data run_adding_gain;
 
70
} MultivoiceChorus;
 
71
 
 
72
_WINDOWS_DLL_EXPORT_
 
73
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
 
74
 
 
75
#ifdef WIN32
 
76
        if (bIsFirstTime) {
 
77
                _init();
 
78
                bIsFirstTime = 0;
 
79
        }
 
80
#endif
 
81
        switch (index) {
 
82
        case 0:
 
83
                return multivoiceChorusDescriptor;
 
84
        default:
 
85
                return NULL;
 
86
        }
 
87
}
 
88
 
 
89
static void activateMultivoiceChorus(LADSPA_Handle instance) {
 
90
        MultivoiceChorus *plugin_data = (MultivoiceChorus *)instance;
 
91
        long count = plugin_data->count;
 
92
        unsigned int delay_mask = plugin_data->delay_mask;
 
93
        unsigned int delay_pos = plugin_data->delay_pos;
 
94
        unsigned int delay_size = plugin_data->delay_size;
 
95
        float *delay_tbl = plugin_data->delay_tbl;
 
96
        float *dp_curr = plugin_data->dp_curr;
 
97
        float *dp_targ = plugin_data->dp_targ;
 
98
        int last_law_p = plugin_data->last_law_p;
 
99
        int law_pos = plugin_data->law_pos;
 
100
        int law_roll = plugin_data->law_roll;
 
101
        int max_law_p = plugin_data->max_law_p;
 
102
        float *next_peak_amp = plugin_data->next_peak_amp;
 
103
        unsigned int *next_peak_pos = plugin_data->next_peak_pos;
 
104
        float *prev_peak_amp = plugin_data->prev_peak_amp;
 
105
        unsigned int *prev_peak_pos = plugin_data->prev_peak_pos;
 
106
        long sample_rate = plugin_data->sample_rate;
 
107
#line 46 "multivoice_chorus_1201.xml"
 
108
        memset(delay_tbl, 0, sizeof(float) * delay_size);
 
109
        memset(prev_peak_pos, 0, sizeof(unsigned int) * MAX_LAWS);
 
110
        memset(next_peak_pos, 0, sizeof(unsigned int) * MAX_LAWS);
 
111
        memset(prev_peak_amp, 0, sizeof(float) * MAX_LAWS);
 
112
        memset(next_peak_amp, 0, sizeof(float) * MAX_LAWS);
 
113
        memset(dp_targ, 0, sizeof(float) * MAX_LAWS);
 
114
        memset(dp_curr, 0, sizeof(float) * MAX_LAWS);
 
115
        plugin_data->count = count;
 
116
        plugin_data->delay_mask = delay_mask;
 
117
        plugin_data->delay_pos = delay_pos;
 
118
        plugin_data->delay_size = delay_size;
 
119
        plugin_data->delay_tbl = delay_tbl;
 
120
        plugin_data->dp_curr = dp_curr;
 
121
        plugin_data->dp_targ = dp_targ;
 
122
        plugin_data->last_law_p = last_law_p;
 
123
        plugin_data->law_pos = law_pos;
 
124
        plugin_data->law_roll = law_roll;
 
125
        plugin_data->max_law_p = max_law_p;
 
126
        plugin_data->next_peak_amp = next_peak_amp;
 
127
        plugin_data->next_peak_pos = next_peak_pos;
 
128
        plugin_data->prev_peak_amp = prev_peak_amp;
 
129
        plugin_data->prev_peak_pos = prev_peak_pos;
 
130
        plugin_data->sample_rate = sample_rate;
 
131
 
 
132
}
 
133
 
 
134
static void cleanupMultivoiceChorus(LADSPA_Handle instance) {
 
135
#line 56 "multivoice_chorus_1201.xml"
 
136
        MultivoiceChorus *plugin_data = (MultivoiceChorus *)instance;
 
137
        free(plugin_data->delay_tbl);
 
138
        free(plugin_data->prev_peak_pos);
 
139
        free(plugin_data->next_peak_pos);
 
140
        free(plugin_data->prev_peak_amp);
 
141
        free(plugin_data->next_peak_amp);
 
142
        free(plugin_data->dp_targ);
 
143
        free(plugin_data->dp_curr);
 
144
        free(instance);
 
145
}
 
146
 
 
147
static void connectPortMultivoiceChorus(
 
148
 LADSPA_Handle instance,
 
149
 unsigned long port,
 
150
 LADSPA_Data *data) {
 
151
        MultivoiceChorus *plugin;
 
152
 
 
153
        plugin = (MultivoiceChorus *)instance;
 
154
        switch (port) {
 
155
        case MULTIVOICECHORUS_VOICES:
 
156
                plugin->voices = data;
 
157
                break;
 
158
        case MULTIVOICECHORUS_DELAY_BASE:
 
159
                plugin->delay_base = data;
 
160
                break;
 
161
        case MULTIVOICECHORUS_VOICE_SPREAD:
 
162
                plugin->voice_spread = data;
 
163
                break;
 
164
        case MULTIVOICECHORUS_DETUNE:
 
165
                plugin->detune = data;
 
166
                break;
 
167
        case MULTIVOICECHORUS_LAW_FREQ:
 
168
                plugin->law_freq = data;
 
169
                break;
 
170
        case MULTIVOICECHORUS_ATTENDB:
 
171
                plugin->attendb = data;
 
172
                break;
 
173
        case MULTIVOICECHORUS_INPUT:
 
174
                plugin->input = data;
 
175
                break;
 
176
        case MULTIVOICECHORUS_OUTPUT:
 
177
                plugin->output = data;
 
178
                break;
 
179
        }
 
180
}
 
181
 
 
182
static LADSPA_Handle instantiateMultivoiceChorus(
 
183
 const LADSPA_Descriptor *descriptor,
 
184
 unsigned long s_rate) {
 
185
        MultivoiceChorus *plugin_data = (MultivoiceChorus *)malloc(sizeof(MultivoiceChorus));
 
186
        long count;
 
187
        unsigned int delay_mask;
 
188
        unsigned int delay_pos;
 
189
        unsigned int delay_size;
 
190
        float *delay_tbl = NULL;
 
191
        float *dp_curr = NULL;
 
192
        float *dp_targ = NULL;
 
193
        int last_law_p;
 
194
        int law_pos;
 
195
        int law_roll;
 
196
        int max_law_p;
 
197
        float *next_peak_amp = NULL;
 
198
        unsigned int *next_peak_pos = NULL;
 
199
        float *prev_peak_amp = NULL;
 
200
        unsigned int *prev_peak_pos = NULL;
 
201
        long sample_rate;
 
202
 
 
203
#line 20 "multivoice_chorus_1201.xml"
 
204
        int min_size;
 
205
        
 
206
        sample_rate = s_rate;
 
207
        
 
208
        max_law_p = s_rate/2;
 
209
        last_law_p = -1;
 
210
        law_pos = 0;
 
211
        law_roll = 0;
 
212
        
 
213
        min_size = sample_rate / 10;
 
214
        for (delay_size = 1024; delay_size < min_size; delay_size *= 2);
 
215
        delay_mask = delay_size - 1;
 
216
        delay_tbl = calloc(sizeof(float), delay_size);
 
217
        delay_pos = 0;
 
218
        
 
219
        prev_peak_pos = malloc(sizeof(unsigned int) * MAX_LAWS);
 
220
        next_peak_pos = malloc(sizeof(unsigned int) * MAX_LAWS);
 
221
        prev_peak_amp = malloc(sizeof(float) * MAX_LAWS);
 
222
        next_peak_amp = malloc(sizeof(float) * MAX_LAWS);
 
223
        dp_targ = malloc(sizeof(float) * MAX_LAWS);
 
224
        dp_curr = malloc(sizeof(float) * MAX_LAWS);
 
225
        
 
226
        count = 0;
 
227
 
 
228
        plugin_data->count = count;
 
229
        plugin_data->delay_mask = delay_mask;
 
230
        plugin_data->delay_pos = delay_pos;
 
231
        plugin_data->delay_size = delay_size;
 
232
        plugin_data->delay_tbl = delay_tbl;
 
233
        plugin_data->dp_curr = dp_curr;
 
234
        plugin_data->dp_targ = dp_targ;
 
235
        plugin_data->last_law_p = last_law_p;
 
236
        plugin_data->law_pos = law_pos;
 
237
        plugin_data->law_roll = law_roll;
 
238
        plugin_data->max_law_p = max_law_p;
 
239
        plugin_data->next_peak_amp = next_peak_amp;
 
240
        plugin_data->next_peak_pos = next_peak_pos;
 
241
        plugin_data->prev_peak_amp = prev_peak_amp;
 
242
        plugin_data->prev_peak_pos = prev_peak_pos;
 
243
        plugin_data->sample_rate = sample_rate;
 
244
 
 
245
        return (LADSPA_Handle)plugin_data;
 
246
}
 
247
 
 
248
#undef buffer_write
 
249
#undef RUN_ADDING
 
250
#undef RUN_REPLACING
 
251
 
 
252
#define buffer_write(b, v) (b = v)
 
253
#define RUN_ADDING    0
 
254
#define RUN_REPLACING 1
 
255
 
 
256
static void runMultivoiceChorus(LADSPA_Handle instance, unsigned long sample_count) {
 
257
        MultivoiceChorus *plugin_data = (MultivoiceChorus *)instance;
 
258
 
 
259
        /* Number of voices (float value) */
 
260
        const LADSPA_Data voices = *(plugin_data->voices);
 
261
 
 
262
        /* Delay base (ms) (float value) */
 
263
        const LADSPA_Data delay_base = *(plugin_data->delay_base);
 
264
 
 
265
        /* Voice separation (ms) (float value) */
 
266
        const LADSPA_Data voice_spread = *(plugin_data->voice_spread);
 
267
 
 
268
        /* Detune (%) (float value) */
 
269
        const LADSPA_Data detune = *(plugin_data->detune);
 
270
 
 
271
        /* LFO frequency (Hz) (float value) */
 
272
        const LADSPA_Data law_freq = *(plugin_data->law_freq);
 
273
 
 
274
        /* Output attenuation (dB) (float value) */
 
275
        const LADSPA_Data attendb = *(plugin_data->attendb);
 
276
 
 
277
        /* Input (array of floats of length sample_count) */
 
278
        const LADSPA_Data * const input = plugin_data->input;
 
279
 
 
280
        /* Output (array of floats of length sample_count) */
 
281
        LADSPA_Data * const output = plugin_data->output;
 
282
        long count = plugin_data->count;
 
283
        unsigned int delay_mask = plugin_data->delay_mask;
 
284
        unsigned int delay_pos = plugin_data->delay_pos;
 
285
        unsigned int delay_size = plugin_data->delay_size;
 
286
        float * delay_tbl = plugin_data->delay_tbl;
 
287
        float * dp_curr = plugin_data->dp_curr;
 
288
        float * dp_targ = plugin_data->dp_targ;
 
289
        int last_law_p = plugin_data->last_law_p;
 
290
        int law_pos = plugin_data->law_pos;
 
291
        int law_roll = plugin_data->law_roll;
 
292
        int max_law_p = plugin_data->max_law_p;
 
293
        float * next_peak_amp = plugin_data->next_peak_amp;
 
294
        unsigned int * next_peak_pos = plugin_data->next_peak_pos;
 
295
        float * prev_peak_amp = plugin_data->prev_peak_amp;
 
296
        unsigned int * prev_peak_pos = plugin_data->prev_peak_pos;
 
297
        long sample_rate = plugin_data->sample_rate;
 
298
 
 
299
#line 66 "multivoice_chorus_1201.xml"
 
300
        unsigned long pos;
 
301
        int d_base, t;
 
302
        LADSPA_Data out;
 
303
        float delay_depth;
 
304
        float dp; // float delay position
 
305
        float dp_frac; // fractional part
 
306
        int dp_idx; // Integer delay index
 
307
        int laws, law_separation, base_offset;
 
308
        int law_p; // Period of law
 
309
        float atten; // Attenuation
 
310
        
 
311
        // Set law params
 
312
        laws = LIMIT(f_round(voices) - 1, 0, 7);
 
313
        law_p = LIMIT(f_round(sample_rate/f_clamp(law_freq, 0.0001f, 1000.0f)), 1, max_law_p);
 
314
        if (laws > 0) {
 
315
                law_separation = law_p / laws;
 
316
        } else {
 
317
                law_separation = 0;
 
318
        }
 
319
        
 
320
        // Calculate voice spread in samples
 
321
        base_offset = (f_clamp(voice_spread, 0.0f, 2.0f) * sample_rate) / 1000;
 
322
        // Calculate base delay size in samples
 
323
        d_base = (f_clamp(delay_base, 5.0f, 40.0f) * sample_rate) / 1000;
 
324
        // Calculate delay depth in samples
 
325
        delay_depth = f_clamp((law_p * f_clamp(detune, 0.0f, 10.0f)) / (100.0f * M_PI), 0.0f, delay_size - d_base - 1 - (base_offset * laws));
 
326
        
 
327
        // Calculate output attenuation
 
328
        atten = DB_CO(f_clamp(attendb, -100.0, 24.0));
 
329
        
 
330
        for (pos = 0; pos < sample_count; pos++) {
 
331
                // N times per law 'frequency' splurge a new set of windowed data
 
332
                // into one of the N law buffers. Keeps the laws out of phase.
 
333
                if (laws > 0 && (count % law_separation) == 0) {
 
334
                        next_peak_amp[law_roll] = (float)rand() / (float)RAND_MAX;
 
335
                        next_peak_pos[law_roll] = count + law_p;
 
336
                }
 
337
                if (laws > 0 && (count % law_separation) == law_separation/2) {
 
338
                        prev_peak_amp[law_roll] = (float)rand() / (float)RAND_MAX;
 
339
                        prev_peak_pos[law_roll] = count + law_p;
 
340
                        // Pick the next law to be changed
 
341
                        law_roll = (law_roll + 1) % laws;
 
342
                }
 
343
        
 
344
                out = input[pos];
 
345
                if (count % 16 < laws) {
 
346
                        unsigned int t = count % 16;
 
347
                        // Calculate sinus phases
 
348
                        float n_ph = (float)(law_p - abs(next_peak_pos[t] - count))/law_p;
 
349
                        float p_ph = n_ph + 0.5f;
 
350
                        if (p_ph > 1.0f) {
 
351
                                p_ph -= 1.0f;
 
352
                        }
 
353
        
 
354
                        dp_targ[t] = f_sin_sq(3.1415926f*p_ph)*prev_peak_amp[t] + f_sin_sq(3.1415926f*n_ph)*next_peak_amp[t];
 
355
                }
 
356
                for (t=0; t<laws; t++) {
 
357
                        dp_curr[t] = 0.9f*dp_curr[t] + 0.1f*dp_targ[t];
 
358
                        //dp_curr[t] = dp_targ[t];
 
359
                        dp = (float)(delay_pos + d_base - (t*base_offset)) - delay_depth * dp_curr[t];
 
360
                        // Get the integer part
 
361
                        dp_idx = f_round(dp-0.5f);
 
362
                        // Get the fractional part
 
363
                        dp_frac = dp - dp_idx;
 
364
                        // Calculate the modulo'd table index
 
365
                        dp_idx = dp_idx & delay_mask;
 
366
        
 
367
                        // Accumulate into output buffer
 
368
                        out += cube_interp(dp_frac, delay_tbl[(dp_idx-1) & delay_mask], delay_tbl[dp_idx], delay_tbl[(dp_idx+1) & delay_mask], delay_tbl[(dp_idx+2) & delay_mask]);
 
369
                }
 
370
                law_pos = (law_pos + 1) % (max_law_p * 2);
 
371
        
 
372
                // Store new delay value
 
373
                delay_tbl[delay_pos] = input[pos];
 
374
                delay_pos = (delay_pos + 1) & delay_mask;
 
375
        
 
376
                buffer_write(output[pos], out * atten);
 
377
                count++;
 
378
        }
 
379
        
 
380
        plugin_data->count = count;
 
381
        plugin_data->law_pos = law_pos;
 
382
        plugin_data->last_law_p = last_law_p;
 
383
        plugin_data->law_roll = law_roll;
 
384
        plugin_data->delay_pos = delay_pos;
 
385
}
 
386
#undef buffer_write
 
387
#undef RUN_ADDING
 
388
#undef RUN_REPLACING
 
389
 
 
390
#define buffer_write(b, v) (b += (v) * run_adding_gain)
 
391
#define RUN_ADDING    1
 
392
#define RUN_REPLACING 0
 
393
 
 
394
static void setRunAddingGainMultivoiceChorus(LADSPA_Handle instance, LADSPA_Data gain) {
 
395
        ((MultivoiceChorus *)instance)->run_adding_gain = gain;
 
396
}
 
397
 
 
398
static void runAddingMultivoiceChorus(LADSPA_Handle instance, unsigned long sample_count) {
 
399
        MultivoiceChorus *plugin_data = (MultivoiceChorus *)instance;
 
400
        LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
 
401
 
 
402
        /* Number of voices (float value) */
 
403
        const LADSPA_Data voices = *(plugin_data->voices);
 
404
 
 
405
        /* Delay base (ms) (float value) */
 
406
        const LADSPA_Data delay_base = *(plugin_data->delay_base);
 
407
 
 
408
        /* Voice separation (ms) (float value) */
 
409
        const LADSPA_Data voice_spread = *(plugin_data->voice_spread);
 
410
 
 
411
        /* Detune (%) (float value) */
 
412
        const LADSPA_Data detune = *(plugin_data->detune);
 
413
 
 
414
        /* LFO frequency (Hz) (float value) */
 
415
        const LADSPA_Data law_freq = *(plugin_data->law_freq);
 
416
 
 
417
        /* Output attenuation (dB) (float value) */
 
418
        const LADSPA_Data attendb = *(plugin_data->attendb);
 
419
 
 
420
        /* Input (array of floats of length sample_count) */
 
421
        const LADSPA_Data * const input = plugin_data->input;
 
422
 
 
423
        /* Output (array of floats of length sample_count) */
 
424
        LADSPA_Data * const output = plugin_data->output;
 
425
        long count = plugin_data->count;
 
426
        unsigned int delay_mask = plugin_data->delay_mask;
 
427
        unsigned int delay_pos = plugin_data->delay_pos;
 
428
        unsigned int delay_size = plugin_data->delay_size;
 
429
        float * delay_tbl = plugin_data->delay_tbl;
 
430
        float * dp_curr = plugin_data->dp_curr;
 
431
        float * dp_targ = plugin_data->dp_targ;
 
432
        int last_law_p = plugin_data->last_law_p;
 
433
        int law_pos = plugin_data->law_pos;
 
434
        int law_roll = plugin_data->law_roll;
 
435
        int max_law_p = plugin_data->max_law_p;
 
436
        float * next_peak_amp = plugin_data->next_peak_amp;
 
437
        unsigned int * next_peak_pos = plugin_data->next_peak_pos;
 
438
        float * prev_peak_amp = plugin_data->prev_peak_amp;
 
439
        unsigned int * prev_peak_pos = plugin_data->prev_peak_pos;
 
440
        long sample_rate = plugin_data->sample_rate;
 
441
 
 
442
#line 66 "multivoice_chorus_1201.xml"
 
443
        unsigned long pos;
 
444
        int d_base, t;
 
445
        LADSPA_Data out;
 
446
        float delay_depth;
 
447
        float dp; // float delay position
 
448
        float dp_frac; // fractional part
 
449
        int dp_idx; // Integer delay index
 
450
        int laws, law_separation, base_offset;
 
451
        int law_p; // Period of law
 
452
        float atten; // Attenuation
 
453
        
 
454
        // Set law params
 
455
        laws = LIMIT(f_round(voices) - 1, 0, 7);
 
456
        law_p = LIMIT(f_round(sample_rate/f_clamp(law_freq, 0.0001f, 1000.0f)), 1, max_law_p);
 
457
        if (laws > 0) {
 
458
                law_separation = law_p / laws;
 
459
        } else {
 
460
                law_separation = 0;
 
461
        }
 
462
        
 
463
        // Calculate voice spread in samples
 
464
        base_offset = (f_clamp(voice_spread, 0.0f, 2.0f) * sample_rate) / 1000;
 
465
        // Calculate base delay size in samples
 
466
        d_base = (f_clamp(delay_base, 5.0f, 40.0f) * sample_rate) / 1000;
 
467
        // Calculate delay depth in samples
 
468
        delay_depth = f_clamp((law_p * f_clamp(detune, 0.0f, 10.0f)) / (100.0f * M_PI), 0.0f, delay_size - d_base - 1 - (base_offset * laws));
 
469
        
 
470
        // Calculate output attenuation
 
471
        atten = DB_CO(f_clamp(attendb, -100.0, 24.0));
 
472
        
 
473
        for (pos = 0; pos < sample_count; pos++) {
 
474
                // N times per law 'frequency' splurge a new set of windowed data
 
475
                // into one of the N law buffers. Keeps the laws out of phase.
 
476
                if (laws > 0 && (count % law_separation) == 0) {
 
477
                        next_peak_amp[law_roll] = (float)rand() / (float)RAND_MAX;
 
478
                        next_peak_pos[law_roll] = count + law_p;
 
479
                }
 
480
                if (laws > 0 && (count % law_separation) == law_separation/2) {
 
481
                        prev_peak_amp[law_roll] = (float)rand() / (float)RAND_MAX;
 
482
                        prev_peak_pos[law_roll] = count + law_p;
 
483
                        // Pick the next law to be changed
 
484
                        law_roll = (law_roll + 1) % laws;
 
485
                }
 
486
        
 
487
                out = input[pos];
 
488
                if (count % 16 < laws) {
 
489
                        unsigned int t = count % 16;
 
490
                        // Calculate sinus phases
 
491
                        float n_ph = (float)(law_p - abs(next_peak_pos[t] - count))/law_p;
 
492
                        float p_ph = n_ph + 0.5f;
 
493
                        if (p_ph > 1.0f) {
 
494
                                p_ph -= 1.0f;
 
495
                        }
 
496
        
 
497
                        dp_targ[t] = f_sin_sq(3.1415926f*p_ph)*prev_peak_amp[t] + f_sin_sq(3.1415926f*n_ph)*next_peak_amp[t];
 
498
                }
 
499
                for (t=0; t<laws; t++) {
 
500
                        dp_curr[t] = 0.9f*dp_curr[t] + 0.1f*dp_targ[t];
 
501
                        //dp_curr[t] = dp_targ[t];
 
502
                        dp = (float)(delay_pos + d_base - (t*base_offset)) - delay_depth * dp_curr[t];
 
503
                        // Get the integer part
 
504
                        dp_idx = f_round(dp-0.5f);
 
505
                        // Get the fractional part
 
506
                        dp_frac = dp - dp_idx;
 
507
                        // Calculate the modulo'd table index
 
508
                        dp_idx = dp_idx & delay_mask;
 
509
        
 
510
                        // Accumulate into output buffer
 
511
                        out += cube_interp(dp_frac, delay_tbl[(dp_idx-1) & delay_mask], delay_tbl[dp_idx], delay_tbl[(dp_idx+1) & delay_mask], delay_tbl[(dp_idx+2) & delay_mask]);
 
512
                }
 
513
                law_pos = (law_pos + 1) % (max_law_p * 2);
 
514
        
 
515
                // Store new delay value
 
516
                delay_tbl[delay_pos] = input[pos];
 
517
                delay_pos = (delay_pos + 1) & delay_mask;
 
518
        
 
519
                buffer_write(output[pos], out * atten);
 
520
                count++;
 
521
        }
 
522
        
 
523
        plugin_data->count = count;
 
524
        plugin_data->law_pos = law_pos;
 
525
        plugin_data->last_law_p = last_law_p;
 
526
        plugin_data->law_roll = law_roll;
 
527
        plugin_data->delay_pos = delay_pos;
 
528
}
 
529
 
 
530
void _init() {
 
531
        char **port_names;
 
532
        LADSPA_PortDescriptor *port_descriptors;
 
533
        LADSPA_PortRangeHint *port_range_hints;
 
534
 
 
535
#ifdef ENABLE_NLS
 
536
#define D_(s) dgettext(PACKAGE, s)
 
537
        setlocale(LC_ALL, "");
 
538
        bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
 
539
#else
 
540
#define D_(s) (s)
 
541
#endif
 
542
 
 
543
 
 
544
        multivoiceChorusDescriptor =
 
545
         (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
 
546
 
 
547
        if (multivoiceChorusDescriptor) {
 
548
                multivoiceChorusDescriptor->UniqueID = 1201;
 
549
                multivoiceChorusDescriptor->Label = "multivoiceChorus";
 
550
                multivoiceChorusDescriptor->Properties =
 
551
                 0;
 
552
                multivoiceChorusDescriptor->Name =
 
553
                 D_("Multivoice Chorus");
 
554
                multivoiceChorusDescriptor->Maker =
 
555
                 "Steve Harris <steve@plugin.org.uk>";
 
556
                multivoiceChorusDescriptor->Copyright =
 
557
                 "GPL";
 
558
                multivoiceChorusDescriptor->PortCount = 8;
 
559
 
 
560
                port_descriptors = (LADSPA_PortDescriptor *)calloc(8,
 
561
                 sizeof(LADSPA_PortDescriptor));
 
562
                multivoiceChorusDescriptor->PortDescriptors =
 
563
                 (const LADSPA_PortDescriptor *)port_descriptors;
 
564
 
 
565
                port_range_hints = (LADSPA_PortRangeHint *)calloc(8,
 
566
                 sizeof(LADSPA_PortRangeHint));
 
567
                multivoiceChorusDescriptor->PortRangeHints =
 
568
                 (const LADSPA_PortRangeHint *)port_range_hints;
 
569
 
 
570
                port_names = (char **)calloc(8, sizeof(char*));
 
571
                multivoiceChorusDescriptor->PortNames =
 
572
                 (const char **)port_names;
 
573
 
 
574
                /* Parameters for Number of voices */
 
575
                port_descriptors[MULTIVOICECHORUS_VOICES] =
 
576
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
577
                port_names[MULTIVOICECHORUS_VOICES] =
 
578
                 D_("Number of voices");
 
579
                port_range_hints[MULTIVOICECHORUS_VOICES].HintDescriptor =
 
580
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_INTEGER | LADSPA_HINT_DEFAULT_1;
 
581
                port_range_hints[MULTIVOICECHORUS_VOICES].LowerBound = 1;
 
582
                port_range_hints[MULTIVOICECHORUS_VOICES].UpperBound = 8;
 
583
 
 
584
                /* Parameters for Delay base (ms) */
 
585
                port_descriptors[MULTIVOICECHORUS_DELAY_BASE] =
 
586
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
587
                port_names[MULTIVOICECHORUS_DELAY_BASE] =
 
588
                 D_("Delay base (ms)");
 
589
                port_range_hints[MULTIVOICECHORUS_DELAY_BASE].HintDescriptor =
 
590
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_MINIMUM;
 
591
                port_range_hints[MULTIVOICECHORUS_DELAY_BASE].LowerBound = 10;
 
592
                port_range_hints[MULTIVOICECHORUS_DELAY_BASE].UpperBound = 40;
 
593
 
 
594
                /* Parameters for Voice separation (ms) */
 
595
                port_descriptors[MULTIVOICECHORUS_VOICE_SPREAD] =
 
596
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
597
                port_names[MULTIVOICECHORUS_VOICE_SPREAD] =
 
598
                 D_("Voice separation (ms)");
 
599
                port_range_hints[MULTIVOICECHORUS_VOICE_SPREAD].HintDescriptor =
 
600
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_LOW;
 
601
                port_range_hints[MULTIVOICECHORUS_VOICE_SPREAD].LowerBound = 0;
 
602
                port_range_hints[MULTIVOICECHORUS_VOICE_SPREAD].UpperBound = 2;
 
603
 
 
604
                /* Parameters for Detune (%) */
 
605
                port_descriptors[MULTIVOICECHORUS_DETUNE] =
 
606
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
607
                port_names[MULTIVOICECHORUS_DETUNE] =
 
608
                 D_("Detune (%)");
 
609
                port_range_hints[MULTIVOICECHORUS_DETUNE].HintDescriptor =
 
610
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_1;
 
611
                port_range_hints[MULTIVOICECHORUS_DETUNE].LowerBound = 0;
 
612
                port_range_hints[MULTIVOICECHORUS_DETUNE].UpperBound = 5;
 
613
 
 
614
                /* Parameters for LFO frequency (Hz) */
 
615
                port_descriptors[MULTIVOICECHORUS_LAW_FREQ] =
 
616
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
617
                port_names[MULTIVOICECHORUS_LAW_FREQ] =
 
618
                 D_("LFO frequency (Hz)");
 
619
                port_range_hints[MULTIVOICECHORUS_LAW_FREQ].HintDescriptor =
 
620
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_LOW;
 
621
                port_range_hints[MULTIVOICECHORUS_LAW_FREQ].LowerBound = 2;
 
622
                port_range_hints[MULTIVOICECHORUS_LAW_FREQ].UpperBound = 30;
 
623
 
 
624
                /* Parameters for Output attenuation (dB) */
 
625
                port_descriptors[MULTIVOICECHORUS_ATTENDB] =
 
626
                 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
 
627
                port_names[MULTIVOICECHORUS_ATTENDB] =
 
628
                 D_("Output attenuation (dB)");
 
629
                port_range_hints[MULTIVOICECHORUS_ATTENDB].HintDescriptor =
 
630
                 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_0;
 
631
                port_range_hints[MULTIVOICECHORUS_ATTENDB].LowerBound = -20;
 
632
                port_range_hints[MULTIVOICECHORUS_ATTENDB].UpperBound = 0;
 
633
 
 
634
                /* Parameters for Input */
 
635
                port_descriptors[MULTIVOICECHORUS_INPUT] =
 
636
                 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
 
637
                port_names[MULTIVOICECHORUS_INPUT] =
 
638
                 D_("Input");
 
639
                port_range_hints[MULTIVOICECHORUS_INPUT].HintDescriptor = 0;
 
640
 
 
641
                /* Parameters for Output */
 
642
                port_descriptors[MULTIVOICECHORUS_OUTPUT] =
 
643
                 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
 
644
                port_names[MULTIVOICECHORUS_OUTPUT] =
 
645
                 D_("Output");
 
646
                port_range_hints[MULTIVOICECHORUS_OUTPUT].HintDescriptor = 0;
 
647
 
 
648
                multivoiceChorusDescriptor->activate = activateMultivoiceChorus;
 
649
                multivoiceChorusDescriptor->cleanup = cleanupMultivoiceChorus;
 
650
                multivoiceChorusDescriptor->connect_port = connectPortMultivoiceChorus;
 
651
                multivoiceChorusDescriptor->deactivate = NULL;
 
652
                multivoiceChorusDescriptor->instantiate = instantiateMultivoiceChorus;
 
653
                multivoiceChorusDescriptor->run = runMultivoiceChorus;
 
654
                multivoiceChorusDescriptor->run_adding = runAddingMultivoiceChorus;
 
655
                multivoiceChorusDescriptor->set_run_adding_gain = setRunAddingGainMultivoiceChorus;
 
656
        }
 
657
}
 
658
 
 
659
void _fini() {
 
660
        if (multivoiceChorusDescriptor) {
 
661
                free((LADSPA_PortDescriptor *)multivoiceChorusDescriptor->PortDescriptors);
 
662
                free((char **)multivoiceChorusDescriptor->PortNames);
 
663
                free((LADSPA_PortRangeHint *)multivoiceChorusDescriptor->PortRangeHints);
 
664
                free(multivoiceChorusDescriptor);
 
665
        }
 
666
 
 
667
}