lib/Peta/Synth/Voice.pm

filter: Statementstmt 98.8% 660 / 668 Branchbran 89.0% 349 / 392 Conditioncond 79.4% 155 / 195 MC/DCmcdc 58.5% 65 / 111 Subroutinesub 100% 60 / 60 Podpod 100% 15 / 15 totaltotal 90.4% 1304 / 1441 CC 315 SCAR 27.0
CC
315
Cov
93%
controls
45.0
_partials
25.2
_string
19.5
CRAP 14.9
« lib/Peta/MIDI/Device/Out.pm ↑ Summary lib/Peta/Synth/Engine/PDL.pm »
1 package Peta::Synth::Voice;
2 # ABSTRACT: a sounding note: partials, noise, envelope, pitch
3  
4 # A sounding note: its partials and noise, made from a patch, and what it
5 # does over time (envelope, pitch, noise level). The synthesizer asks a
6 # voice, block by block, for its controls; an engine turns the controls and
7 # the partials into samples.
8 #
9 # What an engine reads, for a quantum of nb control blocks of B samples:
10 #
11 # ratio amp decay attack per partial: frequency ratio to the fundamental,
12 # amplitude, decay (1/s), fade-in time (s, 0: none)
13 # phase per partial, in radians; the engine keeps it
14 # t0 the voice's time (s) at the quantum's first block
15 # first the first block the voice sounds in (it starts there)
16 # w[nb] the fundamental, radians per sample, per block
17 # (key, tuning, bend, vibrato, sweep, glide)
18 # env[nb+1] gl gr gs envelope and channel gains (left, right, reverb send)
19 # at the blocks' edges, to be interpolated across a block
20 # noise how many bands of noise (0 .. 3)
21 # bands each: g k mode poles (its filter: tan(pi fc / rate),
22 # 1/q; 0 band-, 1 high-, 2 low-pass; 2 or 4: a second
23 # stage the same), state [ic1, ic2, table position, the
24 # second stage's jc1, jc2], env[nb+1] (its level at the
25 # edges, its grains in it)
26 # rough, rstate noise on the partials: [depth, a] (a one-pole
27 # low-pass's coefficient), [its state, table position]
28 # string a plucked or struck string: level, g (the loop's gain),
29 # s1 (its low-pass: (1 - s1) x[n] + s1 x[n-1]), buf (its
30 # delay line), pos (where it writes next), prev (the
31 # low-pass's last input), ax ay (the fractional allpass's
32 # last input and output), stages (0 or 4) and a: the
33 # stiffness, first-order allpasses (a + z^-1) / (1 + a z^-1)
34 # after the fractional one, disp their last inputs and
35 # outputs [x1 .. x4, y1 .. y4]; the delay read is the
36 # period (2 pi / w) less off, the rest of the loop's delay;
37 # exc (a period of the pluck or blow), fed (how much of it
38 # has gone into the loop, added to what the loop puts out)
39 # fm four operators: level, ratio[4] (to the fundamental),
40 # coef [a34 a24 a23 a14 a13 a12] (operator k modulated by
41 # a_km times m's output), carrier[4] (0, 1), fb (operator
42 # 4's feedback, radians), lv[4][nb+1] (their levels at the
43 # edges), phase[4], hist (operator 4's last two outputs)
44 # fmode, fx[nb+1] fq[nb+1] the filter (0 none; 1 low-pass of 12, 2 of 24 dB
45 # an octave; 3, 4 high-pass; 5 band-pass), and at
46 # the edges the fundamental over the cutoff and the Q
47 #
48 # The partials' amplitude at time t is
49 #
50 # amp * exp(-decay t) * (attack > 0 ? 1 - exp(-t / attack) : 1) * H(ratio fx, fq)
51 #
52 # at the blocks' edges, interpolated in between, times env and the gains;
53 # H is the filter's gain (see filter_gain), 1 without one.
54 #
55 # Modulation (a patch's filter, its routes): sources taken at the blocks'
56 # edges (velocity, key, the channel's controllers, the second envelope, two
57 # LFOs), each times a route's amount, summed into destinations (pitch,
58 # cutoff, resonance, level, pan, noise). A patch without either costs
59 # nothing of it.
60  
61 17 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 17 ▶use Peta::Synth::Patch;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 17 ▶use Peta::Synth::Wavetable;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 17 our $VERSION = '0.2610180';
66  
67 17 ▶use constant TWO_PI => 6.283185307179586;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 17 ▶use constant PI => 3.141592653589793;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 17 ▶use constant NOISE_SIZE => 1 << 16;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 17 ▶use constant SILENT => 1e-4; # -80 dB: a voice below this is done
Subroutine BEGIN: called CC=1 SCAR=0.0
71  
72 # The filter's modes, as engines read them (fmode).
73 17 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
74 17 LOW_PASS_12 => 1,
75 LOW_PASS_24 => 2,
76 HIGH_PASS_12 => 3,
77 HIGH_PASS_24 => 4,
78 BAND_PASS => 5,
79 17 };
80 17 my %FILTER = (off => 0, lp12 => LOW_PASS_12, lp24 => LOW_PASS_24, hp12 => HIGH_PASS_12, hp24 => HIGH_PASS_24, bp => BAND_PASS);
81  
82 17 my %SHAPE = (sine => 0, triangle => 1, square => 2, saw => 3, random => 4);
83 17 my %NOISE_MODE = (band => 0, high => 1, low => 2);
84  
85 17 my @SOURCES = @{ Peta::Synth::Patch::mod_sources() };
86 17 my @DESTINATIONS = @{ Peta::Synth::Patch::mod_destinations() };
87 17 my %SOURCE = map { $SOURCES[$_] => $_ } 0 .. $#SOURCES;
88 17 my %DESTINATION = map { $DESTINATIONS[$_] => $_ } 0 .. $#DESTINATIONS;
89  
90 # The white noise every engine reads: the same on every perl.
91 17 my @NOISE;
92  
93 14129 ▶sub noise_table () {
Subroutine noise_table: called CC=3 SCAR=11.0
Pod documented
94 14129 ▶ return \@NOISE if @NOISE;
Branch: 1 branch
BranchTrueFalseTotal
if @NOISE 14115 14 14129
95  
96 14 my $state = 0x2545f491;
97 14 for (1 .. NOISE_SIZE) {
98 917504 $state = ($state * 1103515245 + 12345) % 2147483648;
99 917504 push @NOISE, $state / 1073741824 - 1;
100 }
101 14 return \@NOISE;
102 }
103  
104 # new($patch, %note): key, velocity (1..127), channel, rate, seed, partials
105 # (the most this voice may have), and the time it starts (start, in samples).
106 # What the channel makes of the note:
107 #
108 # shape factors on the patch: attack, decay, release (times), bright
109 # (the cutoff), q (the cutoff's resonance), level
110 # tune semitones the note is moved by (a drum key's pitch)
111 # glide [semitones, seconds]: the pitch starts that far off and comes
112 # to the note's in that time (portamento)
113 # legato it takes over from a note that sounds: no attack
114 # pan a pan of its own, added to the channel's (-1 .. 1; a drum key's)
115 # send the channel's reverb send times this
116 # cutoff_control
117 # a controller moves the cutoff (GM2's destinations): a patch
118 # without a filter gets a low-pass of 12 dB, at half the rate
119 # waves a store of wave sequences (Peta::Synth::Wavetable) to read from
120 1153 ▶sub new ($class, $patch, %arg) {
Subroutine new: called CC=19 SCAR=29.5
Pod documented
121 1153 my $rate = $arg{rate};
122 1153 my $key = $arg{key};
123 1153 my $vel = $arg{velocity} / 127;
124 1153 ▶ my $shape = { attack => 1, decay => 1, release => 1, bright => 1, q => 1, level => 1, %{ $arg{shape} // {} } };
Condition100%2/2
$arg{"shape"} // {}
l!l
703450
MC/DC100%1/1
$arg{"shape"} // {}
$arg{"shape"}
Truth table100%2/2
A
A= $arg{"shape"}
Aresult
11
00
125 1153 ▶ my $pitch = $patch->{fixed} || 440 * 2**(($key + $patch->{transpose} - 69) / 12);
Condition66%2/3
$patch->{'fixed'} || 440 * 2 ** (($key + $patch->{'transpose'} - 69) / 12)
l!l&&r!l&&!r
1769770
MC/DC0%0/2
$patch->{'fixed'} || 440 * 2 ** (($key + $patch->{'transpose'} - 69) / 12)
$patch->{'fixed'} 440 * 2 ** (($key + $patch->{'transpose'} - 69) / 12)
Truth table66%2/3
A || B
A= $patch->{'fixed'}B= 440 * 2 ** (($key + $patch->{'transpose'} - 69) / 12)
ABresult
1X1
011
000
126 1153 ▶ my $f0 = $pitch * 2**(($arg{tune} // 0) / 12);
Condition100%2/2
$arg{"tune"} // 0
l!l
703450
MC/DC100%1/1
$arg{"tune"} // 0
$arg{"tune"}
Truth table100%2/2
A
A= $arg{"tune"}
Aresult
11
00
127 1153 ▶ my $seed = (($arg{seed} // 1) * 2654435761 + 1) % 4294967296 || 1;
Condition75%3/4
(($arg{'seed'} // 1) * 2654435761 + 1) % 4294967296 || 1
l!l
11530
$arg{"seed"} // 1
l!l
708445
MC/DC100%1/1
(($arg{'seed'} // 1) * 2654435761 + 1) % 4294967296 || 1
$arg{"seed"}
Truth table100%2/2
A
A= $arg{"seed"}
Aresult
11
00
128  
129 my $self = bless {
130 patch => $patch,
131 key => $key,
132 channel => $arg{channel} // 0,
133 velocity => $arg{velocity},
134 f0 => $f0,
135 rate => $rate,
136 start => $arg{start} // 0,
137 released => undef, # the sample the key was let go at
138 seed => $seed, # of the voice's own random numbers
139 shape => $shape,
140 level => $patch->{level} * $vel**$patch->{velocity} * $shape->{level},
141 id => $arg{id} // 0,
142 chorus => $arg{chorus} // 0,
143 legato => $arg{legato} ? 1 : 0,
144 rfactor => 1, # the note-off velocity's factor on the release
145 pressure => 0, # key pressure, 0 .. 1
146 1153 ▶ cutoff_control => $arg{cutoff_control} ? 1 : 0,
Branch: 2 branches
BranchTrueFalseTotal
$arg{'legato'} ? : 3 1150 1153
$arg{'cutoff_control'} ? : 1 1152 1153
Condition100%8/8
$arg{"channel"} // 0
l!l
703450
$arg{"start"} // 0
l!l
704449
$arg{"id"} // 0
l!l
703450
$arg{"chorus"} // 0
l!l
703450
MC/DC100%1/1
$arg{"channel"} // 0
$arg{"channel"}
MC/DC100%1/1
$arg{"start"} // 0
$arg{"start"}
MC/DC100%1/1
$arg{"id"} // 0
$arg{"id"}
MC/DC100%1/1
$arg{"chorus"} // 0
$arg{"chorus"}
Truth table100%2/2
A
A= $arg{"channel"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"start"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"id"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"chorus"}
Aresult
11
00
147 }, $class;
148 1153 ▶ $self->{glide} = [ $arg{glide}[0], 0, 0, $arg{glide}[1] ] if $arg{glide} && $arg{glide}[0];
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"glide"} and $arg{"glide"}[0] 2 1151 1153
Condition66%2/3
$arg{"glide"} and $arg{"glide"}[0]
!ll&&!rl&&r
115102
MC/DC50%1/2
$arg{"glide"} and $arg{"glide"}[0]
$arg{"glide"} $arg{"glide"}[0]
Truth table66%2/3
A and B
A= $arg{"glide"}B= $arg{"glide"}[0]
ABresult
0X0
100
111
149 1153 ▶ $self->{pan} = $arg{pan} if $arg{pan};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"pan"} 1 1152 1153
150 1153 ▶ $self->{send} = $arg{send} if defined $arg{send} && $arg{send} != 1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $arg{"send"} and $arg{"send"} != 1 1 1152 1153
Condition100%3/3
defined $arg{"send"} and $arg{"send"} != 1
!ll&&!rl&&r
4507021
MC/DC100%2/2
defined $arg{"send"} and $arg{"send"} != 1
defined $arg{"send"} $arg{"send"} != 1
Truth table100%3/3
A and B
A= defined $arg{"send"}B= $arg{"send"} != 1
ABresult
0X0
100
111
151  
152 1153 ▶ $self->_partials($vel, $arg{partials} // 64);
Condition100%2/2
$arg{"partials"} // 64
l!l
109756
MC/DC100%1/1
$arg{"partials"} // 64
$arg{"partials"}
Truth table100%2/2
A
A= $arg{"partials"}
Aresult
11
00
153 1153 $self->_noise;
154 1153 $self->_string($vel);
155 1153 $self->_fm($vel);
156 1153 $self->_modulation($vel);
157 1152 ▶ $self->_waves($arg{waves}) if $arg{waves};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"waves"} 686 466 1152
158  
159 1152 return $self;
160 }
161  
162 436 ▶sub partials ($self) { return scalar @{ $self->{ratio} } }
Subroutine partials: called CC=1 SCAR=0.0
Pod documented
163  
164 19844 ▶sub _random ($self) {
Subroutine _random: called CC=1 SCAR=0.0
165 19844 $self->{seed} = ($self->{seed} * 1103515245 + 12345) % 2147483648;
166 19844 return $self->{seed} / 2147483648;
167 }
168  
169 # The partials: the patch's spectrum at this key and velocity, the strongest
170 # that fit under the budget, detuned copies for unison, normalized to unit
171 # RMS before the voice's level.
172 1153 ▶sub _partials ($self, $vel, $budget) {
Subroutine _partials: called CC=25 SCAR=32.3
173 1153 my $p = $self->{patch};
174 1153 my $f0 = $self->{f0};
175 1153 my $rate = $self->{rate};
176 1153 my $top = 0.45 * $rate;
177  
178 1153 ▶ my @set = grep { $_->[0] * $f0 <= $top && $_->[1] > 0 } $self->_spectrum($vel);
Condition66%2/3
$_->[0] * $f0 <= $top && $_->[1] > 0
!ll&&!rl&&r
88048378
MC/DC50%1/2
$_->[0] * $f0 <= $top && $_->[1] > 0
$_->[0] * $f0 <= $top $_->[1] > 0
Truth table66%2/3
A && B
A= $_->[0] * $f0 <= $topB= $_->[1] > 0
ABresult
0X0
100
111
179  
180 # The strongest within the budget; unison copies share it. The channel's
181 # chorus (0 .. 1) makes at least three copies, and spreads them up to 24
182 # cents further.
183 1153 ▶ my $copies = $p->{unison} > 1 ? $p->{unison} : 1;
Branch: 1 branch
BranchTrueFalseTotal
$p->{'unison'} > 1 ? : 368 785 1153
184 1153 my $detune = $p->{detune};
185 1153 ▶ if (my $chorus = $self->{chorus}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chorus = $self->{"chorus"}) 1 1152 1153
186 1 ▶ $copies = 3 if $copies < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $copies < 3 1 0 1
187 1 $detune += 24 * $chorus;
188 }
189 1153 $self->{copies} = $copies;
190  
191 1153 my $keep = $budget; # partials of the first copy
192 1153 ▶ my $extra = $copies > 1 ? $p->{unison_k} : 0; # and of each of the others
Branch: 1 branch
BranchTrueFalseTotal
$copies > 1 ? : 369 784 1153
193 1153 ▶ if ($copies > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copies > 1) 369 784 1153
194 369 ▶ $keep = int($budget / (1 + ($copies - 1) * 0.5)) || 1;
Condition50%1/2
int $budget / (1 + ($copies - 1) * 0.5) || 1
l!l
3690
MC/DC0%0/1
int $budget / (1 + ($copies - 1) * 0.5) || 1
int $budget / (1 + ($copies - 1) * 0.5)
Truth table50%1/2
A
A= int $budget / (1 + ($copies - 1) * 0.5)
Aresult
11
00
195 369 ▶ $extra = $budget - $keep >= $copies - 1 ? int(($budget - $keep) / ($copies - 1)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$budget - $keep >= $copies - 1 ? : 369 0 369
196 369 ▶ $extra = $p->{unison_k} if $extra > $p->{unison_k};
Branch: 1 branch
BranchTrueFalseTotal
if $extra > $p->{"unison_k"} 64 305 369
197 }
198 1153 my @strong = sort { $b->[1] <=> $a->[1] } @set;
199 1153 ▶ splice @strong, $keep if @strong > $keep;
Branch: 1 branch
BranchTrueFalseTotal
if @strong > $keep 446 707 1153
200 1153 ▶ my $peak = @strong ? $strong[0][1] : 0;
Branch: 1 branch
BranchTrueFalseTotal
@strong ? : 1057 96 1153
201 1153 @strong = grep { $_->[1] >= $peak * 1e-3 } @strong;
202  
203 # Each partial its phase; with unison_sync its copies the same one.
204 1153 my (@all, @phase);
205 1153 ▶ if ($copies > 1 && $extra) {
Branch: 1 branch
BranchTrueFalseTotal
if ($copies > 1 and $extra) { } 369 784 1153
Condition66%2/3
$copies > 1 and $extra
!ll&&!rl&&r
7840369
MC/DC50%1/2
$copies > 1 and $extra
$copies > 1 $extra
Truth table66%2/3
A and B
A= $copies > 1B= $extra
ABresult
0X0
100
111
206 369 ▶ my @detuned = @strong > $extra ? @strong[ 0 .. $extra - 1 ] : @strong;
Branch: 1 branch
BranchTrueFalseTotal
@strong > $extra ? : 322 47 369
207 369 ▶ my @own = $p->{unison_sync} ? map { TWO_PI * $self->_random } @strong : ();
Branch: 1 branch
BranchTrueFalseTotal
$p->{'unison_sync'} ? : 157 212 369
208 369 for my $copy (0 .. $copies - 1) {
209 911 my $cents = $detune * ($copy / ($copies - 1) - 0.5);
210 911 my $shift = 2**($cents / 1200);
211 911 ▶ my @these = $copy == 0 ? @strong : @detuned;
Branch: 1 branch
BranchTrueFalseTotal
$copy == 0 ? : 369 542 911
212 911 push @all, map { [ $_->[0] * $shift, @$_[ 1 .. 3 ] ] } @these;
213 911 ▶ push @phase, $p->{unison_sync} ? @own[ 0 .. $#these ] : map { TWO_PI * $self->_random } @these;
Branch: 1 branch
BranchTrueFalseTotal
$p->{'unison_sync'} ? : 315 596 911
214 }
215 }
216 else {
217 784 @all = @strong;
218 784 @phase = map { TWO_PI * $self->_random } @all;
219 }
220  
221 # An RMS of 0.5 before the level; partials near Nyquist fade out, so that
222 # a bend cannot alias them in.
223 1153 my $sum = 0;
224 1153 $sum += $_->[1]**2 for @all;
225 1153 ▶ my $norm = $sum > 0 ? 0.5 / sqrt($sum / 2) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sum > 0 ? : 1057 96 1153
226 1153 for my $partial (@all) {
227 17861 my $f = $partial->[0] * $f0;
228 17861 my $fade = (0.48 * $rate - $f) / (0.03 * $rate);
229 17861 ▶ $fade = 1 if $fade > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $fade > 1 17861 0 17861
230 17861 ▶ $fade = 0 if $fade < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $fade < 0 0 17861 17861
231 17861 $partial->[1] *= $norm * $fade;
232 }
233  
234 1153 $self->{ratio} = [ map { $_->[0] } @all ];
235 1153 $self->{amp} = [ map { $_->[1] } @all ];
236 1153 $self->{decay} = [ map { $_->[2] } @all ];
237 1153 ▶ $self->{attack} = [ map { $self->{legato} ? 0 : $_->[3] } @all ];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'legato'} ? : 36 17825 17861
238 1153 $self->{phase} = \@phase;
239 1153 return;
240 }
241  
242 # The patch's partials at this key and velocity, as [ ratio, amplitude,
243 # decay, attack ]: its explicit ones, or its harmonic spectrum.
244 1153 ▶sub _spectrum ($self, $vel) {
Subroutine _spectrum: called CC=9 SCAR=22.0
245 1153 my $p = $self->{patch};
246 1153 my $f0 = $self->{f0};
247 1153 my $top = 0.45 * $self->{rate};
248 1153 my $keyfactor = 2**(($self->{key} - 60) / 12 * $p->{decay_key});
249 1153 my $decay_k = $p->{decay_k} * 2**(($self->{key} - 60) / 12 * $p->{decay_k_key});
250  
251 1153 ▶ if ($p->{partials}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"partials"}) 485 668 1153
252 485 my @set;
253 485 my $i = 0;
254 485 for my $partial (@{ $p->{partials} }) {
255 2233 my ($ratio, $amp, $factor) = @$partial;
256 2233 ▶ push @set, [ $ratio, $amp, $p->{decay} * ($factor // 1) * $keyfactor, $p->{attack_k} * $i++ ];
Condition50%1/2
$factor // 1
l!l
22330
MC/DC0%0/1
$factor // 1
$factor
Truth table50%1/2
A
A= $factor
Aresult
11
00
257 }
258 485 return @set;
259 }
260  
261 668 my $cutoff = $p->{cutoff} * ($f0 / 261.6)**$p->{track} * 2**($p->{bright} * ($vel - 0.75) * 2) * $self->{shape}{bright};
262 668 my $q = 0.7071067811865476 * $self->{shape}{q}; # two poles; at the patches' own q, without a peak
263 668 my $B = $p->{stretch} * 2**(($self->{key} - 60) / 12 * $p->{stretch_key});
264  
265 668 my @set;
266 668 for my $k (1 .. $p->{max}) {
267 46779 my $ratio = $k * sqrt(1 + $B * $k * $k);
268 46779 my $f = $ratio * $f0;
269 46779 ▶ last if $f > $top;
Branch: 1 branch
BranchTrueFalseTotal
if $f > $top 546 46233 46779
270  
271 46233 my $x = $f / $cutoff;
272 46233 my $amp = $k**-$p->{slope} / sqrt((1 - $x * $x)**2 + ($x / $q)**2)**(1 + $p->{rolloff});
273 46233 ▶ $amp *= $p->{even} if $k % 2 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $k % 2 == 0 22941 23292 46233
274 46233 ▶ $amp *= abs(sin(PI * $k * $p->{pluck})) if $p->{pluck};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"pluck"} 8869 37364 46233
275 46233 for my $formant (@{ $p->{formants} }) {
276 41344 my ($centre, $gain, $width) = @$formant;
277 41344 $amp *= 1 + $gain * exp(-(log($f / $centre) / log(2) / $width)**2);
278 }
279 46233 push @set, [ $ratio, $amp, $p->{decay} * (1 + $decay_k * ($k - 1)) * $keyfactor, $p->{attack_k} * ($k - 1) ];
280 }
281 668 return @set;
282 }
283  
284 # The noise: up to three bands, white noise each through its filter (band-,
285 # high- or low-pass) under its own level, attack and decay; and noise on
286 # the partials, low-passed white noise moving their amplitude.
287 1153 ▶sub _noise ($self) {
Subroutine _noise: called CC=13 SCAR=25.7
288 1153 my $p = $self->{patch};
289 1153 my $rate = $self->{rate};
290  
291 1153 my @bands;
292 1153 for my $name ('noise', 'noise2', 'noise3') {
293 3459 ▶ next if $p->{$name} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p->{$name} <= 0 2486 973 3459
294  
295 973 ▶ my $centre = $p->{"${name}_ratio"} ? $p->{"${name}_ratio"} * $self->{f0} : $p->{"${name}_center"};
Branch: 1 branch
BranchTrueFalseTotal
$$p{"${name}_ratio"} ? : 178 795 973
296 973 ▶ $centre = 0.45 * $rate if $centre > 0.45 * $rate;
Branch: 1 branch
BranchTrueFalseTotal
if $centre > "0.45" * $rate 2 971 973
297 973 ▶ $centre = 20 if $centre < 20;
Branch: 1 branch
BranchTrueFalseTotal
if $centre < 20 0 973 973
298 973 ▶ my $mode = $NOISE_MODE{ $p->{"${name}_mode"} } // die "no noise mode '" . $p->{"${name}_mode"} . "'\n";
Condition50%1/2
$NOISE_MODE{$$p{"${name}_mode"}} // die(q[no noise mode '] . $$p{"${name}_mode"} . "'\n")
l!l
9730
MC/DC0%0/1
$NOISE_MODE{$$p{"${name}_mode"}} // die(q[no noise mode '] . $$p{"${name}_mode"} . "'\n")
$NOISE_MODE{$$p{"${name}_mode"}}
Truth table50%1/2
A
A= $NOISE_MODE{$$p{"${name}_mode"}}
Aresult
11
00
299 973 ▶ my $q = $p->{"${name}_q"} > 0.1 ? $p->{"${name}_q"} : 0.1;
Branch: 1 branch
BranchTrueFalseTotal
$$p{"${name}_q"} > 0.1 ? : 973 0 973
300 973 ▶ my $poles = $p->{"${name}_poles"} == 4 ? 4 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$$p{"${name}_poles"} == 4 ? : 16 957 973
301 973 my $share = _noise_share($mode, $centre, $q, $rate);
302 973 ▶ $share /= 2 if $poles == 4 && $mode == 0; # a band-pass squared lets half the noise through
Branch: 1 branch
BranchTrueFalseTotal
if $poles == 4 and $mode == 0 13 960 973
Condition100%3/3
$poles == 4 and $mode == 0
!ll&&!rl&&r
957313
MC/DC100%2/2
$poles == 4 and $mode == 0
$poles == 4 $mode == 0
Truth table100%3/3
A and B
A= $poles == 4B= $mode == 0
ABresult
0X0
100
111
303  
304 my %band = (
305 level => $p->{$name} * 0.7 / (0.577 * sqrt($share)),
306 centre => $centre,
307 mode => $mode,
308 poles => $poles,
309 k => 1 / $q,
310 g => sin(PI * $centre / $rate) / cos(PI * $centre / $rate),
311 attack => $p->{"${name}_attack"},
312 decay => $p->{"${name}_decay"},
313 973 sustain => $p->{"${name}_sustain"},
314 state => [ 0, 0, int($self->_random * NOISE_SIZE), 0, 0 ], # the filter's two, the table's position, the second stage's
315 );
316 973 ▶ if ((my $grains = $p->{"${name}_grains"}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $grains = $$p{"${name}_grains"}) > 0) 13 960 973
317 # A grain's loudness has mean 3/4 and mean square 7/12: the sum's
318 # mean and variance as Campbell's theorem has them, made 1.
319 13 my $decay = $p->{"${name}_grain_decay"};
320 13 my $mean = $grains * 0.75 / $decay;
321 13 my $variance = $grains * 7 / 12 / (2 * $decay);
322 13 $band{grains} = $grains;
323 13 $band{grain_decay} = $decay;
324 13 $band{grain_scale} = 1 / sqrt($mean * $mean + $variance);
325 13 $band{grain} = [ 0, 0, $self->_wait($grains) ]; # time, sum, the next grain's time
326 }
327 973 push @bands, \%band;
328 }
329 1153 $self->{bands} = \@bands;
330 1153 $self->{noise} = scalar @bands;
331  
332 1153 ▶ if ($p->{rough} > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"rough"} > 0) 3 1150 1153
333 # White noise of the table (RMS 1/sqrt 3) through a one-pole low-pass
334 # has an RMS of sqrt(a / (2 - a) / 3): made 1, times the depth.
335 3 my $a = 1 - exp(-TWO_PI * $p->{rough_rate} / $rate);
336 3 $self->{rough} = [ $p->{rough} * sqrt(3 * (2 - $a) / $a), $a ];
337 3 $self->{rstate} = [ 0, int($self->_random * NOISE_SIZE) ];
338 }
339 1153 return;
340 }
341  
342 # Seconds to the next of $rate events a second that come at random moments.
343 653 ▶sub _wait ($self, $rate) { return -log(1 - $self->_random) / $rate }
Subroutine _wait: called CC=1 SCAR=0.0
344  
345 # A band's grains at time $t: bursts at random moments, grains a second,
346 # each of a loudness between half and whole that falls at grain_decay; their
347 # sum, of mean square 1 (whatever the rate). Asked for in time order; the
348 # same time twice (a quantum's last edge and the next's first), the same.
349 1591 ▶sub _grains ($self, $band, $t) {
Subroutine _grains: called CC=3 SCAR=11.0
350 1591 my ($last, $sum, $next) = @{ $band->{grain} };
351 1591 ▶ if ($t > $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t > $last) 1457 134 1591
352 1457 my ($rate, $decay) = @$band{qw(grains grain_decay)};
353 1457 $sum *= exp(-$decay * ($t - $last));
354 1457 ▶ while ($next <= $t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next <= $t) 640 1457 2097
355 640 $sum += (0.5 + 0.5 * $self->_random) * exp(-$decay * ($t - $next));
356 640 $next += $self->_wait($rate);
357 }
358 1457 $band->{grain} = [ $t, $sum, $next ];
359 }
360 1591 return $sum * $band->{grain_scale};
361 }
362  
363 # A band's share of the spectrum, which sets how much white noise it lets
364 # through: its level is made independent of the band's width.
365 973 ▶sub _noise_share ($mode, $centre, $q, $rate) {
Subroutine _noise_share: called CC=5 SCAR=16.1
366 973 my $nyquist = 0.5 * $rate;
367 973 ▶ my $share = $mode == 1 ? ($nyquist - $centre) / $nyquist # high-pass
Branch: 2 branches
BranchTrueFalseTotal
$mode == 1 ? : 3 970 973
$mode == 2 ? : 208 762 970
368 : $mode == 2 ? $centre / $nyquist # low-pass
369 : ($centre / $q) / $nyquist; # band-pass
370 973 ▶ return 1 if $share > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $share > 1 44 929 973
371 929 ▶ return 0.002 if $share < 0.002;
Branch: 1 branch
BranchTrueFalseTotal
if $share < "0.002" 5 924 929
372 924 return $share;
373 }
374  
375 # The string (Karplus-Strong, as Jaffe and Smith extend it): a delay line
376 # of a period, its loop a two-tap low-pass and a gain that makes it fall
377 # 60 dB in the patch's time, whatever the pitch. It starts with a period
378 # of the string's velocity, fed into the loop as it runs (so that the loop
379 # repeats it whole, its filters' delay as much part of the period as the
380 # line's): plucked, as the pick lets it go, two levels that change where it
381 # is plucked (its harmonics fall as sin(k pi pick) / k, none with a node at
382 # the pick); struck, the hammer's blow, a pulse less its copy a pick's
383 # length round (harmonics as sin(k pi pick), not falling); and a share of
384 # noise taken less its copy (the same nodes). Low-passed by how bright the
385 # pluck is (a hammer's felt: how hard it is); at unit RMS. A stiff string
386 # has four allpasses in its loop (see _dispersion). The line is long enough
387 # for the pitch to fall two octaves.
388 1153 ▶sub _string ($self, $vel) {
Subroutine _string: called CC=19 SCAR=29.7
389 1153 my $p = $self->{patch};
390 1153 ▶ return if $p->{string} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"string"} <= 0 1130 23 1153
391  
392 23 my $excite = $p->{string_excite};
393 23 ▶ die "no string excitation '$excite'\n" if $excite ne 'pluck' && $excite ne 'strike';
Branch: 1 branch
BranchTrueFalseTotal
if $excite ne "pluck" and $excite ne "strike" 0 23 23
Condition66%2/3
$excite ne "pluck" and $excite ne "strike"
!ll&&!rl&&r
1760
MC/DC0%0/2
$excite ne "pluck" and $excite ne "strike"
$excite ne "pluck" $excite ne "strike"
Truth table66%2/3
A and B
A= $excite ne "pluck"B= $excite ne "strike"
ABresult
0X0
100
111
394  
395 23 my $rate = $self->{rate};
396 23 my $period = $rate / $self->{f0};
397 23 my $size = 64;
398 23 ▶ $size *= 2 while $size < 4 * $period + 8;
Branch: 1 branch
BranchTrueFalseTotal
$size < 4 * $period + 8 and do { $size *= 2 } 90 23 113
399 23 my $t60 = $p->{string_decay} * (261.6 / $self->{f0})**$p->{string_key};
400 23 my $bright = $p->{string_bright} * 2**($p->{string_vel} * ($vel - 0.75) * 2);
401 23 ▶ $bright = 1 if $bright > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bright > 1 2 21 23
402 23 ▶ $bright = 0.005 if $bright < 0.005;
Branch: 1 branch
BranchTrueFalseTotal
if $bright < "0.005" 0 23 23
403  
404 # Where along the period the pick is.
405 23 ▶ my $length = int($period + 0.5) || 1;
Condition50%1/2
int $period + 0.5 || 1
l!l
230
MC/DC0%0/1
int $period + 0.5 || 1
int $period + 0.5
Truth table50%1/2
A
A= int $period + 0.5
Aresult
11
00
406 23 my $at = int($p->{string_pick} * $period + 0.5);
407 23 ▶ $at = 1 if $at < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 1 0 23 23
408 23 ▶ $at = $length - 1 if $at > $length - 1 && $length > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $length - 1 and $length > 1 0 23 23
Condition33%1/3
$at > $length - 1 and $length > 1
!ll&&!rl&&r
2300
MC/DC0%0/2
$at > $length - 1 and $length > 1
$at > $length - 1 $length > 1
Truth table33%1/3
A and B
A= $at > $length - 1B= $length > 1
ABresult
0X0
100
111
409  
410 my @shape = $excite eq 'pluck'
411 3852 ▶ ? _unit(map { $_ < $at ? 1.0 : 0.0 } 0 .. $length - 1)
Branch: 1 branch
BranchTrueFalseTotal
$_ < $at ? : 611 3241 3852
412 23 ▶ : _unit(map { $_ == 0 ? 1.0 : $_ == $at ? -1.0 : 0.0 } 0 .. $length - 1);
Branch: 3 branches
BranchTrueFalseTotal
$excite eq 'pluck' ? : 17 6 23
$_ == 0 ? : 6 1486 1492
$_ == $at ? : 6 1480 1486
413  
414 # The noise, less its copy taken round the period, as the loop repeats it.
415 23 my $table = noise_table();
416 23 my $index = int($self->_random * NOISE_SIZE);
417 23 my @noise = map { $table->[ ($index + $_) % NOISE_SIZE ] } 0 .. $length - 1;
418 23 @noise = _unit(map { $noise[$_] - $noise[ ($_ - $at) % $length ] } 0 .. $length - 1);
419  
420 23 my $mix = $p->{string_noise};
421 23 my @pluck = map { (1 - $mix) * $shape[$_] + $mix * $noise[$_] } 0 .. $length - 1;
422  
423 # Low-passed, round the period too (twice through, the second kept).
424 23 my $y = 0;
425 23 for my $pass (1, 2) {
426 46 for my $i (0 .. $length - 1) {
427 10688 $y += $bright * ($pluck[$i] - $y);
428 10688 ▶ $pluck[$i] = $y if $pass == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $pass == 2 5344 5344 10688
429 }
430 }
431  
432 23 my $s1 = $p->{string_damp};
433 23 ▶ my ($stages, $a) = $p->{string_stiff} > 0 ? _dispersion($rate, $self->{f0}, $s1, $p->{string_stiff}) : (0, 0.0);
Branch: 1 branch
BranchTrueFalseTotal
$p->{'string_stiff'} > 0 ? : 6 17 23
434 23 my $w0 = TWO_PI * $self->{f0} / $rate;
435  
436 $self->{string} = {
437 23 level => $p->{string} * 0.5,
438 g => 10**(-3 * $period / ($t60 * $rate)), # the loop's gain
439 s1 => $s1,
440 off => (_lp_lag($s1, $w0) + $stages * _ap_lag($a, $w0)) / $w0,
441 stages => $stages,
442 a => $a,
443 buf => [ (0.0) x $size ],
444 pos => 0,
445 prev => 0.0,
446 ax => 0.0,
447 ay => 0.0,
448 disp => [ (0.0) x 8 ],
449 exc => [ _unit(@pluck) ],
450 fed => 0,
451 t60 => $t60,
452 };
453 23 return;
454 }
455  
456 # Values less their mean, at unit RMS.
457 69 ▶sub _unit (@x) {
Subroutine _unit: called CC=4 SCAR=13.9
458 69 my $mean = 0;
459 69 $mean += $_ for @x;
460 69 $mean /= @x;
461  
462 69 my $sum = 0;
463 69 $sum += ($_ - $mean)**2 for @x;
464 69 ▶ my $norm = $sum > 0 ? 1 / sqrt($sum / @x) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$sum > 0 ? : 69 0 69
465  
466 69 return map { ($_ - $mean) * $norm } @x;
467 }
468  
469 # Phase lags (radians) at w (radians a sample): of a first-order allpass
470 # (a + z^-1) / (1 + a z^-1); of the string's low-pass (1 - s1) + s1 z^-1.
471 20223 ▶sub _ap_lag ($a, $w) { return atan2(sin $w, $a + cos $w) - atan2($a * sin $w, 1 + $a * cos $w) }
Subroutine _ap_lag: called CC=1 SCAR=0.0
472 10223 ▶sub _lp_lag ($s1, $w) { return atan2($s1 * sin $w, 1 - $s1 + $s1 * cos $w) }
Subroutine _lp_lag: called CC=1 SCAR=0.0
473  
474 # A stiff string's harmonics sharp, as stretch has the partials: harmonic
475 # k at k sqrt(1 + B k^2) / sqrt(1 + B) times the fundamental. Four
476 # first-order allpasses in the loop, all of one coefficient (negative: a
477 # delay that falls with frequency), fitted so that the loop as the engines
478 # run it (the line, the fractional allpass, the low-pass and the four)
479 # rings harmonic K there, K = 8 or the highest below 0.45 of the rate:
480 # the harmonics up to the tenth within some 3 cents at B = 0.0004 (25 at
481 # 0.002), those above less sharp than they should be. Bisection on the
482 # coefficient, a resonance found by bisection too; remembered by note.
483 my %DISPERSION;
484  
485 6 ▶sub _dispersion ($rate, $f0, $s1, $B) {
Subroutine _dispersion: called CC=10 SCAR=23.3
486 6 my $K = int(0.45 * $rate / $f0);
487 6 ▶ $K = 8 if $K > 8;
Branch: 1 branch
BranchTrueFalseTotal
if $K > 8 6 0 6
488 6 ▶ return (0, 0.0) if $K < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $K < 2 0 6 6
489  
490 6 my $note = "$rate $f0 $s1 $B";
491 6 ▶ return @{ $DISPERSION{$note} } if $DISPERSION{$note};
Branch: 1 branch
BranchTrueFalseTotal
if $DISPERSION{$note} 1 5 6
492  
493 5 my $w0 = TWO_PI * $f0 / $rate;
494 5 my $target = $w0 * $K * sqrt((1 + $B * $K * $K) / (1 + $B));
495 5 my ($lo, $hi) = (-0.98, 0.5);
496 5 for (1 .. 40) {
497 200 my $a = 0.5 * ($lo + $hi);
498 200 my $d = TWO_PI / $w0 - (_lp_lag($s1, $w0) + 4 * _ap_lag($a, $w0)) / $w0;
499 0 ▶ if ($d < 2) { $lo = $a; next } # more delay than the period has
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 2) 0 200 200
500  
501 200 my $whole = int($d - 0.1);
502 200 my $frac = $d - $whole;
503 200 my $eta = (1 - $frac) / (1 + $frac);
504  
505 # Where the loop rings harmonic K, at this coefficient.
506 200 ▶ my ($wl, $wh) = (0.5 * $target, $target * 1.5 > 3.14 ? 3.14 : $target * 1.5);
Branch: 1 branch
BranchTrueFalseTotal
$target * 1.5 > 3.14 ? : 0 200 200
507 200 for (1 .. 50) {
508 10000 my $w = 0.5 * ($wl + $wh);
509 10000 my $lag = $w * $whole + _ap_lag($eta, $w) + _lp_lag($s1, $w) + 4 * _ap_lag($a, $w);
510 10000 ▶ if ($lag < TWO_PI * $K) { $wl = $w } else { $wh = $w }
Branch: 1 branch
BranchTrueFalseTotal
if ($lag < 6.283185307179586232 * $K) { } 5140 4860 10000
511 }
512 200 ▶ if (0.5 * ($wl + $wh) < $target) { $hi = $a } else { $lo = $a } # flat: more dispersion
Branch: 1 branch
BranchTrueFalseTotal
if (0.5 * ($wl + $wh) < $target) { } 112 88 200
513 }
514  
515 5 $DISPERSION{$note} = [ 4, 0.5 * ($lo + $hi) ];
516 5 return @{ $DISPERSION{$note} };
517 }
518  
519 # FM's algorithms: [a34 a24 a23 a14 a13 a12] (who modulates whom), and which
520 # operators are carriers [1 2 3 4]. Modulation flows from higher operators
521 # to lower, so they are computed 4, 3, 2, 1.
522 17 my %ALGORITHM = (
523 1 => [ [ 1, 0, 1, 0, 0, 1 ], [ 1, 0, 0, 0 ] ], # 4 > 3 > 2 > 1
524 2 => [ [ 0, 1, 1, 0, 0, 1 ], [ 1, 0, 0, 0 ] ], # (4 + 3) > 2 > 1
525 3 => [ [ 0, 0, 1, 1, 0, 1 ], [ 1, 0, 0, 0 ] ], # (4 + (3 > 2)) > 1
526 4 => [ [ 1, 0, 0, 0, 1, 1 ], [ 1, 0, 0, 0 ] ], # ((4 > 3) + 2) > 1
527 5 => [ [ 1, 0, 0, 0, 0, 1 ], [ 1, 0, 1, 0 ] ], # 4 > 3, 2 > 1
528 6 => [ [ 1, 1, 0, 1, 0, 0 ], [ 1, 1, 1, 0 ] ], # 4 > 3, 2, 1
529 7 => [ [ 1, 0, 0, 0, 0, 0 ], [ 1, 1, 1, 0 ] ], # 4 > 3; 2; 1
530 8 => [ [ 0, 0, 0, 0, 0, 0 ], [ 1, 1, 1, 1 ] ], # 4; 3; 2; 1
531 );
532  
533 # FM made ready: each operator's frequency to the fundamental, its level
534 # (velocity and key scaled), the algorithm as coefficients.
535 1153 ▶sub _fm ($self, $vel) {
Subroutine _fm: called CC=4 SCAR=13.9
536 1153 my $p = $self->{patch};
537 1153 ▶ return if $p->{fm} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"fm"} <= 0 1137 16 1153
538  
539 16 ▶ my $algorithm = $ALGORITHM{ int $p->{fm_alg} } // die "no FM algorithm '$p->{fm_alg}'\n";
Condition50%1/2
$ALGORITHM{int $p->{"fm_alg"}} // die(qq[no FM algorithm '$p->{"fm_alg"}'\n])
l!l
160
MC/DC0%0/1
$ALGORITHM{int $p->{"fm_alg"}} // die(qq[no FM algorithm '$p->{"fm_alg"}'\n])
$ALGORITHM{int $p->{"fm_alg"}}
Truth table50%1/2
A
A= $ALGORITHM{int $p->{"fm_alg"}}
Aresult
11
00
540 16 my (@ratio, @base);
541 16 for my $o (1 .. 4) {
542 64 push @ratio, $p->{"op${o}_ratio"} * 2**($p->{"op${o}_detune"} / 1200);
543 64 my $sens = $p->{"op${o}_vel"};
544 64 push @base, $p->{"op${o}_level"} * (1 - $sens + $sens * $vel) * ($self->{f0} / 261.6)**(-$p->{"op${o}_key"});
545 }
546  
547 $self->{fm} = {
548 level => $p->{fm} * 0.5,
549 ratio => \@ratio,
550 base => \@base,
551 coef => $algorithm->[0],
552 carrier => $algorithm->[1],
553 16 fb => $p->{fm_fb} * PI,
554 phase => [ 0, 0, 0, 0 ],
555 hist => [ 0, 0 ], # operator 4's last two outputs
556 };
557 16 return;
558 }
559  
560 # The patch's filter and routes, made ready: [source, destination, amount]
561 # by number; with a filter, brightness (CC74) and resonance (CC71) move it
562 # (as the sound controllers would the spectrum) and so do the second
563 # envelope and velocity as the patch says. Without one, where a controller
564 # moves the cutoff, a low-pass of 12 dB at half the rate (as good as none
565 # until it is moved).
566 1153 ▶sub _modulation ($self, $vel) {
Subroutine _modulation: called CC=15 SCAR=27.1
567 1153 my $p = $self->{patch};
568 1153 ▶ my $mode = $FILTER{ $p->{filter} } // die "no filter '$p->{filter}'\n";
Condition50%1/2
$FILTER{$p->{"filter"}} // die(qq[no filter '$p->{"filter"}'\n])
l!l
11530
MC/DC0%0/1
$FILTER{$p->{"filter"}} // die(qq[no filter '$p->{"filter"}'\n])
$FILTER{$p->{"filter"}}
Truth table50%1/2
A
A= $FILTER{$p->{"filter"}}
Aresult
11
00
569  
570 1153 my @routes;
571 1153 ▶ for my $route (@{ $p->{mods} // [] }) {
Condition50%1/2
$p->{"mods"} // []
l!l
11530
MC/DC0%0/1
$p->{"mods"} // []
$p->{"mods"}
Truth table50%1/2
A
A= $p->{"mods"}
Aresult
11
00
572 25 my ($from, $to, $amount) = @$route;
573 25 ▶ my $source = $SOURCE{$from} // die "no modulation source '$from'\n";
Condition100%2/2
$SOURCE{$from} // die("no modulation source '${from}'\n")
l!l
241
MC/DC100%1/1
$SOURCE{$from} // die("no modulation source '${from}'\n")
$SOURCE{$from}
Truth table100%2/2
A
A= $SOURCE{$from}
Aresult
11
00
574 24 ▶ my $destination = $DESTINATION{$to} // die "no modulation destination '$to'\n";
Condition50%1/2
$DESTINATION{$to} // die("no modulation destination '${to}'\n")
l!l
240
MC/DC0%0/1
$DESTINATION{$to} // die("no modulation destination '${to}'\n")
$DESTINATION{$to}
Truth table50%1/2
A
A= $DESTINATION{$to}
Aresult
11
00
575 24 push @routes, [ $source, $destination, $amount ];
576 }
577 1152 ▶ if ($mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode) 24 1128 1152
578 24 push @routes, [ $SOURCE{cc74}, $DESTINATION{cutoff}, 4 ], [ $SOURCE{cc71}, $DESTINATION{q}, 64 / 24 ];
579 24 ▶ push @routes, [ $SOURCE{env2}, $DESTINATION{cutoff}, $p->{filter_env} ] if $p->{filter_env};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"filter_env"} 13 11 24
580 }
581  
582 1152 ▶ my $implied = !$mode && $self->{cutoff_control};
Condition100%3/3
!$mode && $self->{'cutoff_control'}
!ll&&!rl&&r
2411271
MC/DC100%2/2
!$mode && $self->{'cutoff_control'}
!$mode $self->{'cutoff_control'}
Truth table100%3/3
not(A) && B
A= !$modeB= $self->{'cutoff_control'}
ABresult
0X0
100
111
583 1152 ▶ return if !$mode && !$implied && !@routes;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode or $implied or @routes 1118 34 1152
Condition100%6/6
$mode or $implied or @routes
l!l&&r!l&&!r
2591118
$mode or $implied
l!l&&r!l&&!r
2411127
MC/DC100%3/3
$mode or $implied or @routes
$mode $implied @routes
Truth table100%4/4
A or B or C
A= $modeB= $impliedC= @routes
ABCresult
1XX1
01X1
0011
0000
584  
585 my $cutoff = $implied
586 ? 0.5 * $self->{rate}
587 34 ▶ : $p->{filter_cutoff} * ($self->{f0} / 261.6)**$p->{filter_track} * 2**($p->{filter_vel} * ($vel - 0.75) * 2);
Branch: 1 branch
BranchTrueFalseTotal
$implied ? : 1 33 34
588 34 ▶ $self->{fmode} = $implied ? LOW_PASS_12 : $mode;
Branch: 1 branch
BranchTrueFalseTotal
$implied ? : 1 33 34
589 $self->{mod} = {
590 routes => \@routes,
591 85 used => { map { $_->[0] => 1 } @routes }, # the sources that are read
592 85 ▶ to => { map { $_->[1] => 1 } @routes }, # the destinations that are written
Branch: 1 branch
BranchTrueFalseTotal
$implied ? : 1 33 34
593 cutoff => $cutoff,
594 q => $implied ? 0.7071 : $p->{filter_q},
595 34 lfo => [ map { _lfo_spec($p, $_) } 1, 2 ],
596 };
597 34 return;
598 }
599  
600 # LFO $n of a patch: [ rate, shape, delay ].
601 68 ▶sub _lfo_spec ($p, $n) {
Subroutine _lfo_spec: called CC=2 SCAR=7.2
602 68 my $shape = $p->{"lfo${n}_shape"};
603 68 ▶ return [ $p->{"lfo${n}_rate"}, $SHAPE{$shape} // die("no LFO shape '$shape'\n"), $p->{"lfo${n}_delay"} ];
Condition50%1/2
$SHAPE{$shape} // die("no LFO shape '${shape}'\n")
l!l
680
MC/DC0%0/1
$SHAPE{$shape} // die("no LFO shape '${shape}'\n")
$SHAPE{$shape}
Truth table50%1/2
A
A= $SHAPE{$shape}
Aresult
11
00
604 }
605  
606 # An LFO ([rate, shape, delay]) at a time: -1 .. 1, faded in over its delay.
607 3059 ▶sub _lfo ($self, $lfo, $t, $n) {
Subroutine _lfo: called CC=10 SCAR=23.0
608 3059 my ($rate, $shape, $delay) = @$lfo;
609 3059 ▶ return 0 if $t <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t <= 0 14 3045 3059
610  
611 3045 my $phase = $rate * $t;
612 3045 my $frac = $phase - int $phase;
613 3045 my $v;
614 3045 ▶ if ($shape == 0) {
Branch: 4 branches
BranchTrueFalseTotal
if ($shape == 0) { } 1052 1993 3045
elsif ($shape == 1) { } 1068 925 1993
elsif ($shape == 2) { } 553 372 925
elsif ($shape == 3) { } 1 371 372
615 1052 $v = sin(TWO_PI * $phase);
616 }
617 elsif ($shape == 1) {
618 1068 my $f = $frac + 0.25;
619 1068 ▶ $f -= 1 if $f >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $f >= 1 120 948 1068
620 1068 $v = 1 - 4 * abs($f - 0.5);
621 }
622 elsif ($shape == 2) {
623 553 ▶ $v = $frac < 0.5 ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$frac < 0.5 ? : 312 241 553
624 }
625 elsif ($shape == 3) {
626 1 $v = 2 * $frac - 1;
627 }
628 else {
629 # Random, held for a period: the period's number and the voice's seed hashed.
630 371 my $h = ((int($phase) + 1) * 2654435761 + $self->{seed} * 40503 + $n * 977) % 4294967296;
631 371 $h = ($h * 1103515245 + 12345) % 2147483648;
632 371 $v = $h / 1073741824 - 1;
633 }
634 3045 ▶ return $delay > 0 && $t < $delay ? $v * $t / $delay : $v;
Branch: 1 branch
BranchTrueFalseTotal
$delay > 0 && $t < $delay ? : 1 3044 3045
Condition66%2/3
$delay > 0 and $t < $delay
!ll&&!rl&&r
304401
MC/DC50%1/2
$delay > 0 and $t < $delay
$delay > 0 $t < $delay
Truth table66%2/3
A and B
A= $delay > 0B= $t < $delay
ABresult
0X0
100
111
635 }
636  
637 # The second envelope at a time (s since the voice started).
638 1462 ▶sub _env2 ($self, $t) {
Subroutine _env2: called CC=1 SCAR=0.0
639 1462 my $p = $self->{patch};
640 1462 return $self->_adsr($t, @$p{qw(env2_attack env2_decay env2_sustain env2_release)});
641 }
642  
643 # An envelope of attack (linear), decay (a time constant) to sustain, and
644 # release (a time constant, from where the key was let go) at a time; a
645 # legato note has no attack.
646 15774 ▶sub _adsr ($self, $t, $attack, $decay, $sustain, $release) {
Subroutine _adsr: called CC=6 SCAR=18.0
647 15774 ▶ $attack = 0 if $self->{legato};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"legato"} 0 15774 15774
648 15774 ▶ $decay ||= 1e-3;
Condition50%1/2
$decay ||= "0.001"
l!l
157740
MC/DC0%0/1
$decay ||= "0.001"
$decay
Truth table50%1/2
A
A= $decay
Aresult
11
00
649  
650 15774 ▶ my $held = sub ($t) {
Subroutine __ANON__: called CC=3 SCAR=11.0
651 15774 ▶ return 0 if $t <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t <= 0 71 15703 15774
652 15703 ▶ return $t / $attack if $t < $attack;
Branch: 1 branch
BranchTrueFalseTotal
if $t < $attack 462 15241 15703
653 15241 return $sustain + (1 - $sustain) * exp(-($t - $attack) / $decay);
654 15774 };
655 15774 ▶ return $held->($t) if !defined $self->{released};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"released"} 13776 1998 15774
656  
657 1998 my $released = ($self->{released} - $self->{start}) / $self->{rate};
658 1998 ▶ return $held->($t) if $t <= $released;
Branch: 1 branch
BranchTrueFalseTotal
if $t <= $released 36 1962 1998
659 1962 ▶ return $held->($released) * exp(-($t - $released) / ($release || 1e-3));
Condition50%1/2
$release || 0.001
l!l
19620
MC/DC0%0/1
$release || 0.001
$release
Truth table50%1/2
A
A= $release
Aresult
11
00
660 }
661  
662 # The destinations at the edges of a quantum's blocks: [pitch, cutoff, q,
663 # level, pan, noise, fm], each an array of $nb + 1 (undef where nothing routes).
664 506 ▶sub _destinations ($self, $first, $nb, $t0, $dt, $channel) {
Subroutine _destinations: called CC=12 SCAR=25.6
665 506 my $m = $self->{mod};
666 506 my ($used, $routes) = @$m{qw(used routes)};
667  
668 506 ▶ my @out = map { $m->{to}{$_} ? [ (0.0) x ($nb + 1) ] : undef } 0 .. $#DESTINATIONS;
Branch: 1 branch
BranchTrueFalseTotal
$m->{'to'}{$_} ? : 1093 2449 3542
669 506 my @value = (0) x @SOURCES;
670 506 my @live = grep { $used->{$_} } 0 .. $#SOURCES;
671 506 for my $j ($first .. $nb) {
672 4388 my $t = $t0 + $j * $dt;
673  
674 # Each source read at this edge.
675 4388 for my $s (@live) {
676 11726 my $name = $SOURCES[$s];
677 11726 ▶ if ($name eq 'velocity') { $value[$s] = $self->{velocity} / 127 }
Branch: 6 branches
BranchTrueFalseTotal
if ($name eq 'velocity') { } 778 10948 11726
elsif ($name eq 'key') { } 0 10948 10948
elsif ($name eq 'env2') { } 1462 9486 10948
elsif ($name eq 'lfo1') { } 2139 7347 9486
elsif ($name eq 'lfo2') { } 910 6437 7347
elsif ($name eq 'pressure') { } 0 6437 6437
678 0 elsif ($name eq 'key') { $value[$s] = ($self->{key} - 60) / 12 }
679 1462 elsif ($name eq 'env2') { $value[$s] = $self->_env2($t) }
680 2139 elsif ($name eq 'lfo1') { $value[$s] = $self->_lfo($m->{lfo}[0], $t, 1) }
681 910 elsif ($name eq 'lfo2') { $value[$s] = $self->_lfo($m->{lfo}[1], $t, 2) }
682 elsif ($name eq 'pressure') {
683 0 my $channel_pressure = $channel->{chp}[$j];
684 0 ▶ $value[$s] = $self->{pressure} > $channel_pressure ? $self->{pressure} : $channel_pressure;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'pressure'} > $channel_pressure ? : 0 0 0
685 }
686 6437 else { $value[$s] = $channel->{$name}[$j] }
687 }
688  
689 4388 $out[ $_->[1] ][$j] += $_->[2] * $value[ $_->[0] ] for @$routes;
690 }
691 506 return \@out;
692 }
693  
694 # A filter's gain at x (a frequency over the cutoff) and Q, by mode (see
695 # the top): what every engine computes, for the edges of each partial.
696 9 ▶sub filter_gain ($x, $q, $mode) {
Subroutine filter_gain: called CC=6 SCAR=17.9
Pod documented
697 9 ▶ return 1 if !$mode;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode 1 8 9
698  
699 8 my $x2 = $x * $x;
700 8 my $d = sqrt((1 - $x2) * (1 - $x2) + $x2 / ($q * $q));
701 8 ▶ return 1 / $d if $mode == LOW_PASS_12;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == 1 3 5 8
702 5 ▶ return 1 / ($d * sqrt(1 + $x2 * $x2)) if $mode == LOW_PASS_24;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == 2 1 4 5
703 4 ▶ return $x2 / $d if $mode == HIGH_PASS_12;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == 3 2 2 4
704 2 ▶ return $x2 * $x2 / ($d * sqrt(1 + $x2 * $x2)) if $mode == HIGH_PASS_24;
Branch: 1 branch
BranchTrueFalseTotal
if $mode == 4 0 2 2
705 2 return $x / $q / $d;
706 }
707  
708 # A channel's equalizer on the voice: each partial's amplitude, and the
709 # noise's and the string's levels, times the equalizer's gain at its
710 # frequency (a band of noise's at its centre, the string's at its pitch).
711 # From the levels the voice was made with, so it can be applied again as
712 # the equalizer changes. Nothing is done where nothing would change: a flat
713 # equalizer on a voice never equalized, or a change that leaves every gain
714 # as it was (a band at 0 dB moved).
715 7 ▶sub equalize ($self, $eq) {
Subroutine equalize: called CC=15 SCAR=27.9
Pod documented
716 7 ▶ return $self if !$eq->active && !$self->{amp0};
Branch: 1 branch
BranchTrueFalseTotal
unless $eq->active or $self->{"amp0"} 1 6 7
Condition100%3/3
$eq->active or $self->{"amp0"}
l!l&&r!l&&!r
511
MC/DC100%2/2
$eq->active or $self->{"amp0"}
$eq->active $self->{"amp0"}
Truth table100%3/3
A or B
A= $eq->activeB= $self->{"amp0"}
ABresult
1X1
011
000
717  
718 6 ▶ $self->{amp0} //= [ @{ $self->{amp} } ];
Condition100%2/2
$self->{"amp0"} //= [@{$self->{"amp"};}]
l!l
42
MC/DC100%1/1
$self->{"amp0"} //= [@{$self->{"amp"};}]
$self->{"amp0"}
Truth table100%2/2
A
A= $self->{"amp0"}
Aresult
11
00
719 6 ▶ $_->{level0} //= $_->{level} for @{ $self->{bands} };
Condition66%2/3
$_->{"level0"} //= $_->{"level"}
l!l&&r!l&&!r
420
MC/DC0%0/2
$_->{"level0"} //= $_->{"level"}
$_->{"level0"} $_->{"level"}
Truth table66%2/3
A //= B
A= $_->{"level0"}B= $_->{"level"}
ABresult
1X1
011
000
720 6 my $string = $self->{string};
721 6 ▶ $string->{level0} //= $string->{level} if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 6 6
Condition0%0/3
$string->{"level0"} //= $string->{"level"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$string->{"level0"} //= $string->{"level"}
$string->{"level0"} $string->{"level"}
Truth table0%0/3
A //= B
A= $string->{"level0"}B= $string->{"level"}
ABresult
1X1
011
000
722  
723 6 my $f0 = $self->{f0};
724 6 my $ratio = $self->{ratio};
725 6 my $amp0 = $self->{amp0};
726 6 my @amp = map { $amp0->[$_] * 10**($eq->response($f0 * $ratio->[$_]) / 20) } 0 .. $#$amp0;
727 6 my @levels = map { $_->{level0} * 10**($eq->response($_->{centre}) / 20) } @{ $self->{bands} };
728 6 ▶ push @levels, $string->{level0} * 10**($eq->response($f0) / 20) if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 6 6
729 6 ▶ my @now = (map({ $_->{level} } @{ $self->{bands} }), $string ? $string->{level} : ());
Branch: 1 branch
BranchTrueFalseTotal
$string ? : 0 6 6
730 6 ▶ return $self if !_differ(\@amp, $self->{amp}) && !_differ(\@levels, \@now);
Branch: 1 branch
BranchTrueFalseTotal
unless _differ(\@amp, $self->{"amp"}) or _differ(\@levels, \@now) 1 5 6
Condition66%2/3
_differ(\@amp, $self->{"amp"}) or _differ(\@levels, \@now)
l!l&&r!l&&!r
501
MC/DC50%1/2
_differ(\@amp, $self->{"amp"}) or _differ(\@levels, \@now)
_differ(\@amp, $self->{"amp"}) _differ(\@levels, \@now)
Truth table66%2/3
A or B
A= _differ(\@amp, $self->{"amp"})B= _differ(\@levels, \@now)
ABresult
1X1
011
000
731  
732 5 $self->{amp} = \@amp;
733 5 $_->{level} = shift @levels for @{ $self->{bands} };
734 5 ▶ $string->{level} = shift @levels if $string;
Branch: 1 branch
BranchTrueFalseTotal
if $string 0 5 5
735 5 delete $self->{gpu_parts}; # what an engine keeps of the partials
736 5 ▶ $self->_waves($self->{waves}) if $self->{wave};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"wave"} 0 5 5
737 5 return $self;
738 }
739  
740 # Whether two lists of numbers differ.
741 7 ▶sub _differ ($x, $y) {
Subroutine _differ: called CC=4 SCAR=13.9
742 7 ▶ return 1 if @$x != @$y;
Branch: 1 branch
BranchTrueFalseTotal
if @$x != @$y 0 7 7
743 7 for my $i (0 .. $#$x) {
744 35 ▶ return 1 if $x->[$i] != $y->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $x->[$i] != $y->[$i] 5 30 35
745 }
746 2 return 0;
747 }
748  
749 # The partials as wave sequences (Peta::Synth::Wavetable), where they are
750 # harmonics and no filter moves over them: for each copy of the unison its
751 # shift (its fundamental over the voice's), its sequence, its place in the
752 # period (radians; the copies one place with unison_sync) and its table.
753 # An engine reads those and leaves the partials be. Made again, the places
754 # kept, when the partials change (an equalizer).
755 686 ▶sub _waves ($self, $store) {
Subroutine _waves: called CC=9 SCAR=22.1
756 686 my $was = delete $self->{wave};
757 686 ▶ return if $self->{fmode} || !@{ $self->{ratio} };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fmode"} or not @{$self->{"ratio"};} 86 600 686
Condition100%3/3
$self->{"fmode"} or not @{$self->{"ratio"};}
l!l&&r!l&&!r
1967600
MC/DC100%2/2
$self->{"fmode"} or not @{$self->{"ratio"};}
$self->{"fmode"} not @{$self->{"ratio"};}
Truth table100%3/3
A or not(B)
A= $self->{"fmode"}B= not @{$self->{"ratio"};}
ABresult
1X1
011
000
758  
759 600 ▶ my $copies = Peta::Synth::Wavetable::harmonics(@$self{qw(ratio amp decay attack)}, $self->{copies}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $copies = Peta::Synth::Wavetable::harmonics(@$self{"ratio", "amp", "decay", "attack"}, $self->{"copies"}) or return 207 393 600
760 207 $self->{waves} = $store;
761  
762 207 ▶ my $place = $was ? undef : TWO_PI * $self->_random;
Branch: 1 branch
BranchTrueFalseTotal
$was ? : 0 207 207
763 207 my @wave;
764 207 for my $c (0 .. $#$copies) {
765 361 my ($shift, $partials) = @{ $copies->[$c] };
766 361 my ($at, $table);
767 361 ▶ if ($was) {
Branch: 1 branch
BranchTrueFalseTotal
if ($was) { } 0 361 361
768 0 ($at, $table) = @{ $was->[$c] }[ 2, 3 ];
769 }
770 else {
771 361 ▶ $at = $self->{patch}{unison_sync} || $c == 0 ? $place : TWO_PI * $self->_random;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'patch'}{'unison_sync'} || $c == 0 ? : 207 154 361
Condition100%3/3
$self->{"patch"}{"unison_sync"} or $c == 0
l!l&&r!l&&!r
2205154
MC/DC100%2/2
$self->{"patch"}{"unison_sync"} or $c == 0
$self->{"patch"}{"unison_sync"} $c == 0
Truth table100%3/3
A or B
A= $self->{"patch"}{"unison_sync"}B= $c == 0
ABresult
1X1
011
000
772 361 $table = 0;
773 }
774 361 push @wave, [ $shift, $store->sequence($partials), $at, $table ];
775 }
776 207 $self->{wave} = \@wave;
777 207 return;
778 }
779  
780 # The key was let go at sample $at, with a note-off velocity (64: as the
781 # patch says; faster lets go faster; 0, as many files write, says nothing).
782 414 ▶sub release ($self, $at, $velocity = undef) {
Subroutine release: called CC=4 SCAR=13.9
Pod documented
783 414 $self->off_velocity($velocity);
784 414 ▶ $self->{released} //= $at if !$self->{patch}{drum};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"patch"}{"drum"} 335 79 414
Condition66%2/3
$self->{"released"} //= $at
l!l&&r!l&&!r
43310
MC/DC0%0/2
$self->{"released"} //= $at
$self->{"released"} $at
Truth table66%2/3
A //= B
A= $self->{"released"}B= $at
ABresult
1X1
011
000
785 414 delete $self->{envelope_state};
786 414 return;
787 }
788  
789 # A note-off velocity, for a release to come (a pedal may hold the note):
790 # its release time 1.41 times as long at 1, as the patch says at 64, 0.71
791 # times at 127.
792 418 ▶sub off_velocity ($self, $velocity) {
Subroutine off_velocity: called CC=2 SCAR=6.9
Pod documented
793 418 ▶ $self->{rfactor} = 2**((64 - $velocity) / 128) if $velocity;
Branch: 1 branch
BranchTrueFalseTotal
if $velocity 6 412 418
794 418 delete $self->{envelope_state};
795 418 return;
796 }
797  
798 # Ends the voice quickly (voice stealing, all sound off): a release of 5 ms.
799 31 ▶sub kill ($self, $at) {
Subroutine kill: called CC=2 SCAR=7.0
Pod documented
800 31 ▶ $self->{killed} //= $at;
Condition66%2/3
$self->{"killed"} //= $at
l!l&&r!l&&!r
2290
MC/DC0%0/2
$self->{"killed"} //= $at
$self->{"killed"} $at
Truth table66%2/3
A //= B
A= $self->{"killed"}B= $at
ABresult
1X1
011
000
801 31 delete $self->{envelope_state};
802 31 return;
803 }
804  
805 # The envelope's attack, hold, fall and sustain: the patch's, as the channel
806 # shaped them; a legato note has no attack.
807 1711 ▶sub _envelope ($self) {
Subroutine _envelope: called CC=3 SCAR=11.0
808 1711 my ($p, $shape) = @$self{qw(patch shape)};
809 1711 ▶ my $attack = $self->{legato} ? 0 : $p->{attack} * $shape->{attack};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'legato'} ? : 1 1710 1711
810 1711 ▶ my $fall = ($p->{fall} || 1e-3) * $shape->{decay};
Condition50%1/2
$p->{'fall'} || 0.001
l!l
17110
MC/DC0%0/1
$p->{'fall'} || 0.001
$p->{'fall'}
Truth table50%1/2
A
A= $p->{'fall'}
Aresult
11
00
811 1711 return ($attack, $p->{hold}, $fall, $p->{sustain});
812 }
813  
814 # The envelope before any release, at a time (s since the voice started).
815 329 ▶sub _held ($self, $t) {
Subroutine _held: called CC=4 SCAR=14.0
816 329 my ($attack, $hold, $fall, $sustain) = $self->_envelope;
817 329 ▶ return 0 if $t < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 0 329 329
818 329 ▶ return $t / $attack if $t < $attack;
Branch: 1 branch
BranchTrueFalseTotal
if $t < $attack 30 299 329
819  
820 299 $t -= $attack + $hold;
821 299 ▶ return 1 if $t <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t <= 0 0 299 299
822 299 return $sustain + (1 - $sustain) * exp(-$t / $fall);
823 }
824  
825 # What the envelope is made of as things stand: [ attack, hold, fall,
826 # sustain; the time the key was let go (-1: not), the level then and the
827 # release's time constant; the time the voice was killed (-1: not); a
828 # drum's length (-1: none) ], times in seconds since the voice started.
829 # Kept until the voice is let go, killed or given a note-off velocity.
830 255 ▶sub _envelope_state ($self) {
Subroutine _envelope_state: called CC=2 SCAR=7.2
831 255 ▶ return $self->{envelope_state} //= $self->_make_envelope_state;
Condition66%2/3
$self->{"envelope_state"} //= $self->_make_envelope_state
l!l&&r!l&&!r
243120
MC/DC0%0/2
$self->{"envelope_state"} //= $self->_make_envelope_state
$self->{"envelope_state"} $self->_make_envelope_state
Truth table66%2/3
A //= B
A= $self->{"envelope_state"}B= $self->_make_envelope_state
ABresult
1X1
011
000
832 }
833  
834 1014 ▶sub _make_envelope_state ($self) {
Subroutine _make_envelope_state: called CC=6 SCAR=17.9
835 1014 my $p = $self->{patch};
836 1014 my $rate = $self->{rate};
837  
838 1014 ▶ my $released = defined $self->{released} ? ($self->{released} - $self->{start}) / $rate : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'released'} ? : 329 685 1014
839 1014 ▶ my $from = $released >= 0 ? $self->_held($released) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$released >= 0 ? : 329 685 1014
840 1014 ▶ my $release = ($p->{release} || 1e-3) * $self->{shape}{release} * $self->{rfactor};
Condition50%1/2
$p->{'release'} || 0.001
l!l
10140
MC/DC0%0/1
$p->{'release'} || 0.001
$p->{'release'}
Truth table50%1/2
A
A= $p->{'release'}
Aresult
11
00
841 1014 ▶ my $killed = defined $self->{killed} ? ($self->{killed} - $self->{start}) / $rate : -1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'killed'} ? : 7 1007 1014
842 1014 ▶ my $length = $p->{drum} ? $p->{length} : -1;
Branch: 1 branch
BranchTrueFalseTotal
$p->{'drum'} ? : 215 799 1014
843  
844 1014 return [ $self->_envelope, $released, $from, $release, $killed, $length ];
845 }
846  
847 # The envelope at time $t (raw: before the level and the tremolo): attack,
848 # hold, fall to sustain; release after the key; 5 ms after a kill; a drum
849 # fades past its length.
850 255 ▶sub _envelope_value ($state, $t) {
Subroutine _envelope_value: called CC=10 SCAR=23.6
851 255 my ($attack, $hold, $fall, $sustain, $released, $from, $release, $killed, $length) = @$state;
852  
853 255 ▶ my $e = $t <= 0 ? 0
Branch: 3 branches
BranchTrueFalseTotal
$t <= 0 ? : 0 255 255
$t < $attack ? : 0 255 255
$t <= $attack + $hold ? : 8 247 255
854 : $t < $attack ? $t / $attack
855 : $t <= $attack + $hold ? 1
856 : $sustain + (1 - $sustain) * exp(-($t - $attack - $hold) / $fall);
857  
858 255 ▶ $e = $from * exp(-($t - $released) / $release) if $released >= 0 && $t > $released;
Branch: 1 branch
BranchTrueFalseTotal
if $released >= 0 and $t > $released 98 157 255
Condition100%3/3
$released >= 0 and $t > $released
!ll&&!rl&&r
1312698
MC/DC100%2/2
$released >= 0 and $t > $released
$released >= 0 $t > $released
Truth table100%3/3
A and B
A= $released >= 0B= $t > $released
ABresult
0X0
100
111
859 255 ▶ $e *= exp(-($t - $killed) / 0.005) if $killed >= 0 && $t > $killed;
Branch: 1 branch
BranchTrueFalseTotal
if $killed >= 0 and $t > $killed 1 254 255
Condition66%2/3
$killed >= 0 and $t > $killed
!ll&&!rl&&r
25401
MC/DC50%1/2
$killed >= 0 and $t > $killed
$killed >= 0 $t > $killed
Truth table66%2/3
A and B
A= $killed >= 0B= $t > $killed
ABresult
0X0
100
111
860 255 ▶ $e *= exp(-($t - $length) / 0.01) if $length >= 0 && $t > $length;
Branch: 1 branch
BranchTrueFalseTotal
if $length >= 0 and $t > $length 0 255 255
Condition66%2/3
$length >= 0 and $t > $length
!ll&&!rl&&r
189660
MC/DC0%0/2
$length >= 0 and $t > $length
$length >= 0 $t > $length
Truth table66%2/3
A and B
A= $length >= 0B= $t > $length
ABresult
0X0
100
111
861 255 return $e;
862 }
863  
864 # The envelope at a time (s since the voice started) as controls works it
865 # out (raw: before the level and the tremolo), the key let go or not as it
866 # stands now.
867 255 ▶sub envelope_at ($self, $t) { return _envelope_value($self->_envelope_state, $t) }
Subroutine envelope_at: called CC=1 SCAR=0.0
Pod documented
868  
869 # The pitch's offset from the note (semitones) at a time, from its glide:
870 # [from, to, the time it starts, how long it takes].
871 164 ▶sub _glide ($self, $t) {
Subroutine _glide: called CC=4 SCAR=14.1
872 164 ▶ my $g = $self->{glide} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $g = $self->{"glide"} or return 0 164 0 164
873 164 my ($from, $to, $start, $length) = @$g;
874 164 ▶ return $to if $t >= $start + $length;
Branch: 1 branch
BranchTrueFalseTotal
if $t >= $start + $length 108 56 164
875 56 ▶ return $from if $t <= $start;
Branch: 1 branch
BranchTrueFalseTotal
if $t <= $start 0 56 56
876 56 return $from + ($to - $from) * ($t - $start) / $length;
877 }
878  
879 # The voice's controls for a quantum that starts at sample $now: $nb blocks
880 # of $B samples, the channel's gains, bend (semitones) and modulation at the
881 # blocks' edges in $channel (and its vibrato's rate, depth and delay as
882 # factors, its modulation range in cents). Fills t0, w, env, the bands' env,
883 # gl, gr, gs, the operators' levels, the filter's edges and boost (see the
884 # top). The parts every voice has are written out here, as this runs for
885 # every voice and quantum; the parts few have are subs of their own.
886 17081 ▶sub controls ($self, $now, $nb, $B, $channel) {
Subroutine controls: called CC=45 SCAR=38.1
Pod documented
887 17081 my $p = $self->{patch};
888 17081 my $rate = $self->{rate};
889 17081 my $first = $self->{first};
890 17081 my $dt = $B / $rate;
891 17081 my $t0 = ($now - $self->{start}) / $rate;
892 17081 my @edges = ($first, $nb, $t0, $dt);
893 17081 $self->{t0} = $t0;
894  
895 # The envelope at the edges (as _envelope_value has it), times the
896 # tremolo and the voice's level.
897 17081 ▶ my $state = $self->{envelope_state} //= $self->_make_envelope_state;
Condition66%2/3
$self->{"envelope_state"} //= $self->_make_envelope_state
l!l&&r!l&&!r
1607910020
MC/DC0%0/2
$self->{"envelope_state"} //= $self->_make_envelope_state
$self->{"envelope_state"} $self->_make_envelope_state
Truth table66%2/3
A //= B
A= $self->{"envelope_state"}B= $self->_make_envelope_state
ABresult
1X1
011
000
898 17081 my ($attack, $hold, $fall, $sustain, $released, $from, $release, $killed, $length) = @$state;
899 17081 my ($trem, $trem_rate) = @$p{qw(tremolo trem_rate)};
900 17081 my $level = $self->{level};
901 17081 my @env = (0.0) x ($nb + 1);
902 17081 my $raw = 0;
903 17081 for my $j ($first .. $nb) {
904 168339 my $t = $t0 + $j * $dt;
905 168339 ▶ my $e = $t <= 0 ? 0
Branch: 3 branches
BranchTrueFalseTotal
$t <= 0 ? : 596 167743 168339
$t < $attack ? : 10482 157261 167743
$t <= $attack + $hold ? : 41 157220 157261
906 : $t < $attack ? $t / $attack
907 : $t <= $attack + $hold ? 1
908 : $sustain + (1 - $sustain) * exp(-($t - $attack - $hold) / $fall);
909 168339 ▶ $e = $from * exp(-($t - $released) / $release) if $released >= 0 && $t > $released;
Branch: 1 branch
BranchTrueFalseTotal
if $released >= 0 and $t > $released 37997 130342 168339
Condition100%3/3
$released >= 0 and $t > $released
!ll&&!rl&&r
12956977337997
MC/DC100%2/2
$released >= 0 and $t > $released
$released >= 0 $t > $released
Truth table100%3/3
A and B
A= $released >= 0B= $t > $released
ABresult
0X0
100
111
910 168339 ▶ $e *= exp(-($t - $killed) / 0.005) if $killed >= 0 && $t > $killed;
Branch: 1 branch
BranchTrueFalseTotal
if $killed >= 0 and $t > $killed 73 168266 168339
Condition100%3/3
$killed >= 0 and $t > $killed
!ll&&!rl&&r
168258873
MC/DC100%2/2
$killed >= 0 and $t > $killed
$killed >= 0 $t > $killed
Truth table100%3/3
A and B
A= $killed >= 0B= $t > $killed
ABresult
0X0
100
111
911 168339 ▶ $e *= exp(-($t - $length) / 0.01) if $length >= 0 && $t > $length;
Branch: 1 branch
BranchTrueFalseTotal
if $length >= 0 and $t > $length 274 168065 168339
Condition100%3/3
$length >= 0 and $t > $length
!ll&&!rl&&r
14557022495274
MC/DC100%2/2
$length >= 0 and $t > $length
$length >= 0 $t > $length
Truth table100%3/3
A and B
A= $length >= 0B= $t > $length
ABresult
0X0
100
111
912  
913 168339 $raw = $e;
914 168339 ▶ $env[$j] = $e * $level * ($trem ? 1 - $trem * (0.5 - 0.5 * cos(TWO_PI * $trem_rate * $t)) : 1);
Branch: 1 branch
BranchTrueFalseTotal
$trem ? : 7547 160792 168339
915 }
916 17081 $self->{raw_env} = $raw; # at the quantum's end
917  
918 # What few voices have: noise bands, modulation, FM, GM2's controller
919 # destinations.
920 17081 ▶ $self->_noise_edges(@edges) if @{ $self->{bands} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"bands"};} 13801 3280 17081
921  
922 17081 ▶ my $dest = $self->{mod} ? $self->_destinations(@edges, $channel) : [];
Branch: 1 branch
BranchTrueFalseTotal
$self->{'mod'} ? : 506 16575 17081
923 17081 my ($m_pitch, $m_cutoff, $m_q, $m_level, $m_pan, $m_noise, $m_fm) = @$dest;
924 17081 ▶ $self->_fm_levels(@edges, $m_fm) if $self->{fm};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fm"} 268 16813 17081
925 17081 ▶ _scale_edges(\@env, $m_level, $first, $nb) if $m_level;
Branch: 1 branch
BranchTrueFalseTotal
if $m_level 62 17019 17081
926 17081 ▶ _scale_edges($_->{env}, $m_noise, $first, $nb) for $m_noise ? @{ $self->{bands} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$m_noise ? : 54 17027 17081
927  
928 17081 ▶ my $gm = $channel->{dest} || $channel->{kp} ? $self->_gm_destinations(\@env, @edges, $channel) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$channel->{'dest'} || $channel->{'kp'} ? : 37 17044 17081
Condition100%3/3
$channel->{"dest"} or $channel->{"kp"}
l!l&&r!l&&!r
35217044
MC/DC100%2/2
$channel->{"dest"} or $channel->{"kp"}
$channel->{"dest"} $channel->{"kp"}
Truth table100%3/3
A or B
A= $channel->{"dest"}B= $channel->{"kp"}
ABresult
1X1
011
000
929 17081 ▶ my $gml = $gm ? $gm->{levels} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$gm ? : 37 17044 17081
930  
931 # The fundamental at each block's middle, in radians a sample: key, bend
932 # and tuning, vibrato (the modulation wheel and the pressure deepen it),
933 # sweep, glide.
934 17081 my $f0 = $self->{f0};
935 17081 my ($sweep, $sweep_time) = @$p{qw(sweep sweep_time)};
936 17081 ▶ my $vibrato = $p->{vibrato} * ($channel->{vdepth} // 1);
Condition100%2/2
$channel->{"vdepth"} // 1
l!l
170765
MC/DC100%1/1
$channel->{"vdepth"} // 1
$channel->{"vdepth"}
Truth table100%2/2
A
A= $channel->{"vdepth"}
Aresult
11
00
937 17081 ▶ my $vib_rate = $p->{vib_rate} * ($channel->{vrate} // 1);
Condition100%2/2
$channel->{"vrate"} // 1
l!l
170765
MC/DC100%1/1
$channel->{"vrate"} // 1
$channel->{"vrate"}
Truth table100%2/2
A
A= $channel->{"vrate"}
Aresult
11
00
938 17081 ▶ my $vib_delay = $p->{vib_delay} * ($channel->{vdelay} // 1);
Condition100%2/2
$channel->{"vdelay"} // 1
l!l
170765
MC/DC100%1/1
$channel->{"vdelay"} // 1
$channel->{"vdelay"}
Truth table100%2/2
A
A= $channel->{"vdelay"}
Aresult
11
00
939 17081 ▶ my $pressure = $channel->{kp} ? 0 : $self->{pressure} * ($channel->{modrange} // 50);
Branch: 1 branch
BranchTrueFalseTotal
$channel->{'kp'} ? : 2 17079 17081
Condition100%2/2
$channel->{"modrange"} // 50
l!l
170745
MC/DC100%1/1
$channel->{"modrange"} // 50
$channel->{"modrange"}
Truth table100%2/2
A
A= $channel->{"modrange"}
Aresult
11
00
940 17081 my ($bend, $mod) = @$channel{qw(bend mod)};
941 17081 my $glide = $self->{glide};
942 17081 my $scale = TWO_PI / $rate;
943 17081 my @w = (0.0) x $nb;
944 17081 for my $j ($first .. $nb - 1) {
945 151258 my $t = $t0 + ($j + 0.5) * $dt;
946 151258 my $semis = $bend->[$j];
947 151258 ▶ $semis += $self->_glide($t) if $glide;
Branch: 1 branch
BranchTrueFalseTotal
if $glide 164 151094 151258
948 151258 ▶ $semis += 0.5 * ($m_pitch->[$j] + $m_pitch->[$j + 1]) if $m_pitch;
Branch: 1 branch
BranchTrueFalseTotal
if $m_pitch 982 150276 151258
949 151258 ▶ $semis += 0.5 * ($gml->[0][$j] + $gml->[0][$j + 1]) if $gml;
Branch: 1 branch
BranchTrueFalseTotal
if $gml 640 150618 151258
950  
951 151258 ▶ my $depth = $vibrato + $mod->[$j] + $pressure + ($gml ? $gml->[3][$j] : 0);
Branch: 1 branch
BranchTrueFalseTotal
$gml ? : 640 150618 151258
952 151258 ▶ if ($depth > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth > 0) 44186 107072 151258
953 44186 my $ramp = ($t - $vib_delay) / 0.3;
954 44186 ▶ $ramp = $ramp < 0 ? 0 : $ramp > 1 ? 1 : $ramp;
Branch: 2 branches
BranchTrueFalseTotal
$ramp < 0 ? : 15327 28859 44186
$ramp > 1 ? : 18282 10577 28859
955 44186 $semis += $depth / 100 * $ramp * sin(TWO_PI * $vib_rate * $t);
956 }
957  
958 151258 my $f = $f0 * 2**($semis / 12);
959 151258 ▶ $f *= 1 + $sweep * exp(-$t / $sweep_time) if $sweep;
Branch: 1 branch
BranchTrueFalseTotal
if $sweep 13007 138251 151258
960 151258 $w[$j] = $scale * $f;
961 }
962  
963 17081 $self->{env} = \@env;
964 17081 $self->{w} = \@w;
965 17081 $self->{boost} = 1;
966 17081 ▶ $self->_filter_edges(@edges, \@w, $m_cutoff, $m_q, $gm) if $self->{fmode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fmode"} 361 16720 17081
967  
968 # How far modulation can lift the voice above its envelope.
969 17081 ▶ $self->{boost} *= _most_of(1, map { 1 + $_ } @$m_level) if $m_level;
Branch: 1 branch
BranchTrueFalseTotal
if $m_level 62 17019 17081
970 17081 ▶ $self->{boost} *= _most_of(1, @{ $gml->[2] }) if $gml;
Branch: 1 branch
BranchTrueFalseTotal
if $gml 37 17044 17081
971  
972 # The channel's gains (left, right, reverb send) at the edges; with a pan
973 # of the voice's own, or one modulated, the channel's level at the
974 # channel's pan and the voice's.
975 17081 @$self{qw(gl gr gs)} = @$channel{qw(gl gr gs)};
976 17081 ▶ if ($self->{pan} || $m_pan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pan"} or $m_pan) 62 17019 17081
Condition100%3/3
$self->{"pan"} or $m_pan
l!l&&r!l&&!r
16117019
MC/DC100%2/2
$self->{"pan"} or $m_pan
$self->{"pan"} $m_pan
Truth table100%3/3
A or B
A= $self->{"pan"}B= $m_pan
ABresult
1X1
011
000
977 62 my ($lv, $pn) = @$channel{qw(lv pn)};
978 62 ▶ my $pan = $self->{pan} // 0;
Condition100%2/2
$self->{"pan"} // 0
l!l
161
MC/DC100%1/1
$self->{"pan"} // 0
$self->{"pan"}
Truth table100%2/2
A
A= $self->{"pan"}
Aresult
11
00
979 62 my (@gl, @gr);
980 62 for my $j (0 .. $nb) {
981 574 ▶ my ($l, $r) = pan_law($pn->[$j] + $pan + ($m_pan ? $m_pan->[$j] : 0));
Branch: 1 branch
BranchTrueFalseTotal
$m_pan ? : 557 17 574
982 574 push @gl, $lv->[$j] * $l;
983 574 push @gr, $lv->[$j] * $r;
984 }
985 62 @$self{qw(gl gr)} = (\@gl, \@gr);
986 }
987 17081 ▶ $self->{gs} = [ map { $_ * $self->{send} } @{ $self->{gs} } ] if defined $self->{send};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"send"} 1 17080 17081
988  
989 17081 return $self;
990 }
991  
992 # Each band of noise's level at the edges: its attack, its decay to what it
993 # sustains, its grains.
994 13801 ▶sub _noise_edges ($self, $first, $nb, $t0, $dt) {
Subroutine _noise_edges: called CC=7 SCAR=19.5
995 13801 for my $band (@{ $self->{bands} }) {
996 14860 my ($level, $attack, $decay, $sustain) = @$band{qw(level attack decay sustain)};
997 14860 my @nenv = (0.0) x ($nb + 1);
998 14860 for my $j ($first .. $nb) {
999 148201 my $t = $t0 + $j * $dt;
1000 148201 ▶ my $n = $t <= 0 ? 0
Branch: 2 branches
BranchTrueFalseTotal
$t <= 0 ? : 526 147675 148201
$t < $attack ? : 2062 145613 147675
1001 : $t < $attack ? $t / $attack
1002 : $sustain + (1 - $sustain) * exp(-$decay * ($t - $attack));
1003 148201 ▶ $n *= $self->_grains($band, $t) if $band->{grains} && $t > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $band->{"grains"} and $t > 0 1591 146610 148201
Condition100%3/3
$band->{"grains"} and $t > 0
!ll&&!rl&&r
14660371591
MC/DC100%2/2
$band->{"grains"} and $t > 0
$band->{"grains"} $t > 0
Truth table100%3/3
A and B
A= $band->{"grains"}B= $t > 0
ABresult
0X0
100
111
1004 148201 $nenv[$j] = $n * $level;
1005 }
1006 14860 $band->{env} = \@nenv;
1007 }
1008 13801 return;
1009 }
1010  
1011 # Each FM operator's level at the edges: its envelope; a modulator's (its
1012 # index) moved by modulation's fm, in octaves.
1013 268 ▶sub _fm_levels ($self, $first, $nb, $t0, $dt, $m_fm) {
Subroutine _fm_levels: called CC=5 SCAR=16.1
1014 268 my $p = $self->{patch};
1015 268 my $fm = $self->{fm};
1016  
1017 268 my @lv;
1018 268 for my $o (0 .. 3) {
1019 1072 my $q = $o + 1;
1020 1072 my @adsr = @$p{ "op${q}_attack", "op${q}_decay", "op${q}_sustain", "op${q}_release" };
1021 1072 my $base = $fm->{base}[$o];
1022 1072 my $carrier = $fm->{carrier}[$o];
1023 1072 my @l = (0.0) x ($nb + 1);
1024 1072 for my $j ($first .. $nb) {
1025 14312 ▶ $l[$j] = $base * $self->_adsr($t0 + $j * $dt, @adsr) * ($m_fm && !$carrier ? 2**$m_fm->[$j] : 1);
Branch: 1 branch
BranchTrueFalseTotal
$m_fm && !$carrier ? : 1676 12636 14312
Condition100%3/3
$m_fm and not $carrier
!ll&&!rl&&r
1136812681676
MC/DC100%2/2
$m_fm and not $carrier
$m_fm not $carrier
Truth table100%3/3
A and not(B)
A= $m_fmB= not $carrier
ABresult
0X0
100
111
1026 }
1027 1072 push @lv, \@l;
1028 }
1029 268 $fm->{lv} = \@lv;
1030 268 return;
1031 }
1032  
1033 # Levels at the edges times 1 + modulation, never below 0.
1034 62 ▶sub _scale_edges ($levels, $modulation, $first, $nb) {
Subroutine _scale_edges: called CC=3 SCAR=11.1
1035 62 for my $j ($first .. $nb) {
1036 574 my $g = 1 + $modulation->[$j];
1037 574 ▶ $levels->[$j] *= $g > 0 ? $g : 0;
Branch: 1 branch
BranchTrueFalseTotal
$g > 0 ? : 574 0 574
1038 }
1039 62 return;
1040 }
1041  
1042 # GM2's controller destinations (pitch, cutoff, amplitude, the LFO's depth
1043 # on each): the channel's at the edges, key pressure's (this voice's own)
1044 # on top; the amplitude applied to the envelope here. The LFO is the
1045 # vibrato's: at the edges, its ramp in after its delay and its sine, for the
1046 # cutoff and the amplitude.
1047 37 ▶sub _gm_destinations ($self, $env, $first, $nb, $t0, $dt, $channel) {
Subroutine _gm_destinations: called CC=12 SCAR=24.9
1048 37 my ($dest, $kp) = @$channel{qw(dest kp)};
1049 37 my $pressure = $self->{pressure};
1050 37 my @levels;
1051 37 for my $i (0 .. 5) {
1052 222 ▶ my $k = $kp ? $kp->[$i] * $pressure : 0;
Branch: 1 branch
BranchTrueFalseTotal
$kp ? : 12 210 222
1053 222 ▶ my $from = $dest ? $dest->[$i] : [ ($i == 2 ? 1.0 : 0.0) x ($nb + 1) ];
Branch: 2 branches
BranchTrueFalseTotal
$dest ? : 210 12 222
$i == 2 ? : 2 10 12
1054 222 ▶ $levels[$i] = $i == 2 ? [ map { $_ * (1 + $k) } @$from ] : [ map { $_ + $k } @$from ];
Branch: 1 branch
BranchTrueFalseTotal
$i == 2 ? : 37 185 222
1055 }
1056  
1057 37 ▶ my $vib_rate = $self->{patch}{vib_rate} * ($channel->{vrate} // 1);
Condition50%1/2
$channel->{"vrate"} // 1
l!l
370
MC/DC0%0/1
$channel->{"vrate"} // 1
$channel->{"vrate"}
Truth table50%1/2
A
A= $channel->{"vrate"}
Aresult
11
00
1058 37 ▶ my $vib_delay = $self->{patch}{vib_delay} * ($channel->{vdelay} // 1);
Condition50%1/2
$channel->{"vdelay"} // 1
l!l
370
MC/DC0%0/1
$channel->{"vdelay"} // 1
$channel->{"vdelay"}
Truth table50%1/2
A
A= $channel->{"vdelay"}
Aresult
11
00
1059 37 my (@ramp, @sine);
1060 37 for my $j (0 .. $nb) {
1061 677 my $t = $t0 + $j * $dt;
1062 677 my $r = ($t - $vib_delay) / 0.3;
1063 677 ▶ push @ramp, $r < 0 ? 0 : $r > 1 ? 1 : $r;
Branch: 2 branches
BranchTrueFalseTotal
$r < 0 ? : 389 288 677
$r > 1 ? : 169 119 288
1064 677 push @sine, sin(TWO_PI * $vib_rate * $t);
1065 }
1066  
1067 37 for my $j ($first .. $nb) {
1068 677 $env->[$j] *= $levels[2][$j] * (1 - $levels[5][$j] * $ramp[$j] * (1 - $sine[$j]) / 2);
1069 }
1070 37 return { levels => \@levels, ramp => \@ramp, sine => \@sine };
1071 }
1072  
1073 # The filter at the edges: the fundamental (as the block's pitch has it)
1074 # over the cutoff, and the Q, held to what stays finite. A resonance lifts
1075 # a partial by up to its Q: the boost.
1076 361 ▶sub _filter_edges ($self, $first, $nb, $t0, $dt, $w, $m_cutoff, $m_q, $gm) {
Subroutine _filter_edges: called CC=13 SCAR=25.7
1077 361 my $rate = $self->{rate};
1078 361 my $scale = TWO_PI / $rate;
1079 361 my ($cutoff, $q) = @{ $self->{mod} }{qw(cutoff q)};
1080  
1081 361 my (@fx, @fq);
1082 361 my $peak = 1;
1083 361 for my $j (0 .. $nb) {
1084 3045 ▶ my $b = $j < $first ? $first : $j < $nb ? $j : $nb - 1; # the block whose pitch counts
Branch: 2 branches
BranchTrueFalseTotal
$j < $first ? : 26 3019 3045
$j < $nb ? : 2658 361 3019
1085 3045 ▶ my $f = ($w->[$b] // 0) / $scale;
Condition50%1/2
$w->[$b] // 0
l!l
30450
MC/DC0%0/1
$w->[$b] // 0
$w->[$b]
Truth table50%1/2
A
A= $w->[$b]
Aresult
11
00
1086  
1087 3045 ▶ my $octaves = $m_cutoff ? $m_cutoff->[$j] : 0;
Branch: 1 branch
BranchTrueFalseTotal
$m_cutoff ? : 2977 68 3045
1088 3045 ▶ $octaves += $gm->{levels}[1][$j] + $gm->{levels}[4][$j] / 1200 * $gm->{ramp}[$j] * $gm->{sine}[$j] if $gm;
Branch: 1 branch
BranchTrueFalseTotal
if $gm 68 2977 3045
1089 3045 my $fc = $cutoff * 2**$octaves;
1090 3045 ▶ $fc = 10 if $fc < 10;
Branch: 1 branch
BranchTrueFalseTotal
if $fc < 10 0 3045 3045
1091 3045 ▶ $fc = 0.5 * $rate if $fc > 0.5 * $rate;
Branch: 1 branch
BranchTrueFalseTotal
if $fc > "0.5" * $rate 30 3015 3045
1092  
1093 3045 ▶ my $qq = $q * 2**($m_q ? $m_q->[$j] : 0);
Branch: 1 branch
BranchTrueFalseTotal
$m_q ? : 2977 68 3045
1094 3045 ▶ $qq = 0.1 if $qq < 0.1;
Branch: 1 branch
BranchTrueFalseTotal
if $qq < "0.1" 0 3045 3045
1095 3045 ▶ $qq = 50 if $qq > 50;
Branch: 1 branch
BranchTrueFalseTotal
if $qq > 50 0 3045 3045
1096  
1097 3045 push @fx, $f / $fc;
1098 3045 push @fq, $qq;
1099 3045 ▶ $peak = $qq if $qq > $peak;
Branch: 1 branch
BranchTrueFalseTotal
if $qq > $peak 298 2747 3045
1100 }
1101 361 @$self{qw(fx fq)} = (\@fx, \@fq);
1102 361 $self->{boost} = $peak;
1103 361 return;
1104 }
1105  
1106 99 ▶sub _most_of (@values) {
Subroutine _most_of: called CC=3 SCAR=11.0
1107 99 my $most = shift @values;
1108 99 ▶ for (@values) { $most = $_ if $_ > $most }
Branch: 1 branch
BranchTrueFalseTotal
if $_ > $most 1 1250 1251
1109 99 return $most;
1110 }
1111  
1112 # Left and right gains of a pan (-1 left .. 1 right; beyond it, held there):
1113 # constant power, both 1 in the middle.
1114 16510 ▶sub pan_law ($pan) {
Subroutine pan_law: called CC=3 SCAR=11.4
Pod documented
1115 16510 ▶ $pan = -1 if $pan < -1;
Branch: 1 branch
BranchTrueFalseTotal
if $pan < -1 0 16510 16510
1116 16510 ▶ $pan = 1 if $pan > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $pan > 1 0 16510 16510
1117 16510 my $angle = ($pan + 1) * 0.7853981633974483;
1118 16510 return (cos($angle) * 1.4142135623730951, sin($angle) * 1.4142135623730951);
1119 }
1120  
1121 # Whether the voice has fallen under $level (of its full level) for good, at
1122 # time t: past its attack and hold, its envelope as the last quantum ended
1123 # times the loudest it can still get (see bound).
1124 352 ▶sub under ($self, $t, $level) {
Subroutine under: called CC=3 SCAR=11.7
Pod documented
1125 352 my ($attack, $hold) = $self->_envelope;
1126 352 ▶ return $t > $attack + $hold && ($self->{raw_env} // 1) * $self->bound($t) < $level;
Condition60%3/5
$t > $attack + $hold && ($self->{'raw_env'} // 1) * $self->bound($t) < $level
!ll&&!rl&&r
03484
$self->{"raw_env"} // 1
l!l
3520
MC/DC0%0/2
$t > $attack + $hold && ($self->{'raw_env'} // 1) * $self->bound($t) < $level
$t > $attack + $hold $self->{"raw_env"}
Truth table33%1/3
A && B
A= $t > $attack + $holdB= $self->{"raw_env"}
ABresult
0X0
100
111
1127 }
1128  
1129 # The loudest the voice can still get from time t on, for deciding it is done.
1130 17673 ▶sub bound ($self, $t) {
Subroutine bound: called CC=9 SCAR=22.0
Pod documented
1131 17673 my ($amp, $decay) = @$self{qw(amp decay)};
1132 17673 my $peak = 0;
1133 17673 for my $k (0 .. $#$amp) {
1134 286371 my $a = $amp->[$k] * exp(-$decay->[$k] * $t);
1135 286371 ▶ $peak = $a if $a > $peak;
Branch: 1 branch
BranchTrueFalseTotal
if $a > $peak 18016 268355 286371
1136 }
1137  
1138 17673 my $noise = 0;
1139 17673 for my $band (@{ $self->{bands} }) {
1140 15451 ▶ my $now = $t < $band->{attack} ? 1 : $band->{sustain} + (1 - $band->{sustain}) * exp(-$band->{decay} * ($t - $band->{attack}));
Branch: 1 branch
BranchTrueFalseTotal
$t < $band->{'attack'} ? : 224 15227 15451
1141 15451 $noise += $band->{level} * $now;
1142 }
1143  
1144 # Noise on the partials: its peaks some three times its RMS.
1145 17673 my $rough = 1 + 3 * $self->{patch}{rough};
1146  
1147 # The string: its pluck's peaks some four times its RMS, falling 60 dB in
1148 # t60. FM: its carriers at their full levels (their envelopes are not
1149 # followed here).
1150 17673 ▶ my $string = $self->{string} ? 4 * $self->{string}{level} * 10**(-3 * $t / $self->{string}{t60}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'string'} ? : 678 16995 17673
1151 17673 ▶ if (my $fm = $self->{fm}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fm = $self->{"fm"}) 268 17405 17673
1152 268 $string += $fm->{level} * $fm->{carrier}[$_] * $fm->{base}[$_] for 0 .. 3;
1153 }
1154  
1155 17673 ▶ return (($peak * 1.5 * ($self->{boost} // 1) + $string) * $rough + $noise) * $self->{level};
Condition100%2/2
$self->{"boost"} // 1
l!l
17433240
MC/DC100%1/1
$self->{"boost"} // 1
$self->{"boost"}
Truth table100%2/2
A
A= $self->{"boost"}
Aresult
11
00
1156 }
1157  
1158 # Whether the voice can only fall from time $t on: let go, killed, a drum,
1159 # or a patch that does not sustain past its attack.
1160 17318 ▶sub _ending ($self, $t) {
Subroutine _ending: called CC=5 SCAR=16.1
1161 17318 my $p = $self->{patch};
1162 return defined $self->{released} || defined $self->{killed} || $p->{drum}
1163 17318 ▶ || ($p->{sustain} == 0 && $t > $p->{attack} + $p->{hold});
Condition91%11/12
defined $self->{'released'} || defined $self->{'killed'} || $p->{'drum'} || $p->{'sustain'} == 0 && $t > $p->{'attack'} + $p->{'hold'}
l!l&&r!l&&!r
65032810787
defined $self->{"released"} or defined $self->{"killed"} or $p->{"drum"}
l!l&&r!l&&!r
4022248110815
defined $self->{"released"} or defined $self->{"killed"}
l!l&&r!l&&!r
4016613296
$p->{"sustain"} == 0 and $t > $p->{"attack"} + $p->{"hold"}
!ll&&!rl&&r
10787028
MC/DC80%4/5
defined $self->{'released'} || defined $self->{'killed'} || $p->{'drum'} || $p->{'sustain'} == 0 && $t > $p->{'attack'} + $p->{'hold'}
defined $self->{"released"} defined $self->{"killed"} $p->{"drum"} $p->{"sustain"} == 0 $t > $p->{"attack"} + $p->{"hold"}
Truth table83%5/6
A or B or C || D and E
A= defined $self->{"released"}B= defined $self->{"killed"}C= $p->{"drum"}D= $p->{"sustain"} == 0E= $t > $p->{"attack"} + $p->{"hold"}
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
1164 }
1165  
1166 # Whether the voice is silent for good at time $t, the end of the quantum its
1167 # controls were last computed for.
1168 17078 ▶sub done ($self, $t) {
Subroutine done: called CC=4 SCAR=13.9
Pod documented
1169 17078 ▶ return 0 if !$self->_ending($t) && $self->bound($t) >= SILENT;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->_ending($t) and $self->bound($t) >= "0.0001" 10660 6418 17078
Condition100%3/3
not $self->_ending($t) and $self->bound($t) >= "0.0001"
!ll&&!rl&&r
6415310660
MC/DC100%2/2
not $self->_ending($t) and $self->bound($t) >= "0.0001"
not $self->_ending($t) $self->bound($t) >= "0.0001"
Truth table100%3/3
not(A) and B
A= not $self->_ending($t)B= $self->bound($t) >= "0.0001"
ABresult
0X0
100
111
1170 6418 ▶ return ($self->{raw_env} // 1) * $self->bound($t) < SILENT;
Condition50%1/2
$self->{"raw_env"} // 1
l!l
64180
MC/DC0%0/1
$self->{"raw_env"} // 1
$self->{"raw_env"}
Truth table50%1/2
A
A= $self->{"raw_env"}
Aresult
11
00
1171 }
1172  
1173 # When the voice will be silent for good (s since it started; 'inf' if it
1174 # never will), as things stand: let go or held, killed or not. done's test,
1175 # searched from the envelope's peak on, where the envelope and the bound
1176 # only fall: by doubling steps, then halving the last, to some 5 ms. Kept
1177 # until the voice is let go or killed.
1178 58 ▶sub ends ($self) {
Subroutine ends: called CC=8 SCAR=20.8
Pod documented
1179 58 ▶ my $state = join ',', map { $_ // '' } @$self{qw(released killed rfactor)};
Condition100%2/2
$_ // ""
l!l
72102
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
1180 58 ▶ return $self->{ends}[1] if $self->{ends} && $self->{ends}[0] eq $state;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ends"} and $self->{"ends"}[0] eq $state 44 14 58
Condition100%3/3
$self->{"ends"} and $self->{"ends"}[0] eq $state
!ll&&!rl&&r
8644
MC/DC100%2/2
$self->{"ends"} and $self->{"ends"}[0] eq $state
$self->{"ends"} $self->{"ends"}[0] eq $state
Truth table100%3/3
A and B
A= $self->{"ends"}B= $self->{"ends"}[0] eq $state
ABresult
0X0
100
111
1181  
1182 240 ▶ my $silent = sub ($t) {
Subroutine __ANON__: called CC=3 SCAR=11.0
1183 240 my $bound = $self->bound($t);
1184 240 ▶ return ($self->_ending($t) || $bound < SILENT) && $self->envelope_at($t) * $bound < SILENT;
Condition100%6/6
($self->_ending($t) || $bound < 0.0001) && $self->envelope_at($t) * $bound < 0.0001
!ll&&!rl&&r
867876
$self->_ending($t) or $bound < "0.0001"
l!l&&r!l&&!r
1163886
MC/DC100%3/3
($self->_ending($t) || $bound < 0.0001) && $self->envelope_at($t) * $bound < 0.0001
$self->_ending($t) $bound < "0.0001" $self->envelope_at($t) * $bound < 0.0001
Truth table80%4/5
A or B && C
A= $self->_ending($t)B= $bound < "0.0001"C= $self->envelope_at($t) * $bound < 0.0001
ABCresult
00X0
1X00
0100
1X11
0111
1185 14 };
1186  
1187 14 my ($attack, $hold) = $self->_envelope;
1188 14 my ($low, $step) = ($attack + $hold, 0.05);
1189 14 my $end;
1190 14 ▶ if ($silent->($low)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$silent($low)) { } 0 14 14
1191 0 $end = $low;
1192 }
1193 else {
1194 14 ▶ while (!$silent->($low + $step)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$silent($low + $step)) 100 12 112
1195 100 $low += $step;
1196 100 $step *= 2;
1197 100 ▶ last if $low > 3600;
Branch: 1 branch
BranchTrueFalseTotal
if $low > 3600 2 98 100
1198 }
1199 14 ▶ $end = $low > 3600 ? 'inf' : _bisect($silent, $low, $low + $step);
Branch: 1 branch
BranchTrueFalseTotal
$low > 3600 ? : 2 12 14
1200 }
1201  
1202 14 $self->{ends} = [ $state, $end ];
1203 14 return $end;
1204 }
1205  
1206 # Where $test turns true between $low (false) and $high (true), to 5 ms.
1207 12 ▶sub _bisect ($test, $low, $high) {
Subroutine _bisect: called CC=3 SCAR=11.0
1208 12 ▶ while ($high - $low > 0.005) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low > "0.005") 114 12 126
1209 114 my $mid = ($low + $high) / 2;
1210 114 ▶ if ($test->($mid)) { $high = $mid } else { $low = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if (&$test($mid)) { } 64 50 114
1211 }
1212 12 return $high;
1213 }
1214  
1215 17 1;
1216  
1217 __END__
« lib/Peta/MIDI/Device/Out.pm ↑ Summary lib/Peta/Synth/Engine/PDL.pm »