lib/Peta/Synth/Reverb.pm

filter: Statementstmt 92.1% 152 / 165 Branchbran 83.3% 35 / 42 Conditioncond 68.1% 15 / 22 MC/DCmcdc 57.1% 8 / 14 Subroutinesub 84.6% 11 / 13 Podpod 100% 7 / 7 totaltotal 86.6% 228 / 263 CC 37 SCAR 21.8
CC
37
Cov
88%
advance
20.0
process
15.0
active
12.0
CRAP 8.9
« lib/Peta/Synth/Output/FLAC.pm ↑ Summary lib/Peta/Synth/Server.pm »
1 package Peta::Synth::Reverb;
2 # ABSTRACT: a feedback delay network reverb
3  
4 # A feedback delay network: eight delay lines of mutually prime lengths, fed
5 # back through a Hadamard matrix (which keeps the energy and mixes every
6 # line into every other), each line damped by a low-pass and attenuated so
7 # the tail falls 60 dB in the reverb time. Left and right are taken from
8 # different signs of the lines, which decorrelates them.
9 #
10 # It is linear and time-invariant: the reverb of a sum is the sum of the
11 # reverbs, which is what lets an offline render split its voices among
12 # processes that each run their own.
13 #
14 # An engine may run it elsewhere (the GPU engine, on the card): it reads the
15 # parameters and the lines' state from here, advances the positions here
16 # (advance), and leaves a way to fetch its state back (elsewhere), which
17 # process calls before it goes on in Perl. version changes whenever the
18 # lines are made anew or cleared: then what is here is what holds.
19  
20 16 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 16 our $VERSION = '0.2610180';
24  
25 # Line lengths in milliseconds at size 1: spread between 30 and 80 ms.
26 16 my @LENGTH_MS = (29.7, 37.1, 41.1, 43.7, 53.3, 59.9, 67.1, 79.3);
27  
28 16 my %DEFAULT = (time => 2.2, size => 1.0, damp => 0.45, level => 0.5, predelay => 0.02, on => 1);
29  
30 527 ▶sub new ($class, %arg) {
Subroutine new: called CC=4 SCAR=14.3
Pod documented
31 527 ▶ my $self = bless { rate => $arg{rate} // 48000, %DEFAULT }, $class;
Condition50%1/2
$arg{"rate"} // 48000
l!l
5270
MC/DC0%0/1
$arg{"rate"} // 48000
$arg{"rate"}
Truth table50%1/2
A
A= $arg{"rate"}
Aresult
11
00
32 527 ▶ for my $name (keys %DEFAULT) { $self->{$name} = $arg{$name} if defined $arg{$name} }
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{$name} 0 3162 3162
33 527 $self->_build;
34 527 return $self;
35 }
36  
37 31 ▶sub params ($self) { return { map { $_ => $self->{$_} } keys %DEFAULT } }
Subroutine params: called CC=1 SCAR=0.0
Pod documented
38  
39 49 ▶sub set ($self, $name, $value) {
Subroutine set: called CC=4 SCAR=13.9
Pod documented
40 49 ▶ die "the reverb has no '$name'\n" unless exists $DEFAULT{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $DEFAULT{$name} 0 49 49
41 49 $self->{$name} = $value;
42 49 ▶ $self->_build if $name eq 'size' || $name eq 'predelay';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "size" or $name eq "predelay" 2 47 49
Condition100%3/3
$name eq "size" or $name eq "predelay"
l!l&&r!l&&!r
1147
MC/DC100%2/2
$name eq "size" or $name eq "predelay"
$name eq "size" $name eq "predelay"
Truth table100%3/3
A or B
A= $name eq "size"B= $name eq "predelay"
ABresult
1X1
011
000
43 49 $self->_gains;
44 49 return $self;
45 }
46  
47 529 ▶sub _build ($self) {
Subroutine _build: called CC=4 SCAR=13.9
48 529 my $rate = $self->{rate};
49 529 my @len = map { int($_ / 1000 * $self->{size} * $rate) } @LENGTH_MS;
50 529 $_ = 16 for grep { $_ < 16 } @len;
51 529 ▶ my $pre = int($self->{predelay} * $rate) || 1;
Condition50%1/2
int $self->{'predelay'} * $rate || 1
l!l
5290
MC/DC0%0/1
int $self->{'predelay'} * $rate || 1
int $self->{'predelay'} * $rate
Truth table50%1/2
A
A= int $self->{'predelay'} * $rate
Aresult
11
00
52  
53 # The lines and the predelay one after another in one buffer.
54 529 my @offset;
55 529 my $total = 0;
56 529 for my $l (@len, $pre) {
57 4761 push @offset, $total;
58 4761 $total += $l;
59 }
60 529 $self->{len} = \@len;
61 529 $self->{pre} = $pre;
62 529 $self->{reach} = $pre + max(@len); # the longest way through
63 529 $self->{offset} = \@offset;
64 529 $self->{buffer} = [ (0.0) x $total ];
65 529 $self->{pos} = [ (0) x 9 ];
66 529 $self->{lp} = [ (0.0) x 8 ];
67 529 $self->{since} = $self->{reach}; # samples since anything went in
68 529 $self->{loudest} = 0.0;
69 529 $self->{clean} = 1;
70 529 $self->{version}++;
71 529 delete $self->{elsewhere};
72 529 $self->_gains;
73 529 return;
74 }
75  
76 578 ▶sub _gains ($self) {
Subroutine _gains: called CC=1 SCAR=0.0
77 578 my $rate = $self->{rate};
78 578 $self->{g} = [ map { 10**(-3 * $_ / ($self->{time} * $rate)) } @{ $self->{len} } ];
79 578 return;
80 }
81  
82 # Whether the tail has died away: nothing has gone in for longer than the
83 # longest way through (the predelay and the longest line), and what came
84 # out of the last quantum is below -100 dB (before the master gain). A
85 # reverb that is off is always quiet.
86 16192 ▶sub quiet ($self) {
Subroutine quiet: called CC=4 SCAR=13.9
Pod documented
87 16192 ▶ return 1 unless $self->{on} && $self->{level} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"on"} and $self->{"level"} > 0 820 15372 16192
Condition66%2/3
$self->{"on"} and $self->{"level"} > 0
!ll&&!rl&&r
820015372
MC/DC50%1/2
$self->{"on"} and $self->{"level"} > 0
$self->{"on"} $self->{"level"} > 0
Truth table66%2/3
A and B
A= $self->{"on"}B= $self->{"level"} > 0
ABresult
0X0
100
111
88 15372 ▶ return $self->{since} >= $self->{reach} && $self->{loudest} < 1e-5;
Condition100%3/3
$self->{'since'} >= $self->{'reach'} && $self->{'loudest'} < 1e-05
!ll&&!rl&&r
48159059652
MC/DC100%2/2
$self->{'since'} >= $self->{'reach'} && $self->{'loudest'} < 1e-05
$self->{'since'} >= $self->{'reach'} $self->{'loudest'} < 1e-05
Truth table100%3/3
A && B
A= $self->{'since'} >= $self->{'reach'}B= $self->{'loudest'} < 1e-05
ABresult
0X0
100
111
89 }
90  
91 10097 ▶sub clear ($self) {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
92 10097 ▶ return $self if $self->{clean};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"clean"} 10096 1 10097
93 1 $_ = 0.0 for @{ $self->{buffer} }, @{ $self->{lp} };
94 1 $self->{since} = $self->{reach};
95 1 $self->{loudest} = 0.0;
96 1 $self->{clean} = 1;
97 1 $self->{version}++;
98 1 delete $self->{elsewhere};
99 1 return $self;
100 }
101  
102 # The reverb of $send (an array reference): left and right, as array
103 # references of the same length, or nothing when the reverb is off. They are
104 # fresh arrays, not added into the caller's: a loop that stores into two arrays
105 # that might be the same is one pperl's JIT leaves alone.
106 6108 ▶sub process ($self, $send) {
Subroutine process: called CC=15 SCAR=27.1
107 6108 ▶ return unless $self->{on} && $self->{level} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"on"} and $self->{"level"} > 0 820 5288 6108
Condition66%2/3
$self->{"on"} and $self->{"level"} > 0
!ll&&!rl&&r
82005288
MC/DC50%1/2
$self->{"on"} and $self->{"level"} > 0
$self->{"on"} $self->{"level"} > 0
Truth table66%2/3
A and B
A= $self->{"on"}B= $self->{"level"} > 0
ABresult
0X0
100
111
108 0 ▶ if (my $back = delete $self->{elsewhere}) { $back->() }
Branch: 1 branch
BranchTrueFalseTotal
if (my $back = delete $self->{"elsewhere"}) 0 5288 5288
109 5288 my $n = scalar @$send;
110 5288 my @wl = (0.0) x $n;
111 5288 my @wr = (0.0) x $n;
112 5288 my $d = $self->{buffer};
113 5288 my ($o0, $o1, $o2, $o3, $o4, $o5, $o6, $o7, $op) = @{ $self->{offset} };
114 5288 my ($l0, $l1, $l2, $l3, $l4, $l5, $l6, $l7) = @{ $self->{len} };
115 5288 my $lp_len = $self->{pre};
116 5288 my ($p0, $p1, $p2, $p3, $p4, $p5, $p6, $p7, $pp) = @{ $self->{pos} };
117 5288 my ($g0, $g1, $g2, $g3, $g4, $g5, $g6, $g7) = @{ $self->{g} };
118 5288 my ($s0, $s1, $s2, $s3, $s4, $s5, $s6, $s7) = @{ $self->{lp} };
119 5288 my $damp = $self->{damp};
120 5288 my $keep = 1.0 - $damp;
121 5288 my $level = $self->{level} * 0.35;
122 5288 my $loudest = 0.0;
123  
124 5288 for my $i (0 .. $n - 1) {
125 # The input, delayed.
126 2871552 my $in = $d->[ $op + $pp ];
127 2871552 $d->[ $op + $pp ] = $send->[$i];
128 2871552 $pp++;
129 2871552 ▶ $pp = 0 if $pp >= $lp_len;
Branch: 1 branch
BranchTrueFalseTotal
if $pp >= $lp_len 6245 2865307 2871552
130  
131 # The lines' outputs, damped.
132 2871552 $s0 = $keep * $d->[ $o0 + $p0 ] + $damp * $s0;
133 2871552 $s1 = $keep * $d->[ $o1 + $p1 ] + $damp * $s1;
134 2871552 $s2 = $keep * $d->[ $o2 + $p2 ] + $damp * $s2;
135 2871552 $s3 = $keep * $d->[ $o3 + $p3 ] + $damp * $s3;
136 2871552 $s4 = $keep * $d->[ $o4 + $p4 ] + $damp * $s4;
137 2871552 $s5 = $keep * $d->[ $o5 + $p5 ] + $damp * $s5;
138 2871552 $s6 = $keep * $d->[ $o6 + $p6 ] + $damp * $s6;
139 2871552 $s7 = $keep * $d->[ $o7 + $p7 ] + $damp * $s7;
140  
141 2871552 $wl[$i] = $level * ($s0 - $s1 + $s2 - $s3 + $s4 - $s5 + $s6 - $s7);
142 2871552 $wr[$i] = $level * ($s0 + $s1 - $s2 - $s3 + $s4 + $s5 - $s6 - $s7);
143 2871552 my $out = abs($wl[$i]) + abs($wr[$i]);
144 2871552 ▶ $loudest = $out if $out > $loudest;
Branch: 1 branch
BranchTrueFalseTotal
if $out > $loudest 75666 2795886 2871552
145  
146 # Hadamard 8x8, in three butterfly stages, scaled by 1/sqrt(8).
147 2871552 my $a0 = $s0 + $s1; my $a1 = $s0 - $s1; my $a2 = $s2 + $s3; my $a3 = $s2 - $s3;
148 2871552 my $a4 = $s4 + $s5; my $a5 = $s4 - $s5; my $a6 = $s6 + $s7; my $a7 = $s6 - $s7;
149 2871552 my $b0 = $a0 + $a2; my $b1 = $a1 + $a3; my $b2 = $a0 - $a2; my $b3 = $a1 - $a3;
150 2871552 my $b4 = $a4 + $a6; my $b5 = $a5 + $a7; my $b6 = $a4 - $a6; my $b7 = $a5 - $a7;
151 2871552 my $h = 0.35355339059327373;
152 2871552 $d->[ $o0 + $p0 ] = $g0 * (($b0 + $b4) * $h + $in);
153 2871552 $d->[ $o1 + $p1 ] = $g1 * (($b1 + $b5) * $h + $in);
154 2871552 $d->[ $o2 + $p2 ] = $g2 * (($b2 + $b6) * $h + $in);
155 2871552 $d->[ $o3 + $p3 ] = $g3 * (($b3 + $b7) * $h + $in);
156 2871552 $d->[ $o4 + $p4 ] = $g4 * (($b0 - $b4) * $h - $in);
157 2871552 $d->[ $o5 + $p5 ] = $g5 * (($b1 - $b5) * $h - $in);
158 2871552 $d->[ $o6 + $p6 ] = $g6 * (($b2 - $b6) * $h - $in);
159 2871552 $d->[ $o7 + $p7 ] = $g7 * (($b3 - $b7) * $h - $in);
160  
161 2871552 ▶ $p0++; $p0 = 0 if $p0 >= $l0;
Branch: 1 branch
BranchTrueFalseTotal
if $p0 >= $l0 4184 2867368 2871552
162 2871552 ▶ $p1++; $p1 = 0 if $p1 >= $l1;
Branch: 1 branch
BranchTrueFalseTotal
if $p1 >= $l1 3356 2868196 2871552
163 2871552 ▶ $p2++; $p2 = 0 if $p2 >= $l2;
Branch: 1 branch
BranchTrueFalseTotal
if $p2 >= $l2 3030 2868522 2871552
164 2871552 ▶ $p3++; $p3 = 0 if $p3 >= $l3;
Branch: 1 branch
BranchTrueFalseTotal
if $p3 >= $l3 2820 2868732 2871552
165 2871552 ▶ $p4++; $p4 = 0 if $p4 >= $l4;
Branch: 1 branch
BranchTrueFalseTotal
if $p4 >= $l4 2323 2869229 2871552
166 2871552 ▶ $p5++; $p5 = 0 if $p5 >= $l5;
Branch: 1 branch
BranchTrueFalseTotal
if $p5 >= $l5 2057 2869495 2871552
167 2871552 ▶ $p6++; $p6 = 0 if $p6 >= $l6;
Branch: 1 branch
BranchTrueFalseTotal
if $p6 >= $l6 1837 2869715 2871552
168 2871552 ▶ $p7++; $p7 = 0 if $p7 >= $l7;
Branch: 1 branch
BranchTrueFalseTotal
if $p7 >= $l7 1557 2869995 2871552
169 }
170 5288 $self->{pos} = [ $p0, $p1, $p2, $p3, $p4, $p5, $p6, $p7, $pp ];
171 5288 $self->{lp} = [ $s0, $s1, $s2, $s3, $s4, $s5, $s6, $s7 ];
172 5288 $self->{loudest} = $loudest;
173 5288 $self->_fed($send);
174 5288 return (\@wl, \@wr);
175 }
176  
177 # The input of a quantum: how long ago it last had anything in it.
178 5288 ▶sub _fed ($self, $send) {
Subroutine _fed: called CC=4 SCAR=13.9
179 5288 my $i = $#$send;
180 5288 ▶ $i-- while $i >= 0 && $send->[$i] == 0;
Branch: 1 branch
BranchTrueFalseTotal
$i >= 0 and $send->[$i] == 0 and do { --$i } 243455 5288 248743
Condition100%3/3
$i >= 0 and $send->[$i] == 0
!ll&&!rl&&r
4914797243455
MC/DC100%2/2
$i >= 0 and $send->[$i] == 0
$i >= 0 $send->[$i] == 0
Truth table100%3/3
A and B
A= $i >= 0B= $send->[$i] == 0
ABresult
0X0
100
111
181 5288 ▶ $self->{since} = $i < 0 ? $self->{since} + @$send : $#$send - $i;
Branch: 1 branch
BranchTrueFalseTotal
$i < 0 ? : 491 4797 5288
182 5288 $self->{clean} = 0;
183 5288 return;
184 }
185  
186 # Whether it sounds at all.
187 0 ▶sub active ($self) { return $self->{on} && $self->{level} > 0 ? 1 : 0 }
Subroutine active: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self->{'on'} && $self->{'level'} > 0 ? : 0 0 0
Condition0%0/3
$self->{"on"} and $self->{"level"} > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"on"} and $self->{"level"} > 0
$self->{"on"} $self->{"level"} > 0
Truth table0%0/3
A and B
A= $self->{"on"}B= $self->{"level"} > 0
ABresult
0X0
100
111
188  
189 # After $n samples processed elsewhere, their send and wet left and right:
190 # the positions moved on, the input and the loudest noted (for quiet).
191 0 ▶sub advance ($self, $n, $send, $wl, $wr) {
Subroutine advance: not called CC=4 SCAR=30.0
Pod documented
192 0 my @len = (@{ $self->{len} }, $self->{pre});
193 0 my $pos = $self->{pos};
194 0 $pos->[$_] = ($pos->[$_] + $n) % $len[$_] for 0 .. 8;
195 0 my $loudest = 0.0;
196 0 for my $i (0 .. $n - 1) {
197 0 my $out = abs($wl->[$i]) + abs($wr->[$i]);
198 0 ▶ $loudest = $out if $out > $loudest;
Branch: 1 branch
BranchTrueFalseTotal
if $out > $loudest 0 0 0
199 }
200 0 $self->{loudest} = $loudest;
201 0 $self->_fed($send);
202 0 return;
203 }
204  
205 16 1;
206  
207 __END__
« lib/Peta/Synth/Output/FLAC.pm ↑ Summary lib/Peta/Synth/Server.pm »