lib/Peta/Synth/Synth.pm

filter: Statementstmt 94.1% 809 / 859 Branchbran 81.4% 355 / 436 Conditioncond 69.5% 294 / 423 MC/DCmcdc 46.5% 108 / 232 Subroutinesub 95.7% 90 / 94 Podpod 100% 39 / 39 totaltotal 81.3% 1695 / 2083 CC 458 SCAR 28.4
CC
458
Cov
85%
_apply
40.6
_controller
35.2
_data_entry
32.2
CRAP 17.1
« blib/lib/Peta/MIDI/Device/In.pm ↑ Summary lib/Peta/Synth/Engine/Perl.pm »
1 package Peta::Synth::Synth;
2 # ABSTRACT: the synthesizer: channels, voices, engine, reverb
3  
4 # The synthesizer as it runs: sixteen MIDI channels, the voices that sound,
5 # an engine that renders them and the reverb, quantum after quantum. Events
6 # come in two ways: played now (a key pressed in the web page, a message
7 # from a MIDI device) or scheduled at a sample (a file played). Everything
8 # is applied at the edge of a control block of 64 samples (1.3 ms at 48 kHz).
9 #
10 # A quantum is rendered in three steps:
11 #
12 # 1. block by block: the events of the block are applied, and every voice
13 # computes its controls (pitch, envelope, gains) for the block
14 # 2. the engine renders the voices into left, right and a reverb send
15 # 3. the reverb, the master gain, the equalizer and the limiter
16 #
17 # The patches are the synth's own copies of Peta::Synth::Patch's, so that
18 # one synth can be played with while another renders.
19  
20 16 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 16 ▶use Peta::Synth::Patch;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use Peta::Synth::Voice;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use Peta::Synth::Wavetable;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 16 ▶use Peta::Synth::Reverb;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 16 ▶use Peta::Synth::EQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 ▶use Peta::Synth::Arpeggiator;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 16 our $VERSION = '0.2610180';
29  
30 16 ▶use constant BLOCK => 64;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 16 my $DRUM_CHANNEL = 9;
33  
34 # Drum keys that cut each other off (GM's exclusive classes): the hi-hat's
35 # closed, pedal and open; the whistles, the guiros, the cuicas, the triangles.
36 16 my %EXCLUSIVE = (42 => 1, 44 => 1, 46 => 1, 71 => 2, 72 => 2, 73 => 3, 74 => 3, 78 => 4, 79 => 4, 80 => 5, 81 => 5);
37  
38 # GS and XG's part numbers (as their system exclusive messages count them)
39 # to channels: GS counts part 10 first.
40 16 my @GS_PART = (9, 0 .. 8, 10 .. 15);
41  
42 # Fidelity: what may be left out that is hardly heard, for playing on a
43 # machine that cannot keep up. Voices cost (above all on the GPU, where a
44 # partial costs next to nothing): a voice that has fallen under quiet of its
45 # full level (dB) fades out in 5 ms and ends, not at -80 dB; at most voices
46 # sound at once. High leaves nothing out: what renders sample for sample as
47 # it always has.
48 16 my %FIDELITY = (
49 high => {},
50 mid => { quiet => -60 },
51 low => { quiet => -45, voices => 48 },
52 );
53  
54 514 ▶sub new ($class, %arg) {
Subroutine new: called CC=16 SCAR=28.9
Pod documented
55 my $self = bless {
56 rate => $arg{rate} // 48000,
57 partials => $arg{partials} // 48, # most partials per voice
58 voices => $arg{voices} // 256, # most voices at once
59 fidelity => $arg{fidelity} // 'high', # what may be left out (%FIDELITY)
60 gain => $arg{gain} // 0.3, # master
61 limiter => $arg{limiter} // 1,
62 filter => $arg{filter}, # a sub: which notes to play (for the workers of an offline render)
63 514 ▶ now => 0, # the sample the next quantum starts at
Condition91%11/12
$arg{"rate"} // 48000
l!l
5140
$arg{"partials"} // 48
l!l
50311
$arg{"voices"} // 256
l!l
1513
$arg{"fidelity"} // "high"
l!l
11503
$arg{"gain"} // "0.3"
l!l
360154
$arg{"limiter"} // 1
l!l
43183
MC/DC0%0/1
$arg{"rate"} // 48000
$arg{"rate"}
MC/DC100%1/1
$arg{"partials"} // 48
$arg{"partials"}
MC/DC100%1/1
$arg{"voices"} // 256
$arg{"voices"}
MC/DC100%1/1
$arg{"fidelity"} // "high"
$arg{"fidelity"}
MC/DC100%1/1
$arg{"gain"} // "0.3"
$arg{"gain"}
MC/DC100%1/1
$arg{"limiter"} // 1
$arg{"limiter"}
Truth table50%1/2
A
A= $arg{"rate"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"partials"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"voices"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"fidelity"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"gain"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"limiter"}
Aresult
11
00
64 queue => [], # [sample, event], in order
65 active => [], # the voices that sound
66 count => 0, # notes started, the seed of the next voice
67 programs => {}, # the synth's own patches, as asked for
68 drums => {},
69 limit => 1, # the limiter's gain
70 stats => { notes => 0, peak_voices => 0, voice_blocks => 0, partial_blocks => 0 },
71 }, $class;
72 514 ▶ die "no fidelity '$self->{fidelity}'\n" if !$FIDELITY{ $self->{fidelity} };
Branch: 1 branch
BranchTrueFalseTotal
unless $FIDELITY{$self->{"fidelity"}} 1 513 514
73 513 ▶ die "no fidelity '$self->{fidelity}'\n" if !$FIDELITY{ $self->{fidelity} };
Branch: 1 branch
BranchTrueFalseTotal
unless $FIDELITY{$self->{"fidelity"}} 0 513 513
74  
75 513 ▶ $self->{engine} = $arg{engine} // do {
Condition33%1/3
$arg{"engine"} // do { require Peta::Synth::Engine; Peta::Synth::Engine::create("auto", "rate", $self->{"rate"}, "partials", $self->{"partials"}) }
l!l&&r!l&&!r
51300
MC/DC0%0/2
$arg{"engine"} // do { require Peta::Synth::Engine; Peta::Synth::Engine::create("auto", "rate", $self->{"rate"}, "partials", $self->{"partials"}) }
$arg{"engine"} do { require Peta::Synth::Engine; Peta::Synth::Engine::create("auto", "rate", $self->{"rate"}, "partials", $self->{"partials"}) }
Truth table33%1/3
A // B
A= $arg{"engine"}B= do { require Peta::Synth::Engine; Peta::Synth::Engine::create("auto", "rate", $self->{"rate"}, "partials", $self->{"partials"}) }
ABresult
1X1
011
000
76 0 require Peta::Synth::Engine;
77 0 Peta::Synth::Engine::create('auto', rate => $self->{rate}, partials => $self->{partials});
78 };
79  
80 # Harmonic partials read from wave sequences (Peta::Synth::Wavetable): a
81 # store, kept with the synthesizer, or given (shared, made ahead); 0 for
82 # none, every partial turned. By default as the engine fares better (the
83 # GPU turning partials, the others reading tables).
84 513 my $engine = $self->{engine};
85 513 ▶ my $waves = $arg{waves} // ($engine->can('reads_waves') ? $engine->reads_waves : 1);
Branch: 1 branch
BranchTrueFalseTotal
$engine->can('reads_waves') ? : 0 148 148
Condition66%2/3
$arg{"waves"} // ($engine->can("reads_waves") ? $engine->reads_waves : 1)
l!l&&r!l&&!r
3651480
MC/DC0%0/2
$arg{"waves"} // ($engine->can("reads_waves") ? $engine->reads_waves : 1)
$arg{"waves"} ($engine->can("reads_waves") ? $engine->reads_waves : 1)
Truth table66%2/3
A // B
A= $arg{"waves"}B= ($engine->can("reads_waves") ? $engine->reads_waves : 1)
ABresult
1X1
011
000
86 513 ▶ $self->{waves} = ref $waves ? $waves : $waves ? Peta::Synth::Wavetable->new : undef;
Branch: 2 branches
BranchTrueFalseTotal
ref $waves ? : 1 512 513
$waves ? : 507 5 512
87  
88 513 ▶ $self->{reverb} = Peta::Synth::Reverb->new(rate => $self->{rate}, %{ $arg{reverb} // {} });
Condition50%1/2
$arg{"reverb"} // {}
l!l
0513
MC/DC0%0/1
$arg{"reverb"} // {}
$arg{"reverb"}
Truth table50%1/2
A
A= $arg{"reverb"}
Aresult
11
00
89 513 ▶ $self->{eq} = Peta::Synth::EQ->new(rate => $self->{rate}, bands => $arg{eq} // {});
Condition50%1/2
$arg{"eq"} // {}
l!l
0513
MC/DC0%0/1
$arg{"eq"} // {}
$arg{"eq"}
Truth table50%1/2
A
A= $arg{"eq"}
Aresult
11
00
90  
91 # Each channel's equalizer: gains on its voices' partials. A mixer
92 # setting, not MIDI's: a reset leaves it.
93 513 $self->{channel_eq} = [ map { Peta::Synth::EQ->new(rate => $self->{rate}) } 0 .. 15 ];
94  
95 # Each channel's arpeggiator, made when it is first asked for; a reset
96 # forgets its keys and what a file set, not its own settings.
97 513 $self->{arps} = [];
98 513 $self->{arp_own} = [];
99  
100 513 $self->reset;
101 513 return $self;
102 }
103  
104 0 ▶sub rate ($self) { return $self->{rate} }
Subroutine rate: not called CC=1 SCAR=6.9
Pod documented
105 509 ▶sub now ($self) { return $self->{now} }
Subroutine now: called CC=1 SCAR=0.0
Pod documented
106 6 ▶sub engine ($self) { return $self->{engine} }
Subroutine engine: called CC=1 SCAR=0.0
Pod documented
107 2 ▶sub waves ($self) { return $self->{waves} }
Subroutine waves: called CC=1 SCAR=0.0
Pod documented
108 48 ▶sub reverb ($self) { return $self->{reverb} }
Subroutine reverb: called CC=1 SCAR=0.0
Pod documented
109 11 ▶sub eq ($self) { return $self->{eq} }
Subroutine eq: called CC=1 SCAR=0.0
Pod documented
110 3 ▶sub stats ($self) { return $self->{stats} }
Subroutine stats: called CC=1 SCAR=0.0
Pod documented
111 3610 ▶sub active ($self) { return scalar @{ $self->{active} } }
Subroutine active: called CC=1 SCAR=0.0
Pod documented
112 40 ▶sub pending ($self) { return scalar @{ $self->{queue} } }
Subroutine pending: called CC=1 SCAR=0.0
Pod documented
113 1189 ▶sub channel ($self, $number) { return $self->{channels}[$number] }
Subroutine channel: called CC=1 SCAR=0.0
Pod documented
114  
115 # A channel's equalizer, and changes to it: set_channel_eq($channel, band =>
116 # { freq, gain, q }, ...), heard at once in the voices that sound.
117 68 ▶sub channel_eq ($self, $n) { return $self->{channel_eq}[$n] }
Subroutine channel_eq: called CC=1 SCAR=0.0
Pod documented
118  
119 11 ▶sub set_channel_eq ($self, $n, %bands) {
Subroutine set_channel_eq: called CC=3 SCAR=11.0
Pod documented
120 11 ▶ my $eq = $self->{channel_eq}[$n] // die "no channel $n\n";
Condition50%1/2
$self->{"channel_eq"}[$n] // die("no channel $n\n")
l!l
110
MC/DC0%0/1
$self->{"channel_eq"}[$n] // die("no channel $n\n")
$self->{"channel_eq"}[$n]
Truth table50%1/2
A
A= $self->{"channel_eq"}[$n]
Aresult
11
00
121 11 $eq->set(%bands);
122 11 $_->equalize($eq) for grep { $_->{channel} == $n } @{ $self->{active} };
123 11 return $self;
124 }
125  
126 # How hard the machine works to keep up, for playing as it renders: the
127 # seconds a render took against the seconds of sound it made, smoothed.
128 256 ▶sub account ($self, $took, $samples) {
Subroutine account: called CC=3 SCAR=11.0
Pod documented
129 256 ▶ $self->{load} = 0.9 * ($self->{load} // 0) + 0.1 * ($took * $self->{rate} / $samples);
Condition100%2/2
$self->{"load"} // 0
l!l
2551
MC/DC100%1/1
$self->{"load"} // 0
$self->{"load"}
Truth table100%2/2
A
A= $self->{"load"}
Aresult
11
00
130 256 ▶ $self->{since} = ($self->{since} // 0) + $samples;
Condition100%2/2
$self->{"since"} // 0
l!l
2551
MC/DC100%1/1
$self->{"since"} // 0
$self->{"since"}
Truth table100%2/2
A
A= $self->{"since"}
Aresult
11
00
131 256 return $self->{load};
132 }
133  
134 283 ▶sub load ($self) { return $self->{load} // 0 }
Subroutine load: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$self->{"load"} // 0
l!l
2794
MC/DC100%1/1
$self->{"load"} // 0
$self->{"load"}
Truth table100%2/2
A
A= $self->{"load"}
Aresult
11
00
135  
136 # Quality that follows the machine: fewer partials for new notes when the
137 # rendering takes most of the time there is, more again (up to $most) when
138 # there is room; at most once a second of sound.
139 256 ▶sub adapt ($self, $most) {
Subroutine adapt: called CC=9 SCAR=30.7
Pod documented
140 256 ▶ return if ($self->{since} // 0) < $self->{rate};
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"since"} // 0) < $self->{"rate"} 31 225 256
Condition50%1/2
$self->{"since"} // 0
l!l
2560
MC/DC0%0/1
$self->{"since"} // 0
$self->{"since"}
Truth table50%1/2
A
A= $self->{"since"}
Aresult
11
00
141  
142 225 my $partials = $self->{partials};
143 225 my $load = $self->load;
144 225 ▶ if ($load > 0.75 && $partials > 4) {
Branch: 2 branches
BranchTrueFalseTotal
if ($load > 0.75 and $partials > 4) { } 0 225 225
elsif ($load < 0.35 and $partials < $most) { } 0 225 225
Condition33%1/3
$load > "0.75" and $partials > 4
!ll&&!rl&&r
22500
MC/DC0%0/2
$load > "0.75" and $partials > 4
$load > "0.75" $partials > 4
Truth table33%1/3
A and B
A= $load > "0.75"B= $partials > 4
ABresult
0X0
100
111
145 0 ▶ $self->{partials} = int($partials * 0.75) || 4;
Condition0%0/2
int $partials * 0.75 || 4
l!l
00
MC/DC0%0/1
int $partials * 0.75 || 4
int $partials * 0.75
Truth table0%0/2
A
A= int $partials * 0.75
Aresult
11
00
146 }
147 ▶ elsif ($load < 0.35 && $partials < $most) {
Condition66%2/3
$load < "0.35" and $partials < $most
!ll&&!rl&&r
193320
MC/DC0%0/2
$load < "0.35" and $partials < $most
$load < "0.35" $partials < $most
Truth table66%2/3
A and B
A= $load < "0.35"B= $partials < $most
ABresult
0X0
100
111
148 0 my $more = int($partials * 1.25) + 1;
149 0 ▶ $self->{partials} = $more > $most ? $most : $more;
Branch: 1 branch
BranchTrueFalseTotal
$more > $most ? : 0 0 0
150 }
151 else {
152 225 return;
153 }
154 0 $self->{since} = 0;
155 0 return $self->{partials};
156 }
157  
158 4 ▶sub set ($self, $name, $value) {
Subroutine set: called CC=4 SCAR=14.1
Pod documented
159 4 ▶ die "cannot set '$name'\n" if $name !~ /\A(?:partials|voices|gain|limiter|fidelity)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A(?:partials|voices|gain|limiter|fidelity)\z/u 0 4 4
160 4 ▶ die "no fidelity '$value'\n" if $name eq 'fidelity' && !$FIDELITY{$value};
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "fidelity" and not $FIDELITY{$value} 2 2 4
Condition66%2/3
$name eq "fidelity" and not $FIDELITY{$value}
!ll&&!rl&&r
022
MC/DC50%1/2
$name eq "fidelity" and not $FIDELITY{$value}
$name eq "fidelity" not $FIDELITY{$value}
Truth table66%2/3
A and not(B)
A= $name eq "fidelity"B= not $FIDELITY{$value}
ABresult
0X0
100
111
161 2 $self->{$name} = $value;
162 2 return $self;
163 }
164  
165 # A channel as MIDI starts it.
166 8464 ▶sub _channel ($number, $mute = 0) {
Subroutine _channel: called CC=3 SCAR=11.0
167 return {
168 number => $number,
169 program => 0,
170 bank => 0, # CC0, as the last bank select said
171 variation => 0, # the GS bank of the program's variation, as the last program change picked it
172 kit => 0, # the drum kit, the same way
173 drum => $number == $DRUM_CHANNEL ? 1 : 0,
174 volume => 100, # CC7
175 expression => 127, # CC11
176 pan => 64, # CC10
177 send => 40, # CC91, the reverb
178 chorus => 0, # CC93
179 mod => 0, # CC1, 0 .. 1
180 breath => 0, # CC2, 0 .. 1
181 foot => 0, # CC4, 0 .. 1
182 pressure => 0, # channel pressure, 0 .. 1
183 bend => 0, # -1 .. 1
184 range => 2, # the bend's, semitones (RPN 0)
185 tune => 0, # semitones (RPN 1 and 2)
186 mod_range => 50, # cents of vibrato at full modulation (RPN 5)
187 84640 sound => { map { $_ => 64 } 70 .. 79 }, # CC70 .. 79, the sound controllers (64: as the patch)
188 sustain => 0, # the pedals: CC64,
189 sostenuto => {}, # CC66 (the keys it holds),
190 soft => 0, # CC67,
191 legato => 0, # CC68
192 portamento => 0, # CC65
193 porta_time => 0, # CC5
194 porta_from => undef, # CC84
195 mono => 0, # mono mode (CC126); legato then too
196 param => undef, # the RPN or NRPN data entry sets: [ 'r' or 'n', msb, lsb ]
197 data => {}, # the values set, by "r msb lsb" or "n msb lsb"
198 drum_keys => {}, # per drum key: pitch, level, pan, reverb, chorus (GS and XG's NRPNs)
199 8464 ▶ cc => { 7 => 100, 10 => 64, 11 => 127, 91 => 40, map { $_ => 64 } 70 .. 79 }, # every controller's last value
Branch: 1 branch
BranchTrueFalseTotal
$number == $DRUM_CHANNEL ? : 529 7935 8464
200 dest => {}, # GM2's controller destinations, by source (see _destination)
201 held => {}, # the voices held by key,
202 pedal => [], # and by the pedals
203 stack => [], # in mono, the keys held, in order
204 last_key => undef,
205 mute => $mute, # the page's, not MIDI's
206 };
207 }
208  
209 # All channels to their defaults and every voice gone; the patches stay.
210 514 ▶sub reset ($self) {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
211 514 $self->{channels} = [ map { _channel($_) } 0 .. 15 ];
212 514 $self->{master} = { volume => 1, balance => 0, fine => 0, coarse => 0 };
213 514 $self->{active} = [];
214 514 $self->{queue} = [];
215 514 delete $self->{state};
216 514 $self->{reverb}->clear;
217 514 $self->{eq}->clear;
218 514 $self->_arps_stop;
219 514 $self->forget_held;
220 514 delete $self->{held_back};
221 514 return $self;
222 }
223  
224 # A channel's arpeggiator (Peta::Synth::Arpeggiator): while it is on, the
225 # channel's keys go to it and it plays them. set_arpeggiator($n, %fields)
226 # changes its settings; turned off, what it plays is let go.
227 1188 ▶sub arpeggiator ($self, $n) {
Subroutine arpeggiator: called CC=5 SCAR=17.8
Pod documented
228 1188 ▶ die "no channel $n\n" if $n < 0 || $n > 15;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 or $n > 15 0 1188 1188
Condition33%1/3
$n < 0 or $n > 15
l!l&&r!l&&!r
001188
MC/DC0%0/2
$n < 0 or $n > 15
$n < 0 $n > 15
Truth table33%1/3
A or B
A= $n < 0B= $n > 15
ABresult
1X1
011
000
229 return $self->{arps}[$n] //= Peta::Synth::Arpeggiator->new(rate => $self->{rate}, channel => $n,
230 1188 ▶ settings => { defined $self->{tempo} ? (tempo => $self->{tempo}) : () });
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'tempo'} ? : 2 36 38
Condition66%2/3
$self->{"arps"}[$n] //= "Peta::Synth::Arpeggiator"->new("rate", $self->{"rate"}, "channel", $n, "settings", {defined $self->{"tempo"} ? ("tempo", $self->{"tempo"}) : ()})
l!l&&r!l&&!r
1150380
MC/DC0%0/2
$self->{"arps"}[$n] //= "Peta::Synth::Arpeggiator"->new("rate", $self->{"rate"}, "channel", $n, "settings", {defined $self->{"tempo"} ? ("tempo", $self->{"tempo"}) : ()})
$self->{"arps"}[$n] "Peta::Synth::Arpeggiator"->new("rate", $self->{"rate"}, "channel", $n, "settings", {defined $self->{"tempo"} ? ("tempo", $self->{"tempo"}) : ()})
Truth table66%2/3
A //= B
A= $self->{"arps"}[$n]B= "Peta::Synth::Arpeggiator"->new("rate", $self->{"rate"}, "channel", $n, "settings", {defined $self->{"tempo"} ? ("tempo", $self->{"tempo"}) : ()})
ABresult
1X1
011
000
231 }
232  
233 # set_arpeggiator: the channel's own settings (the page's, played live, a
234 # bank's), which a reset and a new file come back to; what a file sets
235 # (_arp_set) lasts until then.
236 9 ▶sub set_arpeggiator ($self, $n, %fields) {
Subroutine set_arpeggiator: called CC=2 SCAR=6.9
Pod documented
237 9 ▶ $self->{arp_own}[$n] = { %{ $self->{arp_own}[$n] // {} }, %fields };
Condition100%2/2
$self->{"arp_own"}[$n] // {}
l!l
45
MC/DC100%1/1
$self->{"arp_own"}[$n] // {}
$self->{"arp_own"}[$n]
Truth table100%2/2
A
A= $self->{"arp_own"}[$n]
Aresult
11
00
238 9 return $self->_arp_change($n, %fields);
239 }
240  
241 129 ▶sub _arp_change ($self, $n, %fields) {
Subroutine _arp_change: called CC=3 SCAR=11.0
242 129 my $arp = $self->arpeggiator($n);
243 129 my $was = $arp->on;
244 129 $arp->set(%fields);
245 129 ▶ $self->_enqueue($arp->stop($self->{now})) if $was && !$arp->on;
Branch: 1 branch
BranchTrueFalseTotal
if $was and not $arp->on 4 125 129
Condition100%3/3
$was and not $arp->on
!ll&&!rl&&r
113124
MC/DC100%2/2
$was and not $arp->on
$was not $arp->on
Truth table100%3/3
A and not(B)
A= $wasB= not $arp->on
ABresult
0X0
100
111
246 129 return $self;
247 }
248  
249 # A file's tempo: the arpeggiators', until something else sets theirs.
250 3 ▶sub _tempo ($self, $usec, $at) {
Subroutine _tempo: called CC=5 SCAR=16.6
251 3 ▶ return if !$usec;
Branch: 1 branch
BranchTrueFalseTotal
unless $usec 0 3 3
252 3 my $bpm = 60_000_000 / $usec;
253 3 ▶ $bpm = $bpm < 30 ? 30 : $bpm > 300 ? 300 : $bpm;
Branch: 2 branches
BranchTrueFalseTotal
$bpm < 30 ? : 0 3 3
$bpm > 300 ? : 0 3 3
254 3 $self->{tempo} = $bpm;
255 3 for my $n (grep { defined $self->{arps}[$_] } 0 .. 15) {
256 3 $self->_arp_set($n, tempo => $bpm, $at);
257 }
258 3 return;
259 }
260  
261 # Settings made by hand (on the page, or by MIDI played live) are held
262 # against the other MIDI (a file's): what a channel plays ('program': its
263 # bank select and program change), its mixer's controllers ('cc 7'), its
264 # arpeggiator's settings ('arp mode'). While one is held, the file's events
265 # for it are kept aside, the last of each; let go, they apply, and the
266 # channel is as the file has it by then. A reset keeps what is held, and the
267 # file's value is the reset's.
268 16 my %HELD_CC = map { $_ => 1 } 5, 7, 10, 91, 93, 70 .. 79; # settings; not what is played (wheels, pedals, expression)
269  
270 # An event's setting, if it can be held: (what, its part).
271 2050 ▶sub _setting ($event) {
Subroutine _setting: called CC=7 SCAR=19.5
272 2050 my $type = $event->{type};
273 2050 ▶ return ("arp $event->{name}", '') if $type eq 'arp';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "arp" 29 2021 2050
274 2021 ▶ return ('program', 'program') if $type eq 'program';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "program" 326 1695 2021
275 1695 ▶ return if $type ne 'controller';
Branch: 1 branch
BranchTrueFalseTotal
if $type ne "controller" 1160 535 1695
276 535 my $number = $event->{number};
277 535 ▶ return ('program', "cc $number") if $number == 0 || $number == 32;
Branch: 1 branch
BranchTrueFalseTotal
if $number == 0 or $number == 32 16 519 535
Condition100%3/3
$number == 0 or $number == 32
l!l&&r!l&&!r
106519
MC/DC100%2/2
$number == 0 or $number == 32
$number == 0 $number == 32
Truth table100%3/3
A or B
A= $number == 0B= $number == 32
ABresult
1X1
011
000
278 519 ▶ return ("cc $number", '') if $HELD_CC{$number};
Branch: 1 branch
BranchTrueFalseTotal
if $HELD_CC{$number} 394 125 519
279 125 return;
280 }
281  
282 # An event played by hand: applied at once, what it sets held.
283 28 ▶sub by_hand ($self, $event) {
Subroutine by_hand: called CC=1 SCAR=0.0
Pod documented
284 28 local $self->{by_hand} = 1;
285 28 return $self->play($event);
286 }
287  
288 # What a channel holds, by name ('program', 'cc 7', 'arp mode').
289 1130 ▶sub held ($self, $n) { return [ sort keys %{ $self->{hand}[$n] } ] }
Subroutine held: called CC=1 SCAR=0.0
Pod documented
290  
291 # A setting let go (or every one of the channel's): as the file has it now.
292 6 ▶sub let_go ($self, $n, $what = undef) {
Subroutine let_go: called CC=6 SCAR=18.0
Pod documented
293 6 ▶ for my $name (defined $what ? $what : @{ $self->held($n) }) {
Branch: 1 branch
BranchTrueFalseTotal
defined $what ? : 3 3 6
294 8 ▶ delete $self->{hand}[$n]{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
delete $self->{"hand"}[$n]{$name} or next 8 0 8
295 8 my $aside = delete $self->{aside}[$n]{$name};
296 8 $self->_apply($aside->{$_}, $self->{now}) for sort keys %$aside;
297 }
298 6 return $self;
299 }
300  
301 # Nothing held any more, as it is now: a new file.
302 520 ▶sub forget_held ($self) {
Subroutine forget_held: called CC=1 SCAR=0.0
Pod documented
303 520 $self->{hand} = [ map { {} } 0 .. 15 ];
304 520 $self->{aside} = [ map { {} } 0 .. 15 ];
305 520 return $self;
306 }
307  
308 # A setting as the channel has it now, as the events that would set it.
309 23 ▶sub _as_now ($self, $n, $what) {
Subroutine _as_now: called CC=5 SCAR=16.1
310 23 my $c = $self->{channels}[$n];
311 23 ▶ if ($what eq 'program') {
Branch: 1 branch
BranchTrueFalseTotal
if ($what eq "program") 12 11 23
312 return {
313 'cc 0' => { type => 'controller', channel => $n, number => 0, value => $c->{bank} },
314 'cc 32' => { type => 'controller', channel => $n, number => 32, value => $c->{cc}{32} // 0 },
315 program => { type => 'program', channel => $n, program => $c->{program} },
316 12 ▶ };
Condition100%2/2
$c->{"cc"}{32} // 0
l!l
48
MC/DC100%1/1
$c->{"cc"}{32} // 0
$c->{"cc"}{32}
Truth table100%2/2
A
A= $c->{"cc"}{32}
Aresult
11
00
317 }
318 11 ▶ if ($what =~ /\Acc (\d+)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($what =~ /\Acc (\d+)\z/u) 6 5 11
319 6 ▶ return { '' => { type => 'controller', channel => $n, number => $1, value => $c->{cc}{$1} // 0 } };
Condition50%1/2
$c->{"cc"}{$1} // 0
l!l
60
MC/DC0%0/1
$c->{"cc"}{$1} // 0
$c->{"cc"}{$1}
Truth table50%1/2
A
A= $c->{"cc"}{$1}
Aresult
11
00
320 }
321 5 my ($name) = $what =~ /\Aarp (\w+)\z/;
322 5 return { '' => { type => 'arp', channel => $n, name => $name, value => $self->arpeggiator($n)->params->{$name} } };
323 }
324  
325 # An arpeggiator's setting as MIDI sets it: an event, so that it can be held (see by_hand).
326 20 ▶sub _arp_set ($self, $n, $name, $value, $at) {
Subroutine _arp_set: called CC=1 SCAR=0.0
327 20 $self->_apply({ type => 'arp', channel => $n, name => $name, value => $value }, $at);
328 20 return;
329 }
330  
331 # Every arpeggiator's keys forgotten (what they play is let go with the voices).
332 537 ▶sub _arps_stop ($self) {
Subroutine _arps_stop: called CC=3 SCAR=11.1
333 537 ▶ $_->stop($self->{now}) for grep { defined } @{ $self->{arps} // [] };
Condition50%1/2
$self->{"arps"} // []
l!l
5370
MC/DC0%0/1
$self->{"arps"} // []
$self->{"arps"}
Truth table50%1/2
A
A= $self->{"arps"}
Aresult
11
00
334 537 return;
335 }
336  
337 # What the arpeggiators that are on play in [$from, $to), queued.
338 374 ▶sub _arpeggiate ($self, $from, $to) {
Subroutine _arpeggiate: called CC=3 SCAR=11.0
339 374 ▶ $self->_enqueue($_->events($from, $to)) for grep { defined && $_->on } @{ $self->{arps} };
Condition100%3/3
defined $_ && $_->on
!ll&&!rl&&r
5416551
MC/DC100%2/2
defined $_ && $_->on
defined $_ $_->on
Truth table100%3/3
A && B
A= defined $_B= $_->on
ABresult
0X0
100
111
340 374 return;
341 }
342  
343 # Events ([sample, event]) into the queue at their places, after what is
344 # already there at the same sample.
345 61 ▶sub _enqueue ($self, @entries) {
Subroutine _enqueue: called CC=4 SCAR=17.9
346 61 my $queue = $self->{queue};
347 61 for my $entry (@entries) {
348 8 my ($low, $high) = (0, scalar @$queue);
349 8 ▶ while ($low < $high) {
Branch: 1 branch
BranchTrueFalseTotal
if ($low < $high) 0 8 8
350 0 my $mid = ($low + $high) >> 1;
351 0 ▶ if ($queue->[$mid][0] <= $entry->[0]) { $low = $mid + 1 } else { $high = $mid }
Branch: 1 branch
BranchTrueFalseTotal
if ($queue->[$mid][0] <= $entry->[0]) { } 0 0 0
352 }
353 8 splice @$queue, $low, 0, $entry;
354 }
355 61 return;
356 }
357  
358 # Every channel and the master as MIDI starts them, what sounds let go: a
359 # reset by MIDI (GM, GS, XG), or a file played from a point in it. The
360 # mutes are the page's, not MIDI's.
361 15 ▶sub reset_channels ($self, $at = $self->{now}) {
Subroutine reset_channels: called CC=10 SCAR=23.0
Pod documented
362 15 for my $c (@{ $self->{channels} }) {
363 240 $_->release($at) for $self->_voices($c);
364 240 $c = _channel($c->{number}, $c->{mute});
365 }
366 15 $self->{master} = { volume => 1, balance => 0, fine => 0, coarse => 0 };
367 15 delete $self->{state};
368 15 $self->_arps_stop;
369  
370 # The arpeggiators as their own settings have them, not as a file set them.
371 15 delete $self->{tempo};
372 15 for my $n (grep { defined $self->{arps}[$_] } 0 .. 15) {
373 99 ▶ $self->_arp_change($n, %{ Peta::Synth::Arpeggiator::defaults() }, %{ $self->{arp_own}[$n] // {} });
Condition100%2/2
$self->{"arp_own"}[$n] // {}
l!l
396
MC/DC100%1/1
$self->{"arp_own"}[$n] // {}
$self->{"arp_own"}[$n]
Truth table100%2/2
A
A= $self->{"arp_own"}[$n]
Aresult
11
00
374 }
375  
376 # A reset by hand lets go of what is held. The file's keeps it (the
377 # arpeggiators' are their own settings again, which those held are);
378 # the file's value is the reset's.
379 15 ▶ return $self->forget_held if $self->{by_hand};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"by_hand"} 2 13 15
380 13 for my $n (0 .. 15) {
381 208 my $hand = $self->{hand}[$n];
382 208 for my $what (grep { !/\Aarp / } sort keys %$hand) {
383 2 $self->{aside}[$n]{$what} = $self->_as_now($n, $what);
384 2 local $self->{by_hand} = 1;
385 2 $self->_apply($hand->{$what}{$_}, $at) for sort keys %{ $hand->{$what} };
386 }
387 }
388 13 return $self;
389 }
390  
391 # The synth's patch for a program (or its variation, by GS bank) or a drum
392 # key (in a kit), made from the defaults the first time it is asked for.
393 # Changing its fields changes how the next notes of it sound (and the
394 # voices that sound, where a field is read live: envelope, pitch, noise
395 # level). A key a kit does not have as its own is the standard kit's.
396 # They are kept by number, or 'bank:number' for a variation or a kit's own.
397 652 ▶sub program_patch ($self, $number, $bank = 0) {
Subroutine program_patch: called CC=3 SCAR=11.0
Pod documented
398 652 ▶ return $self->{programs}{ _patch_key($number, $bank) } //= { %{ Peta::Synth::Patch::program($number, $bank) } };
Condition100%2/2
$self->{"programs"}{_patch_key($number, $bank)} //= {%{Peta::Synth::Patch::program($number, $bank);}}
l!l
209443
MC/DC100%1/1
$self->{"programs"}{_patch_key($number, $bank)} //= {%{Peta::Synth::Patch::program($number, $bank);}}
$self->{"programs"}{_patch_key($number, $bank)}
Truth table100%2/2
A
A= $self->{"programs"}{_patch_key($number, $bank)}
Aresult
11
00
399 }
400  
401 151 ▶sub drum_patch ($self, $key, $kit = 0) {
Subroutine drum_patch: called CC=3 SCAR=11.0
Pod documented
402 151 $kit = Peta::Synth::Patch::kit_key($kit, $key);
403 151 ▶ return $self->{drums}{ _patch_key($key, $kit) } //= { %{ Peta::Synth::Patch::drum($key, $kit) } };
Condition100%2/2
$self->{"drums"}{_patch_key($key, $kit)} //= {%{Peta::Synth::Patch::drum($key, $kit);}}
l!l
11140
MC/DC100%1/1
$self->{"drums"}{_patch_key($key, $kit)} //= {%{Peta::Synth::Patch::drum($key, $kit);}}
$self->{"drums"}{_patch_key($key, $kit)}
Truth table100%2/2
A
A= $self->{"drums"}{_patch_key($key, $kit)}
Aresult
11
00
404 }
405  
406 0 ▶sub restore_patch ($self, $kind, $number, $bank = 0) {
Subroutine restore_patch: not called CC=5 SCAR=34.0
Pod documented
407 0 my $drum = $kind eq 'drum';
408 0 ▶ $bank = Peta::Synth::Patch::kit_key($bank, $number) if $drum;
Branch: 1 branch
BranchTrueFalseTotal
if $drum 0 0 0
409 0 ▶ delete $self->{ $drum ? 'drums' : 'programs' }{ _patch_key($number, $bank) };
Branch: 1 branch
BranchTrueFalseTotal
$drum ? : 0 0 0
410 0 ▶ return $drum ? $self->drum_patch($number, $bank) : $self->program_patch($number, $bank);
Branch: 1 branch
BranchTrueFalseTotal
$drum ? : 0 0 0
411 }
412  
413 803 ▶sub _patch_key ($number, $bank) { return $bank ? "$bank:$number" : $number }
Subroutine _patch_key: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$bank ? : 8 795 803
414  
415 # A patch's key: (number, bank).
416 88 ▶sub _patch_id ($key) {
Subroutine _patch_id: called CC=2 SCAR=6.9
417 88 my ($number, $bank) = reverse split /:/, $key;
418 88 ▶ return ($number, $bank // 0);
Condition100%2/2
$bank // 0
l!l
682
MC/DC100%1/1
$bank // 0
$bank
Truth table100%2/2
A
A= $bank
Aresult
11
00
419 }
420  
421 # What has been changed from the defaults, as a bank: { programs => { n or
422 # 'bank:n' => { field => value } }, drums => { key or 'kit:key' => { ... } },
423 # master => { reverb, eq, channels => { n => eq }, arps => { n => settings } } }.
424 13 ▶sub edits ($self) {
Subroutine edits: called CC=14 SCAR=26.4
Pod documented
425 13 require JSON::PP;
426 13 my $json = JSON::PP->new->canonical;
427 13 my %bank;
428  
429 13 for my $kind ([ programs => \&Peta::Synth::Patch::program ], [ drums => \&Peta::Synth::Patch::drum ]) {
430 26 my ($name, $default) = @$kind;
431 26 for my $number (keys %{ $self->{$name} }) {
432 16 my $patch = $self->{$name}{$number};
433 16 my $base = $default->(_patch_id($number));
434 16 for my $field (keys %$patch) {
435 # Lists (formants, partials) compared as what they hold; none is null.
436 2240 ▶ my ($now, $was) = map { ref ? $json->encode($_) : $_ // '' } $patch->{$field}, $base->{$field};
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 73 4407 4480
Condition100%2/2
$_ // ""
l!l
438423
MC/DC100%1/1
$_ // ""
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
437 2240 ▶ $bank{$name}{$number}{$field} = $patch->{$field} if $now ne $was;
Branch: 1 branch
BranchTrueFalseTotal
if $now ne $was 13 2227 2240
438 }
439 }
440 }
441  
442 # The master: the reverb's and the equalizers' settings, where they are
443 # not the defaults.
444 13 my $rate = $self->{rate};
445 13 my %fresh = (reverb => Peta::Synth::Reverb->new(rate => $rate)->params, eq => Peta::Synth::EQ->new(rate => $rate)->params);
446 13 my %now = (reverb => $self->{reverb}->params, eq => $self->{eq}->params);
447 13 for my $part (qw(reverb eq)) {
448 26 ▶ $bank{master}{$part} = $now{$part} if $json->encode($now{$part}) ne $json->encode($fresh{$part});
Branch: 1 branch
BranchTrueFalseTotal
if $json->encode($now{$part}) ne $json->encode($fresh{$part}) 5 21 26
449 }
450 13 for my $n (0 .. 15) {
451 208 my $eq = $self->{channel_eq}[$n];
452 208 ▶ $bank{master}{channels}{$n} = $eq->params if $eq->active;
Branch: 1 branch
BranchTrueFalseTotal
if $eq->active 4 204 208
453 }
454 13 my $plain = Peta::Synth::Arpeggiator::defaults();
455 13 for my $n (0 .. 15) {
456 208 ▶ my $mine = $self->{arp_own}[$n] or next; # not what a file set
Branch: 1 branch
BranchTrueFalseTotal
my $mine = $self->{"arp_own"}[$n] or next 5 203 208
457 5 my $own = { %$plain, %$mine };
458 5 ▶ $bank{master}{arps}{$n} = $own if $json->encode($own) ne $json->encode($plain);
Branch: 1 branch
BranchTrueFalseTotal
if $json->encode($own) ne $json->encode($plain) 5 0 5
459 }
460 13 return \%bank;
461 }
462  
463 # A bank's changes applied to this synth's patches and master. Unknown
464 # fields die.
465 63 ▶sub apply_edits ($self, $bank) {
Subroutine apply_edits: called CC=14 SCAR=26.4
Pod documented
466 63 my %known = map { $_ => 1 } Peta::Synth::Patch::fields();
467 63 for my $kind ([ programs => 'program_patch' ], [ drums => 'drum_patch' ]) {
468 126 my ($name, $method) = @$kind;
469 126 ▶ for my $number (keys %{ $bank->{$name} // {} }) {
Condition100%2/2
$bank->{$name} // {}
l!l
6165
MC/DC100%1/1
$bank->{$name} // {}
$bank->{$name}
Truth table100%2/2
A
A= $bank->{$name}
Aresult
11
00
470 72 my $patch = $self->$method(_patch_id($number));
471 72 my $changes = $bank->{$name}{$number};
472 72 for my $field (keys %$changes) {
473 661 ▶ die "a patch has no field '$field'\n" if !$known{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless $known{$field} 0 661 661
474 661 $patch->{$field} = $changes->{$field};
475 }
476 }
477 }
478  
479 63 ▶ my $master = $bank->{master} // {};
Condition100%2/2
$bank->{"master"} // {}
l!l
459
MC/DC100%1/1
$bank->{"master"} // {}
$bank->{"master"}
Truth table100%2/2
A
A= $bank->{"master"}
Aresult
11
00
480 63 ▶ $self->{reverb}->set($_ => $master->{reverb}{$_}) for keys %{ $master->{reverb} // {} };
Condition100%2/2
$master->{"reverb"} // {}
l!l
162
MC/DC100%1/1
$master->{"reverb"} // {}
$master->{"reverb"}
Truth table100%2/2
A
A= $master->{"reverb"}
Aresult
11
00
481 63 ▶ $self->{eq}->set(%{ $master->{eq} // {} });
Condition100%2/2
$master->{"eq"} // {}
l!l
261
MC/DC100%1/1
$master->{"eq"} // {}
$master->{"eq"}
Truth table100%2/2
A
A= $master->{"eq"}
Aresult
11
00
482 63 ▶ $self->set_channel_eq($_, %{ $master->{channels}{$_} }) for keys %{ $master->{channels} // {} };
Condition100%2/2
$master->{"channels"} // {}
l!l
261
MC/DC100%1/1
$master->{"channels"} // {}
$master->{"channels"}
Truth table100%2/2
A
A= $master->{"channels"}
Aresult
11
00
483 63 ▶ $self->set_arpeggiator($_, %{ $master->{arps}{$_} }) for keys %{ $master->{arps} // {} };
Condition100%2/2
$master->{"arps"} // {}
l!l
162
MC/DC100%1/1
$master->{"arps"} // {}
$master->{"arps"}
Truth table100%2/2
A
A= $master->{"arps"}
Aresult
11
00
484 63 return $self;
485 }
486  
487 # The events of a key: left out where a file is played from a point in it.
488 16 my %NOTE = map { $_ => 1 } qw(note_on note_off key_pressure);
489  
490 # Events scheduled at times (seconds) from now, as Peta::Synth::MIDI's
491 # timeline gives them: the channels' and system exclusive messages, and the
492 # tempo (the arpeggiators'). From
493 # $from seconds into them, what comes before that sets the channels up at
494 # once, in its order (programs, controllers, bends, system exclusive), and
495 # its notes are left out.
496 391 ▶sub schedule ($self, $events, $offset = 0, $from = 0) {
Subroutine schedule: called CC=11 SCAR=25.0
Pod documented
497 391 my $rate = $self->{rate};
498 391 my $base = $self->{now} + $offset * $rate;
499  
500 # In time order, whatever order they come in (sort is stable: same-sample order kept).
501 391 ▶ my @wanted = grep { defined $_->{channel} || $_->{type} eq 'sysex' || $_->{type} eq 'tempo' } @$events;
Condition100%6/6
defined $_->{'channel'} || $_->{'type'} eq 'sysex' || $_->{'type'} eq 'tempo'
l!l&&r!l&&!r
339715
defined $_->{"channel"} or $_->{"type"} eq "sysex"
l!l&&r!l&&!r
339616
MC/DC100%3/3
defined $_->{'channel'} || $_->{'type'} eq 'sysex' || $_->{'type'} eq 'tempo'
defined $_->{"channel"} $_->{"type"} eq "sysex" $_->{'type'} eq 'tempo'
Truth table100%4/4
A or B || C
A= defined $_->{"channel"}B= $_->{"type"} eq "sysex"C= $_->{'type'} eq 'tempo'
ABCresult
1XX1
01X1
0011
0000
502 391 ▶ @wanted = grep { $_->{time} >= $from || !$NOTE{ $_->{type} } } @wanted if $from;
Branch: 1 branch
BranchTrueFalseTotal
if $from 4 387 391
Condition100%3/3
$_->{'time'} >= $from || !$NOTE{$_->{'type'}}
l!l&&r!l&&!r
1711292
MC/DC100%2/2
$_->{'time'} >= $from || !$NOTE{$_->{'type'}}
$_->{'time'} >= $from !$NOTE{$_->{'type'}}
Truth table100%3/3
A || not(B)
A= $_->{'time'} >= $fromB= !$NOTE{$_->{'type'}}
ABresult
1X1
011
000
503 391 ▶ my @new = sort { $a->[0] <=> $b->[0] } map { [ int($base + ($_->{time} > $from ? $_->{time} - $from : 0) * $rate + 0.5), $_ ] } @wanted;
Branch: 1 branch
BranchTrueFalseTotal
$_->{'time'} > $from ? : 2249 1057 3306
504  
505 391 my $queue = $self->{queue};
506 391 ▶ if (!@$queue || $new[0] && $new[0][0] >= $queue->[-1][0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$queue or $new[0] and $new[0][0] >= $queue->[-1][0]) { } 391 0 391
Condition16%1/6
not @$queue or $new[0] and $new[0][0] >= $queue->[-1][0]
l!l&&r!l&&!r
39100
$new[0] and $new[0][0] >= $queue->[-1][0]
!ll&&!rl&&r
000
MC/DC0%0/3
not @$queue or $new[0] and $new[0][0] >= $queue->[-1][0]
not @$queue $new[0] $new[0][0] >= $queue->[-1][0]
Truth table25%1/4
not(A) or B and C
A= not @$queueB= $new[0]C= $new[0][0] >= $queue->[-1][0]
ABCresult
1XX1
0111
00X0
0100
507 391 push @$queue, @new;
508 }
509 else {
510 0 @$queue = sort { $a->[0] <=> $b->[0] } @$queue, @new;
511 }
512 391 return scalar @new;
513 }
514  
515 # Whether there is anything still to render: events to come, voices that
516 # sound, or a reverb tail that has not died away.
517 460 ▶sub busy ($self) {
Subroutine busy: called CC=3 SCAR=11.0
Pod documented
518 460 ▶ return @{ $self->{queue} } || @{ $self->{active} } || !$self->{reverb}->quiet;
Condition100%6/6
@{$self->{'queue'};} || @{$self->{'active'};} || !$self->{'reverb'}->quiet
l!l&&r!l&&!r
2462113
@{$self->{"queue"};} or @{$self->{"active"};}
l!l&&r!l&&!r
111135214
MC/DC100%3/3
@{$self->{'queue'};} || @{$self->{'active'};} || !$self->{'reverb'}->quiet
@{$self->{"queue"};} @{$self->{"active"};} !$self->{'reverb'}->quiet
Truth table100%4/4
A or B || not(C)
A= @{$self->{"queue"};}B= @{$self->{"active"};}C= !$self->{'reverb'}->quiet
ABCresult
1XX1
01X1
0011
0000
519 }
520  
521 # Scheduled events dropped and every voice released: a file stopped.
522 8 ▶sub stop ($self) {
Subroutine stop: called CC=3 SCAR=11.0
Pod documented
523 8 $self->{queue} = [];
524 8 $self->_arps_stop;
525 8 for my $c (@{ $self->{channels} }) {
526 128 @$c{qw(held pedal sustain stack)} = ({}, [], 0, []);
527 }
528 8 $_->release($self->{now}) for @{ $self->{active} };
529 8 return $self;
530 }
531  
532 6 ▶sub panic ($self) {
Subroutine panic: called CC=2 SCAR=6.9
Pod documented
533 6 $self->stop;
534 6 $_->kill($self->{now}) for @{ $self->{active} };
535 6 return $self;
536 }
537  
538 # An event now: it takes effect at the start of the next quantum.
539 439 ▶sub play ($self, $event) {
Subroutine play: called CC=1 SCAR=0.0
Pod documented
540 439 $self->_apply($event, $self->{now});
541 438 return $self;
542 }
543  
544 2082 ▶sub _apply ($self, $event, $at) {
Subroutine _apply: called CC=37 SCAR=37.0
545 2082 my $type = $event->{type};
546 2082 delete $self->{state}; # the channels' gains, bend and modulation, to be worked out again
547  
548 2082 ▶ return $self->_sysex($event->{data}, $at) if $type eq 'sysex';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sysex" 29 2053 2082
549 2053 ▶ return $self->_tempo($event->{usec}, $at) if $type eq 'tempo';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "tempo" 3 2050 2053
550  
551 2050 ▶ my $c = $self->{channels}[ $event->{channel} // return ];
Condition50%1/2
$event->{"channel"} // (return)
l!l
20500
MC/DC0%0/1
$event->{"channel"} // (return)
$event->{"channel"}
Truth table50%1/2
A
A= $event->{"channel"}
Aresult
11
00
552 2050 my $n = $c->{number};
553 2050 ▶ if (my ($what, $part) = _setting($event)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = _setting($event)) 765 1285 2050
554 # Held by hand, or kept aside while it is (see by_hand).
555 765 my $held = $self->{hand}[$n]{$what};
556 765 ▶ if ($self->{by_hand}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'by_hand'}) { } 25 740 765
elsif ($held) { } 6 734 740
557 25 ▶ $self->{aside}[$n]{$what} //= $self->_as_now($n, $what);
Condition66%2/3
$self->{"aside"}[$n]{$what} //= $self->_as_now($n, $what)
l!l&&r!l&&!r
10150
MC/DC0%0/2
$self->{"aside"}[$n]{$what} //= $self->_as_now($n, $what)
$self->{"aside"}[$n]{$what} $self->_as_now($n, $what)
Truth table66%2/3
A //= B
A= $self->{"aside"}[$n]{$what}B= $self->_as_now($n, $what)
ABresult
1X1
011
000
558 25 ▶ $held = $self->{hand}[$n]{$what} //= {};
Condition100%2/2
$self->{"hand"}[$n]{$what} //= {}
l!l
1015
MC/DC100%1/1
$self->{"hand"}[$n]{$what} //= {}
$self->{"hand"}[$n]{$what}
Truth table100%2/2
A
A= $self->{"hand"}[$n]{$what}
Aresult
11
00
559 25 $held->{$part} = $event;
560 25 ▶ if ($part eq 'program') {
Branch: 1 branch
BranchTrueFalseTotal
if ($part eq "program") 6 19 25
561 # A program change alone: with the bank it was chosen from.
562 6 my $now = $self->_as_now($n, 'program');
563 6 ▶ $held->{$_} //= $now->{$_} for 'cc 0', 'cc 32';
Condition66%2/3
$held->{$_} //= $now->{$_}
l!l&&r!l&&!r
840
MC/DC0%0/2
$held->{$_} //= $now->{$_}
$held->{$_} $now->{$_}
Truth table66%2/3
A //= B
A= $held->{$_}B= $now->{$_}
ABresult
1X1
011
000
564 }
565 }
566 elsif ($held) {
567 6 $self->{aside}[$n]{$what}{$part} = $event;
568 6 return;
569 }
570 }
571 2044 ▶ if ($type eq 'arp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "arp") 26 2018 2044
572 26 ▶ return $self->set_arpeggiator($n, $event->{name} => $event->{value}) if $self->{by_hand};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"by_hand"} 5 21 26
573 21 return $self->_arp_change($n, $event->{name} => $event->{value});
574 }
575  
576 2018 my $arp = $self->{arps}[$n];
577 2018 ▶ if ($type eq 'arp_step') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "arp_step") 4 2014 2018
578 # An arpeggiator's step: its notes of the keys held now, and what is due next.
579 4 ▶ return if !$arp;
Branch: 1 branch
BranchTrueFalseTotal
unless $arp 0 4 4
580 4 $self->_apply($_->[1], $at) for $arp->step($at);
581 4 ▶ $self->_enqueue($arp->events($at, $self->{until} // $at));
Condition33%1/3
$self->{"until"} // $at
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->{"until"} // $at
$self->{"until"} $at
Truth table33%1/3
A // B
A= $self->{"until"}B= $at
ABresult
1X1
011
000
582 4 return;
583 }
584 2014 ▶ if ($arp && $arp->on && !$event->{arp} && !$c->{drum} && ($type eq 'note_on' || $type eq 'note_off')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arp and $arp->on and not $event->{"arp"} and not $c->{"drum"} and $type eq "note_on" || $type eq "note_off") 2 2012 2014
Condition86%13/15
$arp and $arp->on and not $event->{"arp"} and not $c->{"drum"} and $type eq "note_on" || $type eq "note_off"
!ll&&!rl&&r
1984282
$arp and $arp->on and not $event->{"arp"} and not $c->{"drum"}
!ll&&!rl&&r
1984030
$arp and $arp->on and not $event->{"arp"}
!ll&&!rl&&r
1976830
$arp and $arp->on
!ll&&!rl&&r
19532338
$type eq "note_on" or $type eq "note_off"
l!l&&r!l&&!r
2028
MC/DC66%4/6
$arp and $arp->on and not $event->{"arp"} and not $c->{"drum"} and $type eq "note_on" || $type eq "note_off"
$arp $arp->on not $event->{"arp"} not $c->{"drum"} $type eq "note_on" $type eq "note_off"
Truth table71%5/7
A and B and not(C) and not(D) and E or F
A= $arpB= $arp->onC= not $event->{"arp"}D= not $c->{"drum"}E= $type eq "note_on"F= $type eq "note_off"
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
1111000
11111X1
1111011
585 # The key to the arpeggiator; its first notes at once.
586 2 ▶ if ($type eq 'note_on' && $event->{velocity}) { $arp->press($event->{key}, $event->{velocity}, $at) }
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'note_on' and $event->{'velocity'}) { } 2 0 2
Condition33%1/3
$type eq "note_on" and $event->{"velocity"}
!ll&&!rl&&r
002
MC/DC0%0/2
$type eq "note_on" and $event->{"velocity"}
$type eq "note_on" $event->{"velocity"}
Truth table33%1/3
A and B
A= $type eq "note_on"B= $event->{"velocity"}
ABresult
0X0
100
111
587 0 else { $arp->release($event->{key}, $at) }
588 2 ▶ $self->_enqueue($arp->events($at, $self->{until} // $at));
Condition33%1/3
$self->{"until"} // $at
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"until"} // $at
$self->{"until"} $at
Truth table33%1/3
A // B
A= $self->{"until"}B= $at
ABresult
1X1
011
000
589 2 return;
590 }
591 2012 ▶ if ($type eq 'note_on') {
Branch: 7 branches
BranchTrueFalseTotal
if ($type eq 'note_on') { } 702 1310 2012
elsif ($type eq 'note_off') { } 418 892 1310
elsif ($type eq 'program') { } 325 567 892
elsif ($type eq 'pitch_bend') { } 23 544 567
elsif ($type eq 'controller') { } 533 11 544
elsif ($type eq 'channel_pressure') { } 9 2 11
elsif ($type eq 'key_pressure') { } 2 0 2
592 702 ▶ if ($event->{velocity}) { $self->_note_on($c, $event->{key}, $event->{velocity}, $at) }
Branch: 1 branch
BranchTrueFalseTotal
if ($event->{'velocity'}) { } 702 0 702
593 0 else { $self->_note_off($c, $event->{key}, $at) }
594 }
595 elsif ($type eq 'note_off') {
596 418 $self->_note_off($c, $event->{key}, $at, $event->{velocity});
597 }
598 elsif ($type eq 'program') {
599 # The bank selected before it picks the variation, and on a drum channel the program the kit.
600 325 $c->{program} = $event->{program};
601 325 ▶ $c->{variation} = Peta::Synth::Patch::variation($c->{bank}, $c->{cc}{32} // 0, $c->{program});
Condition100%2/2
$c->{"cc"}{32} // 0
l!l
6319
MC/DC100%1/1
$c->{"cc"}{32} // 0
$c->{"cc"}{32}
Truth table100%2/2
A
A= $c->{"cc"}{32}
Aresult
11
00
602 325 $c->{kit} = Peta::Synth::Patch::kit($c->{program});
603 }
604 elsif ($type eq 'pitch_bend') {
605 23 $c->{bend} = $event->{value} / 8192;
606 }
607 elsif ($type eq 'controller') {
608 533 $self->_controller($c, $event->{number}, $event->{value}, $at);
609 }
610 elsif ($type eq 'channel_pressure') {
611 9 $c->{pressure} = $event->{value} / 127;
612 }
613 elsif ($type eq 'key_pressure') {
614 2 my $p = $event->{value} / 127;
615 2 $_->{pressure} = $p for grep { $_->{key} == $event->{key} } $self->_voices($c);
616 }
617 2011 return;
618 }
619  
620 # The voices of a channel that have not been let go (held, or held by a pedal).
621 247 ▶sub _voices ($self, $c) {
Subroutine _voices: called CC=3 SCAR=11.0
622 247 my $n = $c->{number};
623 247 ▶ return grep { $_->{channel} == $n && !defined $_->{released} && !defined $_->{killed} } @{ $self->{active} };
Condition100%6/6
$_->{'channel'} == $n && !defined($_->{'released'}) && !defined($_->{'killed'})
!ll&&!rl&&r
9419
$_->{"channel"} == $n and not defined $_->{"released"}
!ll&&!rl&&r
90410
MC/DC100%3/3
$_->{'channel'} == $n && !defined($_->{'released'}) && !defined($_->{'killed'})
$_->{"channel"} == $n not defined $_->{"released"} !defined($_->{'killed'})
Truth table100%4/4
A and not(B) && not(C)
A= $_->{"channel"} == $nB= not defined $_->{"released"}C= !defined($_->{'killed'})
ABCresult
0XX0
10X0
1100
1111
624 }
625  
626 702 ▶sub _note_on ($self, $c, $key, $velocity, $at) {
Subroutine _note_on: called CC=19 SCAR=30.5
627 702 ▶ return $self->_mono_on($c, $key, $velocity, $at) if ($c->{mono} || $c->{legato}) && !$c->{drum};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"mono"} || $c->{"legato"} and not $c->{"drum"} 4 698 702
Condition83%5/6
$c->{"mono"} || $c->{"legato"} and not $c->{"drum"}
!ll&&!rl&&r
69804
$c->{"mono"} or $c->{"legato"}
l!l&&r!l&&!r
22698
MC/DC66%2/3
$c->{"mono"} || $c->{"legato"} and not $c->{"drum"}
$c->{"mono"} $c->{"legato"} not $c->{"drum"}
Truth table60%3/5
A or B and not(C)
A= $c->{"mono"}B= $c->{"legato"}C= not $c->{"drum"}
ABCresult
00X0
1X00
0100
1X11
0111
628  
629 698 ▶ if ($c->{drum}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'drum'}) { } 142 556 698
630 # A drum of an exclusive class cuts off the others of its class.
631 142 ▶ if (my $class = $EXCLUSIVE{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = $EXCLUSIVE{$key}) 34 108 142
632 34 my @mine = grep { $_->{channel} == $c->{number} } @{ $self->{active} };
633 34 ▶ $_->kill($at) for grep { ($EXCLUSIVE{ $_->{key} } // 0) == $class && !defined $_->{killed} } @mine;
Condition100%5/5
($EXCLUSIVE{$_->{'key'}} // 0) == $class && !defined($_->{'killed'})
!ll&&!rl&&r
1112
$EXCLUSIVE{$_->{"key"}} // 0
l!l
311
MC/DC100%2/2
($EXCLUSIVE{$_->{'key'}} // 0) == $class && !defined($_->{'killed'})
$EXCLUSIVE{$_->{"key"}} !defined($_->{'killed'})
Truth table100%3/3
A && not(B)
A= $EXCLUSIVE{$_->{"key"}}B= !defined($_->{'killed'})
ABresult
0X0
100
111
634 }
635 }
636 else {
637 # The same key again: the voice it sounds in is let go.
638 556 ▶ if ($c->{held}{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"held"}{$key}) 0 556 556
639 0 $_->release($at) for @{ delete $c->{held}{$key} };
640 }
641 556 my @pedal;
642 556 for my $voice (@{ $c->{pedal} }) {
643 0 ▶ if ($voice->{key} == $key) { $voice->release($at) } else { push @pedal, $voice }
Branch: 1 branch
BranchTrueFalseTotal
if ($voice->{'key'} == $key) { } 0 0 0
644 }
645 556 $c->{pedal} = \@pedal;
646 }
647  
648 # Portamento (not on drums): from CC84's key, once; else from the last key
649 # while CC65 is down.
650 698 my $from = delete $c->{porta_from};
651 698 ▶ $from //= $c->{last_key} if $c->{portamento};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"portamento"} 2 696 698
Condition66%2/3
$from //= $c->{"last_key"}
l!l&&r!l&&!r
011
MC/DC50%1/2
$from //= $c->{"last_key"}
$from $c->{"last_key"}
Truth table66%2/3
A //= B
A= $fromB= $c->{"last_key"}
ABresult
1X1
011
000
652 698 ▶ $from = undef if $c->{drum};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"drum"} 142 556 698
653 698 ▶ $c->{last_key} = $key if !$c->{drum};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->{"drum"} 556 142 698
654  
655 698 ▶ my @glide = defined $from ? (glide => [ $from - $key, _porta_time($c) ]) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $from ? : 2 696 698
656 698 ▶ my $voice = $self->_voice($c, $key, $velocity, $at, @glide) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $voice = $self->_voice($c, $key, $velocity, $at, @glide) or return 697 0 697
657 697 push @{ $c->{held}{$key} }, $voice;
658 697 return $voice;
659 }
660  
661 # Mono (CC126, or the legato pedal): one voice a channel. A key pressed
662 # while another is held takes over legato (no attack; gliding from the key
663 # that sounded where portamento is on); let go, the key held before it
664 # sounds again. The voice that takes over is a new one: its spectrum is
665 # its key's, and the old one fades in 5 ms.
666 4 ▶sub _mono_on ($self, $c, $key, $velocity, $at) {
Subroutine _mono_on: called CC=4 SCAR=14.5
667 4 my $stack = $c->{stack};
668 4 ▶ my $sounding = @$stack ? $stack->[-1][0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$stack ? : 2 2 4
669 4 @$stack = grep { $_->[0] != $key } @$stack;
670 4 push @$stack, [ $key, $velocity ];
671  
672 4 my $from = delete $c->{porta_from};
673 4 ▶ $from //= $c->{last_key} if $c->{portamento};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"portamento"} 0 4 4
Condition0%0/3
$from //= $c->{"last_key"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$from //= $c->{"last_key"}
$from $c->{"last_key"}
Truth table0%0/3
A //= B
A= $fromB= $c->{"last_key"}
ABresult
1X1
011
000
674 4 $c->{last_key} = $key;
675 4 return $self->_mono_sound($c, $key, $velocity, $at, defined $sounding, $from);
676 }
677  
678 5 ▶sub _mono_sound ($self, $c, $key, $velocity, $at, $legato, $from) {
Subroutine _mono_sound: called CC=6 SCAR=19.3
679 5 $_->kill($at) for $self->_voices($c);
680 5 $c->{pedal} = [];
681 5 $c->{held} = {};
682  
683 5 ▶ my $time = defined $from ? _porta_time($c) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $from ? : 0 5 5
684 5 ▶ my @glide = defined $from && $from != $key ? (glide => [ $from - $key, $time ]) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $from && $from != $key ? : 0 5 5
Condition33%1/3
defined $from and $from != $key
!ll&&!rl&&r
500
MC/DC0%0/2
defined $from and $from != $key
defined $from $from != $key
Truth table33%1/3
A and B
A= defined $fromB= $from != $key
ABresult
0X0
100
111
685 5 ▶ my $voice = $self->_voice($c, $key, $velocity, $at, legato => $legato, @glide) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $voice = $self->_voice($c, $key, $velocity, $at, "legato", $legato, @glide) or return 5 0 5
686 5 $c->{held}{$key} = [$voice];
687 5 return $voice;
688 }
689  
690 2 ▶sub _mono_off ($self, $c, $key, $at, $velocity) {
Subroutine _mono_off: called CC=8 SCAR=21.6
691 2 my $stack = $c->{stack};
692 2 ▶ my $was = @$stack ? $stack->[-1][0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$stack ? : 2 0 2
693 2 @$stack = grep { $_->[0] != $key } @$stack;
694 2 ▶ return if !defined $was || $was != $key;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $was or $was != $key 0 2 2
Condition33%1/3
not defined $was or $was != $key
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $was or $was != $key
not defined $was $was != $key
Truth table33%1/3
not(A) or B
A= not defined $wasB= $was != $key
ABresult
1X1
011
000
695  
696 2 ▶ if (@$stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack) 1 1 2
697 1 my ($back, $back_velocity) = @{ $stack->[-1] };
698 1 $c->{last_key} = $back;
699 1 ▶ return $self->_mono_sound($c, $back, $back_velocity, $at, 1, $c->{portamento} ? $key : undef);
Branch: 1 branch
BranchTrueFalseTotal
$c->{'portamento'} ? : 0 1 1
700 }
701 1 ▶ for my $voice (@{ delete $c->{held}{$key} // [] }) {
Condition50%1/2
delete $c->{"held"}{$key} // []
l!l
10
MC/DC0%0/1
delete $c->{"held"}{$key} // []
delete $c->{"held"}{$key}
Truth table50%1/2
A
A= delete $c->{"held"}{$key}
Aresult
11
00
702 1 $self->_let_go($c, $voice, $at, $velocity);
703 }
704 1 return;
705 }
706  
707 418 ▶sub _note_off ($self, $c, $key, $at, $velocity = undef) {
Subroutine _note_off: called CC=7 SCAR=19.7
708 418 ▶ return $self->_mono_off($c, $key, $at, $velocity) if ($c->{mono} || $c->{legato}) && !$c->{drum};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"mono"} || $c->{"legato"} and not $c->{"drum"} 2 416 418
Condition66%4/6
$c->{"mono"} || $c->{"legato"} and not $c->{"drum"}
!ll&&!rl&&r
41602
$c->{"mono"} or $c->{"legato"}
l!l&&r!l&&!r
20416
MC/DC33%1/3
$c->{"mono"} || $c->{"legato"} and not $c->{"drum"}
$c->{"mono"} $c->{"legato"} not $c->{"drum"}
Truth table40%2/5
A or B and not(C)
A= $c->{"mono"}B= $c->{"legato"}C= not $c->{"drum"}
ABCresult
00X0
1X00
0100
1X11
0111
709  
710 416 ▶ my $held = $c->{held}{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $held = $c->{"held"}{$key} or return 398 18 416
711 398 my $voice = shift @$held;
712 398 ▶ delete $c->{held}{$key} if !@$held;
Branch: 1 branch
BranchTrueFalseTotal
unless @$held 398 0 398
713 398 $self->_let_go($c, $voice, $at, $velocity);
714 398 return;
715 }
716  
717 # A key let go: its voice released, or held by a pedal (its note-off
718 # velocity kept for when the pedal lets go).
719 399 ▶sub _let_go ($self, $c, $voice, $at, $velocity) {
Subroutine _let_go: called CC=3 SCAR=11.0
720 399 ▶ if ($c->{sustain} || $c->{sostenuto}{ $voice->{key} }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'sustain'} or $c->{'sostenuto'}{$voice->{'key'}}) { } 4 395 399
Condition66%2/3
$c->{"sustain"} or $c->{"sostenuto"}{$voice->{"key"}}
l!l&&r!l&&!r
40395
MC/DC50%1/2
$c->{"sustain"} or $c->{"sostenuto"}{$voice->{"key"}}
$c->{"sustain"} $c->{"sostenuto"}{$voice->{"key"}}
Truth table66%2/3
A or B
A= $c->{"sustain"}B= $c->{"sostenuto"}{$voice->{"key"}}
ABresult
1X1
011
000
721 4 push @{ $c->{pedal} }, $voice;
722 4 $voice->off_velocity($velocity);
723 }
724 else {
725 395 $voice->release($at, $velocity);
726 }
727 399 return;
728 }
729  
730 # The seconds a portamento takes (CC5: 0 none, 64 about 0.15 s, 127 about 10 s).
731 2 ▶sub _porta_time ($c) {
Subroutine _porta_time: called CC=2 SCAR=6.9
732 2 my $v = $c->{porta_time};
733 2 ▶ return $v ? 0.002 * exp($v * 0.0675) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 1 1 2
734 }
735  
736 # A new voice of a note on channel $c, as the channel shapes it (the sound
737 # controllers, the soft pedal, a drum key's own settings); undef where this
738 # synth does not play it (a worker's share, a muted channel).
739 703 ▶sub _voice ($self, $c, $key, $velocity, $at, %more) {
Subroutine _voice: called CC=19 SCAR=30.4
740 703 my $id = $self->{count}++;
741 703 ▶ return if $self->{filter} && !$self->{filter}->($id);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"filter"} and not $self->{"filter"}->($id) 0 703 703
Condition33%1/3
$self->{"filter"} and not $self->{"filter"}->($id)
!ll&&!rl&&r
70300
MC/DC0%0/2
$self->{"filter"} and not $self->{"filter"}->($id)
$self->{"filter"} not $self->{"filter"}->($id)
Truth table33%1/3
A and not(B)
A= $self->{"filter"}B= not $self->{"filter"}->($id)
ABresult
0X0
100
111
742 703 ▶ return if $c->{mute};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"mute"} 0 703 703
743  
744 703 ▶ my $patch = $c->{drum} ? $self->drum_patch($key, $c->{kit}) : $self->program_patch($c->{program}, $c->{variation});
Branch: 1 branch
BranchTrueFalseTotal
$c->{'drum'} ? : 142 561 703
745  
746 # The sound controllers, 64 as the patch is: times halve or double every
747 # 16 steps; brightness moves the cutoff by up to four octaves; resonance
748 # raises the cutoff's peak (at most about 16 dB). A patch with a filter
749 # has brightness and resonance move it instead, as the note sounds.
750 703 my $sound = $c->{sound};
751 my %shape = (
752 release => 2**(($sound->{72} - 64) / 16),
753 attack => 2**(($sound->{73} - 64) / 16),
754 decay => 2**(($sound->{75} - 64) / 16),
755 bright => 2**(($sound->{74} - 64) / 16),
756 703 q => 2**(($sound->{71} - 64) / 24),
757 );
758 703 ▶ @shape{qw(bright q)} = (1, 1) if $patch->{filter} ne 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $patch->{"filter"} ne "off" 17 686 703
759 703 ▶ if ($c->{soft}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"soft"}) 1 702 703
760 1 $shape{level} = 0.7;
761 1 $shape{bright} *= 0.7;
762 }
763  
764 # A drum key's own pitch, level, pan and sends (GS and XG).
765 703 my ($tune, $pan, $send, $chorus) = (0, 0, 1, $c->{chorus} / 127);
766 703 ▶ if ($c->{drum} && (my $d = $c->{drum_keys}{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"drum"} and my $d = $c->{"drum_keys"}{$key}) 1 702 703
Condition100%3/3
$c->{"drum"} and my $d = $c->{"drum_keys"}{$key}
!ll&&!rl&&r
5611411
MC/DC100%2/2
$c->{"drum"} and my $d = $c->{"drum_keys"}{$key}
$c->{"drum"} my $d = $c->{"drum_keys"}{$key}
Truth table100%3/3
A and B
A= $c->{"drum"}B= my $d = $c->{"drum_keys"}{$key}
ABresult
0X0
100
111
767 1 ▶ $tune = ($d->{pitch} // 0) + ($d->{fine} // 0) / 100;
Condition50%2/4
$d->{"pitch"} // 0
l!l
10
$d->{"fine"} // 0
l!l
01
MC/DC0%0/1
$d->{"pitch"} // 0
$d->{"pitch"}
MC/DC0%0/1
$d->{"fine"} // 0
$d->{"fine"}
Truth table50%1/2
A
A= $d->{"pitch"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"fine"}
Aresult
11
00
768 1 ▶ $shape{level} = ($shape{level} // 1) * ($d->{level} // 127)**2 / 16129;
Condition50%2/4
$shape{"level"} // 1
l!l
01
$d->{"level"} // 127
l!l
10
MC/DC0%0/1
$shape{"level"} // 1
$shape{"level"}
MC/DC0%0/1
$d->{"level"} // 127
$d->{"level"}
Truth table50%1/2
A
A= $shape{"level"}
Aresult
11
00
Truth table50%1/2
A
A= $d->{"level"}
Aresult
11
00
769 1 ▶ $pan = (($d->{pan} // 64) - 64) / 64;
Condition50%1/2
$d->{"pan"} // 64
l!l
10
MC/DC0%0/1
$d->{"pan"} // 64
$d->{"pan"}
Truth table50%1/2
A
A= $d->{"pan"}
Aresult
11
00
770 1 ▶ $send = ($d->{reverb} // 127) / 127;
Condition50%1/2
$d->{"reverb"} // 127
l!l
10
MC/DC0%0/1
$d->{"reverb"} // 127
$d->{"reverb"}
Truth table50%1/2
A
A= $d->{"reverb"}
Aresult
11
00
771 1 ▶ $chorus *= ($d->{chorus} // 127) / 127;
Condition50%1/2
$d->{"chorus"} // 127
l!l
01
MC/DC0%0/1
$d->{"chorus"} // 127
$d->{"chorus"}
Truth table50%1/2
A
A= $d->{"chorus"}
Aresult
11
00
772 }
773  
774 # A controller destination that moves the cutoff gives the note a filter.
775 703 ▶ my $cutoff = grep { $_->[1] != 64 || $_->[4] } values %{ $c->{dest} };
Condition66%2/3
$_->[1] != 64 || $_->[4]
l!l&&r!l&&!r
108
MC/DC50%1/2
$_->[1] != 64 || $_->[4]
$_->[1] != 64 $_->[4]
Truth table66%2/3
A || B
A= $_->[1] != 64B= $_->[4]
ABresult
1X1
011
000
776  
777 my $voice = Peta::Synth::Voice->new($patch,
778 key => $key,
779 velocity => $velocity,
780 channel => $c->{number},
781 rate => $self->{rate},
782 partials => $self->{partials},
783 start => $at,
784 seed => $id + 1,
785 id => $id,
786 chorus => $chorus,
787 shape => \%shape,
788 tune => $tune,
789 pan => $pan,
790 send => $send,
791 cutoff_control => $cutoff,
792 waves => $self->{waves},
793 703 %more,
794 );
795 702 my $eq = $self->{channel_eq}[ $c->{number} ];
796 702 ▶ $voice->equalize($eq) if $eq->active;
Branch: 1 branch
BranchTrueFalseTotal
if $eq->active 1 701 702
797  
798 702 push @{ $self->{active} }, $voice;
799 702 $self->{stats}{notes}++;
800 702 ▶ $self->_steal($at) if @{ $self->{active} } > $self->_most_voices;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"active"};} > $self->_most_voices 16 686 702
801 702 return $voice;
802 }
803  
804 # The most voices at once: the set number, or the fidelity's if fewer.
805 718 ▶sub _most_voices ($self) {
Subroutine _most_voices: called CC=3 SCAR=11.1
806 718 ▶ my $most = $FIDELITY{ $self->{fidelity} }{voices} // return $self->{voices};
Condition100%2/2
$FIDELITY{$self->{"fidelity"}}{"voices"} // (return $self->{"voices"})
l!l
74644
MC/DC100%1/1
$FIDELITY{$self->{"fidelity"}}{"voices"} // (return $self->{"voices"})
$FIDELITY{$self->{"fidelity"}}{"voices"}
Truth table100%2/2
A
A= $FIDELITY{$self->{"fidelity"}}{"voices"}
Aresult
11
00
807 74 ▶ return $most < $self->{voices} ? $most : $self->{voices};
Branch: 1 branch
BranchTrueFalseTotal
$most < $self->{'voices'} ? : 74 0 74
808 }
809  
810 # Too many voices: those already let go end first, oldest first, then the oldest of the rest.
811 16 ▶sub _steal ($self, $at) {
Subroutine _steal: called CC=5 SCAR=16.6
812 16 my @live = grep { !defined $_->{killed} } @{ $self->{active} };
813 16 my $over = @live - $self->_most_voices;
814 16 ▶ return if $over <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $over <= 0 0 16 16
815  
816 608 my @order = map { $_->[1] }
817 608 ▶ sort { $b->[0] <=> $a->[0] || $a->[1]{start} <=> $b->[1]{start} }
Condition33%1/3
$Peta::Synth::Synth::b->[0] <=> $Peta::Synth::Synth::a->[0] || $Peta::Synth::Synth::a->[1]{'start'} <=> $Peta::Synth::Synth::b->[1]{'start'}
l!l&&r!l&&!r
00608
MC/DC0%0/2
$Peta::Synth::Synth::b->[0] <=> $Peta::Synth::Synth::a->[0] || $Peta::Synth::Synth::a->[1]{'start'} <=> $Peta::Synth::Synth::b->[1]{'start'}
$Peta::Synth::Synth::b->[0] <=> $Peta::Synth::Synth::a->[0] $Peta::Synth::Synth::a->[1]{'start'} <=> $Peta::Synth::Synth::b->[1]{'start'}
Truth table33%1/3
A || B
A= $Peta::Synth::Synth::b->[0] <=> $Peta::Synth::Synth::a->[0]B= $Peta::Synth::Synth::a->[1]{'start'} <=> $Peta::Synth::Synth::b->[1]{'start'}
ABresult
1X1
011
000
818 16 ▶ map { [ defined $_->{released} ? 1 : 0, $_ ] } @live;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'released'} ? : 0 608 608
819 16 $_->kill($at) for @order[ 0 .. $over - 1 ];
820 16 return;
821 }
822  
823 # The controllers that only set a channel's field: the field, and what the
824 # value is divided by.
825 16 my %SIMPLE_CONTROLLER = (
826 1 => [ mod => 127 ],
827 2 => [ breath => 127 ],
828 4 => [ foot => 127 ],
829 5 => [ porta_time => 1 ],
830 7 => [ volume => 1 ],
831 10 => [ pan => 1 ],
832 11 => [ expression => 1 ],
833 84 => [ porta_from => 1 ],
834 91 => [ send => 1 ],
835 93 => [ chorus => 1 ],
836 );
837  
838 533 ▶sub _controller ($self, $c, $number, $value, $at) {
Subroutine _controller: called CC=34 SCAR=35.6
839 533 ▶ $c->{cc}{$number} = $value if $number < 120;
Branch: 1 branch
BranchTrueFalseTotal
if $number < 120 513 20 533
840 533 ▶ my $on = $value >= 64 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value >= 64 ? : 66 467 533
841  
842 533 ▶ if (my $simple = $SIMPLE_CONTROLLER{$number}) {
Branch: 19 branches
BranchTrueFalseTotal
if (my $simple = $SIMPLE_CONTROLLER{$number}) { } 397 136 533
elsif ($number == 0) { } 9 127 136
elsif ($number >= 70 and $number <= 79) { } 10 117 127
elsif ($number == 65) { } 1 116 117
elsif ($number == 67) { } 3 113 116
elsif ($number == 68) { } 1 112 113
elsif ($number == 64) { } 10 102 112
elsif ($number == 66) { } 0 102 102
elsif ($number == 101 or $number == 100) { } 12 90 102
elsif ($number == 99 or $number == 98) { } 32 58 90
elsif ($number == 6 or $number == 38 or $number == 96 or $number == 97) { } 25 33 58
elsif ($number == 80) { } 5 28 33
elsif ($number == 81) { } 1 27 28
elsif ($number == 120) { } 1 26 27
elsif ($number == 123) { } 16 10 26
elsif ($number == 121) { } 1 9 10
elsif ($number == 124 or $number == 125) { } 0 9 9
elsif ($number == 126) { } 1 8 9
elsif ($number == 127) { } 1 7 8
843 397 my ($field, $scale) = @$simple;
844 397 $c->{$field} = $value / $scale;
845 }
846 elsif ($number == 0) {
847 # Bank 120 and 127 are drums (GM2, XG), 121 melodic (GM2); GS and XG
848 # banks of variations play their GM program.
849 9 $c->{bank} = $value;
850 9 ▶ $c->{drum} = 1 if $value == 120 || $value == 127;
Branch: 1 branch
BranchTrueFalseTotal
if $value == 120 or $value == 127 0 9 9
Condition33%1/3
$value == 120 or $value == 127
l!l&&r!l&&!r
009
MC/DC0%0/2
$value == 120 or $value == 127
$value == 120 $value == 127
Truth table33%1/3
A or B
A= $value == 120B= $value == 127
ABresult
1X1
011
000
851 9 ▶ $c->{drum} = 0 if $value == 121;
Branch: 1 branch
BranchTrueFalseTotal
if $value == 121 2 7 9
852 }
853 10 ▶ elsif ($number >= 70 && $number <= 79) { $c->{sound}{$number} = $value }
Condition100%3/3
$number >= 70 and $number <= 79
!ll&&!rl&&r
457210
MC/DC100%2/2
$number >= 70 and $number <= 79
$number >= 70 $number <= 79
Truth table100%3/3
A and B
A= $number >= 70B= $number <= 79
ABresult
0X0
100
111
854 1 elsif ($number == 65) { $c->{portamento} = $on }
855 3 elsif ($number == 67) { $c->{soft} = $on }
856 1 elsif ($number == 68) { $self->_mode($c, legato => $on, $at) }
857 10 elsif ($number == 64) { $self->_sustain($c, $on, $at) }
858 0 elsif ($number == 66) { $self->_sostenuto($c, $on, $at) }
859 12 ▶ elsif ($number == 101 || $number == 100) { _select_param($c, 'r', $number == 101 ? 1 : 2, $value) }
Branch: 1 branch
BranchTrueFalseTotal
$number == 101 ? : 6 6 12
Condition100%3/3
$number == 101 or $number == 100
l!l&&r!l&&!r
6690
MC/DC100%2/2
$number == 101 or $number == 100
$number == 101 $number == 100
Truth table100%3/3
A or B
A= $number == 101B= $number == 100
ABresult
1X1
011
000
860 32 ▶ elsif ($number == 99 || $number == 98) { _select_param($c, 'n', $number == 99 ? 1 : 2, $value) }
Branch: 1 branch
BranchTrueFalseTotal
$number == 99 ? : 16 16 32
Condition100%3/3
$number == 99 or $number == 98
l!l&&r!l&&!r
161658
MC/DC100%2/2
$number == 99 or $number == 98
$number == 99 $number == 98
Truth table100%3/3
A or B
A= $number == 99B= $number == 98
ABresult
1X1
011
000
861 25 ▶ elsif ($number == 6 || $number == 38 || $number == 96 || $number == 97) { $self->_data_entry($c, $number, $value, $at) }
Condition88%8/9
$number == 6 or $number == 38 or $number == 96 or $number == 97
l!l&&r!l&&!r
25033
$number == 6 or $number == 38 or $number == 96
l!l&&r!l&&!r
23233
$number == 6 or $number == 38
l!l&&r!l&&!r
20335
MC/DC75%3/4
$number == 6 or $number == 38 or $number == 96 or $number == 97
$number == 6 $number == 38 $number == 96 $number == 97
Truth table80%4/5
A or B or C or D
A= $number == 6B= $number == 38C= $number == 96D= $number == 97
ABCDresult
1XXX1
01XX1
001X1
00011
00000
862 5 elsif ($number == 80) { $self->_arp_set($c->{number}, on => $on, $at) } # general purpose 5
863 1 elsif ($number == 81) { $self->_arp_set($c->{number}, latch => $on, $at) } # general purpose 6
864 1 elsif ($number == 120) { $self->_sound_off($c, $at) }
865 16 elsif ($number == 123) { $self->_notes_off($c, $at) }
866 1 elsif ($number == 121) { $self->_reset_controllers($c, $at) }
867 0 ▶ elsif ($number == 124 || $number == 125) { $self->_notes_off($c, $at) } # omni off, on
Condition33%1/3
$number == 124 or $number == 125
l!l&&r!l&&!r
009
MC/DC0%0/2
$number == 124 or $number == 125
$number == 124 $number == 125
Truth table33%1/3
A or B
A= $number == 124B= $number == 125
ABresult
1X1
011
000
868 1 elsif ($number == 126) { $self->_mode($c, mono => 1, $at) }
869 1 elsif ($number == 127) { $self->_mode($c, mono => 0, $at) }
870 533 return;
871 }
872  
873 # The sustain pedal: let up, the voices it held are let go (but those the
874 # sostenuto pedal holds).
875 10 ▶sub _sustain ($self, $c, $down, $at) {
Subroutine _sustain: called CC=5 SCAR=16.4
876 10 ▶ if ($c->{sustain} && !$down) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"sustain"} and not $down) 4 6 10
Condition66%2/3
$c->{"sustain"} and not $down
!ll&&!rl&&r
604
MC/DC50%1/2
$c->{"sustain"} and not $down
$c->{"sustain"} not $down
Truth table66%2/3
A and not(B)
A= $c->{"sustain"}B= not $down
ABresult
0X0
100
111
877 4 my @still;
878 4 for my $voice (@{ $c->{pedal} }) {
879 0 ▶ if ($c->{sostenuto}{ $voice->{key} }) { push @still, $voice } else { $voice->release($at) }
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'sostenuto'}{$voice->{'key'}}) { } 0 4 4
880 }
881 4 $c->{pedal} = \@still;
882 }
883 10 $c->{sustain} = $down;
884 10 return;
885 }
886  
887 # The sostenuto pedal: pressed, it holds the keys held now; let up, the
888 # voices it held are let go (but those the sustain pedal holds).
889 0 ▶sub _sostenuto ($self, $c, $down, $at) {
Subroutine _sostenuto: not called CC=4 SCAR=30.0
890 0 ▶ if ($down) {
Branch: 1 branch
BranchTrueFalseTotal
if ($down) 0 0 0
891 0 $c->{sostenuto} = { map { $_ => 1 } keys %{ $c->{held} } };
892 0 return;
893 }
894 0 $c->{sostenuto} = {};
895 0 my @still;
896 0 for my $voice (@{ $c->{pedal} }) {
897 0 ▶ if ($c->{sustain}) { push @still, $voice } else { $voice->release($at) }
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{'sustain'}) { } 0 0 0
898 }
899 0 $c->{pedal} = \@still;
900 0 return;
901 }
902  
903 # All sound off: every voice of the channel ends within milliseconds.
904 1 ▶sub _sound_off ($self, $c, $at) {
Subroutine _sound_off: called CC=2 SCAR=6.9
905 1 $_->kill($at) for grep { $_->{channel} == $c->{number} } @{ $self->{active} };
906 1 @$c{qw(held pedal)} = ({}, []);
907 1 return;
908 }
909  
910 # All notes off: every key and pedal let go.
911 22 ▶sub _notes_off ($self, $c, $at) {
Subroutine _notes_off: called CC=4 SCAR=13.9
912 22 for my $voices (values %{ $c->{held} }) { $_->release($at) for @$voices }
913 22 $_->release($at) for @{ $c->{pedal} };
914 22 @$c{qw(held pedal)} = ({}, []);
915 22 return;
916 }
917  
918 # Reset all controllers, as GM2 says: not volume, pan, the sends, the sound controllers.
919 1 ▶sub _reset_controllers ($self, $c, $at) {
Subroutine _reset_controllers: called CC=3 SCAR=11.0
920 1 @$c{qw(expression mod breath foot pressure bend sustain soft portamento param)} = (127, 0, 0, 0, 0, 0, 0, 0, 0, undef);
921 1 @{ $c->{cc} }{ 1, 2, 4, 11, 64 .. 67 } = (0, 0, 0, 127, 0, 0, 0, 0);
922 1 $c->{sostenuto} = {};
923 0 $_->{pressure} = 0 for grep { $_->{channel} == $c->{number} } @{ $self->{active} };
924 1 $_->release($at) for @{ $c->{pedal} };
925 1 $c->{pedal} = [];
926 1 return;
927 }
928  
929 # Mono or poly mode, the legato pedal: the notes are let go first.
930 3 ▶sub _mode ($self, $c, $name, $on, $at) {
Subroutine _mode: called CC=2 SCAR=7.0
931 3 ▶ return if $c->{$name} == $on;
Branch: 1 branch
BranchTrueFalseTotal
if $c->{$name} == $on 0 3 3
932 3 $self->_notes_off($c, $at);
933 3 $c->{$name} = $on;
934 3 $c->{stack} = [];
935 3 return;
936 }
937  
938 # An RPN ('r') or NRPN ('n') chosen for data entry, one byte at a time
939 # ($which: 1 the MSB, 2 the LSB); 127, 127 is none.
940 44 ▶sub _select_param ($c, $kind, $which, $value) {
Subroutine _select_param: called CC=5 SCAR=16.1
941 44 ▶ my @p = $c->{param} && $c->{param}[0] eq $kind ? @{ $c->{param} } : ($kind, 127, 127);
Branch: 1 branch
BranchTrueFalseTotal
$c->{'param'} && $c->{'param'}[0] eq $kind ? : 35 9 44
Condition66%2/3
$c->{"param"} and $c->{"param"}[0] eq $kind
!ll&&!rl&&r
9035
MC/DC50%1/2
$c->{"param"} and $c->{"param"}[0] eq $kind
$c->{"param"} $c->{"param"}[0] eq $kind
Truth table66%2/3
A and B
A= $c->{"param"}B= $c->{"param"}[0] eq $kind
ABresult
0X0
100
111
942 44 $p[$which] = $value;
943 44 ▶ $c->{param} = $p[1] == 127 && $p[2] == 127 ? undef : \@p;
Branch: 1 branch
BranchTrueFalseTotal
$p[1] == 127 && $p[2] == 127 ? : 1 43 44
Condition100%3/3
$p[1] == 127 and $p[2] == 127
!ll&&!rl&&r
4211
MC/DC100%2/2
$p[1] == 127 and $p[2] == 127
$p[1] == 127 $p[2] == 127
Truth table100%3/3
A and B
A= $p[1] == 127B= $p[2] == 127
ABresult
0X0
100
111
944 44 return;
945 }
946  
947 # The RPNs and NRPNs, as their values are, from data entry: CC6 (the MSB,
948 # which clears the LSB), CC38 (the LSB), CC96 and 97 (one step up or down:
949 # the LSB where it counts cents, else the MSB).
950 16 my %PARAM_DEFAULT = ('r 0 0' => [ 2, 0 ], 'r 0 1' => [ 64, 0 ], 'r 0 2' => [ 64, 0 ], 'r 0 5' => [ 0, 64 ]);
951 16 my %FINE = map { $_ => 1 } 'r 0 0', 'r 0 1', 'r 0 5';
952  
953 # GS and XG's NRPNs: the part's vibrato, filter and envelope (1, lsb), as
954 # the sound controllers; a drum key's pitch, level, pan and sends (msb, key).
955 16 my %NRPN_SOUND = (0x08 => 76, 0x09 => 77, 0x0a => 78, 0x20 => 74, 0x21 => 71, 0x63 => 73, 0x64 => 75, 0x66 => 72);
956 16 my %NRPN_KEY = (0x18 => 'pitch', 0x19 => 'fine', 0x1a => 'level', 0x1c => 'pan', 0x1d => 'reverb', 0x1e => 'chorus');
957  
958 25 ▶sub _data_entry ($self, $c, $number, $value, $at) {
Subroutine _data_entry: called CC=29 SCAR=34.7
959 25 ▶ my $p = $c->{param} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $p = $c->{"param"} or return 24 1 25
960 24 my $name = join ' ', @$p;
961 24 ▶ my $data = $c->{data}{$name} //= [ @{ $PARAM_DEFAULT{$name} // [ 64, 0 ] } ];
Condition100%4/4
$c->{"data"}{$name} //= [@{$PARAM_DEFAULT{$name} // [64, 0];}]
l!l
519
$PARAM_DEFAULT{$name} // [64, 0]
l!l
415
MC/DC100%1/1
$c->{"data"}{$name} //= [@{$PARAM_DEFAULT{$name} // [64, 0];}]
$c->{"data"}{$name}
MC/DC100%1/1
$PARAM_DEFAULT{$name} // [64, 0]
$PARAM_DEFAULT{$name}
Truth table100%2/2
A
A= $c->{"data"}{$name}
Aresult
11
00
Truth table100%2/2
A
A= $PARAM_DEFAULT{$name}
Aresult
11
00
962  
963 24 ▶ if ($number == 6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($number == 6) { } 19 5 24
elsif ($number == 38) { } 3 2 5
964 19 @$data = ($value, 0);
965 }
966 elsif ($number == 38) {
967 3 $data->[1] = $value;
968 }
969 else {
970 2 ▶ my $v = $FINE{$name} ? $data->[0] * 128 + $data->[1] : $data->[0] * 128;
Branch: 1 branch
BranchTrueFalseTotal
$FINE{$name} ? : 2 0 2
971 2 ▶ $v += ($number == 96 ? 1 : -1) * ($FINE{$name} ? 1 : 128);
Branch: 2 branches
BranchTrueFalseTotal
$number == 96 ? : 2 0 2
$FINE{$name} ? : 2 0 2
972 2 ▶ $v = 0 if $v < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 0 2 2
973 2 ▶ $v = 16383 if $v > 16383;
Branch: 1 branch
BranchTrueFalseTotal
if $v > 16383 0 2 2
974 2 @$data = (int($v / 128), $v % 128);
975 }
976  
977 24 my ($msb, $lsb) = @$data;
978 24 my ($kind, $hi, $lo) = @$p;
979 24 ▶ if ($kind eq 'r') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "r") 7 17 24
980 7 ▶ if ($hi == 0 && $lo == 0) { $c->{range} = $msb + $lsb / 100 }
Branch: 4 branches
BranchTrueFalseTotal
if ($hi == 0 and $lo == 0) { } 2 5 7
elsif ($hi == 0 and $lo == 1) { } 2 3 5
elsif ($hi == 0 and $lo == 2) { } 1 2 3
elsif ($hi == 0 and $lo == 5) { } 2 0 2
Condition66%2/3
$hi == 0 and $lo == 0
!ll&&!rl&&r
052
MC/DC50%1/2
$hi == 0 and $lo == 0
$hi == 0 $lo == 0
Truth table66%2/3
A and B
A= $hi == 0B= $lo == 0
ABresult
0X0
100
111
981 2 ▶ elsif ($hi == 0 && $lo == 1) { $c->{fine} = ($msb * 128 + $lsb - 8192) / 8192 }
Condition66%2/3
$hi == 0 and $lo == 1
!ll&&!rl&&r
032
MC/DC50%1/2
$hi == 0 and $lo == 1
$hi == 0 $lo == 1
Truth table66%2/3
A and B
A= $hi == 0B= $lo == 1
ABresult
0X0
100
111
982 1 ▶ elsif ($hi == 0 && $lo == 2) { $c->{coarse} = $msb - 64 }
Condition66%2/3
$hi == 0 and $lo == 2
!ll&&!rl&&r
021
MC/DC50%1/2
$hi == 0 and $lo == 2
$hi == 0 $lo == 2
Truth table66%2/3
A and B
A= $hi == 0B= $lo == 2
ABresult
0X0
100
111
983 2 ▶ elsif ($hi == 0 && $lo == 5) { $c->{mod_range} = $msb * 100 + $lsb * 100 / 128 }
Condition33%1/3
$hi == 0 and $lo == 5
!ll&&!rl&&r
002
MC/DC0%0/2
$hi == 0 and $lo == 5
$hi == 0 $lo == 5
Truth table33%1/3
A and B
A= $hi == 0B= $lo == 5
ABresult
0X0
100
111
984 7 ▶ $c->{tune} = ($c->{coarse} // 0) + ($c->{fine} // 0);
Condition100%4/4
$c->{"coarse"} // 0
l!l
52
$c->{"fine"} // 0
l!l
43
MC/DC100%1/1
$c->{"coarse"} // 0
$c->{"coarse"}
MC/DC100%1/1
$c->{"fine"} // 0
$c->{"fine"}
Truth table100%2/2
A
A= $c->{"coarse"}
Aresult
11
00
Truth table100%2/2
A
A= $c->{"fine"}
Aresult
11
00
985 7 return;
986 }
987  
988 17 ▶ if ($hi == Peta::Synth::Arpeggiator::NRPN) {
Branch: 3 branches
BranchTrueFalseTotal
if ($hi == 125) { } 12 5 17
elsif ($hi == 1 and $NRPN_SOUND{$lo}) { } 1 4 5
elsif (my $what = $NRPN_KEY{$hi}) { } 4 0 4
989 12 ▶ my ($name, $setting) = Peta::Synth::Arpeggiator::nrpn($lo, $msb, $lsb) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = Peta::Synth::Arpeggiator::nrpn($lo, $msb, $lsb) or return 11 1 12
990 11 $self->_arp_set($c->{number}, $name => $setting, $at);
991 }
992 ▶ elsif ($hi == 1 && $NRPN_SOUND{$lo}) {
Condition66%2/3
$hi == 1 and $NRPN_SOUND{$lo}
!ll&&!rl&&r
401
MC/DC50%1/2
$hi == 1 and $NRPN_SOUND{$lo}
$hi == 1 $NRPN_SOUND{$lo}
Truth table66%2/3
A and B
A= $hi == 1B= $NRPN_SOUND{$lo}
ABresult
0X0
100
111
993 1 $c->{sound}{ $NRPN_SOUND{$lo} } = $msb;
994 }
995 elsif (my $what = $NRPN_KEY{$hi}) {
996 4 ▶ $c->{drum_keys}{$lo}{$what} = $what eq 'pitch' || $what eq 'fine' ? $msb - 64 : $msb;
Branch: 1 branch
BranchTrueFalseTotal
$what eq 'pitch' || $what eq 'fine' ? : 1 3 4
Condition66%2/3
$what eq "pitch" or $what eq "fine"
l!l&&r!l&&!r
103
MC/DC50%1/2
$what eq "pitch" or $what eq "fine"
$what eq "pitch" $what eq "fine"
Truth table66%2/3
A or B
A= $what eq "pitch"B= $what eq "fine"
ABresult
1X1
011
000
997 }
998 16 return;
999 }
1000  
1001 # System exclusive: the resets (GM, GM2, GS, XG), the master's volume,
1002 # balance and tuning (universal, GS, XG), GM2's controller destinations, and
1003 # a part made a drum part (GS, XG). $data is what follows F0.
1004 29 ▶sub _sysex ($self, $data, $at) {
Subroutine _sysex: called CC=15 SCAR=32.8
1005 29 ▶ my @b = unpack 'C*', $data // '';
Condition50%1/2
$data // ""
l!l
290
MC/DC0%0/1
$data // ""
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
1006 29 ▶ pop @b if @b && $b[-1] == 0xf7;
Branch: 1 branch
BranchTrueFalseTotal
if @b and $b[-1] == 247 29 0 29
Condition33%1/3
@b and $b[-1] == 247
!ll&&!rl&&r
0029
MC/DC0%0/2
@b and $b[-1] == 247
@b $b[-1] == 247
Truth table33%1/3
A and B
A= @bB= $b[-1] == 247
ABresult
0X0
100
111
1007 29 ▶ return if @b < 4;
Branch: 1 branch
BranchTrueFalseTotal
if @b < 4 0 29 29
1008  
1009 29 ▶ return $self->_universal($at, @b) if $b[0] == 0x7e || $b[0] == 0x7f;
Branch: 1 branch
BranchTrueFalseTotal
if $b[0] == 126 or $b[0] == 127 23 6 29
Condition100%3/3
$b[0] == 126 or $b[0] == 127
l!l&&r!l&&!r
6176
MC/DC100%2/2
$b[0] == 126 or $b[0] == 127
$b[0] == 126 $b[0] == 127
Truth table100%3/3
A or B
A= $b[0] == 126B= $b[0] == 127
ABresult
1X1
011
000
1010 6 ▶ return $self->_roland($at, @b) if $b[0] == 0x41 && @b >= 8 && $b[2] == 0x42 && $b[3] == 0x12;
Branch: 1 branch
BranchTrueFalseTotal
if $b[0] == 65 and @b >= 8 and $b[2] == 66 and $b[3] == 18 4 2 6
Condition66%6/9
$b[0] == 65 and @b >= 8 and $b[2] == 66 and $b[3] == 18
!ll&&!rl&&r
204
$b[0] == 65 and @b >= 8 and $b[2] == 66
!ll&&!rl&&r
204
$b[0] == 65 and @b >= 8
!ll&&!rl&&r
204
MC/DC25%1/4
$b[0] == 65 and @b >= 8 and $b[2] == 66 and $b[3] == 18
$b[0] == 65 @b >= 8 $b[2] == 66 $b[3] == 18
Truth table40%2/5
A and B and C and D
A= $b[0] == 65B= @b >= 8C= $b[2] == 66D= $b[3] == 18
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1011 2 ▶ return $self->_yamaha($at, @b) if $b[0] == 0x43 && @b >= 7 && ($b[1] & 0xf0) == 0x10 && $b[2] == 0x4c;
Branch: 1 branch
BranchTrueFalseTotal
if $b[0] == 67 and @b >= 7 and ($b[1] & 240) == 16 and $b[2] == 76 2 0 2
Condition33%3/9
$b[0] == 67 and @b >= 7 and ($b[1] & 240) == 16 and $b[2] == 76
!ll&&!rl&&r
002
$b[0] == 67 and @b >= 7 and ($b[1] & 240) == 16
!ll&&!rl&&r
002
$b[0] == 67 and @b >= 7
!ll&&!rl&&r
002
MC/DC0%0/4
$b[0] == 67 and @b >= 7 and ($b[1] & 240) == 16 and $b[2] == 76
$b[0] == 67 @b >= 7 ($b[1] & 240) == 16 $b[2] == 76
Truth table20%1/5
A and B and C and D
A= $b[0] == 67B= @b >= 7C= ($b[1] & 240) == 16D= $b[2] == 76
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1012 0 return;
1013 }
1014  
1015 # Universal messages: GM System On and Off, GM2 On (non-real time 09 01-03);
1016 # the master's volume, balance and tuning (real time 04); GM2's controller
1017 # destinations (real time 09).
1018 23 ▶sub _universal ($self, $at, @b) {
Subroutine _universal: called CC=17 SCAR=32.2
1019 23 my $master = $self->{master};
1020 23 ▶ my $word = sub ($i) { ($b[$i + 1] // 0) * 128 + ($b[$i] // 0) };
Subroutine __ANON__: called CC=3 SCAR=12.0
Condition50%2/4
$b[$i + 1] // 0
l!l
50
$b[$i] // 0
l!l
50
MC/DC0%0/1
$b[$i + 1] // 0
$b[$i + 1]
MC/DC0%0/1
$b[$i] // 0
$b[$i]
Truth table50%1/2
A
A= $b[$i + 1]
Aresult
11
00
Truth table50%1/2
A
A= $b[$i]
Aresult
11
00
1021  
1022 23 ▶ if ($b[0] == 0x7e && $b[2] == 0x09 && $b[3] >= 1 && $b[3] <= 3) {
Branch: 3 branches
BranchTrueFalseTotal
if ($b[0] == 126 and $b[2] == 9 and $b[3] >= 1 and $b[3] <= 3) { } 6 17 23
elsif ($b[0] == 127 and $b[2] == 4) { } 6 11 17
elsif ($b[0] == 127 and $b[2] == 9 and @b >= 5 and $b[3] >= 1 and $b[3] <= 3) { } 11 0 11
Condition66%6/9
$b[0] == 126 and $b[2] == 9 and $b[3] >= 1 and $b[3] <= 3
!ll&&!rl&&r
1706
$b[0] == 126 and $b[2] == 9 and $b[3] >= 1
!ll&&!rl&&r
1706
$b[0] == 126 and $b[2] == 9
!ll&&!rl&&r
1706
MC/DC25%1/4
$b[0] == 126 and $b[2] == 9 and $b[3] >= 1 and $b[3] <= 3
$b[0] == 126 $b[2] == 9 $b[3] >= 1 $b[3] <= 3
Truth table40%2/5
A and B and C and D
A= $b[0] == 126B= $b[2] == 9C= $b[3] >= 1D= $b[3] <= 3
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1023 6 $self->reset_channels($at);
1024 }
1025 ▶ elsif ($b[0] == 0x7f && $b[2] == 0x04) {
Condition66%2/3
$b[0] == 127 and $b[2] == 4
!ll&&!rl&&r
0116
MC/DC50%1/2
$b[0] == 127 and $b[2] == 4
$b[0] == 127 $b[2] == 4
Truth table66%2/3
A and B
A= $b[0] == 127B= $b[2] == 4
ABresult
0X0
100
111
1026 6 ▶ if ($b[3] == 1) { $master->{volume} = $word->(4) / 16383 }
Branch: 4 branches
BranchTrueFalseTotal
if ($b[3] == 1) { } 3 3 6
elsif ($b[3] == 2) { } 1 2 3
elsif ($b[3] == 3) { } 1 1 2
elsif ($b[3] == 4) { } 1 0 1
1027 1 elsif ($b[3] == 2) { $master->{balance} = ($word->(4) - 8192) / 8192 }
1028 1 elsif ($b[3] == 3) { $master->{fine} = ($word->(4) - 8192) / 8192 }
1029 1 ▶ elsif ($b[3] == 4) { $master->{coarse} = ($b[5] // 64) - 64 }
Condition50%1/2
$b[5] // 64
l!l
10
MC/DC0%0/1
$b[5] // 64
$b[5]
Truth table50%1/2
A
A= $b[5]
Aresult
11
00
1030 }
1031 ▶ elsif ($b[0] == 0x7f && $b[2] == 0x09 && @b >= 5 && $b[3] >= 1 && $b[3] <= 3) {
Condition33%4/12
$b[0] == 127 and $b[2] == 9 and @b >= 5 and $b[3] >= 1 and $b[3] <= 3
!ll&&!rl&&r
0011
$b[0] == 127 and $b[2] == 9 and @b >= 5 and $b[3] >= 1
!ll&&!rl&&r
0011
$b[0] == 127 and $b[2] == 9 and @b >= 5
!ll&&!rl&&r
0011
$b[0] == 127 and $b[2] == 9
!ll&&!rl&&r
0011
MC/DC0%0/5
$b[0] == 127 and $b[2] == 9 and @b >= 5 and $b[3] >= 1 and $b[3] <= 3
$b[0] == 127 $b[2] == 9 @b >= 5 $b[3] >= 1 $b[3] <= 3
Truth table16%1/6
A and B and C and D and E
A= $b[0] == 127B= $b[2] == 9C= @b >= 5D= $b[3] >= 1E= $b[3] <= 3
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
1032 11 $self->_destination(@b[ 3 .. $#b ]);
1033 }
1034 23 return;
1035 }
1036  
1037 # Roland GS (DT1 to a GS device): the reset, the master's volume and
1038 # coarse tuning, a part made a drum part.
1039 4 ▶sub _roland ($self, $at, @b) {
Subroutine _roland: called CC=11 SCAR=25.8
1040 4 my ($a1, $a2, $a3, $v) = @b[ 4 .. 7 ];
1041 4 ▶ return if $a1 != 0x40;
Branch: 1 branch
BranchTrueFalseTotal
if $a1 != 64 0 4 4
1042  
1043 4 ▶ if ($a2 == 0x00 && $a3 == 0x7f) { $self->reset_channels($at) }
Branch: 4 branches
BranchTrueFalseTotal
if ($a2 == 0 and $a3 == 127) { } 1 3 4
elsif ($a2 == 0 and $a3 == 4) { } 1 2 3
elsif ($a2 == 0 and $a3 == 5) { } 0 2 2
elsif (($a2 & 240) == 16 and $a3 == 21) { } 2 0 2
Condition100%3/3
$a2 == 0 and $a3 == 127
!ll&&!rl&&r
211
MC/DC100%2/2
$a2 == 0 and $a3 == 127
$a2 == 0 $a3 == 127
Truth table100%3/3
A and B
A= $a2 == 0B= $a3 == 127
ABresult
0X0
100
111
1044 1 ▶ elsif ($a2 == 0x00 && $a3 == 0x04) { $self->{master}{volume} = $v / 127 }
Condition66%2/3
$a2 == 0 and $a3 == 4
!ll&&!rl&&r
201
MC/DC50%1/2
$a2 == 0 and $a3 == 4
$a2 == 0 $a3 == 4
Truth table66%2/3
A and B
A= $a2 == 0B= $a3 == 4
ABresult
0X0
100
111
1045 0 ▶ elsif ($a2 == 0x00 && $a3 == 0x05) { $self->{master}{coarse} = $v - 64 }
Condition33%1/3
$a2 == 0 and $a3 == 5
!ll&&!rl&&r
200
MC/DC0%0/2
$a2 == 0 and $a3 == 5
$a2 == 0 $a3 == 5
Truth table33%1/3
A and B
A= $a2 == 0B= $a3 == 5
ABresult
0X0
100
111
1046 ▶ elsif (($a2 & 0xf0) == 0x10 && $a3 == 0x15) {
Condition33%1/3
($a2 & 240) == 16 and $a3 == 21
!ll&&!rl&&r
002
MC/DC0%0/2
($a2 & 240) == 16 and $a3 == 21
($a2 & 240) == 16 $a3 == 21
Truth table33%1/3
A and B
A= ($a2 & 240) == 16B= $a3 == 21
ABresult
0X0
100
111
1047 2 my $c = $self->{channels}[ $GS_PART[ $a2 & 0x0f ] ];
1048 2 $self->_notes_off($c, $at);
1049 2 ▶ $c->{drum} = $v ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 1 1 2
1050 }
1051 4 return;
1052 }
1053  
1054 # Yamaha XG (parameter change): the reset, the master's volume, a part made
1055 # a drum part.
1056 2 ▶sub _yamaha ($self, $at, @b) {
Subroutine _yamaha: called CC=11 SCAR=29.7
1057 2 my ($a1, $a2, $a3, $v) = @b[ 3 .. 6 ];
1058  
1059 2 ▶ if ($a1 == 0 && $a2 == 0 && $a3 == 0x7e) { $self->reset_channels($at) }
Branch: 3 branches
BranchTrueFalseTotal
if ($a1 == 0 and $a2 == 0 and $a3 == 126) { } 1 1 2
elsif ($a1 == 0 and $a2 == 0 and $a3 == 4) { } 0 1 1
elsif ($a1 == 8 and $a2 < 16 and $a3 == 7) { } 1 0 1
Condition66%4/6
$a1 == 0 and $a2 == 0 and $a3 == 126
!ll&&!rl&&r
101
$a1 == 0 and $a2 == 0
!ll&&!rl&&r
101
MC/DC33%1/3
$a1 == 0 and $a2 == 0 and $a3 == 126
$a1 == 0 $a2 == 0 $a3 == 126
Truth table50%2/4
A and B and C
A= $a1 == 0B= $a2 == 0C= $a3 == 126
ABCresult
0XX0
10X0
1100
1111
1060 0 ▶ elsif ($a1 == 0 && $a2 == 0 && $a3 == 0x04) { $self->{master}{volume} = $v / 127 }
Condition33%2/6
$a1 == 0 and $a2 == 0 and $a3 == 4
!ll&&!rl&&r
100
$a1 == 0 and $a2 == 0
!ll&&!rl&&r
100
MC/DC0%0/3
$a1 == 0 and $a2 == 0 and $a3 == 4
$a1 == 0 $a2 == 0 $a3 == 4
Truth table25%1/4
A and B and C
A= $a1 == 0B= $a2 == 0C= $a3 == 4
ABCresult
0XX0
10X0
1100
1111
1061 ▶ elsif ($a1 == 0x08 && $a2 < 16 && $a3 == 0x07) {
Condition33%2/6
$a1 == 8 and $a2 < 16 and $a3 == 7
!ll&&!rl&&r
001
$a1 == 8 and $a2 < 16
!ll&&!rl&&r
001
MC/DC0%0/3
$a1 == 8 and $a2 < 16 and $a3 == 7
$a1 == 8 $a2 < 16 $a3 == 7
Truth table25%1/4
A and B and C
A= $a1 == 8B= $a2 < 16C= $a3 == 7
ABCresult
0XX0
10X0
1100
1111
1062 1 my $c = $self->{channels}[$a2];
1063 1 $self->_notes_off($c, $at);
1064 1 ▶ $c->{drum} = $v ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 1 0 1
1065 }
1066 2 return;
1067 }
1068  
1069 # GM2's controller destination setting (universal real-time 09): a source
1070 # (1 channel pressure, 2 key pressure, 3 a controller, 1-31 or 64-95) of a
1071 # channel, given [parameter, range] pairs: 0 pitch (40-88: -24 .. +24
1072 # semitones), 1 the filter's cutoff (-9600 .. +9450 cents), 2 amplitude
1073 # (0 .. 127/64 times), 3, 4, 5 the LFO's depth on pitch (0 .. 600 cents),
1074 # on the cutoff (0 .. 2400 cents), on amplitude (0 .. 100 %); each at full
1075 # from the source at 127. The ranges not sent are their defaults: 64, 64,
1076 # 64, 0, 0, 0 (nothing).
1077 16 my @DEST_DEFAULT = (64, 64, 64, 0, 0, 0);
1078  
1079 11 ▶sub _destination ($self, $kind, $channel, @pairs) {
Subroutine _destination: called CC=15 SCAR=27.8
1080 11 ▶ my $c = $self->{channels}[$channel] // return;
Condition50%1/2
$self->{"channels"}[$channel] // (return)
l!l
110
MC/DC0%0/1
$self->{"channels"}[$channel] // (return)
$self->{"channels"}[$channel]
Truth table50%1/2
A
A= $self->{"channels"}[$channel]
Aresult
11
00
1081  
1082 11 my $source;
1083 11 ▶ if ($kind == 1) { $source = 'chp' }
Branch: 2 branches
BranchTrueFalseTotal
if ($kind == 1) { } 7 4 11
elsif ($kind == 2) { } 1 3 4
1084 1 elsif ($kind == 2) { $source = 'kp' }
1085 else {
1086 3 ▶ $source = shift @pairs // return;
Condition50%1/2
shift @pairs // (return)
l!l
30
MC/DC0%0/1
shift @pairs // (return)
shift @pairs
Truth table50%1/2
A
A= shift @pairs
Aresult
11
00
1087 3 ▶ return if !($source >= 1 && $source <= 31 || $source >= 64 && $source <= 95);
Branch: 1 branch
BranchTrueFalseTotal
unless $source >= 1 and $source <= 31 or $source >= 64 and $source <= 95 1 2 3
Condition55%5/9
$source >= 1 and $source <= 31 or $source >= 64 and $source <= 95
l!l&&r!l&&!r
201
$source >= 1 and $source <= 31
!ll&&!rl&&r
012
$source >= 64 and $source <= 95
!ll&&!rl&&r
100
MC/DC25%1/4
$source >= 1 and $source <= 31 or $source >= 64 and $source <= 95
$source >= 1 $source <= 31 $source >= 64 $source <= 95
Truth table28%2/7
A and B or C and D
A= $source >= 1B= $source <= 31C= $source >= 64D= $source <= 95
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1088 }
1089  
1090 10 ▶ my $d = $c->{dest}{$source} //= [@DEST_DEFAULT];
Condition100%2/2
$c->{"dest"}{$source} //= [@DEST_DEFAULT]
l!l
19
MC/DC100%1/1
$c->{"dest"}{$source} //= [@DEST_DEFAULT]
$c->{"dest"}{$source}
Truth table100%2/2
A
A= $c->{"dest"}{$source}
Aresult
11
00
1091 10 ▶ while (@pairs >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pairs >= 2) 11 10 21
1092 11 my ($parameter, $range) = splice @pairs, 0, 2;
1093 11 ▶ next if $parameter > 5;
Branch: 1 branch
BranchTrueFalseTotal
if $parameter > 5 0 11 11
1094 11 ▶ if ($parameter == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter == 0) 5 6 11
1095 5 ▶ $range = 40 if $range < 40;
Branch: 1 branch
BranchTrueFalseTotal
if $range < 40 0 5 5
1096 5 ▶ $range = 88 if $range > 88;
Branch: 1 branch
BranchTrueFalseTotal
if $range > 88 1 4 5
1097 }
1098 11 $d->[$parameter] = $range;
1099 }
1100 10 return;
1101 }
1102  
1103 # A destination's ranges in the units the voices take, at full: semitones,
1104 # octaves of cutoff, amplitude less 1, cents, cents, the amplitude's depth.
1105 11 ▶sub _dest_units ($d) {
Subroutine _dest_units: called CC=1 SCAR=0.0
1106 11 return ($d->[0] - 64, ($d->[1] - 64) / 8, $d->[2] / 64 - 1, $d->[3] * 600 / 127, $d->[4] * 2400 / 127, $d->[5] / 127);
1107 }
1108  
1109 # What a channel's destinations come to now, from its pressure and
1110 # controllers (key pressure is the voices'): [pitch (semitones), cutoff
1111 # (octaves), amplitude (a factor), the LFO's pitch depth (cents), cutoff
1112 # depth (cents), amplitude depth (0 .. 1)]; undef where none is set.
1113 15936 ▶sub _destinations ($c) {
Subroutine _destinations: called CC=6 SCAR=17.9
1114 15936 my $dest = $c->{dest};
1115 15936 my @sources = grep { $_ ne 'kp' } keys %$dest;
1116 15936 ▶ return undef if !@sources;
Branch: 1 branch
BranchTrueFalseTotal
unless @sources 15926 10 15936
1117  
1118 10 my @sum = (0.0, 0.0, 1.0, 0.0, 0.0, 0.0);
1119 10 for my $source (@sources) {
1120 10 ▶ my $v = $source eq 'chp' ? $c->{pressure} : ($c->{cc}{$source} // 0) / 127;
Branch: 1 branch
BranchTrueFalseTotal
$source eq 'chp' ? : 8 2 10
Condition50%1/2
$c->{"cc"}{$source} // 0
l!l
20
MC/DC0%0/1
$c->{"cc"}{$source} // 0
$c->{"cc"}{$source}
Truth table50%1/2
A
A= $c->{"cc"}{$source}
Aresult
11
00
1121 10 my @u = _dest_units($dest->{$source});
1122 10 $sum[$_] += $v * $u[$_] for 0, 1, 3, 4, 5;
1123 10 $sum[2] *= 1 + $v * $u[2];
1124 }
1125 10 return \@sum;
1126 }
1127  
1128 # The channel's gains: left, right (constant power) and reverb send; its
1129 # level and pan (-1 .. 1).
1130 15936 ▶sub _gains ($self, $c) {
Subroutine _gains: called CC=2 SCAR=7.0
1131 15936 my $level = ($c->{volume} / 127)**2 * ($c->{expression} / 127)**2;
1132 15936 ▶ my $pan = $c->{pan} <= 64 ? ($c->{pan} - 64) / 64 : ($c->{pan} - 64) / 63;
Branch: 1 branch
BranchTrueFalseTotal
$c->{'pan'} <= 64 ? : 15936 0 15936
1133 15936 my ($l, $r) = Peta::Synth::Voice::pan_law($pan);
1134 15936 return ($level * $l, $level * $r, $level * $c->{send} / 127, $level, $pan);
1135 }
1136  
1137 # The next $n samples ($n a multiple of 64), as two strings of packed floats.
1138 0 ▶sub render ($self, $n) {
Subroutine render: not called CC=1 SCAR=6.9
Pod documented
1139 0 my ($left, $right) = $self->render_arrays($n);
1140 0 return (pack('f*', @$left), pack('f*', @$right));
1141 }
1142  
1143 # A whole sound: the next $frames samples, rendered in quanta of $quantum
1144 # (the rest of the last quantum is dropped).
1145 356 ▶sub render_frames ($self, $frames, $quantum = 512) {
Subroutine render_frames: called CC=3 SCAR=11.0
Pod documented
1146 356 my (@left, @right);
1147 356 ▶ while (@left < $frames) {
Branch: 1 branch
BranchTrueFalseTotal
if (@left < $frames) 9528 356 9884
1148 9528 my ($l, $r) = $self->render_arrays($quantum);
1149 9528 push @left, @$l;
1150 9528 push @right, @$r;
1151 }
1152 356 $#left = $#right = $frames - 1;
1153 356 return (\@left, \@right);
1154 }
1155  
1156 # What a channel is, at a block's edge, for its voices' controls (see
1157 # Peta::Synth::Voice::controls): each an array over the quantum's edges.
1158 16 my @EDGE_FIELDS = qw(gl gr gs lv pn bend mod wheel breath foot chp cc74 cc71);
1159  
1160 # The next $n samples ($n a multiple of 64), as two array references.
1161 15486 ▶sub render_arrays ($self, $n) {
Subroutine render_arrays: called CC=31 SCAR=34.4
Pod documented
1162 15486 my $B = BLOCK;
1163 15486 my $nb = $n / $B;
1164 15486 ▶ die "a quantum is a multiple of $B samples\n" if $nb != int $nb;
Branch: 1 branch
BranchTrueFalseTotal
if $nb != int $nb 0 15486 15486
1165  
1166 15486 my $rate = $self->{rate};
1167 15486 my $now = $self->{now};
1168 15486 my $queue = $self->{queue};
1169 15486 $self->{until} = $now + $n;
1170 15486 ▶ $self->_arpeggiate($now, $now + $n) if @{ $self->{arps} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"arps"};} 374 15112 15486
1171  
1172 # Silence, while nothing sounds and nothing is about to: no voice, no
1173 # event in this quantum, the reverb and the equalizer rung out, the
1174 # limiter open.
1175 15486 ▶ if (!@{ $self->{active} } && !(@$queue && $queue->[0][0] < $now + $n - $B / 2) && $self->_at_rest) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @{$self->{"active"};} || @$queue && $queue->[0][0] < $now + $n - $B / 2 and $self->_at_rest) 499 14987 15486
Condition100%9/9
not @{$self->{"active"};} || @$queue && $queue->[0][0] < $now + $n - $B / 2 and $self->_at_rest
!ll&&!rl&&r
14702285499
@{$self->{'active'};} || @$queue && $queue->[0][0] < $now + $n - $B / 2
l!l&&r!l&&!r
14296406784
@$queue and $queue->[0][0] < $now + $n - $B / 2
!ll&&!rl&&r
70876406
MC/DC100%4/4
not @{$self->{"active"};} || @$queue && $queue->[0][0] < $now + $n - $B / 2 and $self->_at_rest
@{$self->{'active'};} @$queue $queue->[0][0] < $now + $n - $B / 2 $self->_at_rest
Truth table100%6/6
not(A || B and C) and D
A= @{$self->{'active'};}B= @$queueC= $queue->[0][0] < $now + $n - $B / 2D= $self->_at_rest
ABCDresult
1XXX0
011X0
00X00
01000
00X11
01011
1176 499 $self->{reverb}->clear;
1177 499 $self->{eq}->clear;
1178 499 $self->{now} = $now + $n;
1179 499 delete $self->{held_back};
1180 499 return ([ (0.0) x $n ], [ (0.0) x $n ]);
1181 }
1182  
1183 14987 $_->{first} = 0 for @{ $self->{active} };
1184  
1185 # The events, block by block. What the channels are changes only after
1186 # an event: from edge j on, as $state (the channels' states, made again
1187 # after an event).
1188 14987 my @runs; # [ the first edge, the states from it ]
1189 14987 for my $j (0 .. $nb) {
1190 144966 ▶ if ($j < $nb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $nb) 129979 14987 144966
1191 129979 my $edge = $now + $j * $B;
1192 129979 ▶ while (@$queue && $queue->[0][0] < $edge + $B / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue and $queue->[0][0] < $edge + $B / 2) 1581 129979 131560
Condition100%3/3
@$queue and $queue->[0][0] < $edge + $B / 2
!ll&&!rl&&r
81694482851581
MC/DC100%2/2
@$queue and $queue->[0][0] < $edge + $B / 2
@$queue $queue->[0][0] < $edge + $B / 2
Truth table100%3/3
A and B
A= @$queueB= $queue->[0][0] < $edge + $B / 2
ABresult
0X0
100
111
1193 1581 my ($at, $event) = @{ shift @$queue };
1194 1581 my $before = @{ $self->{active} };
1195 1581 $self->_apply($event, $edge);
1196 1581 $_->{first} = $j for @{ $self->{active} }[ $before .. $#{ $self->{active} } ];
1197 }
1198 }
1199 144966 ▶ my $state = $self->{state} //= [ map { $self->_channel_state($_) } @{ $self->{channels} } ];
Condition100%2/2
$self->{"state"} //= [map({$self->_channel_state($_);} @{$$self{"channels"};})]
l!l
143970996
MC/DC100%1/1
$self->{"state"} //= [map({$self->_channel_state($_);} @{$$self{"channels"};})]
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
Aresult
11
00
1200 144966 ▶ push @runs, [ $j, $state ] if !@runs || $runs[-1][1] != $state;
Branch: 1 branch
BranchTrueFalseTotal
if not @runs or $runs[-1][1] != $state 15474 129492 144966
Condition100%3/3
not @runs or $runs[-1][1] != $state
l!l&&r!l&&!r
14987487129492
MC/DC100%2/2
not @runs or $runs[-1][1] != $state
not @runs $runs[-1][1] != $state
Truth table100%3/3
not(A) or B
A= not @runsB= $runs[-1][1] != $state
ABresult
1X1
011
000
1201 }
1202 14987 my @snap = map { _edges($_, \@runs, $nb) } 0 .. 15;
1203  
1204 # The vibrato's rate, depth and delay (CC76-78), the modulation range and
1205 # key pressure's destinations, as they are at the quantum's end.
1206 14987 for my $number (0 .. 15) {
1207 239792 my $c = $self->{channels}[$number];
1208 239792 my $sound = $c->{sound};
1209 239792 my $s = $snap[$number];
1210 239792 $s->{vrate} = 2**(($sound->{76} - 64) / 32);
1211 239792 $s->{vdepth} = 2**(($sound->{77} - 64) / 16);
1212 239792 $s->{vdelay} = 2**(($sound->{78} - 64) / 32);
1213 239792 $s->{modrange} = $c->{mod_range};
1214 239792 ▶ $s->{kp} = [ _dest_units($c->{dest}{kp}) ] if $c->{dest}{kp};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"dest"}{"kp"} 1 239791 239792
1215 }
1216  
1217 14987 my $active = $self->{active};
1218 14987 $_->controls($now, $nb, $B, $snap[ $_->{channel} ]) for @$active;
1219  
1220 14987 my $stats = $self->{stats};
1221 14987 ▶ $stats->{peak_voices} = @$active if @$active > $stats->{peak_voices};
Branch: 1 branch
BranchTrueFalseTotal
if @$active > $stats->{"peak_voices"} 501 14486 14987
1222 14987 for my $voice (@$active) {
1223 17076 $stats->{voice_blocks} += $nb - $voice->{first};
1224 17076 $stats->{partial_blocks} += ($nb - $voice->{first}) * @{ $voice->{ratio} };
1225 }
1226  
1227 # The reverb where the engine can run it (on the card), else here; not at
1228 # all while it has rung out and no voice sends to it.
1229 14987 my $engine = $self->{engine};
1230 14987 my ($dry_left, $dry_right, $send, $wet_left, $wet_right);
1231 14987 ▶ if ($self->{reverb}->quiet && !grep { grep { $_ != 0 } @{ $_->{gs} } } @$active) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'reverb'}->quiet and not grep({grep {$_ != 0;} @{$$_{'gs'};};} @$active)) { } 9084 5903 14987
elsif ($engine->can('render_reverb')) { } 0 5903 5903
Condition100%3/3
$self->{"reverb"}->quiet and not grep({grep {$_ != 0;} @{$$_{"gs"};};} @$active)
!ll&&!rl&&r
50198849084
MC/DC100%2/2
$self->{"reverb"}->quiet and not grep({grep {$_ != 0;} @{$$_{"gs"};};} @$active)
$self->{"reverb"}->quiet not grep({grep {$_ != 0;} @{$$_{"gs"};};} @$active)
Truth table100%3/3
A and not(B)
A= $self->{"reverb"}->quietB= not grep({grep {$_ != 0;} @{$$_{"gs"};};} @$active)
ABresult
0X0
100
111
1232 9084 $self->{reverb}->clear;
1233 9084 ($dry_left, $dry_right, $send) = $engine->render($active, $n, $rate);
1234 }
1235 elsif ($engine->can('render_reverb')) {
1236 0 ($dry_left, $dry_right, $send, $wet_left, $wet_right) = $engine->render_reverb($active, $n, $rate, $self->{reverb});
1237 }
1238 else {
1239 5903 ($dry_left, $dry_right, $send) = $engine->render($active, $n, $rate);
1240 5903 ($wet_left, $wet_right) = $self->{reverb}->process($send);
1241 }
1242 14987 ▶ my ($left, $right) = $self->_master($n, $dry_left, $dry_right, $wet_left // $send, $wet_right // $send, $wet_left ? 1.0 : 0.0);
Branch: 1 branch
BranchTrueFalseTotal
$wet_left ? : 5083 9904 14987
Condition66%4/6
$wet_left // $send
l!l&&r!l&&!r
508399040
$wet_right // $send
l!l&&r!l&&!r
508399040
MC/DC0%0/2
$wet_left // $send
$wet_left $send
MC/DC0%0/2
$wet_right // $send
$wet_right $send
Truth table66%2/3
A // B
A= $wet_leftB= $send
ABresult
1X1
011
000
Truth table66%2/3
A // B
A= $wet_rightB= $send
ABresult
1X1
011
000
1243  
1244 14987 $self->{now} = $now + $n;
1245 14987 my $end = $self->{now};
1246  
1247 # The fidelity: a voice fallen under its quiet fades out (each voice
1248 # looked at every eighth quantum).
1249 14987 ▶ if (my $quiet = $FIDELITY{ $self->{fidelity} }{quiet}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $quiet = $FIDELITY{$self->{"fidelity"}}{"quiet"}) 2819 12168 14987
1250 2819 my $level = 10**($quiet / 20);
1251 2819 my $turn = $self->{turn}++ % 8;
1252 2819 for my $i (grep { $_ % 8 == $turn } 0 .. $#$active) {
1253 353 my $voice = $active->[$i];
1254 353 ▶ $voice->kill($end) if !defined $voice->{killed} && $voice->under(($end - $voice->{start}) / $rate, $level);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $voice->{"killed"} and $voice->under(($end - $voice->{"start"}) / $rate, $level) 4 349 353
Condition100%3/3
not defined $voice->{"killed"} and $voice->under(($end - $voice->{"start"}) / $rate, $level)
!ll&&!rl&&r
13484
MC/DC100%2/2
not defined $voice->{"killed"} and $voice->under(($end - $voice->{"start"}) / $rate, $level)
not defined $voice->{"killed"} $voice->under(($end - $voice->{"start"}) / $rate, $level)
Truth table100%3/3
not(A) and B
A= not defined $voice->{"killed"}B= $voice->under(($end - $voice->{"start"}) / $rate, $level)
ABresult
0X0
100
111
1255 }
1256 }
1257 14987 $self->_keep([ grep { !$_->done(($end - $_->{start}) / $rate) } @$active ]);
1258 14987 return ($left, $right);
1259 }
1260  
1261 784 ▶sub _at_rest ($self) {
Subroutine _at_rest: called CC=4 SCAR=14.9
1262 784 ▶ return $self->{reverb}->quiet && $self->{eq}->quiet && (!$self->{limiter} || $self->{limit} == 1.0);
Condition66%6/9
$self->{'reverb'}->quiet && $self->{'eq'}->quiet && (!$self->{'limiter'} || $self->{'limit'} == 1)
!ll&&!rl&&r
2850499
$self->{"reverb"}->quiet and $self->{"eq"}->quiet
!ll&&!rl&&r
2850499
not $self->{"limiter"} or $self->{"limit"} == 1
l!l&&r!l&&!r
470290
MC/DC25%1/4
$self->{'reverb'}->quiet && $self->{'eq'}->quiet && (!$self->{'limiter'} || $self->{'limit'} == 1)
$self->{"reverb"}->quiet $self->{"eq"}->quiet not $self->{"limiter"} $self->{"limit"} == 1
Truth table60%3/5
A and B && not(C) or D
A= $self->{"reverb"}->quietB= $self->{"eq"}->quietC= not $self->{"limiter"}D= $self->{"limit"} == 1
ABCDresult
0XXX0
10XX0
11000
111X1
11011
1263 }
1264  
1265 # Channel $number at the quantum's edges, from the runs of its states: an
1266 # array of $nb + 1 for each of @EDGE_FIELDS; and the controller
1267 # destinations' six, where any of the runs has them (the default where one
1268 # has not).
1269 239792 ▶sub _edges ($number, $runs, $nb) {
Subroutine _edges: called CC=9 SCAR=22.0
1270 239792 my %s;
1271 239792 for my $r (0 .. $#$runs) {
1272 247584 my ($from, $state) = @{ $runs->[$r] };
1273 247584 ▶ my $count = ($r < $#$runs ? $runs->[$r + 1][0] : $nb + 1) - $from;
Branch: 1 branch
BranchTrueFalseTotal
$r < $runs->$#* ? : 7792 239792 247584
1274 247584 my $v = $state->[$number];
1275 247584 push @{ $s{$_} }, ($v->{$_}) x $count for @EDGE_FIELDS;
1276 }
1277 239792 ▶ if (grep { $_->[1][$number]{dest} } @$runs) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_->[1][$number]{"dest"};} @$runs) 35 239757 239792
1278 35 for my $r (0 .. $#$runs) {
1279 35 my ($from, $state) = @{ $runs->[$r] };
1280 35 ▶ my $count = ($r < $#$runs ? $runs->[$r + 1][0] : $nb + 1) - $from;
Branch: 1 branch
BranchTrueFalseTotal
$r < $runs->$#* ? : 0 35 35
1281 35 ▶ my $d = $state->[$number]{dest} // [ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 ];
Condition50%1/2
$state->[$number]{"dest"} // [0, 0, 1, 0, 0, 0]
l!l
350
MC/DC0%0/1
$state->[$number]{"dest"} // [0, 0, 1, 0, 0, 0]
$state->[$number]{"dest"}
Truth table50%1/2
A
A= $state->[$number]{"dest"}
Aresult
11
00
1282 35 push @{ $s{dest}[$_] }, ($d->[$_]) x $count for 0 .. 5;
1283 }
1284 }
1285 239792 return \%s;
1286 }
1287  
1288 # A channel as its voices see it: its gains, level and pan; the bend with
1289 # the tunings (semitones); the modulation wheel and the pressure as cents
1290 # of vibrato (unless a controller destination says what they do); as
1291 # sources of modulation, the wheel, breath, foot, pressure (0 .. 1),
1292 # brightness and resonance (-1 .. 1); the controller destinations.
1293 15936 ▶sub _channel_state ($self, $c) {
Subroutine _channel_state: called CC=4 SCAR=13.9
1294 15936 my $master = $self->{master};
1295 15936 my ($gl, $gr, $gs, $lv, $pn) = $self->_gains($c);
1296 15936 ▶ my $mod = ($c->{dest}{1} ? 0 : $c->{mod}) + ($c->{dest}{chp} ? 0 : $c->{pressure});
Branch: 2 branches
BranchTrueFalseTotal
$c->{'dest'}{'1'} ? : 1 15935 15936
$c->{'dest'}{'chp'} ? : 8 15928 15936
1297  
1298 return {
1299 gl => $gl,
1300 gr => $gr,
1301 gs => $gs,
1302 lv => $lv,
1303 pn => $pn,
1304 bend => $c->{bend} * $c->{range} + $c->{tune} + ($master->{coarse} + $master->{fine}),
1305 mod => $c->{mod_range} * ($mod > 1 ? 1 : $mod),
1306 wheel => $c->{mod},
1307 breath => $c->{breath},
1308 foot => $c->{foot},
1309 chp => $c->{pressure},
1310 cc74 => ($c->{sound}{74} - 64) / 64,
1311 15936 ▶ cc71 => ($c->{sound}{71} - 64) / 64,
Branch: 1 branch
BranchTrueFalseTotal
$mod > 1 ? : 0 15936 15936
1312 dest => _destinations($c),
1313 };
1314 }
1315  
1316 # The voices still sounding; those that are done leave the channels' lists too.
1317 15011 ▶sub _keep ($self, $active) {
Subroutine _keep: called CC=5 SCAR=16.1
1318 15011 my $before = $self->{active};
1319 15011 $self->{active} = $active;
1320 15011 ▶ return if @$active == @$before;
Branch: 1 branch
BranchTrueFalseTotal
if @$active == @$before 14869 142 15011
1321  
1322 142 my %alive = map { $_ => 1 } @$active;
1323 142 for my $c (@{ $self->{channels} }) {
1324 2272 for my $key (keys %{ $c->{held} }) {
1325 177 my @still = grep { $alive{$_} } @{ $c->{held}{$key} };
1326 177 ▶ if (@still) { $c->{held}{$key} = \@still } else { delete $c->{held}{$key} }
Branch: 1 branch
BranchTrueFalseTotal
if (@still) { } 121 56 177
1327 }
1328 2272 $c->{pedal} = [ grep { $alive{$_} } @{ $c->{pedal} } ];
1329 }
1330 142 return;
1331 }
1332  
1333 # What the scheduled events will sound, without sounding them: every voice
1334 # they start, with the second it starts, the second it will be silent
1335 # ('inf': never) and its channel's program (undef on the drums). Event by
1336 # event as rendering takes them; before each, the voices silent by then
1337 # go (Peta::Synth::Voice's ends: rendering finds them done at the end of
1338 # the quantum after). The queue is used up. For planning: see
1339 # Peta::Synth::Profile.
1340 2 ▶sub foresee ($self) {
Subroutine foresee: called CC=9 SCAR=22.0
Pod documented
1341 2 my $rate = $self->{rate};
1342 2 my $queue = $self->{queue};
1343 2 my %program; # what each voice was started with
1344 my @voices;
1345 8 ▶ my $close = sub ($voice) {
Subroutine __ANON__: called CC=2 SCAR=7.1
1346 8 my $ends = $voice->ends;
1347 8 my $start = $voice->{start} / $rate;
1348 push @voices, {
1349 voice => $voice,
1350 8 ▶ program => delete $program{$voice},
Branch: 1 branch
BranchTrueFalseTotal
$ends eq 'inf' ? : 0 8 8
1351 start => $start,
1352 end => $ends eq 'inf' ? 'inf' : $start + $ends,
1353 };
1354 2 };
1355  
1356 2 ▶ while (@$queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue) 22 2 24
1357 22 my ($at, $event) = @{ shift @$queue };
1358  
1359 22 my (@keep, @gone);
1360 22 for my $voice (@{ $self->{active} }) {
1361 50 my $ends = $voice->ends;
1362 50 ▶ if ($ends ne 'inf' && $voice->{start} + $ends * $rate <= $at) { push @gone, $voice } else { push @keep, $voice }
Branch: 1 branch
BranchTrueFalseTotal
if ($ends ne 'inf' and $voice->{'start'} + $ends * $rate <= $at) { } 4 46 50
Condition100%3/3
$ends ne "inf" and $voice->{"start"} + $ends * $rate <= $at
!ll&&!rl&&r
12344
MC/DC100%2/2
$ends ne "inf" and $voice->{"start"} + $ends * $rate <= $at
$ends ne "inf" $voice->{"start"} + $ends * $rate <= $at
Truth table100%3/3
A and B
A= $ends ne "inf"B= $voice->{"start"} + $ends * $rate <= $at
ABresult
0X0
100
111
1363 }
1364 22 $close->($_) for @gone;
1365 22 $self->_keep(\@keep);
1366  
1367 22 my $before = @keep;
1368 22 $self->_apply($event, $at);
1369 22 my $active = $self->{active};
1370 22 for my $voice (@$active[ $before .. $#$active ]) {
1371 8 my $c = $self->{channels}[ $voice->{channel} ];
1372 8 ▶ $program{$voice} = $c->{drum} ? undef : $c->{program};
Branch: 1 branch
BranchTrueFalseTotal
$c->{'drum'} ? : 2 6 8
1373 }
1374 22 $self->{now} = $at;
1375 }
1376  
1377 2 $close->($_) for @{ $self->{active} };
1378 2 $self->_keep([]);
1379 2 return [ sort { $a->{start} <=> $b->{start} } @voices ];
1380 }
1381  
1382 # Dry and wet summed, the master gain, and a limiter that reacts within a
1383 # block and lets go over a quarter of a second; into fresh arrays (which is
1384 # what pperl's JIT compiles).
1385 14987 ▶sub _master ($self, $n, $dry_left, $dry_right, $wet_left, $wet_right, $wet) {
Subroutine _master: called CC=18 SCAR=28.9
1386 14987 my $B = BLOCK;
1387 14987 my $master = $self->{master};
1388 14987 my $gain = $self->{gain} * $master->{volume};
1389 14987 my $balance = $master->{balance};
1390 14987 ▶ my $gain_l = $balance > 0 ? $gain * (1 - $balance) : $gain;
Branch: 1 branch
BranchTrueFalseTotal
$balance > 0 ? : 1 14986 14987
1391 14987 ▶ my $gain_r = $balance < 0 ? $gain * (1 + $balance) : $gain;
Branch: 1 branch
BranchTrueFalseTotal
$balance < 0 ? : 0 14987 14987
1392  
1393 14987 my @left = (0.0) x $n;
1394 14987 my @right = (0.0) x $n;
1395 14987 for my $i (0 .. $n - 1) {
1396 8318656 $left[$i] = $gain_l * ($dry_left->[$i] + $wet * $wet_left->[$i]);
1397 8318656 $right[$i] = $gain_r * ($dry_right->[$i] + $wet * $wet_right->[$i]);
1398 }
1399 14987 ▶ if ($self->{eq}->active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"eq"}->active) 181 14806 14987
1400 181 my ($l, $r) = $self->{eq}->process(\@left, \@right);
1401 181 @left = @$l;
1402 181 @right = @$r;
1403 }
1404 14987 ▶ return (\@left, \@right) if !$self->{limiter};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"limiter"} 10411 4576 14987
1405  
1406 # The limiter: each block's peak brought under 0.98, the gain moving
1407 # linearly across a block, and a block ahead: the sound goes out a block
1408 # late (the last one held back till the next quantum), so that the gain
1409 # is down by the time a peak comes, not on its way down as it passes.
1410 4576 ▶ my $held = $self->{held_back} // [ [ (0.0) x $B ], [ (0.0) x $B ] ];
Condition100%2/2
$self->{"held_back"} // [[(0) x $B], [(0) x $B]]
l!l
452551
MC/DC100%1/1
$self->{"held_back"} // [[(0) x $B], [(0) x $B]]
$self->{"held_back"}
Truth table100%2/2
A
A= $self->{"held_back"}
Aresult
11
00
1411 4576 unshift @left, @{ $held->[0] };
1412 4576 unshift @right, @{ $held->[1] };
1413 4576 my $nb = $n / $B;
1414 4576 my @target;
1415 4576 for my $j (0 .. $nb) {
1416 43647 my $peak = 0.0;
1417 43647 for my $i ($j * $B .. $j * $B + $B - 1) {
1418 2793408 my $x = abs($left[$i]);
1419 2793408 ▶ $peak = $x if $x > $peak;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $peak 363726 2429682 2793408
1420 2793408 $x = abs($right[$i]);
1421 2793408 ▶ $peak = $x if $x > $peak;
Branch: 1 branch
BranchTrueFalseTotal
if $x > $peak 144867 2648541 2793408
1422 }
1423 43647 ▶ $target[$j] = $peak > 0.98 ? 0.98 / $peak : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$peak > 0.98 ? : 350 43297 43647
1424 }
1425 4576 my $limit = $self->{limit};
1426 4576 my $recover = exp(-$B / (0.25 * $self->{rate}));
1427 4576 for my $j (0 .. $nb - 1) {
1428 39071 my $next = 1.0 - (1.0 - $limit) * $recover;
1429 39071 ▶ $next = $target[$j] if $target[$j] < $next;
Branch: 1 branch
BranchTrueFalseTotal
if $target[$j] < $next 52 39019 39071
1430 39071 ▶ $next = $target[ $j + 1 ] if $target[ $j + 1 ] < $next;
Branch: 1 branch
BranchTrueFalseTotal
if $target[$j + 1] < $next 48 39023 39071
1431 39071 ▶ next if $limit == 1.0 && $next == 1.0;
Branch: 1 branch
BranchTrueFalseTotal
if $limit == 1 and $next == 1 38751 320 39071
Condition100%3/3
$limit == 1 and $next == 1
!ll&&!rl&&r
319138751
MC/DC100%2/2
$limit == 1 and $next == 1
$limit == 1 $next == 1
Truth table100%3/3
A and B
A= $limit == 1B= $next == 1
ABresult
0X0
100
111
1432  
1433 320 my $step = ($next - $limit) / $B;
1434 320 my $g = $limit;
1435 320 for my $i ($j * $B .. $j * $B + $B - 1) {
1436 20480 $left[$i] *= $g;
1437 20480 $right[$i] *= $g;
1438 20480 $g += $step;
1439 }
1440 320 $limit = $next;
1441 }
1442 4576 $self->{limit} = $limit;
1443 4576 $self->{held_back} = [ [ splice @left, $n ], [ splice @right, $n ] ];
1444 4576 return (\@left, \@right);
1445 }
1446  
1447 16 1;
1448  
1449 __END__
« blib/lib/Peta/MIDI/Device/In.pm ↑ Summary lib/Peta/Synth/Engine/Perl.pm »