File: blepvco.cc

package info (click to toggle)
blepvco 0.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, stretch, wheezy
  • size: 392 kB
  • sloc: ansic: 2,024; cpp: 790; makefile: 15
file content (578 lines) | stat: -rw-r--r-- 17,390 bytes parent folder | download | duplicates (3)
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/* blepvco - minBLEP-based, hard-sync-capable LADSPA VCOs.
 *
 * Copyright (C) 2004-2005 Sean Bolton.
 *
 * Much of the LADSPA framework used here comes from VCO-plugins
 * 0.3.0, copyright (c) 2003-2004 Fons Adriaensen.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#define _BSD_SOURCE    1
#define _SVID_SOURCE   1
#define _ISOC99_SOURCE 1

#include <stdio.h>
#include <string.h>
#include <math.h>
#include "minblep_tables.h"
#include "blepvco.h"


extern float exp2ap (float x);


void
Ladspa_minBLEP_VCO::place_step_dd(float *buffer, int index, float phase, float w, float scale)
{
    float r;
    int i;

    r = MINBLEP_PHASES * phase / w;
    i = lrintf(r - 0.5f);
    r -= (float)i;
    i &= MINBLEP_PHASE_MASK;  /* extreme modulation can cause i to be out-of-range */
    /* this would be better than the above, but more expensive:
     *  while (i < 0) {
     *    i += MINBLEP_PHASES;
     *    index++;
     *  }
     */

    while (i < MINBLEP_PHASES * STEP_DD_PULSE_LENGTH) {
        buffer[index] += scale * (step_dd_table[i].value + r * step_dd_table[i].delta);
        i += MINBLEP_PHASES;
        index++;
    }
}

void
Ladspa_minBLEP_VCO::place_slope_dd(float *buffer, int index, float phase, float w, float slope_delta)
{
    float r;
    int i;

    r = MINBLEP_PHASES * phase / w;
    i = lrintf(r - 0.5f);
    r -= (float)i;
    i &= MINBLEP_PHASE_MASK;  /* extreme modulation can cause i to be out-of-range */

    slope_delta *= w;

    while (i < MINBLEP_PHASES * SLOPE_DD_PULSE_LENGTH) {
        buffer[index] += slope_delta * (slope_dd_table[i] + r * (slope_dd_table[i + 1] - slope_dd_table[i]));
        i += MINBLEP_PHASES;
        index++;
    }
}

/* ==== hard-sync-capable sawtooth oscillator ==== */

void Ladspa_VCO_blepsaw::setport (unsigned long port, LADSPA_Data *data)
{
    _port [port] = data;
}


void Ladspa_VCO_blepsaw::active (bool act)
{
    _init = 1;
    _z = 0.0f;
    _j = 0;
    memset (_f, 0, (FILLEN + STEP_DD_PULSE_LENGTH) * sizeof (float));
}


