-
Notifications
You must be signed in to change notification settings - Fork 1
/
outformationEOC.lib
709 lines (690 loc) · 26.6 KB
/
outformationEOC.lib
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// =============================================================================
// ========== outformationEOC.lib ==============================================
// =============================================================================
//
// Library of functions for transformation and generation of audio signals. The
// library includes standard techniques such as frequency shifting, artificial
// reverberators with different delay line schemes, and other modulations, as
// well as original techniques such as windowless granular processing based on
// zero-crossing detection. The library also includes self-oscillating systems
// for chaotic behaviours and iterative systems for complex patterns such
// as cellular automata.
//
// The environment prefix is "op".
//
// List of functions:
//
// cheby1N,
// cheby2N,
// eca,
// grains_dl_nhw,
// grains_dl_zc,
// grains_zc,
// lorenz,
// nlfdn,
// pitch_shift,
// pole_mod,
// rev_fdn_smo,
// rev_fdn_pol,
// sampler,
// ssbm,
// time_stretch,
// tvtf.
//
// Copyright (c) 2019-2020, Dario Sanfilippo <sanfilippo.dario at gmail dot com>
// All rights reserved.
declare name "Outformation Library";
declare author "Dario Sanfilippo";
declare copyright "Copyright (c) 2019-2020, Dario Sanfilippo <sanfilippo.dario
at gmail dot com>";
declare version "2.1.0";
declare license "GPLv2.0";
au = library("auxiliaryEOC.lib");
ba = library("basics.lib");
de = library("delays.lib");
d2 = library("delaysEOC.lib");
fi = library("filters.lib");
f2 = library("filtersEOC.lib");
ip = library("informationEOC.lib");
op = library("outformationEOC.lib");
os = library("oscillators.lib");
o2 = library("oscillatorsEOC.lib");
ma = library("maths.lib");
m2 = library("mathsEOC.lib");
ro = library("routes.lib");
si = library("signals.lib");
st = library("stabilityEOC.lib");
// op.cheby1N(N, x[n]); --------------------------------------------------------
//
// Chebyshev polynomials of first type for nonlinear distortion.
//
// 1 inputs:
// x[n], input signal.
//
// 1 outputs:
// y[n], output signal.
//
// 1 compile-time argument:
// N, the order of the polynomial.
cheby1N(0, x) = 1;
cheby1N(1, x) = x;
cheby1N(N, x) = 2 * x * cheby1N(N - 1, x) - cheby1N(N - 2, x);
// -----------------------------------------------------------------------------
// op.cheby2N(N, x[n]); --------------------------------------------------------
//
// Chebyshev polynomials of second type for nonlinear distortion.
//
// 1 inputs:
// x[n], input signal.
//
// 1 outputs:
// y[n], output signal.
//
// 1 compile-time argument:
// N, the order of the polynomial.
cheby2N(0, x) = 1;
cheby2N(1, x) = 2 * x;
cheby2N(N, x) = 2 * x * cheby2N(N - 1, x) - cheby2N(N - 2, x);
// -----------------------------------------------------------------------------
// op.eca(L, R, I, rate[n]); ---------------------------------------------------
//
// One-dimension, two-state, elementary cellular automata with circular
// lattice. The function is defined by the length of the lattice, a rule, and
// an initial condition. Additionally, the function has a "rate" parameter
// that determines the interval between iterations. The rule and the initial
// condition are positive INTs that are converted into binary numbers and
// accordingly zero-padded or limited to reach a binary string of
// appropriate length.
//
// Ref:
// Wolfram, S. (1984). Cellular automata as models of complexity. Nature,
// 311(5985), 419-424.
//
// Wolfram, S. (2018). Cellular automata and complexity: collected papers.
// CRC Press.
//
// 1 inputs:
// rate[n], iteration rate.
//
// L outputs:
// y1[n];
// y2[n];
// ...;
// yL[n], states of the cells in the lattice.
//
// 3 compile-time arguments:
// L, (positive INT) size of the lattice (number of cells);
// R, (positive INT up to 255) rule applied to the 8 possible cases;
// I, (positive INT) initial condition for the cells.
//
eca(L, R, I) = ( si.bus(L) ,
init(I) : ro.interleave(L, 2) :
par(i, L, +) : iterate)
~ si.bus(L)
with {
wrap(M, N) = int(ma.frac(N / M) * M);
w_num = m2.zeropad_up(int(8 - ceil(ma.log2(R1))), m2.dec2bin(R1))
with {
R1 = min(255, R);
};
init(N) = m2.zeropad_up(int(L - (floor(ma.log2(N1)) + 1)),
m2.dec2bin(N1)) : par(i, L, _ <: _ - mem)
with {
N1 = min(N, 2 ^ L - 1);
};
rule(x1, x2, x3) =
ba.if( c1, w_num : route(8, 1, 1, 1),
ba.if( c2, w_num : route(8, 1, 2, 1),
ba.if( c3, w_num : route(8, 1, 3, 1),
ba.if( c4, w_num : route(8, 1, 4, 1),
ba.if( c5, w_num : route(8, 1, 5, 1),
ba.if( c6, w_num : route(8, 1, 6, 1),
ba.if( c7, w_num : route(8, 1, 7, 1),
w_num : route(8, 1, 8, 1))))))))
with {
c1 = (x1 == 1) & (x2 == 1) & (x3 == 1);
c2 = (x1 == 1) & (x2 == 1) & (x3 == 0);
c3 = (x1 == 1) & (x2 == 0) & (x3 == 1);
c4 = (x1 == 1) & (x2 == 0) & (x3 == 0);
c5 = (x1 == 0) & (x2 == 1) & (x3 == 1);
c6 = (x1 == 0) & (x2 == 1) & (x3 == 0);
c7 = (x1 == 0) & (x2 == 0) & (x3 == 1);
c8 = (x1 == 0) & (x2 == 0) & (x3 == 0);
};
iterate = si.bus(L) <:
par(i, L, route(L, 3, wrap(L, i - 1) + 1, 1,
i + 1, 2,
wrap(L, i + 1) + 1, 3) : int(rule));
};
// -----------------------------------------------------------------------------
// op.grains_dl_nhw(S, P[n], R[n], pos[n], E[n], x[n]); ------------------------
//
// Granulator based on delay lines with overlap-add to 1 and non-homogeneous
// windowing and transposition. Hence, for nonlinear factors other than 1
// (the exponent), the windowing function is asymmetrical and the
// reading of each grain includes a pitch modulation.
//
// 5 inputs:
// P[n], linear pitch factor (1 for no transposition; 2 for an octave
// up; .5 for an octave down);
// R[n], amount of grains per second;
// pos[n], position of the grain in the buffer in the range [0; S],
// where "S" is the size of the buffer in seconds;
// E[n], exponent, nonlinearity for the windowing and pitch modulation;
// x[n].
//
// 1 ouputs:
// y[n], granulated x[n].
//
// 1 compile-time arguments:
// S, size of the buffer in seconds, which is converted into the
// closest power-of-two samples that represent such length.
//
grains_dl_nhw(size, pitch, rate, position, exponent, x) = head1 + head2
with {
s = size * ma.SR : m2.round_pow2 / ma.SR;
sah(t, in) = ba.sAndH(m2.diff(t) < 0, in);
ph0 = o2.ph(rate, 0);
ph1 = ma.decimal(pow(ph0, exponent));
ph2 = ma.decimal(ph1 + .5);
w1 = m2.window_hann(ph1);
w2 = m2.window_hann(ph2);
head1 = d2.del_pol(s, del1, x) * w1
with {
del1 = sah(ph1, position) + shift1 : m2.wrap(0, s);
shift1 = (1 - sah(ph1, pitch)) *
m2.div(1, sah(ph1, rate)) * ph1;
};
head2 = d2.del_pol(s, del2, x) * w2
with {
del2 = sah(ph2, position) + shift2 : m2.wrap(0, s);
shift2 = (1 - sah(ph2, pitch)) *
m2.div(1, sah(ph2, rate)) * ph2;
};
};
// -----------------------------------------------------------------------------
// op.grains_dl_zc(V, S, P[n], R[n], pos[n], x[n]); ----------------------------
//
// Delay-line-based windowless (rectangular window) granulator that
// handles discontinuities through zero-crossing detection.
//
// Ref: https://tmblr.co/Zhtq9xYAy2bPee00;
// https://tmblr.co/Zhtq9x2i76aPG.
//
// 4 inputs:
// P[n], linear pitch factor (1 for no transposition; 2 for an octave
// up; .5 for an octave down);
// R[n], amount of grains per second;
// pos[n], position of the grain in the buffer in the range [0; S],
// where "S" is the size of the buffer in seconds;
// x[n].
//
// 1 outputs:
// y[n], granulated x[n].
//
// 2 compile-time arguments:
// V, number of voices;
// S, size of the buffer in seconds, which is converted into the
// closest power-of-two samples that represent such length.
//
grains_dl_zc(voices, size1) = par(i, voices, loop
~ _) :> / (voices)
with {
loop(out, pitch1, rate1, position1, input) =
(ba.sAndH(trigger(out), zc_index(position, input, out))
+ shift(trigger(out))) : m2.wrap(0, size) - 1 ,
input : grain
with {
trigger(y) = loop
~ _
with {
loop(ready) =
ip.zc(y) ,
(m2.line_reset(ba.sAndH(au.dirac + ready, rate),
ready) >= 1) : &;
};
shift(reset) = m2.div(1 - pitch, rate) *
m2.line_reset(rate, reset) * ma.SR;
zc_index(recall, x, y) =
index - m2.if(m2.diff(y) >= 0, zc_up, zc_down) :
m2.wrap(0, size)
with {
zc_up = ba.sAndH(store, index), recall : dl
with {
store = ip.zc(x) ,
(m2.diff(x) > 0) : &;
};
zc_down = ba.sAndH(store, index), recall : dl
with {
store = ip.zc(x) ,
(m2.diff(x) < 0) : &;
};
};
size = size1 * ma.SR : m2.round_pow2;
rate = abs(rate1);
pitch = ba.sAndH(trigger(out), pitch1);
position = position1 * ma.SR : m2.wrap(0, size);
index = ba.period(size);
grain(del, in) = de.fdelayltv(4, size, del, in);
dl(in, del) = de.delay(size, del, in);
};
};
// -----------------------------------------------------------------------------
// op.grains_zc(pos[n], size[n], x[n]); ----------------------------------------
//
// Table-based windowless (rectangular window) granulator that
// handles discontinuities through zero-crossing detection.
//
// Ref: https://tmblr.co/Zhtq9xYAy2bPee00;
// https://tmblr.co/Zhtq9x2i76aPG.
//
// 3 inputs:
// pos[n], position of the grain in the buffer in the range [0; S],
// where "S" is the size of the buffer in seconds;
// size[n], size of grains in seconds;
// x[n].
//
// 1 outputs:
// y[n], granulated x[n].
//
grains_zc(position, g_size, x) = grains
~ _
with {
s = 768000;
l = g_size * ma.SR;
p = position * ma.SR;
input = x;
rec_index = ba.period(s);
grains(fb) = int(s) ,
0.0 ,
int(rec_index) ,
input ,
int(read_frame(fb) % s) : rwtable
with {
sel_zc(x) =
ba.if(m2.diff(x) > 0, zc_up_index, zc_down_index);
frame(start) = % (dur)
~ + (1)
with {
dur = zc_index(start + l) - start : max(2);
};
read_frame(out) = (frame <: _ ,
(== (0) ,
sel_zc(out) : ba.sAndH))
~ ( ! ,
_) : +;
zc_up = (ip.zc(input) ,
(m2.diff(input) > 0) : &);
zc_down = (ip.zc(input) ,
(m2.diff(input) < 0) : &);
zc_index(x) = int(s) ,
0.0 ,
int(rec_index) ,
( ip.zc(input) ,
rec_index : ba.sAndH) ,
int(x % s) : rwtable;
zc_up_index = int(s) ,
0.0 ,
int(rec_index) ,
( zc_up ,
rec_index : ba.sAndH) ,
int(p % s) : rwtable;
zc_down_index = int(s) ,
0.0 ,
int(rec_index) ,
( zc_down ,
rec_index : ba.sAndH) ,
int(p % s) : rwtable;
};
};
// -----------------------------------------------------------------------------
// op.gru(Wf[n], Wh[n], Uf[n], Uh[n], bf[n], x[n]); ----------------------------
//
// Gated recurrent unit as nonlinear distortion.
//
// See https://jatinchowdhury18.medium.com/complex-nonlinearities-episode-10-gated-recurrent-distortion-6d60948323cf.
//
// 6 inputs:
// Wf[n], Wh[n], Uf[n], Uh[n], bf[n], shaping parameters;
// x[n], input signal.
//
// 1 outputs:
// y[n], distorted input.
//
gru(Wf, Wh, Uf, Uh, bf, x) = loop
~ _
with {
loop(s) = f * s + (1 - f) * ma.tanh(Wh * x + Uh * f * s)
with {
f = m2.sigmoid(Wf * x + Uf * s + bf);
};
};
// -----------------------------------------------------------------------------
// op.lorenz(x0, y0, z0, a[n], b[n], r[n], dt[n]); -----------------------------
//
// Lorenz system: chaotic recursive system of differential equations.
//
// Ref: https://ijpam.eu/contents/2013-83-1/9/9.pdf.
//
// Try process = o2.lorenz(1.2, 1.3, 1.6, 10, 8/3, 28, .005); for a strange
// attractor (way out of the [-1; 1] range).
//
// 7 inputs:
// x0, initial condition for the first equation (0 for n != 0);
// y0, initial condition for the second equation (0 for n != 0);
// z0, initial condition for the third equation (0 for n != 0);
// a[n], coefficient in the first equation;
// b[n], coefficient in the third equation;
// r[n], coefficient in the second equation;
// dt[n], discrete time interval.
//
// 3 output:
// y1[n], first equation;
// y2[n], second equation;
// y3[n], third equation.
//
lorenz(x0, y0, z0, a, b, r, dt) = iterate
~ ( _ ,
_ ,
_)
with {
iterate(x, y, z) = x1 + a * (y1 - x1) * dt,
y1 + (r * x1 - y1 - x1 * z1) * dt,
z1 + (x1 * y1 - b * z1) * dt
with {
x1 = x + x0 - x0';
y1 = y + y0 - y0';
z1 = z + z0 - z0';
};
};
// -----------------------------------------------------------------------------
// op.nlfdn(N, D, F, matrix, s[n], fb[n], x1[n], ..., xN[n]); ------------------
//
// Feedback delay network model with nonlinear processing. The function
// creates a standard FDN with arbitrary one-input, one-output nonlinear
// function that is passed as an argument, as well as a "matrix" function,
// that is an N-inputs, N-outputs function to implement different
// network topologies and structures. For example: ro.hadamard(N).
// Other matrices types can be found in mathsEOC.lib. Furthermore, the
// function is also given a list of N values representing the delays of the
// delay lines. Several number sequences are available in mathsEOC.lib. The
// function also has a 'stretch' parameter to compress or expand the
// delays, as well as a global feedback coefficient input. For
// self-oscillating behaviours, bounded nonlinear functions should be used.
// Otherwise, non-bounded nonlinear (or linear) functions can be combined with
// the stability processors in stabilityEOC.lib.
//
// 2 + N inputs:
// s[n], stretch factor for the delays;
// fb[n], global feedback coefficient – note that this should be
// scaled down according to the properties of the matrix that
// you choose;
// x1[n], ..., xN[n], inputs.
//
// N outputs:
// y1[n], ..., yN[n], outputs.
//
// 3 compile-time arguments:
// N, order of the network;
// D, list of N values determining the delay lengths in samples;
// F, the linear or nonlinear function in the feedback loops;
// matrix, a N-inputs, N-outputs function to implement different
// network topologies and structures. For example: ro.hadamard(N).
// Other matrices types can be found in mathsEOC.lib.
//
nlfdn(N, D, F, matrix, s, fb) = ins
~ (delays : matrix : nltf)
with {
ins = ro.interleave(N, 2) : par(i, N, +);
nltf = par(i, N, F);
delays = par(i, N, de.fdelayltv(4, ba.take(i + 1, S),
ba.take(i + 1, D) * s) * fb)
with {
S = par(i, N, ba.take(i + 1, D) : m2.next_pow2);
};
};
// -----------------------------------------------------------------------------
// op.pitch_shift(S, P[n], size[n], x[n]); -------------------------------------
//
// Real-time pitch-shifter using 4th-order Lagrange polynomial fractional
// delay lines.
//
// 3 inputs:
// P[n], linear pitch factor (1 for no transposition; 2 for an octave
// up; .5 for an octave down);
// size[n], size of frames in seconds;
// x[n].
//
// 1 outputs:
// y[n], pitch-shifted x[n].
//
// 1 compile-time arguments:
// S, size of the buffer in seconds, which is converted into the
// closest power-of-two samples that represent such length.
//
pitch_shift(buff_size, factor, frame, x) =
d2.del_pol(buff_size, del1, x) * w1 ,
d2.del_pol(buff_size, del2, x) * w2 :> _
with {
frame_1 = abs(frame);
rate = m2.div(1, frame_1);
shift = (1 - factor) * frame_1;
offset = m2.if(shift < 0, -shift, 0);
limit = m2.round_pow2(buff_size * ma.SR) / ma.SR;
ph1 = m2.ph(rate, 0);
ph2 = ma.decimal(ph1 + .5);
w1 = m2.window_hann(ph1);
w2 = m2.window_hann(ph2);
del1 = shift * ph1 + offset : m2.wrap(0, limit);
del2 = shift * ph2 + offset : m2.wrap(0, limit);
};
// -----------------------------------------------------------------------------
// op.pole_mod(R[n], E[n], x[n]); ----------------------------------------------
//
// Pole modulation of normalised one-pole system, hence oscillating
// between lowpass and highpass. The modulator has a shaping parameter
// going from -1 to 1 where the we have squarewave at -1, a sinewave at 0,
// and impulses at 1.
//
// 3 inputs:
// R[n], modulation rate in Hz;
// E[n], shaping parameter in the range [-1; 1];
// x[n].
//
// 1 outputs:
// y[n], pole-modulated x[n].
//
pole_mod(rate, shaping, x) = x * norm : fi.pole(mod)
with {
norm = 1 - abs(mod);
mod = os.osc(rate) <: ma.signum * (abs : pow(shaping1))
with {
shaping1 = pow(1000, st.clip(-1, 1, shaping));
};
};
// -----------------------------------------------------------------------------
// op.rev_fdn_smo(N, S, IT[n], size[n], FB[n], CF[n], x[n]); -------------------
//
// Elementary Nth-order feedback delay network reverb with non-transposing
// variable delay lines.
//
// 5 inputs:
// IT[n], interpolation time in seconds to transition between
// different delays;
// size[n], exponent for as many prime numbers as the order of the
// network, the result of which determines the length of the
// delay lines in seconds;
// FB[n], feedback coefficient, whose magnitude should be less or
// equal to 1 for stability;
// CF[n], cut-off frequency in Hz, of lowpass filters within the
// feedback loop that model the dampening of high freqiencies.
//
// 1 outputs:
// y[n], normalised sum of the N signals in the network.
//
// 2 compile-time arguments:
// N, order of the network (INT);
// S, max size of the delay lines in seconds.
//
rev_fdn_smo(N, max_size, it, size, fb_coeff, cf, in) =
(summing : delays : filters : matrix : fb)
~ si.bus(N) :> / (N)
with {
st = 1 / sqrt(N);
summing = par(i, N, + (in));
delays = par(i, N, max_size ,
it ,
( size : m2.prime_base_pow(i + 1)) ,
_) : par(i, N, d2.del_smo);
filters = par(i, N, f2.lp1p(cf));
matrix = ro.hadamard(N);
fb = par(i, N, * (fb_coeff * st));
};
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// Feedback delay network reverb with variable DL;
// n must be a power of 2; FB coeffients are stable up to a magnitude of 1
// op.rev_fdn_pol(N, S, size[n], FB[n], CF[n], x[n]); --------------------------
//
// Elementary Nth-order feedback delay network reverb with transposing
// variable delay lines (4th-order Lagrange interpolation).
//
// 5 inputs:
// size[n], exponent for as many prime numbers as the order of the
// network, the result of which determines the length of the
// delay lines in seconds;
// FB[n], feedback coefficient, whose magnitude should be less or
// equal to 1 for stability;
// CF[n], cut-off frequency in Hz, of lowpass filters within the
// feedback loop that model the dampening of high freqiencies.
//
// 1 outputs:
// y[n], normalised sum of the N signals in the network.
//
// 2 compile-time arguments:
// N, order of the network (INT);
// S, max size of the delay lines in seconds.
//
rev_fdn_pol(n, max_size, size, fb_coeff, cf, in) =
(summing : delays : filters : matrix : fb)
~ si.bus(n) :> /(n)
with {
st = 1 / sqrt(n);
summing = par(i, n, + (in));
delays = par(i, n, max_size ,
( size : m2.prime_base_pow(i + 1)),
_) : par(i, n, d2.del_pol);
filters = par(i, n, f2.lp1p(cf));
matrix = ro.hadamard(n);
fb = par(i, n, * (fb_coeff * st));
};
// -----------------------------------------------------------------------------
// op.sampler(S, size[n], pos[n], P[n], x[n]); ---------------------------------
//
// Sampler with pitch, frame size, and buffer position control.
//
// 4 inputs:
// size[n], frame size in seconds;
// pos[n], position of the frame in the buffer in the range [0; S],
// where S is the size of the buffer in seconds;
// P[n], pitch factor;
// x[n].
//
// 1 outputs:
// y[n], sampled x[n].
//
// 1 compile-time arguments:
// S, size of the buffer in seconds, which is converted into the
// closest power-of-two samples that represent such length.
//
sampler(buff_size, frame, position, factor, x) = d2.del_pol(buff_size, del, x)
with {
frame_1 = abs(frame) : f2.lp1p(20);
position_1 = position : f2.lp1p(20);
rate = m2.div(1, frame_1);
shift = (1 - factor) * frame_1;
offset = m2.if(shift < 0, -shift, 0);
limit = buff_size * ma.SR : m2.round_pow2 / ma.SR;
ph = m2.ph(rate, 0);
del = shift * ph + offset+position_1 : m2.wrap(0, limit);
};
// -----------------------------------------------------------------------------
// op.ssbm(F[n], x[n]); --------------------------------------------------------
//
// Single-sideband modulation (positive side).
//
// 2 inputs:
// F[n], frequency shift in Hz;
// x[n].
//
// 1 outputs:
// y[n], frequency-shifted x[n].
//
ssbm(shift, in) = f2.analytic(in) ,
o2.osc_quad(shift) : si.cmul : _ ,
!;
// -----------------------------------------------------------------------------
// op.time_stretch(S, size[n], T[n], x[n]); ------------------------------------
//
// Real-time time stretcher with delay lines.
//
// 3 inputs:
// size[n], frame size in seconds;
// T[n], time stretching factor;
// x[n].
//
// 1 outputs:
// y[n], time-stretched x[n].
//
// 1 compile-time arguments:
// S, size of the buffer in seconds, which is converted into the
// closest power-of-two samples that represent such length.
//
time_stretch(buff_size, frame, factor, x) =
d2.del_pol(buff_size, del1, x) * w1 ,
d2.del_pol(buff_size, del2, x) * w2 :> _
with {
buff = buff_size * ma.SR : m2.round_pow2 / ma.SR;
position = m2.ph((1 - factor) / buff, 0) * buff;
frame_1 = abs(frame);
rate = m2.div(1, frame_1);
ph1 = m2.ph(rate, 0);
ph2 = ma.decimal(ph1 + .5);
w1 = m2.window_hann(ph1);
w2 = m2.window_hann(ph2);
del1 = position : ba.sAndH(m2.diff(ph1) < 0);
del2 = position : ba.sAndH(m2.diff(ph2) < 0);
};
// -----------------------------------------------------------------------------
// op.tvtf(S, ZCR[n], TF[n], x[n]); --------------------------------------------
//
// Time-variant transfer function: the transfer function is determined
// by an incoming signal. The input signal is wrapped around in the
// range [-1; 1]; -1 corresponds to the beginning of the transfer
// function, 0 is the centre of the buffer, whereas 1 is the upper edge.
//
// The input signal that determines the transfer function is lowpassed
// to control the number of zero-crossings in the transfer function, which
// correlates to the number added partials, [Roads 1979] and normalised to
// unit-amplitude peaks.
//
// 3 inputs:
// ZCR[n], (roughly) number of zero-crossings in the transfer
// function;
// TF[n], signal writing the transfer function;
// x[n].
//
// 1 outputs:
// y[n], x[n] processed through the transfer function.
//
// 1 compile-time arguments:
// S, size of the buffer in seconds, which is converted into the
// closest power-of-two samples that represent such length.
//
tvtf(s, zcr, f, in) = d2.del_pol(s, in1, f1)
with {
in1 = in : m2.wrap(-1, 1) : m2.uni * s1;
s1 = s * ma.SR : m2.round_pow2 / ma.SR;
f1 = f : seq(i, 4, f2.lp1p(zcr / s1)) : st.dyn_norm_peak(1 / s1, 1);
};
// -----------------------------------------------------------------------------