~pbatch/soundpipe

soundpipe/tangled/bigverb.c -rw-r--r-- 8.1 KiB
49c52318Paul Batchelor fix talkbox typos 6 days ago
                                                                                
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
#line 93 "bigverb.org"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define SK_BIGVERB_PRIV
#include "bigverb.h"
#line 56 "bigverb.org"
struct bigverb_paramset {
    int delay; /* in samples, 44.1 kHz */
    int drift; /* 1/10 milliseconds */
    int randfreq; /* Hertz * 1000 */
    int seed;
};

static const struct bigverb_paramset params[8] = {
    {0x09a9, 0x0a, 0xc1c, 0x07ae},
    {0x0acf, 0x0b, 0xdac, 0x7333},
    {0x0c91, 0x11, 0x456, 0x5999},
    {0x0de5, 0x06, 0xf85, 0x2666},
    {0x0f43, 0x0a, 0x925, 0x50a3},
    {0x101f, 0x0b, 0x769, 0x5999},
    {0x085f, 0x11, 0x37b, 0x7333},
    {0x078d, 0x06, 0xc95, 0x3851}
};
#line 93 "bigverb.org"
#line 851 "bigverb.org"
#define FRACSCALE 0x10000000
#line 861 "bigverb.org"
#define FRACMASK 0xFFFFFFF
#line 869 "bigverb.org"
#define FRACNBITS 28
#line 93 "bigverb.org"
#line 434 "bigverb.org"
static int get_delay_size(const struct bigverb_paramset *p, int sr);
#line 511 "bigverb.org"
static void delay_init(sk_bigverb_delay *d,
                       const struct bigverb_paramset *p,
                       SKFLT *buf,
                       size_t sz,
                       int sr);
#line 619 "bigverb.org"
static SKFLT delay_compute(sk_bigverb_delay *d,
                           SKFLT in,
                           SKFLT fdbk,
                           SKFLT filt,
                           int sr);