void Ladspa_VCO_blepsaw::runproc (unsigned long len, bool add)
{
    int    j, n;
    float  *outp, *freq, *expm, *linm, *syncin, *syncout;
    float  a, p, t, w, dw, z;

    outp    = _port[OUTP];
    syncout = _port[SYNCOUT];
    syncin  = _port[SYNCIN];
    freq = _port[FREQ] - 1;
    expm = _port[EXPM] - 1;
    linm = _port[LINM] - 1;

    p = _p;  /* phase [0, 1) */
    w = _w;  /* phase increment */
    z = _z;  /* low pass filter state */
    j = _j;  /* index into buffer _f */

    if (_init) {
        p = 0.5f;
        w = (exp2ap (freq[1] + _port[OCTN][0] + _port[TUNE][0] + expm[1] * _port[EXPG][0] + 8.03136)
	     + 1e3 * linm[1] * _port[LING][0]) / _fsam; 
        if (w < 1e-5) w = 1e-5;
        if (w > 0.5) w = 0.5;
        /* if we valued alias-free startup over low startup time, we could do:
         *   p -= w;
         *   place_slope_dd(_f, j, 0.0f, w, -1.0f); */
        _init = 0;
    }

    a = 0.2 + 0.8 * _port [FILT][0];
    do
    {
        n = (len > 24) ? 16 : len;
        freq += n;
        expm += n;
        linm += n;
        len -= n;
 
        t = (exp2ap (*freq + _port[OCTN][0] + _port[TUNE][0] + *expm * _port[EXPG][0] + 8.03136)
	     + 1e3 * *linm * _port[LING][0]) / _fsam; 
        if (t < 1e-5) t = 1e-5;
        if (t > 0.5) t = 0.5;
        dw = (t - w) / n;

        while (n--)
	{
	    w += dw; 
            p += w;

            if (*syncin >= 1e-20f) {  /* sync to master */

                float eof_offset = (*syncin - 1e-20f) * w;
                float p_at_reset = p - eof_offset;
                p = eof_offset;

                /* place any DD that may have occurred in subsample before reset */
                if (p_at_reset >= 1.0f) {
                    p_at_reset -= 1.0f;
                    place_step_dd(_f, j, p_at_reset + eof_offset, w, 1.0f);
                }

                /* now place reset DD */
                place_step_dd(_f, j, p, w, p_at_reset);

                *syncout = *syncin;  /* best we can do is pass on upstream sync */

            } else if (p >= 1.0f) {  /* normal phase reset */

                p -= 1.0f;
                *syncout = p / w + 1e-20f;
                place_step_dd(_f, j, p, w, 1.0f);

            } else {

                *syncout = 0.0f;
            }
            _f[j + DD_SAMPLE_DELAY] += 0.5f - p;

            z += a * (_f[j] - z);
            *outp++ = z;
            syncin++;
            syncout++;
            if (++j == FILLEN)
            { 
                j = 0;
		memcpy (_f, _f + FILLEN, STEP_DD_PULSE_LENGTH * sizeof (float));
                memset (_f + STEP_DD_PULSE_LENGTH, 0,  FILLEN * sizeof (float));
	    }
	}
    }
    while (len);

    _p = p;
    _w = w;
    _z = z;
    _j = j;
}

/* ==== variable-width, hard-sync-capable rectangular oscillator ==== */

void Ladspa_VCO_bleprect::setport (unsigned long port, LADSPA_Data *data)
{
    _port [port] = data;
}


void Ladspa_VCO_bleprect::active (bool act)
{
    _init = 1;
    _z = 0.0f;
    _j = 0;
    memset (_f, 0, (FILLEN + STEP_DD_PULSE_LENGTH) * sizeof (float));
}


