lib/Peta/Synth/Engine/Perl.pm

filter: Statementstmt 99.7% 459 / 460 Branchbran 93.6% 88 / 94 Conditioncond 100% 8 / 8 MC/DCmcdc 100% 5 / 5 Subroutinesub 100% 15 / 15 Podpod 100% 9 / 9 totaltotal 98.8% 584 / 591 CC 72 SCAR 27.3
CC
72
Cov
99%
render
31.0
string
10.0
_band4
8.0
CRAP 15.3
« lib/Peta/Synth/Synth.pm ↑ Summary lib/Peta/MIDI/Device/Out.pm »
1 package Peta::Synth::Engine::Perl;
2 # ABSTRACT: the voices rendered in Perl
3  
4 # The engine in Perl: the reference every other engine is measured against,
5 # and on pperl a fast one, since its loops are what the JIT compiles: plain
6 # lexical arrays of floats, float arithmetic, sin and exp, no calls.
7 #
8 # A partial is a rotating phasor: (c, s) turned by the partial's angle per
9 # sample, which costs four multiplications where sin() would cost a call;
10 # its angle and starting phase are set anew at each control block, so the
11 # pitch follows the voice's controls and the error never accumulates.
12  
13 15 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Peta::Synth::Voice;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 15 our $VERSION = '0.2610180';
17  
18 15 ▶use constant BLOCK => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use constant TWO_PI => 6.283185307179586;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 38 ▶sub new ($class, %arg) { return bless { %arg }, $class }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
22 9 ▶sub name ($self) { return 'perl' }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
23  
24 # The voices' next $n samples, mixed: (\@left, \@right, \@send). For each
25 # voice its partials (or its wave sequences), string, FM and noise summed,
26 # then mixed under its envelope and gains. One loop over the voices with
27 # the partials and the mix written into it: as subs of their own, with the
28 # samples passed by reference, they take a tenth longer under pperl's JIT.
29 10884 ▶sub render ($self, $voices, $n, $rate) {
Subroutine render: called CC=31 SCAR=34.3
Pod documented
30 10884 my $B = BLOCK;
31 10884 my $nb = $n / $B;
32 10884 my @left = (0.0) x $n;
33 10884 my @right = (0.0) x $n;
34 10884 my @send = (0.0) x $n;
35 10884 my $dt = $B / $rate;
36  
37 10884 for my $voice (@$voices) {
38 12376 my @x = (0.0) x $n;
39 12376 my $first = $voice->{first};
40 12376 my $t0 = $voice->{t0};
41 12376 my @w = @{ $voice->{w} };
42 12376 my @ratio = @{ $voice->{ratio} };
43 12376 my @amp = @{ $voice->{amp} };
44 12376 my @decay = @{ $voice->{decay} };
45 12376 my @attack = @{ $voice->{attack} };
46 12376 my @phase = @{ $voice->{phase} };
47 12376 ▶ my $count = $voice->{wave} ? 0 : scalar @ratio; # read from wave sequences instead
Branch: 1 branch
BranchTrueFalseTotal
$voice->{'wave'} ? : 2832 9544 12376
48 # The filter's gain at every partial's edges, H[(nb + 1) k + j]
49 # (Peta::Synth::Voice::filter_gain written out: no calls, which
50 # pperl's JIT would not compile).
51 12376 ▶ my $fmode = $voice->{fmode} // 0;
Condition100%2/2
$voice->{"fmode"} // 0
l!l
29112085
MC/DC100%1/1
$voice->{"fmode"} // 0
$voice->{"fmode"}
Truth table100%2/2
A
A= $voice->{"fmode"}
Aresult
11
00
52 12376 my $edges = $nb + 1;
53 12376 ▶ my @H = (1.0) x ($fmode ? $count * $edges : 0);
Branch: 1 branch
BranchTrueFalseTotal
$fmode ? : 205 12171 12376
54 12376 ▶ if ($fmode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fmode) 205 12171 12376
55 205 my @fx = @{ $voice->{fx} };
56 205 my @fq = @{ $voice->{fq} };
57 205 for my $k (0 .. $count - 1) {
58 5115 my $r = $ratio[$k];
59 5115 for my $j ($first .. $nb) {
60 42431 my $x = $r * $fx[$j];
61 42431 my $q = $fq[$j];
62 42431 my $x2 = $x * $x;
63 42431 my $u = 1.0 - $x2;
64 42431 my $d = sqrt($u * $u + $x2 / ($q * $q));
65 42431 my $h = 1.0;
66 42431 ▶ if ($fmode == 1) { $h = 1.0 / $d }
Branch: 4 branches
BranchTrueFalseTotal
if ($fmode == 1) { } 2075 40356 42431
elsif ($fmode == 2) { } 27436 12920 40356
elsif ($fmode == 3) { } 6480 6440 12920
elsif ($fmode == 4) { } 0 6440 6440
67 27436 elsif ($fmode == 2) { $h = 1.0 / ($d * sqrt(1.0 + $x2 * $x2)) }
68 6480 elsif ($fmode == 3) { $h = $x2 / $d }
69 0 elsif ($fmode == 4) { $h = $x2 * $x2 / ($d * sqrt(1.0 + $x2 * $x2)) }
70 6440 else { $h = $x / $q / $d }
71 42431 $H[ $k * $edges + $j ] = $h;
72 }
73 }
74 }
75  
76 # The partials, each a rotating phasor, its angle and phase set anew at
77 # each block; its amplitude at the edges, interpolated across a block.
78 12376 for my $k (0 .. $count - 1) {
79 101612 my $r = $ratio[$k];
80 101612 my $a = $amp[$k];
81 101612 my $d = $decay[$k];
82 101612 my $att = $attack[$k];
83 101612 my $p = $phase[$k];
84 101612 my $t = $t0 + $first * $dt;
85 101612 my $a0 = $a * exp(-$d * $t);
86 101612 ▶ $a0 *= 1.0 - exp(-$t / $att) if $att > 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if $att > 0 7896 93716 101612
87 101612 ▶ $a0 *= $H[ $k * $edges + $first ] if $fmode;
Branch: 1 branch
BranchTrueFalseTotal
if $fmode 5115 96497 101612
88 101612 for my $j ($first .. $nb - 1) {
89 857520 my $t1 = $t + $dt;
90 857520 my $a1 = $a * exp(-$d * $t1);
91 857520 ▶ $a1 *= 1.0 - exp(-$t1 / $att) if $att > 0.0;
Branch: 1 branch
BranchTrueFalseTotal
if $att > 0 63168 794352 857520
92 857520 ▶ $a1 *= $H[ $k * $edges + $j + 1 ] if $fmode;
Branch: 1 branch
BranchTrueFalseTotal
if $fmode 37316 820204 857520
93 857520 my $wk = $r * $w[$j];
94 857520 my $c = cos($p);
95 857520 my $s = sin($p);
96 857520 my $cw = cos($wk);
97 857520 my $sw = sin($wk);
98 857520 my $g = $a0;
99 857520 my $dg = ($a1 - $a0) / $B;
100 857520 my $i = $j * $B;
101 857520 my $end = $i + $B;
102 857520 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 54881280 857520 55738800
103 54881280 $x[$i] += $g * $s;
104 54881280 my $c2 = $c * $cw - $s * $sw;
105 54881280 $s = $s * $cw + $c * $sw;
106 54881280 $c = $c2;
107 54881280 $g += $dg;
108 54881280 $i++;
109 }
110 857520 $p += $wk * $B;
111 857520 $p -= TWO_PI * int($p / TWO_PI);
112 857520 $a0 = $a1;
113 857520 $t = $t1;
114 }
115 101612 $phase[$k] = $p;
116 }
117 12376 $voice->{phase} = \@phase;
118  
119 12376 ▶ if ($voice->{wave}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($voice->{"wave"}) 2832 9544 12376
120 2832 my $waves = waves($voice, $n, $nb, $rate);
121 2832 for my $i ($first * $B .. $n - 1) { $x[$i] += $waves->[$i] }
122 }
123 12376 ▶ if ($voice->{string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($voice->{"string"}) 619 11757 12376
124 619 my $string = string($voice, $n, $nb);
125 619 for my $i ($first * $B .. $n - 1) { $x[$i] += $string->[$i] }
126 }
127 12376 ▶ if ($voice->{fm}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($voice->{"fm"}) 208 12168 12376
128 208 my $fm = fm($voice, $n, $nb);
129 208 for my $i ($first * $B .. $n - 1) { $x[$i] += $fm->[$i] }
130 }
131 12376 ▶ if ($voice->{rough}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($voice->{"rough"}) 38 12338 12376
132 38 my $am = rough($voice, $n, $nb);
133 38 for my $i ($first * $B .. $n - 1) { $x[$i] *= $am->[$i] }
134 }
135 12376 ▶ if ($voice->{noise}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($voice->{"noise"}) 10348 2028 12376
136 10348 my $noise = noise($voice, $n, $nb);
137 10348 for my $i ($first * $B .. $n - 1) { $x[$i] += $noise->[$i] }
138 }
139  
140 # The envelope and the channel's gains, interpolated across each block.
141 12376 my @env = @{ $voice->{env} };
142 12376 my @gl = @{ $voice->{gl} };
143 12376 my @gr = @{ $voice->{gr} };
144 12376 my @gs = @{ $voice->{gs} };
145 12376 for my $j ($first .. $nb - 1) {
146 112027 my $e0 = $env[$j];
147 112027 my $de = ($env[ $j + 1 ] - $e0) / $B;
148 112027 my $l = $gl[$j];
149 112027 my $dl = ($gl[ $j + 1 ] - $l) / $B;
150 112027 my $r = $gr[$j];
151 112027 my $dr = ($gr[ $j + 1 ] - $r) / $B;
152 112027 my $s = $gs[$j];
153 112027 my $ds = ($gs[ $j + 1 ] - $s) / $B;
154 112027 my $i = $j * $B;
155 112027 my $end = $i + $B;
156 112027 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 7169728 112027 7281755
157 7169728 my $y = $x[$i] * $e0;
158 7169728 $left[$i] += $y * $l;
159 7169728 $right[$i] += $y * $r;
160 7169728 $send[$i] += $y * $s;
161 7169728 $e0 += $de;
162 7169728 $l += $dl;
163 7169728 $r += $dr;
164 7169728 $s += $ds;
165 7169728 $i++;
166 }
167 }
168 }
169 10884 return (\@left, \@right, \@send);
170 }
171  
172 # A voice's noise for a quantum: white noise from the table through each
173 # band's filter (a state-variable one: band-, high- or low-pass; with four
174 # poles, a second the same after it), under its level, summed into a fresh
175 # array of $n samples; zero before the voice's first block. The filters'
176 # states are carried on. The PDL engine uses it as it is.
177 13798 ▶sub noise ($voice, $n, $nb) {
Subroutine noise: called CC=8 SCAR=20.8
Pod documented
178 13798 my $B = BLOCK;
179 13798 my $table = Peta::Synth::Voice::noise_table();
180 13798 my $tsize = scalar @$table;
181 13798 my $first = $voice->{first};
182 13798 my @out = (0.0) x $n;
183 13798 for my $band (@{ $voice->{bands} }) {
184 14857 ▶ if ($band->{poles} == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($band->{"poles"} == 4) 240 14617 14857
185 240 _band4(\@out, $band, $first, $nb);
186 240 next;
187 }
188 14617 my @nenv = @{ $band->{env} };
189 14617 my ($ic1, $ic2, $index, $jc1, $jc2) = @{ $band->{state} };
190 14617 my $g = $band->{g};
191 14617 my $kq = $band->{k};
192 14617 my $mode = $band->{mode};
193 14617 my $a1 = 1.0 / (1.0 + $g * ($g + $kq));
194 14617 my $a2 = $g * $a1;
195 14617 my $a3 = $g * $a2;
196 14617 for my $j ($first .. $nb - 1) {
197 130299 my $level = $nenv[$j];
198 130299 my $step = ($nenv[ $j + 1 ] - $level) / $B;
199 130299 my $i = $j * $B;
200 130299 my $end = $i + $B;
201 130299 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 8339136 130299 8469435
202 8339136 my $v0 = $table->[$index];
203 8339136 $index++;
204 8339136 ▶ $index = 0 if $index >= $tsize;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= $tsize 76 8339060 8339136
205 8339136 my $v3 = $v0 - $ic2;
206 8339136 my $v1 = $a1 * $ic1 + $a2 * $v3;
207 8339136 my $v2 = $ic2 + $a2 * $ic1 + $a3 * $v3;
208 8339136 $ic1 = 2.0 * $v1 - $ic1;
209 8339136 $ic2 = 2.0 * $v2 - $ic2;
210 8339136 ▶ my $out = $mode == 0 ? $kq * $v1 : $mode == 1 ? $v0 - $kq * $v1 - $v2 : $v2;
Branch: 2 branches
BranchTrueFalseTotal
$mode == 0 ? : 7080320 1258816 8339136
$mode == 1 ? : 12288 1246528 1258816
211 8339136 $out[$i] += $level * $out;
212 8339136 $level += $step;
213 8339136 $i++;
214 }
215 }
216 14617 $band->{state} = [ $ic1, $ic2, $index, $jc1, $jc2 ];
217 }
218 13798 return \@out;
219 }
220  
221 # A band of four poles: its filter twice. A sub of its own: inside the loop
222 # above, the bands of two poles, which nearly every noise has, render some
223 # 6% slower on pperl (percussion.mid: 2.52 s against 2.38).
224 240 ▶sub _band4 ($out, $band, $first, $nb) {
Subroutine _band4: called CC=8 SCAR=20.8
225 240 my $B = BLOCK;
226 240 my $table = Peta::Synth::Voice::noise_table();
227 240 my $tsize = scalar @$table;
228 240 my @nenv = @{ $band->{env} };
229 240 my ($ic1, $ic2, $index, $jc1, $jc2) = @{ $band->{state} };
230 240 my $g = $band->{g};
231 240 my $kq = $band->{k};
232 240 my $mode = $band->{mode};
233 240 my $a1 = 1.0 / (1.0 + $g * ($g + $kq));
234 240 my $a2 = $g * $a1;
235 240 my $a3 = $g * $a2;
236 240 for my $j ($first .. $nb - 1) {
237 2092 my $level = $nenv[$j];
238 2092 my $step = ($nenv[ $j + 1 ] - $level) / $B;
239 2092 my $i = $j * $B;
240 2092 my $end = $i + $B;
241 2092 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 133888 2092 135980
242 133888 my $v0 = $table->[$index];
243 133888 $index++;
244 133888 ▶ $index = 0 if $index >= $tsize;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= $tsize 0 133888 133888
245 133888 my $v3 = $v0 - $ic2;
246 133888 my $v1 = $a1 * $ic1 + $a2 * $v3;
247 133888 my $v2 = $ic2 + $a2 * $ic1 + $a3 * $v3;
248 133888 $ic1 = 2.0 * $v1 - $ic1;
249 133888 $ic2 = 2.0 * $v2 - $ic2;
250 133888 ▶ my $u0 = $mode == 0 ? $kq * $v1 : $mode == 1 ? $v0 - $kq * $v1 - $v2 : $v2;
Branch: 2 branches
BranchTrueFalseTotal
$mode == 0 ? : 91520 42368 133888
$mode == 1 ? : 30080 12288 42368
251 133888 my $u3 = $u0 - $jc2;
252 133888 my $u1 = $a1 * $jc1 + $a2 * $u3;
253 133888 my $u2 = $jc2 + $a2 * $jc1 + $a3 * $u3;
254 133888 $jc1 = 2.0 * $u1 - $jc1;
255 133888 $jc2 = 2.0 * $u2 - $jc2;
256 133888 ▶ my $y = $mode == 0 ? $kq * $u1 : $mode == 1 ? $u0 - $kq * $u1 - $u2 : $u2;
Branch: 2 branches
BranchTrueFalseTotal
$mode == 0 ? : 91520 42368 133888
$mode == 1 ? : 30080 12288 42368
257 133888 $out->[$i] += $level * $y;
258 133888 $level += $step;
259 133888 $i++;
260 }
261 }
262 240 $band->{state} = [ $ic1, $ic2, $index, $jc1, $jc2 ];
263 240 return;
264 }
265  
266 # A voice's wave sequences (Peta::Synth::Wavetable), copy by copy, block by
267 # block, for a quantum: the table each block reads (the next one is
268 # crossfaded to: the last one holds), the crossfade where the block begins
269 # and its step a sample, the read step (table samples a sample, the copy's
270 # pitch) and where in the period the block begins (table samples, in
271 # double precision, so that no engine drifts); each copy's place and table
272 # carried on to the quantum's end. Every engine reads its samples from these.
273 4891 ▶sub wave_blocks ($voice, $nb, $rate) {
Subroutine wave_blocks: called CC=7 SCAR=19.5
Pod documented
274 4891 my $B = BLOCK;
275 4891 my $dt = $B / $rate;
276 4891 my $first = $voice->{first};
277 4891 my $t0 = $voice->{t0};
278 4891 my @w = @{ $voice->{w} };
279 4891 my @copies;
280 4891 for my $copy (@{ $voice->{wave} }) {
281 7669 my ($shift, $sequence, $p, $k) = @$copy;
282 7669 my $size = $sequence->{size};
283 7669 my @times = @{ $sequence->{times} };
284 7669 my $last = $#times;
285 7669 my $scale = $size / TWO_PI;
286 7669 my @table = (0) x $nb;
287 7669 my @mix = (0.0) x $nb;
288 7669 my @dmix = (0.0) x $nb;
289 7669 my @step = (0.0) x $nb;
290 7669 my @start = (0.0) x $nb;
291 7669 my $t = $t0 + $first * $dt;
292 7669 my $at = $p * $scale;
293 7669 for my $j ($first .. $nb - 1) {
294 64646 ▶ $k++ while $k < $last && $times[ $k + 1 ] <= $t;
Branch: 1 branch
BranchTrueFalseTotal
$k < $last and $times[$k + 1] <= $t and do { ++$k } 927 64646 65573
Condition100%3/3
$k < $last and $times[$k + 1] <= $t
!ll&&!rl&&r
4441420232927
MC/DC100%2/2
$k < $last and $times[$k + 1] <= $t
$k < $last $times[$k + 1] <= $t
Truth table100%3/3
A and B
A= $k < $lastB= $times[$k + 1] <= $t
ABresult
0X0
100
111
295 64646 $table[$j] = $k;
296 64646 ▶ if ($k < $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $last) 20232 44414 64646
297 20232 my $span = $times[ $k + 1 ] - $times[$k];
298 20232 my $m1 = ($t + $dt - $times[$k]) / $span;
299 20232 $mix[$j] = ($t - $times[$k]) / $span;
300 20232 ▶ $dmix[$j] = (($m1 > 1.0 ? 1.0 : $m1) - $mix[$j]) / $B;
Branch: 1 branch
BranchTrueFalseTotal
$m1 > 1.0 ? : 926 19306 20232
301 }
302 64646 $step[$j] = $w[$j] * $shift * $scale;
303 64646 $start[$j] = $at;
304 64646 $at += $step[$j] * $B;
305 64646 $at -= $size * int($at / $size);
306 64646 $t += $dt;
307 }
308 7669 $copy->[2] = $at / $scale;
309 7669 $copy->[3] = $k;
310 7669 push @copies, {
311 sequence => $sequence,
312 size => $size,
313 last => $last,
314 table => \@table,
315 mix => \@mix,
316 dmix => \@dmix,
317 step => \@step,
318 start => \@start,
319 };
320 }
321 4891 return \@copies;
322 }
323  
324 # A voice's partials read from its wave sequences for a quantum, as a fresh
325 # array of $n samples (zero before its first block): for each copy of the
326 # unison, each block's two tables crossfaded across it, read at the copy's
327 # pitch, between samples linearly (wave_blocks says where); a run of blocks
328 # with the same two tables at a time (_read_tables). The PDL engine uses it
329 # as it is.
330 4891 ▶sub waves ($voice, $n, $nb, $rate) {
Subroutine waves: called CC=6 SCAR=17.9
Pod documented
331 4891 my @x = (0.0) x $n;
332 4891 my $first = $voice->{first};
333 4891 for my $c (@{ wave_blocks($voice, $nb, $rate) }) {
334 7669 my $tables = $c->{sequence}{tables};
335 7669 my @table = @{ $c->{table} };
336 7669 my $last = $c->{last};
337 7669 my $j0 = $first;
338 7669 ▶ while ($j0 < $nb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j0 < $nb) 8503 7669 16172
339 8503 my $kr = $table[$j0];
340 8503 my $j1 = $j0 + 1;
341 8503 ▶ $j1++ while $j1 < $nb && $table[$j1] == $kr;
Branch: 1 branch
BranchTrueFalseTotal
$j1 < $nb and $table[$j1] == $kr and do { ++$j1 } 56143 8503 64646
Condition100%3/3
$j1 < $nb and $table[$j1] == $kr
!ll&&!rl&&r
766983456143
MC/DC100%2/2
$j1 < $nb and $table[$j1] == $kr
$j1 < $nb $table[$j1] == $kr
Truth table100%3/3
A and B
A= $j1 < $nbB= $table[$j1] == $kr
ABresult
0X0
100
111
342 8503 ▶ _read_tables(\@x, $tables->[$kr], $tables->[ $kr < $last ? $kr + 1 : $kr ], @$c{qw(mix dmix step start size)}, $j0, $j1);
Branch: 1 branch
BranchTrueFalseTotal
$kr < $last ? : 3255 5248 8503
343 8503 $j0 = $j1;
344 }
345 }
346 4891 return \@x;
347 }
348  
349 # The samples of the blocks j0 .. j1 - 1 of one copy, read from two tables
350 # (references: a table is not copied) and added into x. A sub of its own:
351 # inside the loop over the copies pperl's JIT leaves the loop at the
352 # interpreter's speed (some ten times slower; reported).
353 8503 ▶sub _read_tables ($x, $ta, $tb, $mixes, $dmixes, $steps, $starts, $size, $j0, $j1) {
Subroutine _read_tables: called CC=4 SCAR=13.9
354 8503 my $B = BLOCK;
355 8503 my @mix = @$mixes;
356 8503 my @dmix = @$dmixes;
357 8503 my @step = @$steps;
358 8503 my @start = @$starts;
359 8503 for my $j ($j0 .. $j1 - 1) {
360 64646 my $m = $mix[$j];
361 64646 my $dm = $dmix[$j];
362 64646 my $inc = $step[$j];
363 64646 my $pos = $start[$j];
364 64646 my $i = $j * $B;
365 64646 my $end = $i + $B;
366 64646 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 4137344 64646 4201990
367 4137344 my $at = int($pos);
368 4137344 my $f = $pos - $at;
369 4137344 my $a = $ta->[$at] + $f * ($ta->[ $at + 1 ] - $ta->[$at]);
370 4137344 my $b = $tb->[$at] + $f * ($tb->[ $at + 1 ] - $tb->[$at]);
371 4137344 $x->[$i] += $a + $m * ($b - $a);
372 4137344 $m += $dm;
373 4137344 $pos += $inc;
374 4137344 ▶ $pos -= $size if $pos >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $size 52701 4084643 4137344
375 4137344 $i++;
376 }
377 }
378 8503 return;
379 }
380  
381 # A voice's string for a quantum, as a fresh array of $n samples (zero
382 # before its first block): the delay line read the block's period back
383 # (less the rest of the loop's delay): a whole number of samples, the rest
384 # (0.1 .. 1.1) through a first-order allpass, which delays without
385 # damping; a stiff string's four allpasses; low-passed, times the loop's
386 # gain, the pluck added while it is fed in, written again. The line, where
387 # it writes, the low-pass's last input, the allpasses' last inputs and
388 # outputs and how much of the pluck is fed are carried on. The PDL engine
389 # uses it as it is.
390 678 ▶sub string ($voice, $n, $nb) {
Subroutine string: called CC=10 SCAR=23.0
Pod documented
391 678 my $B = BLOCK;
392 678 my $first = $voice->{first};
393 678 my @out = (0.0) x $n;
394 678 my $st = $voice->{string};
395 678 my @buf = @{ $st->{buf} };
396 678 my $size = scalar @buf;
397 678 my $level = $st->{level};
398 678 my $g = $st->{g};
399 678 my $s1 = $st->{s1};
400 678 my $s0 = 1.0 - $s1;
401 678 my $off = $st->{off};
402 678 my $stiff = $st->{stages};
403 678 my $a = $st->{a};
404 678 my ($x1, $x2, $x3, $x4, $y1, $y2, $y3, $y4) = @{ $st->{disp} };
405 678 my @exc = @{ $st->{exc} };
406 678 my $todo = scalar @exc;
407 678 my $fed = $st->{fed};
408 678 my $pos = $st->{pos};
409 678 my $prev = $st->{prev};
410 678 my $ax = $st->{ax};
411 678 my $ay = $st->{ay};
412 678 my @w = @{ $voice->{w} };
413 678 for my $j ($first .. $nb - 1) {
414 9904 my $d = TWO_PI / $w[$j] - $off;
415 9904 ▶ $d = 1.1 if $d < 1.1;
Branch: 1 branch
BranchTrueFalseTotal
if $d < "1.1" 0 9904 9904
416 9904 ▶ $d = $size - 2.0 if $d > $size - 2.0;
Branch: 1 branch
BranchTrueFalseTotal
if $d > $size - 2 0 9904 9904
417 9904 my $whole = int($d - 0.1);
418 9904 my $frac = $d - $whole;
419 9904 my $eta = (1.0 - $frac) / (1.0 + $frac);
420 9904 my $i = $j * $B;
421 9904 my $end = $i + $B;
422 9904 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 633856 9904 643760
423 633856 my $r = $pos - $whole;
424 633856 ▶ $r += $size if $r < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r < 0 122130 511726 633856
425 633856 my $in = $buf[$r];
426 633856 my $v = $eta * $in + $ax - $eta * $ay;
427 633856 $ax = $in;
428 633856 $ay = $v;
429 633856 ▶ if ($stiff) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stiff) 157696 476160 633856
430 # The stiffness: four first-order allpasses.
431 157696 my $u = $a * ($v - $y1) + $x1;
432 157696 $x1 = $v;
433 157696 $y1 = $u;
434 157696 $v = $a * ($u - $y2) + $x2;
435 157696 $x2 = $u;
436 157696 $y2 = $v;
437 157696 $u = $a * ($v - $y3) + $x3;
438 157696 $x3 = $v;
439 157696 $y3 = $u;
440 157696 $v = $a * ($u - $y4) + $x4;
441 157696 $x4 = $u;
442 157696 $y4 = $v;
443 }
444 633856 my $o = $g * ($s0 * $v + $s1 * $prev);
445 633856 ▶ if ($fed < $todo) { $o += $exc[$fed]; $fed++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($fed < $todo) 5344 628512 633856
446 633856 $prev = $v;
447 633856 $buf[$pos] = $o;
448 633856 $pos++;
449 633856 ▶ $pos = 0 if $pos >= $size;
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $size 752 633104 633856
450 633856 $out[$i] = $level * $o;
451 633856 $i++;
452 }
453 }
454 678 $st->{buf} = \@buf;
455 678 $st->{pos} = $pos;
456 678 $st->{prev} = $prev;
457 678 $st->{ax} = $ax;
458 678 $st->{ay} = $ay;
459 678 $st->{disp} = [ $x1, $x2, $x3, $x4, $y1, $y2, $y3, $y4 ];
460 678 $st->{fed} = $fed;
461 678 ▶ $st->{exc} = [] if $fed >= $todo;
Branch: 1 branch
BranchTrueFalseTotal
if $fed >= $todo 678 0 678
462 678 return \@out;
463 }
464  
465 # A voice's FM for a quantum, as a fresh array of $n samples (zero before
466 # its first block): operators 4, 3, 2, 1, each a sine of its phase plus
467 # what modulates it (the algorithm's coefficients times the others'
468 # outputs; operator 4 its own last two, averaged, times the feedback), its
469 # level interpolated across each block; the carriers summed. The phases and
470 # operator 4's last outputs are carried on. The PDL engine uses it as it is.
471 268 ▶sub fm ($voice, $n, $nb) {
Subroutine fm: called CC=3 SCAR=11.0
Pod documented
472 268 my $B = BLOCK;
473 268 my $first = $voice->{first};
474 268 my @out = (0.0) x $n;
475 268 my $fm = $voice->{fm};
476 268 my $level = $fm->{level};
477 268 my $fb = $fm->{fb};
478 268 my ($r1, $r2, $r3, $r4) = @{ $fm->{ratio} };
479 268 my ($a34, $a24, $a23, $a14, $a13, $a12) = @{ $fm->{coef} };
480 268 my ($c1, $c2, $c3, $c4) = @{ $fm->{carrier} };
481 268 my ($p1, $p2, $p3, $p4) = @{ $fm->{phase} };
482 268 my ($h1, $h2) = @{ $fm->{hist} };
483 268 my @l1 = @{ $fm->{lv}[0] };
484 268 my @l2 = @{ $fm->{lv}[1] };
485 268 my @l3 = @{ $fm->{lv}[2] };
486 268 my @l4 = @{ $fm->{lv}[3] };
487 268 my @w = @{ $voice->{w} };
488 268 for my $j ($first .. $nb - 1) {
489 3310 my $w0 = $w[$j];
490 3310 my ($d1, $d2, $d3, $d4) = ($w0 * $r1, $w0 * $r2, $w0 * $r3, $w0 * $r4);
491 3310 my $e1 = $l1[$j];
492 3310 my $e2 = $l2[$j];
493 3310 my $e3 = $l3[$j];
494 3310 my $e4 = $l4[$j];
495 3310 my $s1 = ($l1[ $j + 1 ] - $e1) / $B;
496 3310 my $s2 = ($l2[ $j + 1 ] - $e2) / $B;
497 3310 my $s3 = ($l3[ $j + 1 ] - $e3) / $B;
498 3310 my $s4 = ($l4[ $j + 1 ] - $e4) / $B;
499 3310 my $i = $j * $B;
500 3310 my $end = $i + $B;
501 3310 ▶ while ($i < $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $end) 211840 3310 215150
502 211840 my $u4 = sin($p4 + $fb * 0.5 * ($h1 + $h2));
503 211840 $h2 = $h1;
504 211840 $h1 = $u4;
505 211840 my $o4 = $e4 * $u4;
506 211840 my $o3 = $e3 * sin($p3 + $a34 * $o4);
507 211840 my $o2 = $e2 * sin($p2 + $a24 * $o4 + $a23 * $o3);
508 211840 my $o1 = $e1 * sin($p1 + $a14 * $o4 + $a13 * $o3 + $a12 * $o2);
509 211840 $out[$i] = $level * ($c1 * $o1 + $c2 * $o2 + $c3 * $o3 + $c4 * $o4);
510 211840 $p1 += $d1;
511 211840 $p2 += $d2;
512 211840 $p3 += $d3;
513 211840 $p4 += $d4;
514 211840 $e1 += $s1;
515 211840 $e2 += $s2;
516 211840 $e3 += $s3;
517 211840 $e4 += $s4;
518 211840 $i++;
519 }
520 3310 $p1 -= TWO_PI * int($p1 / TWO_PI);
521 3310 $p2 -= TWO_PI * int($p2 / TWO_PI);
522 3310 $p3 -= TWO_PI * int($p3 / TWO_PI);
523 3310 $p4 -= TWO_PI * int($p4 / TWO_PI);
524 }
525 268 $fm->{phase} = [ $p1, $p2, $p3, $p4 ];
526 268 $fm->{hist} = [ $h1, $h2 ];
527 268 return \@out;
528 }
529  
530 # A voice's noise on its partials for a quantum: the factor their sum is
531 # taken times, 1 + depth * (white noise through a one-pole low-pass), as a
532 # fresh array of $n samples (1 before the voice's first block). The PDL
533 # engine uses it as it is.
534 68 ▶sub rough ($voice, $n, $nb) {
Subroutine rough: called CC=3 SCAR=11.0
Pod documented
535 68 my $B = BLOCK;
536 68 my $table = Peta::Synth::Voice::noise_table();
537 68 my $tsize = scalar @$table;
538 68 my $first = $voice->{first};
539 68 my @out = (1.0) x $n;
540 68 my ($depth, $a) = @{ $voice->{rough} };
541 68 my ($y, $index) = @{ $voice->{rstate} };
542 68 for my $i ($first * $B .. $n - 1) {
543 38272 $y += $a * ($table->[$index] - $y);
544 38272 $index++;
545 38272 ▶ $index = 0 if $index >= $tsize;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= $tsize 0 38272 38272
546 38272 $out[$i] = 1.0 + $depth * $y;
547 }
548 68 $voice->{rstate} = [ $y, $index ];
549 68 return \@out;
550 }
551  
552 15 1;
553  
554 __END__
« lib/Peta/Synth/Synth.pm ↑ Summary lib/Peta/MIDI/Device/Out.pm »