lib/Peta/Synth/Server.pm

filter: Statementstmt 92.7% 487 / 525 Branchbran 66.0% 148 / 224 Conditioncond 56.4% 83 / 147 MC/DCmcdc 20.2% 16 / 79 Subroutinesub 92.6% 88 / 95 Podpod 100% 5 / 5 totaltotal 76.9% 827 / 1075 CC 190 SCAR 21.5
CC
190
Cov
80%
new
35.8
_synth
33.0
_save_recording
21.3
CRAP 8.6
« lib/Peta/Synth/Reverb.pm ↑ Summary lib/Peta/Synth/Arpeggiator.pm »
1 package Peta::Synth::Server;
2 # ABSTRACT: the synthesizer behind a web page
3  
4 # The synthesizer behind a web page: one process, one loop (Mojolicious's
5 # server and event loop, on EV where there is EV). It serves the page, talks
6 # to it over a WebSocket (JSON for control, binary frames of sound), and
7 # renders the synth just ahead of what the browsers have played.
8 #
9 # The clock is the listener's: a browser that plays the sound says how many
10 # frames it has played; the server keeps every listener `ahead` seconds in
11 # front of that and renders nothing while nobody listens.
12 #
13 # Messages from the page (JSON, by 'type'):
14 #
15 # hello -> init
16 # note {channel key velocity on} a key pressed or let go
17 # cc {channel number value} bend {channel value}
18 # program {channel program bank} a bank (GS's) selected first, if given
19 # midi {data} a device's message, its bytes (Web MIDI)
20 # mute {channel on}
21 # patch_get {kind number bank} -> patch (kind: program or drum; bank: a
22 # program's variation, by GS bank, or a drum's kit)
23 # patch_set {kind number field value} changes a field, for the next notes; formants,
24 # partials and mods take lists (partials null:
25 # the harmonic series)
26 # patch_reset {kind number bank} -> patch
27 # reverb {name value} time size damp level predelay on
28 # eq {band what value} the master equalizer: band low mid1 mid2 high,
29 # what freq gain q
30 # channel_eq {channel band what value} a channel's equalizer, the same bands
31 # arp {channel name value} a channel's arpeggiator: on mode rate tempo
32 # octaves gate swing latch
33 # let_go {channel what} a setting held against the file let go (what:
34 # program, cc 7, arp mode ...; none: all the channel's)
35 # synth {name value} partials voices gain auto fidelity (low mid high)
36 # play {file position} a file from the start, or from a position (seconds)
37 # pause {on} seek {position} stop panic
38 # reset all as the server started, the instruments
39 # as changed -> init (to every page)
40 # files -> files
41 # record {on} what is played live into a MIDI file -> recorded
42 # bank_save -> saved
43 # audio {on} the page plays the sound (or not)
44 # played {frames} how much it has played
45 #
46 # To the page: init, state (five times a second), patch, files, saved,
47 # recorded, error, and binary frames: interleaved stereo 32-bit floats.
48  
49 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use Mojolicious;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Mojo::Server::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use File::Basename qw(dirname basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 2 ▶use Peta::Synth::Synth;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use Peta::Synth::MIDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 2 ▶use Peta::Synth::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 ▶use Peta::Synth::Patch;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 2 ▶use Peta::Synth::EQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 2 ▶use Peta::Synth::Output;
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 2 our $VERSION = '0.2610180';
68  
69 2 my $MAX_UPLOAD = 4 * 1024 * 1024;
70 2 my $MAX_BACKLOG = 2 * 1024 * 1024; # bytes queued for a client before its sound is dropped
71 2 my $MIDI_FILE = qr/\.(?:mid|midi|kar|rmi)\z/i;
72 2 my $NUMBER = qr/\A\s*-?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?\s*\z/;
73  
74 # The reverb's settings, each with its range.
75 2 my %REVERB = (
76 time => [ 0.1, 20 ],
77 size => [ 0.2, 3 ],
78 damp => [ 0, 0.95 ],
79 level => [ 0, 2 ],
80 predelay => [ 0, 0.2 ],
81 on => [ 0, 1 ],
82 );
83  
84 # The lists of rows: the most rows, and each column's range. Formants are
85 # [centre Hz, gain, width in octaves]; partials [ratio, amplitude, decay factor].
86 2 my %LIST = (
87 formants => [ 8, [ 20, 20000 ], [ -1, 10 ], [ 0.05, 4 ] ],
88 partials => [ 64, [ 0.05, 64 ], [ 0, 1 ], [ 0, 20 ] ],
89 );
90  
91 1 ▶sub new ($class, %arg) {
Subroutine new: called CC=15 SCAR=35.8
Pod documented
92 1 ▶ my $rate = $arg{rate} // 48000;
Condition50%1/2
$arg{"rate"} // 48000
l!l
10
MC/DC0%0/1
$arg{"rate"} // 48000
$arg{"rate"}
Truth table50%1/2
A
A= $arg{"rate"}
Aresult
11
00
93 1 ▶ my $partials = $arg{partials} // 48;
Condition50%1/2
$arg{"partials"} // 48
l!l
10
MC/DC0%0/1
$arg{"partials"} // 48
$arg{"partials"}
Truth table50%1/2
A
A= $arg{"partials"}
Aresult
11
00
94 my $self = bless {
95 host => $arg{host} // '127.0.0.1',
96 port => $arg{port} // 8360,
97 rate => $rate,
98 quantum => $arg{quantum} // 512,
99 ahead => $arg{ahead} // 0.12,
100 midi_dir => $arg{midi_dir} // '.',
101 bank => $arg{bank},
102 partials => $partials,
103 auto => $arg{auto} // 1,
104 fidelity => $arg{fidelity} // 'mid',
105 0 ▶ log => $arg{log} // sub ($line) { say STDERR $line },
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 1 ▶ web_dir => File::Spec->catdir(dirname(__FILE__), 'Server', 'web'),
Condition47%8/17
$arg{"host"} // "127.0.0.1"
l!l
01
$arg{"port"} // 8360
l!l
10
$arg{"quantum"} // 512
l!l
01
$arg{"ahead"} // "0.12"
l!l
01
$arg{"midi_dir"} // "."
l!l
10
$arg{"auto"} // 1
l!l
01
$arg{"fidelity"} // "mid"
l!l
01
$arg{"log"} // sub ($line) { say STDERR $line; }
l!l&&r!l&&!r
100
MC/DC0%0/1
$arg{"host"} // "127.0.0.1"
$arg{"host"}
MC/DC0%0/1
$arg{"port"} // 8360
$arg{"port"}
MC/DC0%0/1
$arg{"quantum"} // 512
$arg{"quantum"}
MC/DC0%0/1
$arg{"ahead"} // "0.12"
$arg{"ahead"}
MC/DC0%0/1
$arg{"midi_dir"} // "."
$arg{"midi_dir"}
MC/DC0%0/1
$arg{"auto"} // 1
$arg{"auto"}
MC/DC0%0/1
$arg{"fidelity"} // "mid"
$arg{"fidelity"}
MC/DC0%0/2
$arg{"log"} // sub ($line) { say STDERR $line; }
$arg{"log"} sub ($line) { say STDERR $line; }
Truth table50%1/2
A
A= $arg{"host"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"port"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"quantum"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"ahead"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"midi_dir"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"auto"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"fidelity"}
Aresult
11
00
Truth table33%1/3
A // B
A= $arg{"log"}B= sub ($line) { say STDERR $line; }
ABresult
1X1
011
000
107 clients => {},
108 json => JSON::PP->new->canonical->allow_nonref,
109 playing => undef,
110 recording => undef,
111 }, $class;
112  
113 1 ▶ if (defined $arg{in}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{"in"}) 0 1 1
114 # MIDI as it is played, besides the page's (see Peta::Synth::Input).
115 0 require Peta::Synth::Input;
116 0 $self->{input} = Peta::Synth::Input->open($arg{in});
117 }
118 1 ▶ my $engine = Peta::Synth::Engine::create($arg{engine} // 'auto', rate => $rate, partials => $partials);
Condition50%1/2
$arg{"engine"} // "auto"
l!l
10
MC/DC0%0/1
$arg{"engine"} // "auto"
$arg{"engine"}
Truth table50%1/2
A
A= $arg{"engine"}
Aresult
11
00
119 1 $self->{start} = { partials => $partials, auto => $self->{auto} }; # what Reset comes back to
120 1 $self->{synth} = $self->_new_synth(undef, engine => $engine);
121 1 ▶ $self->{log}->("instrument changes loaded from $self->{bank}") if $self->{bank} && -e $self->{bank};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"bank"} and -e $self->{"bank"} 0 1 1
Condition33%1/3
$self->{"bank"} and -e $self->{"bank"}
!ll&&!rl&&r
010
MC/DC0%0/2
$self->{"bank"} and -e $self->{"bank"}
$self->{"bank"} -e $self->{"bank"}
Truth table33%1/3
A and B
A= $self->{"bank"}B= -e $self->{"bank"}
ABresult
0X0
100
111
122 1 return $self;
123 }
124  
125 # The synthesizer as the server starts it: the bank's settings, and its
126 # instruments' changes unless others are given (as edits has them); %arg:
127 # the engine, waves.
128 2 ▶sub _new_synth ($self, $instruments, %arg) {
Subroutine _new_synth: called CC=3 SCAR=11.0
129 2 ▶ my $bank = $self->_bank // {};
Condition100%2/2
$self->_bank // {}
l!l
11
MC/DC100%1/1
$self->_bank // {}
$self->_bank
Truth table100%2/2
A
A= $self->_bank
Aresult
11
00
130 2 ▶ $instruments //= $bank;
Condition66%2/3
$instruments //= $bank
l!l&&r!l&&!r
110
MC/DC0%0/2
$instruments //= $bank
$instruments $bank
Truth table66%2/3
A //= B
A= $instrumentsB= $bank
ABresult
1X1
011
000
131 2 my $synth = Peta::Synth::Synth->new(rate => $self->{rate}, partials => $self->{start}{partials}, fidelity => $self->{fidelity}, %arg);
132 2 $synth->apply_edits({ master => $bank->{master}, programs => $instruments->{programs}, drums => $instruments->{drums} });
133 2 return $synth;
134 }
135  
136 # The bank file's changes, if there is one.
137 2 ▶sub _bank ($self) { return $self->{bank} && -e $self->{bank} ? $self->_read_json($self->{bank}) : undef }
Subroutine _bank: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bank'} && -e $self->{'bank'} ? : 1 1 2
Condition66%2/3
$self->{"bank"} and -e $self->{"bank"}
!ll&&!rl&&r
011
MC/DC50%1/2
$self->{"bank"} and -e $self->{"bank"}
$self->{"bank"} -e $self->{"bank"}
Truth table66%2/3
A and B
A= $self->{"bank"}B= -e $self->{"bank"}
ABresult
0X0
100
111
138  
139 0 ▶sub synth ($self) { return $self->{synth} }
Subroutine synth: not called CC=1 SCAR=6.9
Pod documented
140  
141 1 ▶sub url ($self) { return "http://$self->{host}:$self->{port}/" }
Subroutine url: called CC=1 SCAR=0.0
Pod documented
142  
143 1 ▶sub run ($self) {
Subroutine run: called CC=7 SCAR=21.1
Pod documented
144 1 my $loop = Mojo::IOLoop->singleton;
145 1 my $daemon = Mojo::Server::Daemon->new(app => $self->_app, listen => ["http://$self->{host}:$self->{port}"], silent => 1);
146 1 ▶ eval { $daemon->start; 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $daemon->start; 1 } } or die qq[cannot listen on $self->{"host"}:$self->{"port"}: ] . ($@ =~ s/^Can't create listen socket: //ru) =~ s/ at \S+ line \d+.*//rsu . "\n" 1 0 1
147 or die "cannot listen on $self->{host}:$self->{port}: " . ($@ =~ s/^Can't create listen socket: //r =~ s/ at \S+ line \d+.*//sr) . "\n";
148 1 $self->{log}->('Peta::Synth on ' . $self->url . ' (engine ' . $self->{synth}->engine->name . ')');
149  
150 1 my $in = $self->{input};
151 1 ▶ if ($in) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in) 0 1 1
152 0 ▶ $loop->reactor->io($in->handle => sub { $self->_live($_) for $in->events })->watch($in->handle, 1, 0);
Subroutine __ANON__: not called CC=2 SCAR=17.9
153 0 my @from = $in->connected;
154 0 ▶ $self->{log}->('MIDI in at ' . $in->address . (@from ? ' from ' . join(', ', @from) : ''));
Branch: 1 branch
BranchTrueFalseTotal
@from ? : 0 0 0
155 }
156  
157 # Render while the listeners are less than 'ahead' in front, a few quanta at a time.
158 my @timers = (
159 1737 ▶ $loop->recurring(0.005 => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
160 1737 for (1 .. 4) {
161 1929 ▶ last unless $self->_behind;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_behind 1673 256 1929
162 256 $self->_render;
163 }
164 }),
165 1 ▶ $loop->recurring(0.2 => sub { $self->_broadcast_state }),
Subroutine __ANON__: called CC=1 SCAR=0.0
166 );
167 1 ▶ $loop->start unless $self->{quit};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quit"} 1 0 1
168  
169 1 $loop->remove($_) for @timers;
170 1 ▶ $loop->reactor->remove($in->handle) if $in;
Branch: 1 branch
BranchTrueFalseTotal
if $in 0 1 1
171 1 $daemon->stop;
172 1 return;
173 }
174  
175 1 ▶sub stop ($self) {
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
176 1 $self->{quit} = 1;
177 1 Mojo::IOLoop->stop;
178 1 return;
179 }
180  
181 # The listeners furthest behind: true when one has less than 'ahead' queued.
182 1929 ▶sub _behind ($self) {
Subroutine _behind: called CC=4 SCAR=13.9
183 1929 my $target = $self->{ahead} * $self->{rate};
184 1929 for my $client (values %{ $self->{clients} }) {
185 1912 ▶ next unless $client->{audio};
Branch: 1 branch
BranchTrueFalseTotal
unless $client->{"audio"} 91 1821 1912
186 1821 ▶ return 1 if $client->{sent} - $client->{played} < $target;
Branch: 1 branch
BranchTrueFalseTotal
if $client->{"sent"} - $client->{"played"} < $target 256 1565 1821
187 }
188 1673 return 0;
189 }
190  
191 # A quantum rendered and sent to every listener.
192 256 ▶sub _render ($self) {
Subroutine _render: called CC=9 SCAR=23.2
193 256 my $synth = $self->{synth};
194 256 my $n = $self->{quantum};
195 256 my $started = time;
196 256 my ($left, $right) = $synth->render_arrays($n);
197 256 my $took = time - $started;
198 256 my $sound = pack 'f<*', @{ Peta::Synth::Output::interleave($left, $right) };
199  
200 256 for my $client (values %{ $self->{clients} }) {
201 256 ▶ next unless $client->{audio};
Branch: 1 branch
BranchTrueFalseTotal
unless $client->{"audio"} 0 256 256
202 256 $client->{sent} += $n;
203 256 ▶ if ($client->{stream}->bytes_waiting > $MAX_BACKLOG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->{"stream"}->bytes_waiting > $MAX_BACKLOG) 0 256 256
204 0 $client->{played} += $n; # it cannot take more: what is dropped counts as played
205 0 next;
206 }
207 256 $client->{tx}->send({ binary => $sound });
208 }
209  
210 256 $synth->account($took, $n);
211 256 ▶ $synth->adapt($self->{partials}) if $self->{auto};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"auto"} 256 0 256
212 256 my $playing = $self->{playing};
213 256 ▶ $self->{playing} = undef if $playing && !defined $playing->{paused} && !$synth->pending && !$synth->active;
Branch: 1 branch
BranchTrueFalseTotal
if $playing and not defined $playing->{"paused"} and not $synth->pending and not $synth->active 0 256 256
Condition66%6/9
$playing and not defined $playing->{"paused"} and not $synth->pending and not $synth->active
!ll&&!rl&&r
25600
$playing and not defined $playing->{"paused"} and not $synth->pending
!ll&&!rl&&r
216400
$playing and not defined $playing->{"paused"}
!ll&&!rl&&r
1684840
MC/DC0%0/4
$playing and not defined $playing->{"paused"} and not $synth->pending and not $synth->active
$playing not defined $playing->{"paused"} not $synth->pending not $synth->active
Truth table60%3/5
A and not(B) and not(C) and not(D)
A= $playingB= not defined $playing->{"paused"}C= not $synth->pendingD= not $synth->active
ABCDresult
0XXX0
10XX0
110X0
11100
11111
214 256 return;
215 }
216  
217 # The page's files (looked at again by the browser each time: they are
218 # small, and a new version shows at once), uploads of MIDI files, the
219 # WebSocket.
220 1 ▶sub _app ($self) {
Subroutine _app: called CC=1 SCAR=0.0
221 1 my $app = Mojolicious->new(mode => 'production');
222 1 $app->log->level('error');
223 0 ▶ $app->log->unsubscribe('message')->on(message => sub ($log, $level, @lines) { $self->{log}->("@lines") });
Subroutine __ANON__: not called CC=1 SCAR=1.2
224 1 $app->static->paths([ $self->{web_dir} ]);
225 1 $app->max_request_size($MAX_UPLOAD);
226 1 ▶ $app->hook(after_dispatch => sub ($c) { $c->res->headers->cache_control('no-cache') });
Subroutine __ANON__: called CC=1 SCAR=0.0
227  
228 1 my $r = $app->routes;
229 1 ▶ $r->get('/' => sub ($c) { $c->reply->static('index.html') });
Subroutine __ANON__: called CC=1 SCAR=0.0
230 1 ▶ $r->post('/upload' => sub ($c) { $self->_upload($c) });
Subroutine __ANON__: called CC=1 SCAR=0.0
231 1 ▶ $r->websocket('/ws' => sub ($c) { $self->_connect($c) });
Subroutine __ANON__: called CC=1 SCAR=0.0
232 1 return $app;
233 }
234  
235 # A MIDI file sent from the page, saved into the MIDI directory.
236 6 ▶sub _upload ($self, $c) {
Subroutine _upload: called CC=6 SCAR=18.0
237 6 ▶ return $c->render(status => 413, text => "too large\n") if $c->req->is_limit_exceeded;
Branch: 1 branch
BranchTrueFalseTotal
if $c->req->is_limit_exceeded 0 6 6
238 6 ▶ my $name = basename($c->req->url->query->param('name') // '');
Condition50%1/2
$c->req->url->query->param("name") // ""
l!l
60
MC/DC0%0/1
$c->req->url->query->param("name") // ""
$c->req->url->query->param("name")
Truth table50%1/2
A
A= $c->req->url->query->param("name")
Aresult
11
00
239 6 ▶ return $c->render(status => 400, text => "'$name': a MIDI file's name is needed (.mid, .midi, .kar, .rmi; not hidden)\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /\A[^.\x00-\x1f\\][^\x00-\x1f\\]*$MIDI_FILE/u 2 4 6
240 unless $name =~ /\A[^.\x00-\x1f\\][^\x00-\x1f\\]*$MIDI_FILE/;
241 4 my $body = $c->req->body;
242 return $c->render(status => 400, text => "not a MIDI file\n")
243 4 ▶ unless eval { Peta::Synth::MIDI->parse($body); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { "Peta::Synth::MIDI"->parse($body); 1 } } 1 3 4
244  
245 3 my $file = $self->_midi_path($name);
246 3 ▶ open my $out, '>:raw', $file or return $c->render(status => 500, text => "cannot save: $!\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">:raw", $file or return $c->render("status", 500, "text", "cannot save: $!\n") 3 0 3
247 3 print {$out} $body;
248 3 close $out;
249 3 $self->{log}->("uploaded $name");
250 3 $c->render(json => { saved => $name });
251 3 $self->_broadcast({ type => 'files', files => $self->_files });
252 3 return;
253 }
254  
255 # A page connected: its messages are JSON for _message.
256 1 ▶sub _connect ($self, $c) {
Subroutine _connect: called CC=2 SCAR=6.9
257 1 my $id = $c->tx->connection;
258 1 my $client = $self->{clients}{$id} = {
259 tx => $c->tx,
260 stream => Mojo::IOLoop->stream($id),
261 audio => 0, # it plays the sound
262 sent => 0, # frames sent
263 played => 0, # frames it says it has played
264 };
265 1 weaken $client->{$_} for qw(tx stream);
266  
267 561 ▶ $c->on(message => sub ($c, $text) {
Subroutine __ANON__: called CC=3 SCAR=11.0
268 561 my $data = eval { $self->{json}->decode($text) };
269 561 ▶ return unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 561 561
270 561 ▶ eval { $self->_message($client, $data); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->_message($client, $data); 1 } } or $self->_reply($client, {"type", "error", "message", "$@" =~ s/\s+\z//ru}) 554 7 561
271 or $self->_reply($client, { type => 'error', message => "$@" =~ s/\s+\z//r });
272 1 });
273 1 ▶ $c->on(finish => sub { delete $self->{clients}{$id} });
Subroutine __ANON__: called CC=1 SCAR=0.0
274 1 return;
275 }
276  
277 19 ▶sub _reply ($self, $client, $data) {
Subroutine _reply: called CC=1 SCAR=0.0
278 19 $client->{tx}->send($self->{json}->encode($data));
279 19 return;
280 }
281  
282 76 ▶sub _broadcast ($self, $data, $except = undef) {
Subroutine _broadcast: called CC=5 SCAR=16.1
283 76 my $text = $self->{json}->encode($data);
284 76 for my $client (values %{ $self->{clients} }) {
285 75 ▶ next if $except && $client == $except;
Branch: 1 branch
BranchTrueFalseTotal
if $except and $client == $except 5 70 75
Condition66%2/3
$except and $client == $except
!ll&&!rl&&r
7005
MC/DC50%1/2
$except and $client == $except
$except $client == $except
Truth table66%2/3
A and B
A= $exceptB= $client == $except
ABresult
0X0
100
111
286 70 $client->{tx}->send($text);
287 }
288 76 return;
289 }
290  
291 # What the page asks for: a sub for each type of message, called with
292 # ($self, the client, the message).
293  
294 2 my %MESSAGE = (
295 hello => \&_hello,
296 played => \&_played,
297 audio => \&_audio,
298 note => \&_note,
299 cc => \&_cc,
300 program => \&_program,
301 bend => \&_bend,
302 midi => \&_midi,
303 mute => \&_mute,
304 patch_get => \&_patch_get,
305 patch_set => \&_patch_set,
306 patch_reset => \&_patch_reset,
307 reverb => \&_reverb,
308 eq => \&_eq,
309 channel_eq => \&_channel_eq,
310 arp => \&_arp,
311 let_go => \&_let_go,
312 synth => \&_synth,
313 files => \&_list_files,
314 play => \&_play,
315 pause => \&_pause,
316 seek => \&_seek,
317 stop => \&_stop,
318 record => \&_record,
319 panic => \&_panic,
320 reset => \&_reset,
321 bank_save => \&_bank_save,
322 );
323  
324 561 ▶sub _message ($self, $client, $m) {
Subroutine _message: called CC=3 SCAR=11.4
325 561 ▶ my $type = $m->{type} // '';
Condition50%1/2
$m->{"type"} // ""
l!l
5610
MC/DC0%0/1
$m->{"type"} // ""
$m->{"type"}
Truth table50%1/2
A
A= $m->{"type"}
Aresult
11
00
326 561 ▶ my $handler = $MESSAGE{$type} or die "unknown message '$type'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $MESSAGE{$type} or die "unknown message '${type}'\n" 561 0 561
327 561 $self->$handler($client, $m);
328 554 return;
329 }
330  
331 3 ▶sub _hello ($self, $client, $m) {
Subroutine _hello: called CC=1 SCAR=0.0
332 3 $self->_reply($client, $self->_init);
333 3 return;
334 }
335  
336 505 ▶sub _played ($self, $client, $m) {
Subroutine _played: called CC=4 SCAR=14.6
337 505 my $frames = $m->{frames};
338 505 ▶ $client->{played} = $frames if defined $frames && $frames =~ /\A\d+\z/ && $frames > $client->{played};
Branch: 1 branch
BranchTrueFalseTotal
if defined $frames and $frames =~ /\A\d+\z/u and $frames > $client->{"played"} 63 442 505
Condition50%3/6
defined $frames and $frames =~ /\A\d+\z/u and $frames > $client->{"played"}
!ll&&!rl&&r
044263
defined $frames and $frames =~ /\A\d+\z/u
!ll&&!rl&&r
00505
MC/DC33%1/3
defined $frames and $frames =~ /\A\d+\z/u and $frames > $client->{"played"}
defined $frames $frames =~ /\A\d+\z/u $frames > $client->{"played"}
Truth table50%2/4
A and B and C
A= defined $framesB= $frames =~ /\A\d+\z/uC= $frames > $client->{"played"}
ABCresult
0XX0
10X0
1100
1111
339 505 return;
340 }
341  
342 1 ▶sub _audio ($self, $client, $m) {
Subroutine _audio: called CC=2 SCAR=7.0
343 1 ▶ $client->{audio} = $m->{on} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$m->{'on'} ? : 1 0 1
344 1 $client->{sent} = 0;
345 1 $client->{played} = 0;
346 1 return;
347 }
348  
349 # What is played live: the page's keys and controls, a device's MIDI; what
350 # it sets is held against the file (see Peta::Synth::Synth::by_hand).
351 11 ▶sub _live ($self, $event) {
Subroutine _live: called CC=2 SCAR=6.9
352 11 $self->{synth}->by_hand($event);
353 11 ▶ push @{ $self->{recording}{events} }, [ time - $self->{recording}{started}, $event ] if $self->{recording};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"recording"} 4 7 11
354 11 return;
355 }
356  
357 3 ▶sub _note ($self, $client, $m) {
Subroutine _note: called CC=3 SCAR=11.0
358 $self->_live({
359 type => $m->{on} ? 'note_on' : 'note_off',
360 channel => _channel($m),
361 key => _clamp($m->{key}, 0, 127),
362 3 ▶ velocity => _clamp($m->{velocity} // 100, 1, 127),
Branch: 1 branch
BranchTrueFalseTotal
$m->{'on'} ? : 2 1 3
Condition100%2/2
$m->{"velocity"} // 100
l!l
21
MC/DC100%1/1
$m->{"velocity"} // 100
$m->{"velocity"}
Truth table100%2/2
A
A= $m->{"velocity"}
Aresult
11
00
363 });
364 3 return;
365 }
366  
367 1 ▶sub _cc ($self, $client, $m) {
Subroutine _cc: called CC=1 SCAR=0.0
368 $self->_live({
369 type => 'controller',
370 channel => _channel($m),
371 number => _clamp($m->{number}, 0, 127),
372 1 value => _clamp($m->{value}, 0, 127),
373 });
374 1 return;
375 }
376  
377 2 ▶sub _program ($self, $client, $m) {
Subroutine _program: called CC=2 SCAR=6.9
378 2 my $channel = _channel($m);
379 2 ▶ if (defined $m->{bank}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m->{"bank"}) 1 1 2
380 1 $self->_live({ type => 'controller', channel => $channel, number => 0, value => _clamp($m->{bank}, 0, 127) });
381 1 $self->_live({ type => 'controller', channel => $channel, number => 32, value => 0 });
382 }
383 2 $self->_live({ type => 'program', channel => $channel, program => _clamp($m->{program}, 0, 127) });
384 2 return;
385 }
386  
387 0 ▶sub _bend ($self, $client, $m) {
Subroutine _bend: not called CC=1 SCAR=6.9
388 0 $self->_live({ type => 'pitch_bend', channel => _channel($m), value => _clamp($m->{value}, -8192, 8191) });
389 0 return;
390 }
391  
392 # A device's message as the page's Web MIDI passed it: bytes.
393 3 ▶sub _midi ($self, $client, $m) {
Subroutine _midi: called CC=4 SCAR=15.0
394 3 my $data = $m->{data};
395 3 ▶ die "midi: a list of at most 1024 bytes\n" if ref $data ne 'ARRAY' || @$data > 1024;
Branch: 1 branch
BranchTrueFalseTotal
if ref $data ne "ARRAY" or @$data > 1024 0 3 3
Condition33%1/3
ref $data ne "ARRAY" or @$data > 1024
l!l&&r!l&&!r
003
MC/DC0%0/2
ref $data ne "ARRAY" or @$data > 1024
ref $data ne "ARRAY" @$data > 1024
Truth table33%1/3
A or B
A= ref $data ne "ARRAY"B= @$data > 1024
ABresult
1X1
011
000
396  
397 3 my $event = Peta::Synth::MIDI::message(pack 'C*', map { _clamp($_, 0, 255) } @$data);
398 3 ▶ $self->_live($event) if $event;
Branch: 1 branch
BranchTrueFalseTotal
if $event 3 0 3
399 3 return;
400 }
401  
402 1 ▶sub _mute ($self, $client, $m) {
Subroutine _mute: called CC=2 SCAR=7.2
403 1 ▶ $self->{synth}->channel(_channel($m))->{mute} = $m->{on} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$m->{'on'} ? : 1 0 1
404 1 return;
405 }
406  
407 6 ▶sub _patch_get ($self, $client, $m) {
Subroutine _patch_get: called CC=1 SCAR=0.0
408 6 $self->_reply($client, $self->_patch_message(_patch_id($m)));
409 6 return;
410 }
411  
412 # A patch as it was at the start; every page is told.
413 0 ▶sub _patch_reset ($self, $client, $m) {
Subroutine _patch_reset: not called CC=1 SCAR=6.9
414 0 my @id = _patch_id($m);
415 0 $self->{synth}->restore_patch(@id);
416 0 $self->_broadcast($self->_patch_message(@id));
417 0 return;
418 }
419  
420 # A field of a patch set, for the notes that start from now on; the other
421 # pages are told.
422 8 ▶sub _patch_set ($self, $client, $m) {
Subroutine _patch_set: called CC=4 SCAR=13.9
423 8 my ($kind, $number, $bank) = _patch_id($m);
424 8 ▶ my $name = $m->{field} // '';
Condition50%1/2
$m->{"field"} // ""
l!l
80
MC/DC0%0/1
$m->{"field"} // ""
$m->{"field"}
Truth table50%1/2
A
A= $m->{"field"}
Aresult
11
00
425 my $value = $name eq 'mods' ? _mods($m->{value})
426 : $LIST{$name} ? _rows($name, $m->{value})
427 8 ▶ : _field_value($name, $m->{value});
Branch: 2 branches
BranchTrueFalseTotal
$name eq 'mods' ? : 2 6 8
$LIST{$name} ? : 2 4 6
428 5 $self->_patch($kind, $number, $bank)->{$name} = $value;
429  
430 5 my $change = { type => 'patch_field', kind => $kind, number => $number, bank => $bank, field => $name, value => $value };
431 5 $self->_broadcast($change, $client);
432 5 return;
433 }
434  
435 1 ▶sub _reverb ($self, $client, $m) {
Subroutine _reverb: called CC=3 SCAR=11.4
436 1 ▶ my $name = $m->{name} // '';
Condition50%1/2
$m->{"name"} // ""
l!l
10
MC/DC0%0/1
$m->{"name"} // ""
$m->{"name"}
Truth table50%1/2
A
A= $m->{"name"}
Aresult
11
00
437 1 ▶ my $range = $REVERB{$name} or die "the reverb has no '$name'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $range = $REVERB{$name} or die "the reverb has no '${name}'\n" 1 0 1
438 1 $self->{synth}->reverb->set($name => _clamp($m->{value}, @$range));
439 1 return;
440 }
441  
442 1 ▶sub _eq ($self, $client, $m) {
Subroutine _eq: called CC=1 SCAR=0.0
443 1 $self->{synth}->eq->set(_eq_setting($m));
444 1 return;
445 }
446  
447 1 ▶sub _channel_eq ($self, $client, $m) {
Subroutine _channel_eq: called CC=1 SCAR=0.0
448 1 $self->{synth}->set_channel_eq(int _channel($m), _eq_setting($m));
449 1 return;
450 }
451  
452 # A setting of a channel's arpeggiator, kept to what it may be; held against
453 # the file.
454 3 ▶sub _arp ($self, $client, $m) {
Subroutine _arp: called CC=8 SCAR=21.2
455 3 my $channel = int _channel($m);
456 3 ▶ my $name = $m->{name} // '';
Condition50%1/2
$m->{"name"} // ""
l!l
30
MC/DC0%0/1
$m->{"name"} // ""
$m->{"name"}
Truth table50%1/2
A
A= $m->{"name"}
Aresult
11
00
457 3 my ($field) = grep { $_->{name} eq $name } @{ Peta::Synth::Arpeggiator::field_info() };
458 3 ▶ die "the arpeggiator has no '$name'\n" unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 3 3
459  
460 3 my $value = $m->{value};
461 3 ▶ if ($field->{scale} eq 'choice') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{'scale'} eq 'choice') { } 2 1 3
462 2 ▶ die "'" . ($value // '') . "' is not one of @{ $field->{choices} }\n" unless grep { $_ eq ($value // '') } @{ $field->{choices} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq ($value // "");} @{$$field{"choices"};} 1 1 2
Condition50%2/4
$value // ""
l!l
240
$value // ""
l!l
10
MC/DC0%0/1
$value // ""
$value
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
Truth table50%1/2
A
A= $value
Aresult
11
00
463 }
464 else {
465 1 $value = _clamp($value, $field->{min}, $field->{max});
466 1 ▶ $value = int($value + 0.5) if $field->{scale} eq 'int';
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"scale"} eq "int" 0 1 1
467 }
468 2 $self->{synth}->by_hand({ type => 'arp', channel => $channel, name => $name, value => $value });
469 2 return;
470 }
471  
472 # A setting held against the file let go, or all of a channel's: as the file has it.
473 3 ▶sub _let_go ($self, $client, $m) {
Subroutine _let_go: called CC=3 SCAR=11.0
474 3 my $channel = int _channel($m);
475 3 my $what = $m->{what};
476 3 ▶ die "channel $channel does not hold '$what'\n" if defined $what && !grep { $_ eq $what } @{ $self->{synth}->held($channel) };
Branch: 1 branch
BranchTrueFalseTotal
if defined $what and not grep({$_ eq $what;} @{$self->{"synth"}->held($channel);}) 1 2 3
Condition100%3/3
defined $what and not grep({$_ eq $what;} @{$self->{"synth"}->held($channel);})
!ll&&!rl&&r
111
MC/DC100%2/2
defined $what and not grep({$_ eq $what;} @{$self->{"synth"}->held($channel);})
defined $what not grep({$_ eq $what;} @{$self->{"synth"}->held($channel);})
Truth table100%3/3
A and not(B)
A= defined $whatB= not grep({$_ eq $what;} @{$self->{"synth"}->held($channel);})
ABresult
0X0
100
111
477 2 $self->{synth}->let_go($channel, $what);
478 2 return;
479 }
480  
481 3 ▶sub _synth ($self, $client, $m) {
Subroutine _synth: called CC=10 SCAR=35.0
482 3 my $synth = $self->{synth};
483 3 ▶ my $name = $m->{name} // '';
Condition50%1/2
$m->{"name"} // ""
l!l
30
MC/DC0%0/1
$m->{"name"} // ""
$m->{"name"}
Truth table50%1/2
A
A= $m->{"name"}
Aresult
11
00
484 3 my $value = $m->{value};
485  
486 3 ▶ if ($name eq 'partials') {
Branch: 5 branches
BranchTrueFalseTotal
if ($name eq 'partials') { } 0 3 3
elsif ($name eq 'voices') { } 0 3 3
elsif ($name eq 'gain') { } 0 3 3
elsif ($name eq 'fidelity') { } 3 0 3
elsif ($name eq 'auto') { } 0 0 0
487 0 $self->{partials} = _clamp(int $value, 1, 256);
488 0 $synth->set(partials => $self->{partials});
489 }
490 elsif ($name eq 'voices') {
491 0 $synth->set(voices => _clamp(int $value, 1, 1024));
492 }
493 elsif ($name eq 'gain') {
494 0 $synth->set(gain => _clamp($value, 0, 2));
495 }
496 elsif ($name eq 'fidelity') {
497 3 ▶ $synth->set(fidelity => $value // '');
Condition50%1/2
$value // ""
l!l
30
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
498 }
499 elsif ($name eq 'auto') {
500 # Off, the partials it took away are given back.
501 0 ▶ $self->{auto} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
502 0 ▶ $synth->set(partials => $self->{partials}) unless $self->{auto};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"auto"} 0 0 0
503 }
504 else {
505 0 die "cannot set '$name'\n";
506 }
507 2 return;
508 }
509  
510 1 ▶sub _list_files ($self, $client, $m) {
Subroutine _list_files: called CC=1 SCAR=0.0
511 1 $self->_reply($client, { type => 'files', files => $self->_files });
512 1 return;
513 }
514  
515 2 ▶use constant LEAD => 0.05; # seconds before a file's first event: time to render it
Subroutine BEGIN: called CC=1 SCAR=0.0
516  
517 # A file from the MIDI directory, played from its start or from a
518 # position (seconds); what played before stops.
519 3 ▶sub _play ($self, $client, $m) {
Subroutine _play: called CC=7 SCAR=19.9
520 3 ▶ my $name = basename($m->{file} // '');
Condition50%1/2
$m->{"file"} // ""
l!l
30
MC/DC0%0/1
$m->{"file"} // ""
$m->{"file"}
Truth table50%1/2
A
A= $m->{"file"}
Aresult
11
00
521 3 my $file = $self->_midi_path($name);
522 3 ▶ die "no such file: $name\n" unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 3 3
523  
524 3 my $timeline = _instruments_first(Peta::Synth::MIDI->read($file)->timeline);
525 3 ▶ $self->{synth}->forget_held if ($self->{last_file} // '') ne $name; # held against that file, not this one
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"last_file"} // "") ne $name 3 0 3
Condition100%2/2
$self->{"last_file"} // ""
l!l
21
MC/DC100%1/1
$self->{"last_file"} // ""
$self->{"last_file"}
Truth table100%2/2
A
A= $self->{"last_file"}
Aresult
11
00
526 3 $self->{last_file} = $name;
527 $self->{playing} = {
528 file => $name,
529 timeline => $timeline,
530 3 ▶ length => @$timeline ? $timeline->[-1]{time} : 0,
Branch: 1 branch
BranchTrueFalseTotal
@$timeline ? : 3 0 3
531 };
532 3 ▶ $self->_play_from(_clamp($m->{position} // 0, 0, $self->{playing}{length}));
Condition100%2/2
$m->{"position"} // 0
l!l
12
MC/DC100%1/1
$m->{"position"} // 0
$m->{"position"}
Truth table100%2/2
A
A= $m->{"position"}
Aresult
11
00
533 3 return;
534 }
535  
536 # The file with each channel's instrument chosen from its start, so that
537 # the page shows what the file plays before it is heard: the bank selects
538 # and program changes that come before a channel's first note are moved to
539 # the start, or to the system exclusive message last before them (a reset
540 # there would undo them). No note of the channel sounds before them, so
541 # nothing sounds otherwise.
542 4 ▶sub _instruments_first ($timeline) {
Subroutine _instruments_first: called CC=11 SCAR=24.1
543 4 my ($from, $moved, %sounded) = (0, 0);
544 4 my @events;
545 4 for my $event (@$timeline) {
546 615 my ($type, $c) = @$event{qw(type channel)};
547 615 ▶ $from = $event->{time} if $type eq 'sysex';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "sysex" 1 614 615
548 615 ▶ $sounded{$c} = 1 if $type eq 'note_on';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "note_on" 304 311 615
549 615 ▶ my $sets = $type eq 'program' || $type eq 'controller' && ($event->{number} == 0 || $event->{number} == 32);
Condition66%6/9
$type eq 'program' || $type eq 'controller' && ($event->{'number'} == 0 || $event->{'number'} == 32)
l!l&&r!l&&!r
41610
$type eq "controller" and $event->{"number"} == 0 || $event->{"number"} == 32
!ll&&!rl&&r
61001
$event->{"number"} == 0 or $event->{"number"} == 32
l!l&&r!l&&!r
100
MC/DC50%2/4
$type eq 'program' || $type eq 'controller' && ($event->{'number'} == 0 || $event->{'number'} == 32)
$type eq 'program' $type eq "controller" $event->{"number"} == 0 $event->{"number"} == 32
Truth table60%3/5
A || B and C or D
A= $type eq 'program'B= $type eq "controller"C= $event->{"number"} == 0D= $event->{"number"} == 32
ABCDresult
1XXX1
011X1
01011
00XX0
01000
550 615 ▶ if ($sets && !$sounded{$c} && $event->{time} > $from) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sets and not $sounded{$c} and $event->{"time"} > $from) 3 612 615
Condition83%5/6
$sets and not $sounded{$c} and $event->{"time"} > $from
!ll&&!rl&&r
61203
$sets and not $sounded{$c}
!ll&&!rl&&r
61023
MC/DC66%2/3
$sets and not $sounded{$c} and $event->{"time"} > $from
$sets not $sounded{$c} $event->{"time"} > $from
Truth table75%3/4
A and not(B) and C
A= $setsB= not $sounded{$c}C= $event->{"time"} > $from
ABCresult
0XX0
10X0
1100
1111
551 3 push @events, { %$event, time => $from };
552 3 $moved++;
553 3 next;
554 }
555 612 push @events, $event;
556 }
557 4 ▶ return $timeline if !$moved;
Branch: 1 branch
BranchTrueFalseTotal
unless $moved 3 1 4
558 1 return [ sort { $a->{time} <=> $b->{time} } @events ]; # sort is stable: the order kept
559 }
560  
561 # The file playing on from $position seconds: what sounds is silenced, the
562 # channels are as the file has them there.
563 5 ▶sub _play_from ($self, $position) {
Subroutine _play_from: called CC=1 SCAR=0.0
564 5 my $synth = $self->{synth};
565 5 my $playing = $self->{playing};
566 5 $synth->panic;
567 5 $synth->reset_channels;
568 5 $synth->schedule($playing->{timeline}, LEAD, $position);
569 5 $playing->{start} = $synth->now + (LEAD - $position) * $self->{rate};
570 5 $playing->{from} = $position;
571 5 $playing->{paused} = undef;
572 5 $self->_broadcast_channels;
573 5 return;
574 }
575  
576 # Where the file is (seconds): as far as it is rendered (where it was played
577 # from, during the lead before it), or where it is paused.
578 14 ▶sub _position ($self) {
Subroutine _position: called CC=3 SCAR=11.0
579 14 my $playing = $self->{playing};
580 14 ▶ return $playing->{paused} if defined $playing->{paused};
Branch: 1 branch
BranchTrueFalseTotal
if defined $playing->{"paused"} 4 10 14
581 10 my $position = ($self->{synth}->now - $playing->{start}) / $self->{rate};
582 10 ▶ return $position < $playing->{from} ? $playing->{from} : $position;
Branch: 1 branch
BranchTrueFalseTotal
$position < $playing->{'from'} ? : 6 4 10
583 }
584  
585 # Paused, the file holds where it is and what sounds is let go; it goes on
586 # from there, or from where it was sought to meanwhile.
587 2 ▶sub _pause ($self, $client, $m) {
Subroutine _pause: called CC=5 SCAR=16.4
588 2 ▶ my $playing = $self->{playing} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $playing = $self->{"playing"} or return 2 0 2
589 2 ▶ if (!$m->{on}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m->{"on"}) 1 1 2
590 1 ▶ $self->_play_from($playing->{paused}) if defined $playing->{paused};
Branch: 1 branch
BranchTrueFalseTotal
if defined $playing->{"paused"} 1 0 1
591 1 return;
592 }
593 1 ▶ return if defined $playing->{paused};
Branch: 1 branch
BranchTrueFalseTotal
if defined $playing->{"paused"} 0 1 1
594 1 $playing->{paused} = $self->_position;
595 1 $self->{synth}->stop;
596 1 return;
597 }
598  
599 2 ▶sub _seek ($self, $client, $m) {
Subroutine _seek: called CC=3 SCAR=11.0
600 2 ▶ my $playing = $self->{playing} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $playing = $self->{"playing"} or return 2 0 2
601 2 my $position = _clamp($m->{position}, 0, $playing->{length});
602 2 ▶ if (defined $playing->{paused}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $playing->{"paused"}) 1 1 2
603 1 $playing->{paused} = $position;
604 1 return;
605 }
606 1 $self->_play_from($position);
607 1 return;
608 }
609  
610 1 ▶sub _stop ($self, $client, $m) {
Subroutine _stop: called CC=1 SCAR=0.0
611 1 $self->{synth}->stop;
612 1 $self->{playing} = undef;
613 1 return;
614 }
615  
616 0 ▶sub _panic ($self, $client, $m) {
Subroutine _panic: not called CC=1 SCAR=6.9
617 0 $self->{synth}->panic;
618 0 $self->{playing} = undef;
619 0 return;
620 }
621  
622 # Everything as the server started it (the channels, the mixer, the
623 # effects, the arpeggiators, the bank's settings again), but the instruments
624 # as they have been changed; the file stopped; every page told.
625 1 ▶sub _reset ($self, $client, $m) {
Subroutine _reset: called CC=3 SCAR=11.0
626 1 my $old = $self->{synth};
627 1 $old->panic;
628 1 ▶ $self->{synth} = $self->_new_synth($old->edits, engine => $old->engine, waves => $old->waves // 0);
Condition50%1/2
$old->waves // 0
l!l
10
MC/DC0%0/1
$old->waves // 0
$old->waves
Truth table50%1/2
A
A= $old->waves
Aresult
11
00
629 1 $self->{$_} = $self->{start}{$_} for qw(partials auto);
630 1 $self->{playing} = undef;
631 1 $self->_broadcast($self->_init);
632 1 return;
633 }
634  
635 # Recording: what is played live (see _live), at the times it comes, into
636 # a MIDI file in the MIDI directory; a file's events are not recorded.
637 5 ▶sub _record ($self, $client, $m) {
Subroutine _record: called CC=4 SCAR=13.9
638 5 ▶ if ($m->{on}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"on"}) 2 3 5
639 2 ▶ $self->{recording} //= { started => time, events => [], setup => [ map { [ $self->_channel_setup($_) ] } 0 .. 15 ] };
Condition50%1/2
$self->{"recording"} //= {"started", &Peta::Synth::Server::time(), "events", [], "setup", [map({[$self->_channel_setup($_)];} 0..15)]}
l!l
02
MC/DC0%0/1
$self->{"recording"} //= {"started", &Peta::Synth::Server::time(), "events", [], "setup", [map({[$self->_channel_setup($_)];} 0..15)]}
$self->{"recording"}
Truth table50%1/2
A
A= $self->{"recording"}
Aresult
11
00
640 2 return;
641 }
642 3 ▶ my $recording = delete $self->{recording} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $recording = delete $self->{"recording"} or return 2 1 3
643 2 my $name = $self->_save_recording($recording);
644 1 $self->{log}->("recorded $name");
645 1 $self->_reply($client, { type => 'recorded', file => $name });
646 1 $self->_broadcast({ type => 'files', files => $self->_files });
647 1 return;
648 }
649  
650 2 ▶use constant TICKS => 960; # ticks a second: 480 a quarter note at 120 a minute
Subroutine BEGIN: called CC=1 SCAR=0.0
651  
652 # The recording as a file of one track: the channels it plays set up as
653 # they were when it began, then what was played. Keys and pedals still held
654 # when it stops are let go there.
655 2 ▶sub _save_recording ($self, $recording) {
Subroutine _save_recording: called CC=14 SCAR=30.6
656 2 my $events = $recording->{events};
657 2 ▶ die "nothing was played: no recording saved\n" unless grep { $_->[1]{type} eq 'note_on' } @$events;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_->[1]{"type"} eq "note_on";} @$events 1 1 2
658  
659 1 ▶ my %used = map { defined $_->[1]{channel} ? ($_->[1]{channel} => 1) : () } @$events;
Branch: 1 branch
BranchTrueFalseTotal
defined $_->[1]{'channel'} ? : 4 0 4
660 0 my @track = ({ tick => 0, type => 'tempo', usec => 500_000 }, map { @{ $recording->{setup}[$_] } } sort { $a <=> $b } keys %used);
661 1 my (%held, %pedal);
662 1 for (@$events) {
663 4 my ($time, $event) = @$_;
664 4 push @track, { %$event, tick => int($time * TICKS + 0.5) };
665 4 my ($type, $c) = @$event{qw(type channel)};
666 4 ▶ if ($type eq 'note_on') { $held{$c}{ $event->{key} }++ }
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'note_on') { } 2 2 4
elsif ($type eq 'note_off') { } 1 1 2
elsif ($type eq 'controller' and $event->{'number'} >= 64 and $event->{'number'} <= 67) { } 0 2 2
667 1 ▶ elsif ($type eq 'note_off') { delete $held{$c}{ $event->{key} } if $held{$c}{ $event->{key} } && !--$held{$c}{ $event->{key} } }
Branch: 1 branch
BranchTrueFalseTotal
if $held{$c}{$event->{"key"}} and not --$held{$c}{$event->{"key"}} 1 0 1
Condition33%1/3
$held{$c}{$event->{"key"}} and not --$held{$c}{$event->{"key"}}
!ll&&!rl&&r
001
MC/DC0%0/2
$held{$c}{$event->{"key"}} and not --$held{$c}{$event->{"key"}}
$held{$c}{$event->{"key"}} not --$held{$c}{$event->{"key"}}
Truth table33%1/3
A and not(B)
A= $held{$c}{$event->{"key"}}B= not --$held{$c}{$event->{"key"}}
ABresult
0X0
100
111
668 ▶ elsif ($type eq 'controller' && $event->{number} >= 64 && $event->{number} <= 67) {
Condition33%2/6
$type eq "controller" and $event->{"number"} >= 64 and $event->{"number"} <= 67
!ll&&!rl&&r
100
$type eq "controller" and $event->{"number"} >= 64
!ll&&!rl&&r
100
MC/DC0%0/3
$type eq "controller" and $event->{"number"} >= 64 and $event->{"number"} <= 67
$type eq "controller" $event->{"number"} >= 64 $event->{"number"} <= 67
Truth table25%1/4
A and B and C
A= $type eq "controller"B= $event->{"number"} >= 64C= $event->{"number"} <= 67
ABCresult
0XX0
10X0
1100
1111
669 0 $pedal{$c}{ $event->{number} } = $event->{value};
670 }
671 }
672  
673 1 my $end = int((time - $recording->{started}) * TICKS + 0.5);
674 0 for my $c (sort { $a <=> $b } keys %held) {
675 0 push @track, map { +{ tick => $end, type => 'note_off', channel => $c, key => $_, velocity => 64 } } sort { $a <=> $b } keys %{ $held{$c} };
676 }
677 0 for my $c (sort { $a <=> $b } keys %pedal) {
678 0 my $down = $pedal{$c};
679 0 push @track, map { +{ tick => $end, type => 'controller', channel => $c, number => $_, value => 0 } } grep { $down->{$_} >= 64 } sort { $a <=> $b } keys %$down;
680 }
681  
682 1 my $stamp = strftime('recording-%Y%m%d-%H%M%S', localtime $recording->{started});
683 1 my ($name, $more) = ("$stamp.mid", 1);
684 1 ▶ $name = "$stamp-" . ++$more . '.mid' while -e $self->_midi_path($name);
Branch: 1 branch
BranchTrueFalseTotal
-e $self->_midi_path($name) and do { $name = "$stamp-" . ++$more . '.mid' } 0 1 1
685 1 Peta::Synth::MIDI->new(format => 0, division => TICKS / 2, tracks => [ \@track ])->write($self->_midi_path($name));
686 1 return $name;
687 }
688  
689 # A channel as it is: its bank (its variation, or drums), program,
690 # volume, pan, expression, reverb and chorus, as events at the start of a
691 # file.
692 32 ▶sub _channel_setup ($self, $n) {
Subroutine _channel_setup: called CC=6 SCAR=18.8
693 32 my $c = $self->{synth}->channel($n);
694 my $bank = $c->{drum} != ($n == 9 ? 1 : 0) ? ($c->{drum} ? 120 : 121)
695 : $c->{drum} ? 0
696 32 ▶ : $c->{variation};
Branch: 4 branches
BranchTrueFalseTotal
$c->{'drum'} != ($n == 9 ? 1 : 0) ? : 0 32 32
$n == 9 ? : 2 30 32
$c->{'drum'} ? : 0 0 0
$c->{'drum'} ? : 2 30 32
697 32 my @cc = ([ 7 => $c->{volume} ], [ 10 => $c->{pan} ], [ 11 => $c->{expression} ], [ 91 => $c->{send} ], [ 93 => $c->{chorus} ]);
698 32 ▶ unshift @cc, [ 0 => $bank ] if $bank;
Branch: 1 branch
BranchTrueFalseTotal
if $bank 0 32 32
699 return (
700 160 (map { +{ tick => 0, type => 'controller', channel => $n, number => $_->[0], value => $_->[1] } } @cc),
701 { tick => 0, type => 'program', channel => $n, program => $c->{program} },
702 32 );
703 }
704  
705 1 ▶sub _bank_save ($self, $client, $m) {
Subroutine _bank_save: called CC=2 SCAR=7.0
706 1 ▶ die "the server was started without a bank file (--bank)\n" unless $self->{bank};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"bank"} 0 1 1
707 1 $self->_write_json($self->{bank}, $self->{synth}->edits);
708 1 $self->_reply($client, { type => 'saved', file => $self->{bank} });
709 1 return;
710 }
711  
712 # What the page sends, held to what it may be.
713  
714 89 ▶sub _clamp ($value, $low, $high) {
Subroutine _clamp: called CC=5 SCAR=16.7
715 89 ▶ $value = 0 unless defined $value && $value =~ $NUMBER;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /$NUMBER/u 0 89 89
Condition33%1/3
defined $value and $value =~ /$NUMBER/u
!ll&&!rl&&r
0089
MC/DC0%0/2
defined $value and $value =~ /$NUMBER/u
defined $value $value =~ /$NUMBER/u
Truth table33%1/3
A and B
A= defined $valueB= $value =~ /$NUMBER/u
ABresult
0X0
100
111
716 89 ▶ return $low if $value < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $value < $low 1 88 89
717 88 ▶ return $high if $value > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $value > $high 5 83 88
718 83 return 0 + $value;
719 }
720  
721 14 ▶sub _channel ($m) { return _clamp($m->{channel} // 0, 0, 15) }
Subroutine _channel: called CC=2 SCAR=7.6
Condition50%1/2
$m->{"channel"} // 0
l!l
140
MC/DC0%0/1
$m->{"channel"} // 0
$m->{"channel"}
Truth table50%1/2
A
A= $m->{"channel"}
Aresult
11
00
722  
723 # (kind, number, bank): a program, the piano unless told, and its variation
724 # (by GS bank; 0 where there is none); or a drum, the bass drum unless told,
725 # and the kit whose patch it is in the kit asked for (the standard kit's,
726 # 0, where the kit does not have the key as its own).
727 14 ▶sub _patch_id ($m) {
Subroutine _patch_id: called CC=7 SCAR=22.1
728 14 ▶ my $kind = ($m->{kind} // '') eq 'drum' ? 'drum' : 'program';
Branch: 1 branch
BranchTrueFalseTotal
($m->{'kind'} // '') eq 'drum' ? : 0 14 14
Condition50%1/2
$m->{"kind"} // ""
l!l
140
MC/DC0%0/1
$m->{"kind"} // ""
$m->{"kind"}
Truth table50%1/2
A
A= $m->{"kind"}
Aresult
11
00
729 14 ▶ my $number = _clamp(int($m->{number} // ($kind eq 'drum' ? 36 : 0)), 0, 127);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'drum' ? : 0 0 0
Condition50%1/2
$m->{"number"} // ($kind eq "drum" ? 36 : 0)
l!l
140
MC/DC0%0/1
$m->{"number"} // ($kind eq "drum" ? 36 : 0)
$m->{"number"}
Truth table50%1/2
A
A= $m->{"number"}
Aresult
11
00
730 14 ▶ my $bank = _clamp(int($m->{bank} // 0), 0, 127);
Condition100%2/2
$m->{"bank"} // 0
l!l
212
MC/DC100%1/1
$m->{"bank"} // 0
$m->{"bank"}
Truth table100%2/2
A
A= $m->{"bank"}
Aresult
11
00
731 14 ▶ $bank = $kind eq 'drum' ? Peta::Synth::Patch::kit_key(Peta::Synth::Patch::kit($bank), $number)
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'drum' ? : 0 14 14
732 : Peta::Synth::Patch::variation($bank, 0, $number);
733 14 return ($kind, $number, $bank);
734 }
735  
736 # A plain field's value: one of its choices, or a number in its range.
737 4 ▶sub _field_value ($name, $value) {
Subroutine _field_value: called CC=6 SCAR=18.0
738 4 my ($field) = grep { $_->{name} eq $name } @{ Peta::Synth::Patch::field_info() };
739 4 ▶ die "no field '$name' can be set\n" unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 1 3 4
740  
741 3 ▶ if ($field->{scale} eq 'choice') {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"scale"} eq "choice") 2 1 3
742 2 ▶ die "'$value' is not one of @{ $field->{choices} }\n" unless grep { $_ eq ($value // '') } @{ $field->{choices} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq ($value // "");} @{$$field{"choices"};} 1 1 2
Condition50%1/2
$value // ""
l!l
120
MC/DC0%0/1
$value // ""
$value
Truth table50%1/2
A
A= $value
Aresult
11
00
743 1 return $value;
744 }
745 1 $value = _clamp($value, $field->{min}, $field->{max});
746 1 ▶ return $field->{scale} eq 'int' ? int($value + 0.5) : $value;
Branch: 1 branch
BranchTrueFalseTotal
$field->{'scale'} eq 'int' ? : 0 1 1
747 }
748  
749 # Formants or partials. No rows: no formants; partials undef, the harmonic series.
750 2 ▶sub _rows ($name, $value) {
Subroutine _rows: called CC=8 SCAR=24.5
751 2 my ($most, @range) = @{ $LIST{$name} };
752 2 ▶ if (ref $value ne 'ARRAY' || !@$value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY" or not @$value) 0 2 2
Condition33%1/3
ref $value ne "ARRAY" or not @$value
l!l&&r!l&&!r
002
MC/DC0%0/2
ref $value ne "ARRAY" or not @$value
ref $value ne "ARRAY" not @$value
Truth table33%1/3
A or not(B)
A= ref $value ne "ARRAY"B= not @$value
ABresult
1X1
011
000
753 0 ▶ return $name eq 'partials' ? undef : [];
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'partials' ? : 0 0 0
754 }
755 2 ▶ die "$name: at most $most\n" if @$value > $most;
Branch: 1 branch
BranchTrueFalseTotal
if @$value > $most 0 2 2
756  
757 2 my @rows;
758 2 for my $row (@$value) {
759 4 ▶ die "$name: each a list of three numbers\n" if ref $row ne 'ARRAY' || @$row != 3;
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "ARRAY" or @$row != 3 0 4 4
Condition33%1/3
ref $row ne "ARRAY" or @$row != 3
l!l&&r!l&&!r
004
MC/DC0%0/2
ref $row ne "ARRAY" or @$row != 3
ref $row ne "ARRAY" @$row != 3
Truth table33%1/3
A or B
A= ref $row ne "ARRAY"B= @$row != 3
ABresult
1X1
011
000
760 4 push @rows, [ map { _clamp($row->[$_], @{ $range[$_] }) } 0 .. 2 ];
761 }
762 2 return \@rows;
763 }
764  
765 # Modulation routes: at most 16 of [source, destination, amount], the amount
766 # held to -24 .. 24.
767 2 ▶sub _mods ($value) {
Subroutine _mods: called CC=10 SCAR=24.4
768 2 ▶ return [] if ref $value ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 0 2 2
769 2 ▶ die "mods: at most 16\n" if @$value > 16;
Branch: 1 branch
BranchTrueFalseTotal
if @$value > 16 0 2 2
770  
771 2 my %source = map { $_ => 1 } @{ Peta::Synth::Patch::mod_sources() };
772 2 my %destination = map { $_ => 1 } @{ Peta::Synth::Patch::mod_destinations() };
773 2 my @routes;
774 2 for my $row (@$value) {
775 3 ▶ die "mods: each a list of source, destination, amount\n" if ref $row ne 'ARRAY' || @$row != 3;
Branch: 1 branch
BranchTrueFalseTotal
if ref $row ne "ARRAY" or @$row != 3 0 3 3
Condition33%1/3
ref $row ne "ARRAY" or @$row != 3
l!l&&r!l&&!r
003
MC/DC0%0/2
ref $row ne "ARRAY" or @$row != 3
ref $row ne "ARRAY" @$row != 3
Truth table33%1/3
A or B
A= ref $row ne "ARRAY"B= @$row != 3
ABresult
1X1
011
000
776 3 my ($from, $to, $amount) = @$row;
777 3 ▶ die "mods: no source '$from'\n" unless $source{ $from // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless $source{$from // ""} 1 2 3
Condition50%1/2
$from // ""
l!l
30
MC/DC0%0/1
$from // ""
$from
Truth table50%1/2
A
A= $from
Aresult
11
00
778 2 ▶ die "mods: no destination '$to'\n" unless $destination{ $to // '' };
Branch: 1 branch
BranchTrueFalseTotal
unless $destination{$to // ""} 0 2 2
Condition50%1/2
$to // ""
l!l
20
MC/DC0%0/1
$to // ""
$to
Truth table50%1/2
A
A= $to
Aresult
11
00
779 2 push @routes, [ $from, $to, _clamp($amount, -24, 24) ];
780 }
781 1 return \@routes;
782 }
783  
784 2 ▶sub _eq_setting ($m) {
Subroutine _eq_setting: called CC=5 SCAR=17.5
785 2 ▶ my ($band, $what) = ($m->{band} // '', $m->{what} // '');
Condition50%2/4
$m->{"band"} // ""
l!l
20
$m->{"what"} // ""
l!l
20
MC/DC0%0/1
$m->{"band"} // ""
$m->{"band"}
MC/DC0%0/1
$m->{"what"} // ""
$m->{"what"}
Truth table50%1/2
A
A= $m->{"band"}
Aresult
11
00
Truth table50%1/2
A
A= $m->{"what"}
Aresult
11
00
786 2 ▶ die "the equalizer has no band '$band'\n" unless grep { $_ eq $band } Peta::Synth::EQ::names();
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $band;} Peta::Synth::EQ::names() 0 2 2
787 2 ▶ die "an equalizer band has no '$what'\n" unless $what =~ /\A(?:freq|gain|q)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $what =~ /\A(?:freq|gain|q)\z/u 0 2 2
788 2 return ($band => { $what => _clamp($m->{value}, -1e6, 1e6) });
789 }
790  
791 # What the page is told.
792  
793 11 ▶sub _patch ($self, $kind, $number, $bank) {
Subroutine _patch: called CC=2 SCAR=7.2
794 11 my $synth = $self->{synth};
795 11 ▶ return $kind eq 'drum' ? $synth->drum_patch($number, $bank) : $synth->program_patch($number, $bank);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'drum' ? : 0 11 11
796 }
797  
798 # A patch as it is, and its fields as they were at the start.
799 6 ▶sub _patch_message ($self, $kind, $number, $bank) {
Subroutine _patch_message: called CC=3 SCAR=11.1
800 6 my $drum = $kind eq 'drum';
801 6 my $patch = $self->_patch($kind, $number, $bank);
802 6 ▶ my $base = $drum ? Peta::Synth::Patch::drum($number, $bank) : Peta::Synth::Patch::program($number, $bank);
Branch: 1 branch
BranchTrueFalseTotal
$drum ? : 0 6 6
803 6 my @names = map { $_->{name} } @{ Peta::Synth::Patch::field_info() };
804 return {
805 type => 'patch',
806 kind => $kind,
807 number => $number,
808 bank => $bank,
809 name => $drum ? Peta::Synth::Patch::kit_name($bank) . ' kit: ' . Peta::Synth::Patch::drum_name($number)
810 : Peta::Synth::Patch::program_name($number, $bank),
811 partials => $patch->{partials},
812 formants => $patch->{formants},
813 mods => $patch->{mods},
814 816 fields => { map { $_ => $patch->{$_} } @names },
815 6 ▶ defaults => { map { $_ => $base->{$_} } @names },
Branch: 1 branch
BranchTrueFalseTotal
$drum ? : 0 6 6
816 };
817 }
818  
819 # The MIDI files of the directory, their names as text (UTF-8 on the disk).
820 9 ▶sub _files ($self) {
Subroutine _files: called CC=3 SCAR=11.1
821 9 ▶ opendir my $dh, $self->{midi_dir} or return [];
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $self->{"midi_dir"} or return [] 9 0 9
822 9 ▶ my @files = map { decode('UTF-8', $_) } grep { /$MIDI_FILE/ && -f File::Spec->catfile($self->{midi_dir}, $_) } readdir $dh;
Condition66%2/3
/$MIDI_FILE/u && -f 'File::Spec'->catfile($self->{'midi_dir'}, $_)
!ll&&!rl&&r
25025
MC/DC50%1/2
/$MIDI_FILE/u && -f 'File::Spec'->catfile($self->{'midi_dir'}, $_)
/$MIDI_FILE/u -f 'File::Spec'->catfile($self->{'midi_dir'}, $_)
Truth table66%2/3
A && B
A= /$MIDI_FILE/uB= -f 'File::Spec'->catfile($self->{'midi_dir'}, $_)
ABresult
0X0
100
111
823 9 closedir $dh;
824 9 return [ sort { lc $a cmp lc $b } @files ];
825 }
826  
827 # Where a MIDI file of the directory is, by its name (text).
828 8 ▶sub _midi_path ($self, $name) { return File::Spec->catfile($self->{midi_dir}, encode('UTF-8', $name)) }
Subroutine _midi_path: called CC=1 SCAR=0.0
829  
830 4 ▶sub _init ($self) {
Subroutine _init: called CC=2 SCAR=7.0
831 4 my $synth = $self->{synth};
832 return {
833 type => 'init',
834 rate => $self->{rate},
835 quantum => $self->{quantum},
836 engine => $synth->engine->name,
837 perl => Peta::Synth::Engine::perl_info(),
838 programs => Peta::Synth::Patch::program_names(),
839 variations => Peta::Synth::Patch::variations(),
840 drums => Peta::Synth::Patch::drum_names(),
841 kits => Peta::Synth::Patch::kits(),
842 fields => Peta::Synth::Patch::field_info(),
843 mod => Peta::Synth::Patch::mod_info(),
844 files => $self->_files,
845 reverb => $synth->reverb->params,
846 eq => $synth->eq->params,
847 64 channel_eq => [ map { $synth->channel_eq($_)->params } 0 .. 15 ],
848 64 ▶ arp => { fields => Peta::Synth::Arpeggiator::field_info(), channels => [ map { $synth->arpeggiator($_)->params } 0 .. 15 ] },
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bank'} ? : 4 0 4
849 bank => $self->{bank} ? basename($self->{bank}) : undef,
850 channels => $self->_channels,
851 synth => {
852 partials => $self->{partials},
853 voices => $synth->{voices},
854 gain => $synth->{gain},
855 fidelity => $synth->{fidelity},
856 auto => $self->{auto},
857 },
858 4 };
859 }
860  
861 2 my @CHANNEL_FIELDS = qw(program variation kit drum volume expression pan send chorus mute);
862  
863 70 ▶sub _channels ($self) {
Subroutine _channels: called CC=1 SCAR=0.0
864 70 my $synth = $self->{synth};
865 70 return [ map { my $c = $synth->channel($_); +{ (map { $_ => $c->{$_} } @CHANNEL_FIELDS), held => $synth->held($_) } } 0 .. 15 ];
866 }
867  
868 5 ▶sub _broadcast_channels ($self) {
Subroutine _broadcast_channels: called CC=1 SCAR=0.0
869 5 $self->_broadcast({ type => 'channels', channels => $self->_channels });
870 5 return;
871 }
872  
873 # Five times a second: the voices and the loudest of them on each channel,
874 # the load (none while no page plays the sound: nothing renders), the
875 # channels (and what each holds against the file) and arpeggiators as they
876 # are, how far the file has played.
877 61 ▶sub _broadcast_state ($self) {
Subroutine _broadcast_state: called CC=9 SCAR=22.0
878 61 ▶ return unless %{ $self->{clients} };
Branch: 1 branch
BranchTrueFalseTotal
unless %{$self->{"clients"};} 0 61 61
879  
880 61 my $synth = $self->{synth};
881 61 my $playing = $self->{playing};
882 61 ▶ my $load = (grep { $_->{audio} } values %{ $self->{clients} }) ? $synth->load : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$_->{'audio'};} values %{$$self{'clients'};}) ? : 58 3 61
883 61 my @level = (0) x 16;
884 61 my @voices = (0) x 16;
885 61 for my $voice (@{ $synth->{active} }) {
886 58 my $c = $voice->{channel};
887 58 ▶ my $l = ($voice->{raw_env} // 0) * $voice->{level};
Condition100%2/2
$voice->{"raw_env"} // 0
l!l
544
MC/DC100%1/1
$voice->{"raw_env"} // 0
$voice->{"raw_env"}
Truth table100%2/2
A
A= $voice->{"raw_env"}
Aresult
11
00
888 58 $voices[$c]++;
889 58 ▶ $level[$c] = $l if $l > $level[$c];
Branch: 1 branch
BranchTrueFalseTotal
if $l > $level[$c] 54 4 58
890 }
891  
892 $self->_broadcast({
893 type => 'state',
894 61 voices => scalar @{ $synth->{active} },
895 load => 0 + sprintf('%.3f', $load),
896 partials => $synth->{partials},
897 976 level => [ map { 0 + sprintf '%.3f', $_ } @level ],
898 per_channel => \@voices,
899 channels => $self->_channels,
900 976 ▶ arps => [ map { $synth->arpeggiator($_)->params } 0 .. 15 ],
Branch: 3 branches
BranchTrueFalseTotal
$playing ? : 13 48 61
defined $playing->{'paused'} ? : 4 9 13
$self->{'recording'} ? : 3 58 61
901 playing => $playing ? {
902 file => $playing->{file},
903 length => $playing->{length},
904 position => $self->_position,
905 paused => defined $playing->{paused} ? 1 : 0,
906 } : undef,
907 recording => $self->{recording} ? time - $self->{recording}{started} : undef,
908 61 });
909 61 return;
910 }
911  
912 1 ▶sub _read_json ($self, $file) {
Subroutine _read_json: called CC=2 SCAR=7.1
913 1 ▶ open my $fh, '<:raw', $file or die "cannot read '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $file or die "cannot read '${file}': $!\n" 1 0 1
914 1 local $/;
915 1 return $self->{json}->decode(scalar <$fh>);
916 }
917  
918 # Written to a file beside it, then moved over it: never half a file.
919 1 ▶sub _write_json ($self, $file, $data) {
Subroutine _write_json: called CC=4 SCAR=14.3
920 1 my $temp = "$file.tmp";
921 1 ▶ open my $fh, '>:raw', $temp or die "cannot write '$temp': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $temp or die "cannot write '${temp}': $!\n" 1 0 1
922 1 print {$fh} JSON::PP->new->canonical->pretty->encode($data);
923 1 ▶ close $fh or die "cannot write '$temp': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "cannot write '${temp}': $!\n" 1 0 1
924 1 ▶ rename $temp, $file or die "cannot write '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
rename $temp, $file or die "cannot write '${file}': $!\n" 1 0 1
925 1 return;
926 }
927  
928 2 1;
929  
930 __END__
« lib/Peta/Synth/Reverb.pm ↑ Summary lib/Peta/Synth/Arpeggiator.pm »