#line 896 "bigverb.org"
static void generate_next_line(sk_bigverb_delay *d, int sr);
#line 93 "bigverb.org"
#line 148 "bigverb.org"
sk_bigverb * sk_bigverb_new(int sr)
{
    sk_bigverb *bv;

    bv = calloc(1, sizeof(sk_bigverb));

    bv->sr = sr;
#line 207 "bigverb.org"
sk_bigverb_size(bv, 0.93);
#line 233 "bigverb.org"
sk_bigverb_cutoff(bv, 10000.0);
#line 252 "bigverb.org"
bv->pcutoff = -1;
#line 272 "bigverb.org"
bv->filt = 1.0;
#line 402 "bigverb.org"
bv->buf = NULL;
#line 148 "bigverb.org"
#line 407 "bigverb.org"
{
unsigned long total_size;
int i;
SKFLT *buf;

total_size = 0;
buf = NULL;
#line 449 "bigverb.org"
for (i = 0; i < 8; i++) {
    total_size += get_delay_size(&params[i], sr);
}
#line 407 "bigverb.org"
#line 460 "bigverb.org"
buf = calloc(1, sizeof(SKFLT) * total_size);
bv->buf = buf;
#line 407 "bigverb.org"
#line 476 "bigverb.org"
{
    unsigned long bufpos;
    bufpos = 0;
    for (i = 0; i < 8; i++) {
        unsigned int sz;
        sz = get_delay_size(&params[i], sr);

        delay_init(&bv->delay[i], &params[i],
                   &buf[bufpos], sz, sr);
        bufpos += sz;
    }
}
#line 417 "bigverb.org"
}
#line 157 "bigverb.org"

    return bv;
}
#line 172 "bigverb.org"
void sk_bigverb_del(sk_bigverb *bv)
{
#line 466 "bigverb.org"
free(bv->buf);
#line 175 "bigverb.org"
    free(bv);
    bv = NULL;
}
#line 212 "bigverb.org"
void sk_bigverb_size(sk_bigverb *bv, SKFLT size)
{
    bv->size = size;
}
#line 257 "bigverb.org"
void sk_bigverb_cutoff(sk_bigverb *bv, SKFLT cutoff)
{
    bv->cutoff = cutoff;
}
#line 296 "bigverb.org"
void sk_bigverb_tick(sk_bigverb *bv,
                     SKFLT inL, SKFLT inR,
                     SKFLT *outL, SKFLT *outR)
{
    /* TODO: implement */
    SKFLT lsum, rsum;

    lsum = 0;
    rsum = 0;

#line 329 "bigverb.org"
if (bv->pcutoff != bv->cutoff) {
    bv->pcutoff = bv->cutoff;
    bv->filt = 2.0 - cos(bv->pcutoff * 2 * M_PI / bv->sr);
    bv->filt = bv->filt - sqrt(bv->filt * bv->filt - 1.0);
}
#line 296 "bigverb.org"
#line 344 "bigverb.org"
{
    int i;
    SKFLT jp;

    jp = 0;

    for (i = 0; i < 8; i++) {
        jp += bv->delay[i].y;
    }

    jp *= 0.25;

    inL = jp + inL;
    inR = jp + inR;
}
#line 296 "bigverb.org"
#line 370 "bigverb.org"
{
    int i;
    for (i = 0; i < 8; i++) {
        if (i & 1) {
            rsum += delay_compute(&bv->delay[i],
                                  inR,
                                  bv->size,
                                  bv->filt,
                                  bv->sr);
        } else {
            lsum += delay_compute(&bv->delay[i],
                                  inL,
                                  bv->size,
                                  bv->filt,
                                  bv->sr);
        }
    }
}
rsum *= 0.35f;
lsum *= 0.35f;
#line 309 "bigverb.org"

    *outL = lsum;
    *outR = rsum;
}
#line 439 "bigverb.org"
static int get_delay_size(const struct bigverb_paramset *p, int sr)
{
    SKFLT sz;
    sz = (SKFLT)p->delay/44100 + (p->drift * 0.0001) * 1.125;
    return floor(16 + sz*sr);
}
#line 520 "bigverb.org"
static void delay_init(sk_bigverb_delay *d,
                       const struct bigverb_paramset *p,
                       SKFLT *buf,
                       size_t sz,
                       int sr)
{
    SKFLT readpos;
#line 541 "bigverb.org"
d->buf = buf;
d->sz = sz;
#line 554 "bigverb.org"
d->wpos = 0;
#line 578 "bigverb.org"
d->rng = p->seed;
#line 592 "bigverb.org"
readpos = ((SKFLT)p->delay / 44100);
readpos += d->rng * (p->drift * 0.0001) / 32768.0;
readpos = sz - (readpos * sr);
d->irpos = floor(readpos);
d->frpos = floor((readpos - d->irpos) * FRACSCALE);
#line 578 "bigverb.org"
#line 592 "bigverb.org"
#line 886 "bigverb.org"
d->inc = 0;
d->counter = 0;
#line 935 "bigverb.org"
d->maxcount = round((sr / ((SKFLT)p->randfreq * 0.001)));
#line 973 "bigverb.org"
d->dels = p->delay / 44100.0;
#line 983 "bigverb.org"
d->drift = p->drift;
#line 604 "bigverb.org"
generate_next_line(d, sr);
#line 1034 "bigverb.org"
d->y = 0.0;
#line 528 "bigverb.org"
}
#line 628 "bigverb.org"
static SKFLT delay_compute(sk_bigverb_delay *del,
                           SKFLT in,
                           SKFLT fdbk,
                           SKFLT filt,
                           int sr)
{
    SKFLT out;
    SKFLT frac_norm;
    SKFLT a, b, c, d;
    SKFLT s[4];
    out = 0;
#line 666 "bigverb.org"
del->buf[del->wpos] = in - del->y;
#line 628 "bigverb.org"
#line 674 "bigverb.org"
del->wpos++;
if (del->wpos >= del->sz) del->wpos -= del->sz;
#line 628 "bigverb.org"
#line 686 "bigverb.org"
if (del->frpos >= FRACSCALE) {
    del->irpos += del->frpos >> FRACNBITS;
    del->frpos &= FRACMASK;
}
#line 628 "bigverb.org"
#line 696 "bigverb.org"
if (del->irpos >= del->sz) del->irpos -= del->sz;
#line 628 "bigverb.org"
#line 705 "bigverb.org"
frac_norm = del->frpos / (SKFLT)FRACSCALE;
#line 628 "bigverb.org"
#line 718 "bigverb.org"
{
    SKFLT tmp[2];
    d = ((frac_norm * frac_norm) - 1) / 6.0;
    tmp[0] = ((frac_norm + 1.0) * 0.5);
    tmp[1] = 3.0 * d;
    a = tmp[0] - 1.0 - d;
    c = tmp[0] - tmp[1];
    b = tmp[1] - frac_norm;
}
#line 628 "bigverb.org"
#line 737 "bigverb.org"
{
    int n;
    SKFLT *x;
    n = del->irpos;
    x = del->buf;

    if (n > 0 && n < (del->sz - 2)) {
        s[0] = x[n - 1];
        s[1] = x[n];
        s[2] = x[n + 1];
        s[3] = x[n + 2];
    } else {
        int k;
        n--;
        if (n < 0) n += del->sz;
        s[0] = x[n];
        for (k = 0; k < 3; k++) {
            n++;
            if (n >= del->sz) n -= del->sz;
            s[k + 1] = x[n];
        }
    }
}
#line 628 "bigverb.org"
#line 772 "bigverb.org"
out = (a*s[0] + b*s[1] + c*s[2] + d*s[3]) * frac_norm + s[1];
#line 628 "bigverb.org"
#line 780 "bigverb.org"
del->frpos += del->inc;
#line 628 "bigverb.org"
#line 790 "bigverb.org"
out *= fdbk;
out += (del->y - out) * filt;
del->y = out;
#line 628 "bigverb.org"
#line 817 "bigverb.org"
del->counter--;
if (del->counter <= 0) {
    generate_next_line(del, sr);
}
#line 650 "bigverb.org"
    return out;
}
#line 1008 "bigverb.org"
static void generate_next_line(sk_bigverb_delay *d, int sr)
{
    SKFLT curdel;
    SKFLT nxtdel;
    SKFLT inc;
#line 915 "bigverb.org"
if (d->rng < 0) d->rng += 0x10000;
/* 5^6 = 15625 */
d->rng = (1 + d->rng * 0x3d09);
d->rng &= 0xFFFF;
if (d->rng >= 0x8000) d->rng -= 0x10000;
#line 1008 "bigverb.org"
#line 940 "bigverb.org"
d->counter = d->maxcount;
#line 1008 "bigverb.org"
#line 950 "bigverb.org"
curdel = d->wpos -
    (d->irpos + (d->frpos/(SKFLT)FRACSCALE));
while (curdel < 0) curdel += d->sz;
curdel /= sr;
#line 961 "bigverb.org"
nxtdel = (d->rng * (d->drift * 0.0001) / 32768.0) + d->dels;
#line 1008 "bigverb.org"
#line 994 "bigverb.org"
inc = ((curdel - nxtdel) / (SKFLT)d->counter)*sr;
inc += 1;
#line 1008 "bigverb.org"
#line 1003 "bigverb.org"
d->inc = floor(inc * FRACSCALE);
#line 1018 "bigverb.org"
}
#line 93 "bigverb.org"