lib/Peta/Synth/Engine/PDL.pm

filter: Statementstmt 97.9% 145 / 148 Branchbran 96.6% 29 / 30 Conditioncond 83.3% 15 / 18 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 95.0% 210 / 221 CC 27 SCAR 26.4
CC
27
Cov
96%
_group
23.0
render
4.0
name
2.0
CRAP 14.0
« lib/Peta/Synth/Voice.pm ↑ Summary lib/Peta/Synth/Input.pm »
1 package Peta::Synth::Engine::PDL;
2 # ABSTRACT: the voices rendered with PDL
3  
4 # The engine on PDL: the partials of many voices at once, as whole-array
5 # operations, in double precision.
6 #
7 # A partial is the same rotating phasor as in the Perl engine, but the
8 # phasors are not turned one by one: those of every partial, in every
9 # control block, of a group of voices are one array, (partial, block,
10 # voice), and the 64 samples of a block are 64 turns of all of them. A
11 # sin() per sample would cost more than the turn's four multiplications.
12 #
13 # The voices of a group are padded to the group's largest number of
14 # partials (with partials of no amplitude); the voices are grouped by their
15 # number of partials, so that little is padded, and so that an array stays
16 # small enough for the processor's cache.
17 #
18 # The noise band-pass is a recursion, sample after sample, and stays in
19 # Perl (Peta::Synth::Engine::Perl::noise).
20  
21 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # PDL exports dozens of functions into a package; they go to one of their
24 # own, and this one calls PDL by name.
25 {
26 package Peta::Synth::Engine::PDL::Imports;
27 2 ▶ use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 }
29  
30 2 ▶use Peta::Synth::Engine::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 2 our $VERSION = '0.2610180';
33  
34 2 ▶use constant BLOCK => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use constant TWO_PI => 6.283185307179586;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use constant ROWS => 16384; # phasors in an array at most: (partials x blocks x voices)
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 10 ▶sub new ($class, %arg) { return bless { %arg }, $class }
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 0 ▶sub name ($self) { return 'pdl' }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
40  
41 4103 ▶sub render ($self, $voices, $n, $rate) {
Subroutine render: called CC=4 SCAR=13.9
Pod documented
42 4103 my $B = BLOCK;
43 4103 my $nb = $n / $B;
44 4103 my $left = PDL->zeroes(PDL::double(), $B, $nb);
45 4103 my $right = PDL->zeroes(PDL::double(), $B, $nb);
46 4103 my $send = PDL->zeroes(PDL::double(), $B, $nb);
47  
48 # Groups of voices with about as many partials, each small enough (a
49 # voice read from wave sequences has none here).
50 4103 ▶ my $parts = sub ($voice) { $voice->{wave} ? 0 : scalar @{ $voice->{ratio} } };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$voice->{'wave'} ? : 2443 4397 6840
51 4103 my @order = sort { $parts->($b) <=> $parts->($a) } @$voices;
52 4103 ▶ while (@order) {
Branch: 1 branch
BranchTrueFalseTotal
if (@order) 4094 4103 8197
53 4094 ▶ my $k = $parts->($order[0]) || 1;
Condition100%2/2
&$parts($order[0]) || 1
l!l
19182176
MC/DC100%1/1
&$parts($order[0]) || 1
&$parts($order[0])
Truth table100%2/2
A
A= &$parts($order[0])
Aresult
11
00
54 4094 ▶ my $count = int(ROWS / ($k * $nb)) || 1;
Condition50%1/2
int 16384 / ($k * $nb) || 1
l!l
40940
MC/DC0%0/1
int 16384 / ($k * $nb) || 1
int 16384 / ($k * $nb)
Truth table50%1/2
A
A= int 16384 / ($k * $nb)
Aresult
11
00
55 4094 my @group = splice @order, 0, $count;
56 4094 my ($l, $r, $s) = _group(\@group, $k, $n, $nb, $rate);
57 4094 $left += $l;
58 4094 $right += $r;
59 4094 $send += $s;
60 }
61 4103 return map { [ $_->list ] } $left, $right, $send;
62 }
63  
64 # A group of voices of at most $k partials: (left, right, send), each of
65 # dims (B, nb).
66 4094 ▶sub _group ($voices, $k, $n, $nb, $rate) {
Subroutine _group: called CC=23 SCAR=31.4
67 4094 my $B = BLOCK;
68 4094 my $V = @$voices;
69 4094 my $dt = $B / $rate;
70  
71 4094 my (@ratio, @amp, @decay, @attack, @phase, @t0, @w, @env, @gl, @gr, @gs, @fmode, @fx, @fq);
72 4094 for my $voice (@$voices) {
73 # A voice read from wave sequences has no partials here: all padding.
74 4700 ▶ my @own = $voice->{wave} ? ([], [], [], [], []) : @$voice{qw(ratio amp decay attack phase)};
Branch: 1 branch
BranchTrueFalseTotal
$voice->{'wave'} ? : 2059 2641 4700
75 4700 my $pad = $k - @{ $own[0] };
76 4700 push @ratio, @{ $own[0] }, (0.0) x $pad;
77 4700 push @amp, @{ $own[1] }, (0.0) x $pad;
78 4700 push @decay, @{ $own[2] }, (0.0) x $pad;
79 4700 push @attack, @{ $own[3] }, (0.0) x $pad;
80 4700 push @phase, @{ $own[4] }, (0.0) x $pad;
81 4700 push @t0, $voice->{t0};
82 4700 push @w, @{ $voice->{w} };
83 4700 push @env, @{ $voice->{env} };
84 4700 push @gl, @{ $voice->{gl} };
85 4700 push @gr, @{ $voice->{gr} };
86 4700 push @gs, @{ $voice->{gs} };
87 4700 ▶ my $mode = $voice->{fmode} // 0;
Condition100%2/2
$voice->{"fmode"} // 0
l!l
2154485
MC/DC100%1/1
$voice->{"fmode"} // 0
$voice->{"fmode"}
Truth table100%2/2
A
A= $voice->{"fmode"}
Aresult
11
00
88 4700 push @fmode, $mode;
89 4700 ▶ push @fx, $mode ? @{ $voice->{fx} } : (0.0) x ($nb + 1);
Branch: 1 branch
BranchTrueFalseTotal
$mode ? : 156 4544 4700
90 4700 ▶ push @fq, $mode ? @{ $voice->{fq} } : (1.0) x ($nb + 1);
Branch: 1 branch
BranchTrueFalseTotal
$mode ? : 156 4544 4700
91 }
92 4094 ▶ my $partials = sub ($list) { PDL->pdl(PDL::double(), $list)->reshape($k, 1, $V) };
Subroutine __ANON__: called CC=1 SCAR=0.0
93 4094 my $R = $partials->(\@ratio);
94 4094 my $A = $partials->(\@amp);
95 4094 my $D = $partials->(\@decay);
96 4094 my $T = $partials->(\@attack);
97 4094 my $P = $partials->(\@phase);
98 4094 my $W = PDL->pdl(PDL::double(), \@w)->reshape(1, $nb, $V);
99  
100 # The amplitudes at the blocks' edges, (k, nb + 1, V). Before a voice's
101 # first block its envelope is zero; they are taken at time 0 there,
102 # where they are finite, so that zero times them stays zero.
103 4094 my $t = PDL->pdl(PDL::double(), \@t0)->reshape(1, 1, $V) + PDL->sequence(PDL::double(), 1, $nb + 1) * $dt;
104 4094 $t *= $t > 0;
105 4094 my $edge = $A * exp(-$D * $t);
106 4094 my $fades = $T > 0;
107 4094 ▶ if ($fades->any) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fades->any) 0 4094 4094
108 0 my $fade = 1 - exp(-$t / ($T + !$fades));
109 0 $edge *= $fade * $fades + !$fades;
110 }
111 4094 ▶ if (grep { $_ } @fmode) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_;} @fmode) 78 4016 4094
112 # The filter's gain at the edges (see Peta::Synth::Voice::filter_gain), (k, nb + 1, V).
113 78 my $x = $R * PDL->pdl(PDL::double(), \@fx)->reshape(1, $nb + 1, $V);
114 78 my $q = PDL->pdl(PDL::double(), \@fq)->reshape(1, $nb + 1, $V);
115 78 my $mode = PDL->pdl(PDL::double(), \@fmode)->reshape(1, 1, $V);
116 78 my $x2 = $x * $x;
117 78 my $d = sqrt((1 - $x2)**2 + $x2 / ($q * $q));
118 78 my $steep = sqrt(1 + $x2 * $x2);
119  
120 # Each voice's mode picks its term; the others are times 0.
121 78 $edge *= ($mode == 0)
122 + ($mode == 1) / $d
123 + ($mode == 2) / ($d * $steep)
124 + ($mode == 3) * $x2 / $d
125 + ($mode == 4) * $x2 * $x2 / ($d * $steep)
126 + ($mode == 5) * $x / $q / $d;
127 }
128 4094 my $g = $edge->slice(':,0:-2')->copy;
129 4094 my $dg = ($edge->slice(':,1:-1') - $g) / $B;
130  
131 # Each partial's angle per sample in each block, and its phase where
132 # the block begins; the phase where the quantum ends is the voice's.
133 4094 my $step = $R * $W; # (k, nb, V)
134 4094 my $sum = $step->mv(1, 0)->cumusumover->mv(0, 1) * $B; # through the end of each block
135 4094 my $start = $P + $sum - $step * $B;
136 4094 $start -= TWO_PI * PDL::floor($start / TWO_PI);
137 4094 my $end = $P->slice(':,(0)') + $sum->slice(':,(-1)');
138 4094 $end -= TWO_PI * PDL::floor($end / TWO_PI);
139 4094 my @ends = $end->list;
140 4094 for my $v (0 .. $V - 1) {
141 4700 ▶ next if $voices->[$v]{wave};
Branch: 1 branch
BranchTrueFalseTotal
if $voices->[$v]{"wave"} 2059 2641 4700
142 2641 my $count = @{ $voices->[$v]{ratio} };
143 2641 $voices->[$v]{phase} = [ @ends[ $v * $k .. $v * $k + $count - 1 ] ];
144 }
145  
146 # The 64 turns of every phasor: a sample of each block at a time,
147 # summed over the partials of each voice.
148 4094 my $c = cos($start);
149 4094 my $s = sin($start);
150 4094 my $cw = cos($step);
151 4094 my $sw = sin($step);
152 4094 my $x = PDL->zeroes(PDL::double(), $B, $nb, $V);
153 4094 for my $m (0 .. $B - 1) {
154 262016 (my $sample = $x->slice("($m)")) .= ($s * $g)->sumover;
155 262016 ▶ last if $m == $B - 1;
Branch: 1 branch
BranchTrueFalseTotal
if $m == $B - 1 4094 257922 262016
156 257922 my $turn = $c * $sw;
157 257922 $c *= $cw;
158 257922 $c -= $s * $sw;
159 257922 $s *= $cw;
160 257922 $s += $turn;
161 257922 $g += $dg;
162 }
163  
164 # The wave sequences, the string, FM, the noise on the partials, then the
165 # bands of noise (as the Perl engine has them: recursions, and tables read).
166 4094 ▶ my $blocks = sub ($samples) { PDL->pdl(PDL::double(), $samples)->reshape($B, $nb) };
Subroutine __ANON__: called CC=1 SCAR=0.0
167 4094 for my $v (0 .. $V - 1) {
168 4700 my $voice = $voices->[$v];
169 4700 ▶ next if !($voice->{wave} || $voice->{noise} || $voice->{rough} || $voice->{string} || $voice->{fm});
Branch: 1 branch
BranchTrueFalseTotal
unless $voice->{"wave"} or $voice->{"noise"} or $voice->{"rough"} or $voice->{"string"} or $voice->{"fm"} 438 4262 4700
Condition83%10/12
$voice->{"wave"} or $voice->{"noise"} or $voice->{"rough"} or $voice->{"string"} or $voice->{"fm"}
l!l&&r!l&&!r
423230438
$voice->{"wave"} or $voice->{"noise"} or $voice->{"rough"} or $voice->{"string"}
l!l&&r!l&&!r
42320468
$voice->{"wave"} or $voice->{"noise"} or $voice->{"rough"}
l!l&&r!l&&!r
42320468
$voice->{"wave"} or $voice->{"noise"}
l!l&&r!l&&!r
20592173468
MC/DC60%3/5
$voice->{"wave"} or $voice->{"noise"} or $voice->{"rough"} or $voice->{"string"} or $voice->{"fm"}
$voice->{"wave"} $voice->{"noise"} $voice->{"rough"} $voice->{"string"} $voice->{"fm"}
Truth table66%4/6
A or B or C or D or E
A= $voice->{"wave"}B= $voice->{"noise"}C= $voice->{"rough"}D= $voice->{"string"}E= $voice->{"fm"}
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
170  
171 4262 my $own = $x->slice(":,:,($v)");
172 4262 ▶ $own += $blocks->(Peta::Synth::Engine::Perl::waves($voice, $n, $nb, $rate)) if $voice->{wave};
Branch: 1 branch
BranchTrueFalseTotal
if $voice->{"wave"} 2059 2203 4262
173 4262 ▶ $own += $blocks->(Peta::Synth::Engine::Perl::string($voice, $n, $nb)) if $voice->{string};
Branch: 1 branch
BranchTrueFalseTotal
if $voice->{"string"} 59 4203 4262
174 4262 ▶ $own += $blocks->(Peta::Synth::Engine::Perl::fm($voice, $n, $nb)) if $voice->{fm};
Branch: 1 branch
BranchTrueFalseTotal
if $voice->{"fm"} 60 4202 4262
175 4262 ▶ $own *= $blocks->(Peta::Synth::Engine::Perl::rough($voice, $n, $nb)) if $voice->{rough};
Branch: 1 branch
BranchTrueFalseTotal
if $voice->{"rough"} 30 4232 4262
176 4262 ▶ $own += $blocks->(Peta::Synth::Engine::Perl::noise($voice, $n, $nb)) if $voice->{noise};
Branch: 1 branch
BranchTrueFalseTotal
if $voice->{"noise"} 3450 812 4262
177 }
178  
179 # The envelope and the channel's gains, interpolated across each block.
180 4094 my $m = PDL->sequence(PDL::double(), $B) / $B;
181 16376 ▶ my $across = sub ($list) {
Subroutine __ANON__: called CC=1 SCAR=0.0
182 16376 my $edges = PDL->pdl(PDL::double(), $list)->reshape($nb + 1, $V);
183 16376 my $from = $edges->slice('0:-2');
184 16376 return $from->dummy(0, $B) + $m * ($edges->slice('1:-1') - $from)->dummy(0, $B); # (B, nb, V)
185 4094 };
186 4094 my $y = $x * $across->(\@env);
187 4094 return map { ($y * $across->($_))->mv(2, 0)->sumover } \@gl, \@gr, \@gs;
188 }
189  
190 2 1;
191  
192 __END__
« lib/Peta/Synth/Voice.pm ↑ Summary lib/Peta/Synth/Input.pm »