void Ladspa_VCO_bleprect::runproc (unsigned long len, bool add)
{
    int    j, k, n;
    float  *outp, *freq, *expm, *linm, *wavm, *syncin, *syncout;
    float  a, b, db, p, t, w, dw, x, z;

    outp    = _port[OUTP];
    syncout = _port[SYNCOUT];
    syncin  = _port[SYNCIN];
    freq = _port[FREQ] - 1;
    expm = _port[EXPM] - 1;
    linm = _port[LINM] - 1;
    wavm = _port[WAVM] - 1;

    p = _p;  /* phase [0, 1) */
    w = _w;  /* phase increment */
    b = _b;  /* duty cycle (0, 1) */
    x = _x;  /* temporary output variable */
    z = _z;  /* low pass filter state */
    j = _j;  /* index into buffer _f */
    k = _k;  /* output state, 0 = high (0.5f), 1 = low (-0.5f) */

    if (_init) {
        p = 0.0f;
        w = (exp2ap (freq[1] + _port[OCTN][0] + _port[TUNE][0] + expm[1] * _port[EXPG][0] + 8.03136)
	     + 1e3 * linm[1] * _port[LING][0]) / _fsam; 
        if (w < 1e-5) w = 1e-5;
        if (w > 0.5) w = 0.5;
        b = 0.5 * (1.0 + _port [WAVE][0] + wavm[1] * _port [WMDG][0]);
        if (b < w) b = w;
        if (b > 1.0f - w) b = 1.0f - w;
        /* for variable-width rectangular wave, we could do DC compensation with:
         *     x = 1.0f - b;
         * but that doesn't work well with highly modulated hard sync.  Instead,
         * we keep things in the range [-0.5f, 0.5f]. */
        x = 0.5f;
        /* if we valued alias-free startup over low startup time, we could do:
         *   p -= w;
         *   place_step_dd(_f, j, 0.0f, w, 0.5f); */
        k = 0;
        _init = 0;
    }

    a = 0.2 + 0.8 * _port [FILT][0];
    do
    {
        n = (len > 24) ? 16 : len;
        freq += n;
        expm += n;
        linm += n;
        wavm += n;
        len -= n;
 
        t = (exp2ap (*freq + _port[OCTN][0] + _port[TUNE][0] + *expm * _port[EXPG][0] + 8.03136)
	     + 1e3 * *linm * _port[LING][0]) / _fsam; 
        if (t < 1e-5) t = 1e-5;
        if (t > 0.5) t = 0.5;
        dw = (t - w) / n;
        t = 0.5 * (1.0 + _port [WAVE][0] + *wavm * _port [WMDG][0]);
        if (t < w) t = w;
        if (t > 1.0f - w) t = 1.0f - w;
        db = (t - b) / n;

        while (n--)
	{
	    w += dw; 
            b += db;
            p += w;

            if (*syncin >= 1e-20f) {  /* sync to master */

                float eof_offset = (*syncin - 1e-20f) * w;
                float p_at_reset = p - eof_offset;
                p = eof_offset;

                /* place any DDs that may have occurred in subsample before reset */
                if (!k) {
                    if (p_at_reset >= b) {
                        place_step_dd(_f, j, p_at_reset - b + eof_offset, w, -1.0f);
                        k = 1;
                        x = -0.5f;
                    }
                    if (p_at_reset >= 1.0f) {
                        p_at_reset -= 1.0f;
                        place_step_dd(_f, j, p_at_reset + eof_offset, w, 1.0f);
                        k = 0;
                        x = 0.5f;
                    }
                } else {
                    if (p_at_reset >= 1.0f) {
                        p_at_reset -= 1.0f;
                        place_step_dd(_f, j, p_at_reset + eof_offset, w, 1.0f);
                        k = 0;
                        x = 0.5f;
                    }
                    if (!k && p_at_reset >= b) {
                        place_step_dd(_f, j, p_at_reset - b + eof_offset, w, -1.0f);
                        k = 1;
                        x = -0.5f;
                    }
                }

                /* now place reset DD */
                if (k) {
                    place_step_dd(_f, j, p, w, 1.0f);
                    k = 0;
                    x = 0.5f;
                }
                if (p >= b) {
                    place_step_dd(_f, j, p - b, w, -1.0f);
                    k = 1;
                    x = -0.5f;
                }

                *syncout = *syncin;  /* best we can do is pass on upstream sync */

            } else if (!k) {  /* normal operation, signal currently high */

                if (p >= b) {
                    place_step_dd(_f, j, p - b, w, -1.0f);
                    k = 1;
                    x = -0.5f;
                }
                if (p >= 1.0f) {
                    p -= 1.0f;
                    *syncout = p / w + 1e-20f;
                    place_step_dd(_f, j, p, w, 1.0f);
                    k = 0;
                    x = 0.5f;
                } else {
                    *syncout = 0.0f;
                }

            } else {  /* normal operation, signal currently low */

                if (p >= 1.0f) {
                    p -= 1.0f;
                    *syncout = p / w + 1e-20f;
                    place_step_dd(_f, j, p, w, 1.0f);
                    k = 0;
                    x = 0.5f;
                } else {
                    *syncout = 0.0f;
                }
                if (!k && p >= b) {
                    place_step_dd(_f, j, p - b, w, -1.0f);
                    k = 1;
                    x = -0.5f;
                }
            }
            _f[j + DD_SAMPLE_DELAY] += x;

            z += a * (_f[j] - z);
            *outp++ = z;
            syncin++;
            syncout++;
            if (++j == FILLEN)
            { 
                j = 0;
		memcpy (_f, _f + FILLEN, STEP_DD_PULSE_LENGTH * sizeof (float));
                memset (_f + STEP_DD_PULSE_LENGTH, 0,  FILLEN * sizeof (float));
	    }
	}
    }
    while (len);

    _p = p;
    _w = w;
    _b = b;
    _x = x;
    _z = z;
    _j = j;
    _k = k;
}

/* ==== variable-slope, hard-sync-capable triangle oscillator ==== */

void Ladspa_VCO_bleptri::setport (unsigned long port, LADSPA_Data *data)
{
    _port [port] = data;
}


void Ladspa_VCO_bleptri::active (bool act)
{
    _init = 1;
    _z = 0.0f;
    _j = 0;
    memset (_f, 0, (FILLEN + STEP_DD_PULSE_LENGTH) * sizeof (float));
}


void Ladspa_VCO_bleptri::runproc (unsigned long len, bool add)
{
    int    j, k, n;
    float  *outp, *freq, *expm, *linm, *wavm, *syncin, *syncout;
    float  a, b, b1, db, p, t, w, dw, x, z;

    outp    = _port[OUTP];
    syncout = _port[SYNCOUT];
    syncin  = _port[SYNCIN];
    freq = _port[FREQ] - 1;
    expm = _port[EXPM] - 1;
    linm = _port[LINM] - 1;
    wavm = _port[WAVM] - 1;

    p = _p;  /* phase [0, 1) */
    w = _w;  /* phase increment */
    b = _b;  /* duty cycle (0, 1) */
    z = _z;  /* low pass filter state */
    j = _j;  /* index into buffer _f */
    k = _k;  /* output state, 0 = positive slope, 1 = negative slope */

    if (_init) {
        w = (exp2ap (freq[1] + _port[OCTN][0] + _port[TUNE][0] + expm[1] * _port[EXPG][0] + 8.03136)
	     + 1e3 * linm[1] * _port[LING][0]) / _fsam; 
        if (w < 1e-5) w = 1e-5;
        if (w > 0.5) w = 0.5;
        b = 0.5 * (1.0 + _port [WAVE][0] + wavm[1] * _port [WMDG][0]);
        if (b < w) b = w;
        if (b > 1.0f - w) b = 1.0f - w;
        p = 0.5f * b;
        /* if we valued alias-free startup over low startup time, we could do:
         *   p -= w;
         *   place_slope_dd(_f, j, 0.0f, w, 1.0f / b); */
        k = 0;
        _init = 0;
    }

    a = 0.2 + 0.8 * _port [FILT][0];
    do
    {
        n = (len > 24) ? 16 : len;
        freq += n;
        expm += n;
        linm += n;
        wavm += n;
        len -= n;
 
        t = (exp2ap (*freq + _port[OCTN][0] + _port[TUNE][0] + *expm * _port[EXPG][0] + 8.03136)
	     + 1e3 * *linm * _port[LING][0]) / _fsam; 
        if (t < 1e-5) t = 1e-5;
        if (t > 0.5) t = 0.5;
        dw = (t - w) / n;
        t = 0.5 * (1.0 + _port [WAVE][0] + *wavm * _port [WMDG][0]);
        if (t < w) t = w;
        if (t > 1.0f - w) t = 1.0f - w;
        db = (t - b) / n;

        while (n--)
	{
	    w += dw; 
            b += db;
            b1 = 1.0f - b;
            p += w;

            if (*syncin >= 1e-20f) {  /* sync to master */

                float eof_offset = (*syncin - 1e-20f) * w;
                float p_at_reset = p - eof_offset;
                p = eof_offset;

                /* place any DDs that may have occurred in subsample before reset */
                if (!k) {
                    x = -0.5f + p_at_reset / b;
                    if (p_at_reset >= b) {
                        x = 0.5f - (p_at_reset - b) / b1;
                        place_slope_dd(_f, j, p_at_reset - b + eof_offset, w, -1.0f / b1 - 1.0f / b);
                        k = 1;
                    }
                    if (p_at_reset >= 1.0f) {
                        p_at_reset -= 1.0f;
                        x = -0.5f + p_at_reset / b;
                        place_slope_dd(_f, j, p_at_reset + eof_offset, w, 1.0f / b + 1.0f / b1);
                        k = 0;
                    }
                } else {
                    x = 0.5f - (p_at_reset - b) / b1;
                    if (p_at_reset >= 1.0f) {
                        p_at_reset -= 1.0f;
                        x = -0.5f + p_at_reset / b;
                        place_slope_dd(_f, j, p_at_reset + eof_offset, w, 1.0f / b + 1.0f / b1);
                        k = 0;
                    }
                    if (!k && p_at_reset >= b) {
                        x = 0.5f - (p_at_reset - b) / b1;
                        place_slope_dd(_f, j, p_at_reset - b + eof_offset, w, -1.0f / b1 - 1.0f / b);
                        k = 1;
                    }
                }

                /* now place reset DDs */
                if (k)
                    place_slope_dd(_f, j, p, w, 1.0f / b + 1.0f / b1);
                place_step_dd(_f, j, p, w, -0.5f - x);
                x = -0.5f + p / b;
                k = 0;
                if (p >= b) {
                    x = 0.5f - (p - b) / b1;
                    place_slope_dd(_f, j, p - b, w, -1.0f / b1 - 1.0f / b);
                    k = 1;
                }
                *syncout = *syncin;  /* best we can do is pass on upstream sync */

            } else if (!k) {  /* normal operation, slope currently up */

                x = -0.5f + p / b;
                if (p >= b) {
                    x = 0.5f - (p - b) / b1;
                    place_slope_dd(_f, j, p - b, w, -1.0f / b1 - 1.0f / b);
                    k = 1;
                }
                if (p >= 1.0f) {
                    p -= 1.0f;
                    *syncout = p / w + 1e-20f;
                    x = -0.5f + p / b;
                    place_slope_dd(_f, j, p, w, 1.0f / b + 1.0f / b1);
                    k = 0;
                } else {
                    *syncout = 0.0f;
                }

            } else {  /* normal operation, slope currently down */

                x = 0.5f - (p - b) / b1;
                if (p >= 1.0f) {
                    p -= 1.0f;
                    *syncout = p / w + 1e-20f;
                    x = -0.5f + p / b;
                    place_slope_dd(_f, j, p, w, 1.0f / b + 1.0f / b1);
                    k = 0;
                } else {
                    *syncout = 0.0f;
                }
                if (!k && p >= b) {
                    x = 0.5f - (p - b) / b1;
                    place_slope_dd(_f, j, p - b, w, -1.0f / b1 - 1.0f / b);
                    k = 1;
                }
            }
            _f[j + DD_SAMPLE_DELAY] += x;

            z += a * (_f[j] - z);
            *outp++ = z;
            syncin++;
            syncout++;
            if (++j == FILLEN)
            { 
                j = 0;
		memcpy (_f, _f + FILLEN, STEP_DD_PULSE_LENGTH * sizeof (float));
                memset (_f + STEP_DD_PULSE_LENGTH, 0,  FILLEN * sizeof (float));
	    }
	}
    }
    while (len);

    _p = p;
    _w = w;
    _b = b;
    _z = z;
    _j = j;
    _k = k;
}