lib/Peta/MIDI/Decoder.pm

filter: Statementstmt 93.2% 97 / 104 Branchbran 86.2% 50 / 58 Conditioncond 70.2% 26 / 37 MC/DCmcdc 50.0% 11 / 22 Subroutinesub 84.2% 16 / 19 Podpod 100% 10 / 10 totaltotal 84.0% 210 / 250 CC 51 SCAR 22.6
CC
51
Cov
87%
decode
19.4
_push
12.1
new
6.7
CRAP 9.5
« lib/Peta/MIDI/Device.pm ↑ Summary lib/Peta/MP3/Encode.pm »
1 package Peta::MIDI::Decoder;
2 # ABSTRACT: MIDI bytes into events
3  
4 # MIDI bytes, as a device or a port sends them, into events
5 # (Peta::MIDI::Event): running status; system exclusive from F0 to F7 (or
6 # to the next status byte); real-time bytes (clock, start, active sensing,
7 # ...) wherever they come, even inside another message. Bytes may come in
8 # any pieces. The events are kept (retain_events) and handed to callbacks,
9 # by name; the clock's ticks give the tempo.
10 #
11 # With enable_14bit_cc, controllers 0 .. 31 and their fine parts 32 .. 63
12 # are one: the coarse part alone is kept back, the fine part gives one
13 # control_change of the coarse number and a value of 14 bits.
14  
15 3 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Peta::MIDI::Event qw(message_length combine_bytes keys_for);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 our $VERSION = '0.2610180';
20  
21 3 ▶use constant { STOP => 'stop', CONTINUE => 'continue' };
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 my @NAMES = qw(note_off note_on polytouch control_change program_change aftertouch pitch_bend sysex timecode
24 song_position song_select tune_request eox clock start continue stop active_sensing system_reset);
25  
26 13 ▶sub new ($class, %arg) {
Subroutine new: called CC=6 SCAR=19.1
Pod documented
27 return bless {
28 retain_events => $arg{retain_events} // 1,
29 enable_14bit_cc => $arg{enable_14bit_cc} // 0,
30 callback => $arg{callback},
31 clock_ppqn => $arg{clock_ppqn} // 24,
32 clock_samples => $arg{clock_samples} // 24,
33 13 ▶ round_tempo => $arg{round_tempo} // 0,
Condition70%7/10
$arg{"retain_events"} // 1
l!l
112
$arg{"enable_14bit_cc"} // 0
l!l
112
$arg{"clock_ppqn"} // 24
l!l
013
$arg{"clock_samples"} // 24
l!l
013
$arg{"round_tempo"} // 0
l!l
013
MC/DC100%1/1
$arg{"retain_events"} // 1
$arg{"retain_events"}
MC/DC100%1/1
$arg{"enable_14bit_cc"} // 0
$arg{"enable_14bit_cc"}
MC/DC0%0/1
$arg{"clock_ppqn"} // 24
$arg{"clock_ppqn"}
MC/DC0%0/1
$arg{"clock_samples"} // 24
$arg{"clock_samples"}
MC/DC0%0/1
$arg{"round_tempo"} // 0
$arg{"round_tempo"}
Truth table100%2/2
A
A= $arg{"retain_events"}
Aresult
11
00
Truth table100%2/2
A
A= $arg{"enable_14bit_cc"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"clock_ppqn"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"clock_samples"}
Aresult
11
00
Truth table50%1/2
A
A= $arg{"round_tempo"}
Aresult
11
00
34 events => [],
35 pending => [], # the message so far: status, data
36 length => 0, # how long it is whole (0: system exclusive)
37 coarse => [], # controllers' coarse parts, with enable_14bit_cc
38 filters => {}, # name => [ callbacks ]
39 last => undef, # when the last event was
40 ticks => [], # the clock's last intervals
41 tick => undef,
42 }, $class;
43 }
44  
45 1 ▶sub stop ($self) { return STOP }
Subroutine stop: called CC=1 SCAR=0.0
Pod documented
46 0 ▶sub continue ($self) { return CONTINUE }
Subroutine continue: not called CC=1 SCAR=6.9
Pod documented
47  
48 # Bytes in; how many events are kept.
49 26 ▶sub decode ($self, @strings) {
Subroutine decode: called CC=19 SCAR=29.7
Pod documented
50 26 for my $byte (unpack 'C*', join '', @strings) {
51 108 ▶ if ($byte >= 0xf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte >= 248) 22 86 108
52 # Real time: wherever it comes, it is a message of its own.
53 22 ▶ $self->_sample_clock if $byte == 0xf8;
Branch: 1 branch
BranchTrueFalseTotal
if $byte == 248 16 6 22
54 22 ▶ $self->_push([$byte]) if $byte != 0xf9 && $byte != 0xfd;
Branch: 1 branch
BranchTrueFalseTotal
if $byte != 249 and $byte != 253 22 0 22
Condition33%1/3
$byte != 249 and $byte != 253
!ll&&!rl&&r
0022
MC/DC0%0/2
$byte != 249 and $byte != 253
$byte != 249 $byte != 253
Truth table33%1/3
A and B
A= $byte != 249B= $byte != 253
ABresult
0X0
100
111
55 22 next;
56 }
57 86 my $pending = $self->{pending};
58 86 ▶ if ($byte == 0xf7) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 247) 2 84 86
59 # The end of a system exclusive message.
60 2 ▶ $self->_push([ @$pending, 0xf7 ]) if @$pending && $pending->[0] == 0xf0;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending and $pending->[0] == 240 2 0 2
Condition33%1/3
@$pending and $pending->[0] == 240
!ll&&!rl&&r
002
MC/DC0%0/2
@$pending and $pending->[0] == 240
@$pending $pending->[0] == 240
Truth table33%1/3
A and B
A= @$pendingB= $pending->[0] == 240
ABresult
0X0
100
111
61 2 $self->_start;
62 2 next;
63 }
64 84 ▶ if ($byte >= 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ($byte >= 128) 29 55 84
65 # Any other status ends a system exclusive message.
66 29 ▶ $self->_push([ @$pending, 0xf7 ]) if @$pending && $pending->[0] == 0xf0;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending and $pending->[0] == 240 1 28 29
Condition100%3/3
@$pending and $pending->[0] == 240
!ll&&!rl&&r
18101
MC/DC100%2/2
@$pending and $pending->[0] == 240
@$pending $pending->[0] == 240
Truth table100%3/3
A and B
A= @$pendingB= $pending->[0] == 240
ABresult
0X0
100
111
67 29 ▶ if ($byte == 0xf4 || $byte == 0xf5) { # undefined: no running status after them
Branch: 1 branch
BranchTrueFalseTotal
if ($byte == 244 or $byte == 245) 1 28 29
Condition66%2/3
$byte == 244 or $byte == 245
l!l&&r!l&&!r
1028
MC/DC50%1/2
$byte == 244 or $byte == 245
$byte == 244 $byte == 245
Truth table66%2/3
A or B
A= $byte == 244B= $byte == 245
ABresult
1X1
011
000
68 1 $self->_start;
69 1 next;
70 }
71 28 ▶ if (message_length($byte) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (message_length($byte) == 1) 2 26 28
72 2 $self->_push([$byte]);
73 2 $self->_start;
74 2 next;
75 }
76 26 $self->_start($byte);
77 26 next;
78 }
79 55 ▶ next if !@$pending; # a data byte without a status
Branch: 1 branch
BranchTrueFalseTotal
unless @$pending 3 52 55
80 52 push @$pending, $byte;
81 52 ▶ if ($self->{length} && @$pending >= $self->{length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"length"} and @$pending >= $self->{"length"}) 24 28 52
Condition100%3/3
$self->{"length"} and @$pending >= $self->{"length"}
!ll&&!rl&&r
91924
MC/DC100%2/2
$self->{"length"} and @$pending >= $self->{"length"}
$self->{"length"} @$pending >= $self->{"length"}
Truth table100%3/3
A and B
A= $self->{"length"}B= @$pending >= $self->{"length"}
ABresult
0X0
100
111
82 24 $self->_push([@$pending]);
83 24 ▶ $self->_start($pending->[0] < 0xf0 ? $pending->[0] : undef); # running status, for channel messages
Branch: 1 branch
BranchTrueFalseTotal
$pending->[0] < 240 ? : 20 4 24
84 }
85 }
86 26 return scalar @{ $self->{events} };
87 }
88  
89 55 ▶sub _start ($self, $status = undef) {
Subroutine _start: called CC=4 SCAR=13.9
90 55 ▶ $self->{pending} = defined $status ? [$status] : [];
Branch: 1 branch
BranchTrueFalseTotal
defined $status ? : 46 9 55
91 55 ▶ $self->{length} = defined $status ? message_length($status) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $status ? : 46 9 55
92 55 return;
93 }
94  
95 51 ▶sub _push ($self, $message) {
Subroutine _push: called CC=12 SCAR=24.9
96 51 ▶ $message = $self->_fourteen($message) if $self->{enable_14bit_cc} && ($message->[0] & 0xf0) == 0xb0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"enable_14bit_cc"} and ($message->[0] & 240) == 176 3 48 51
Condition66%2/3
$self->{"enable_14bit_cc"} and ($message->[0] & 240) == 176
!ll&&!rl&&r
4803
MC/DC50%1/2
$self->{"enable_14bit_cc"} and ($message->[0] & 240) == 176
$self->{"enable_14bit_cc"} ($message->[0] & 240) == 176
Truth table66%2/3
A and B
A= $self->{"enable_14bit_cc"}B= ($message->[0] & 240) == 176
ABresult
0X0
100
111
97 51 ▶ return if !$message;
Branch: 1 branch
BranchTrueFalseTotal
unless $message 1 50 51
98  
99 50 my $now = time;
100 50 ▶ my $dt = defined $self->{last} ? $now - $self->{last} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'last'} ? : 38 12 50
101 50 my $event = Peta::MIDI::Event->new($message, $dt);
102 50 $self->{last} = $now;
103 50 ▶ push @{ $self->{events} }, $event if $self->{retain_events};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"retain_events"} 48 2 50
104  
105 50 ▶ for my $callback (@{ $self->{filters}{all} // [] }, @{ $self->{filters}{ $event->name } // [] }) {
Condition100%4/4
$self->{"filters"}{"all"} // []
l!l
248
$self->{"filters"}{$event->name} // []
l!l
248
MC/DC100%1/1
$self->{"filters"}{"all"} // []
$self->{"filters"}{"all"}
MC/DC100%1/1
$self->{"filters"}{$event->name} // []
$self->{"filters"}{$event->name}
Truth table100%2/2
A
A= $self->{"filters"}{"all"}
Aresult
11
00
Truth table100%2/2
A
A= $self->{"filters"}{$event->name}
Aresult
11
00
106 3 my $said = $callback->($event);
107 3 ▶ last if defined $said && $said eq STOP;
Branch: 1 branch
BranchTrueFalseTotal
if defined $said and $said eq "stop" 1 2 3
Condition66%2/3
defined $said and $said eq "stop"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $said and $said eq "stop"
defined $said $said eq "stop"
Truth table66%2/3
A and B
A= defined $saidB= $said eq "stop"
ABresult
0X0
100
111
108 }
109 50 ▶ $self->{callback}->($event) if $self->{callback};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"callback"} 0 50 50
110 50 return;
111 }
112  
113 # A controller of 14 bits: the coarse part kept, the fine part given with it.
114 3 ▶sub _fourteen ($self, $message) {
Subroutine _fourteen: called CC=4 SCAR=13.9
115 3 my ($status, $number, $value) = @$message;
116 3 ▶ return $message if $number > 0x3f;
Branch: 1 branch
BranchTrueFalseTotal
if $number > 63 1 2 3
117 2 my $channel = $status & 0x0f;
118 2 ▶ if ($number & 0x20) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number & 32) 1 1 2
119 1 ▶ my $coarse = $self->{coarse}[$channel][ $number & 0x1f ] // return;
Condition50%1/2
$self->{"coarse"}[$channel][$number & 31] // (return)
l!l
10
MC/DC0%0/1
$self->{"coarse"}[$channel][$number & 31] // (return)
$self->{"coarse"}[$channel][$number & 31]
Truth table50%1/2
A
A= $self->{"coarse"}[$channel][$number & 31]
Aresult
11
00
120 1 return [ $status, $number & 0x1f, combine_bytes($value, $coarse) ];
121 }
122 1 $self->{coarse}[$channel][$number] = $value;
123 1 return;
124 }
125  
126 16 ▶sub _sample_clock ($self) {
Subroutine _sample_clock: called CC=3 SCAR=11.1
127 16 my $now = time;
128 16 ▶ if (defined $self->{tick}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"tick"}) 12 4 16
129 12 push @{ $self->{ticks} }, $now - $self->{tick};
130 0 ▶ shift @{ $self->{ticks} } while @{ $self->{ticks} } > $self->{clock_samples};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{"ticks"};} > $self->{"clock_samples"} and do { shift @{$$self{"ticks"};} } 0 12 12
131 }
132 16 $self->{tick} = $now;
133 16 return;
134 }
135  
136 # The tempo (beats a minute) the clock's ticks give; undef before two.
137 1 ▶sub tempo ($self) {
Subroutine tempo: called CC=5 SCAR=16.7
Pod documented
138 1 ▶ my @ticks = @{ $self->{ticks} } or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my(@ticks) = @{$self->{"ticks"};} or return undef 1 0 1
139 1 my $mean = 0;
140 1 $mean += $_ / @ticks for @ticks;
141 1 ▶ return undef if !$mean;
Branch: 1 branch
BranchTrueFalseTotal
unless $mean 0 1 1
142 1 my $tempo = 60 / ($mean * $self->{clock_ppqn});
143 1 ▶ return $self->{round_tempo} ? 0 + sprintf('%.0f', $tempo) : $tempo;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'round_tempo'} ? : 0 1 1
144 }
145  
146 # Callbacks by event name, a pattern of names, a list of them, or 'all';
147 # a callback's returning $decoder->stop ends the others for that event.
148 3 ▶sub attach_callback ($self, $which, $callback) {
Subroutine attach_callback: called CC=2 SCAR=6.9
Pod documented
149 3 push @{ $self->{filters}{$_} }, $callback for $self->_names($which);
150 3 return;
151 }
152  
153 0 ▶sub cancel_event_callback ($self, $which) {
Subroutine cancel_event_callback: not called CC=2 SCAR=17.9
Pod documented
154 0 delete $self->{filters}{$_} for $self->_names($which);
155 0 return;
156 }
157  
158 3 ▶sub _names ($self, $which) {
Subroutine _names: called CC=3 SCAR=11.3
159 0 ▶ return map { $self->_names($_) } @$which if ref $which eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $which eq "ARRAY" 0 3 3
160 3 ▶ return grep { $_ =~ $which } @NAMES if ref $which eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $which eq "Regexp" 1 2 3
161 2 return $which;
162 }
163  
164 0 ▶sub cancel_callback ($self) {
Subroutine cancel_callback: not called CC=1 SCAR=6.9
Pod documented
165 0 $self->{callback} = undef;
166 0 return;
167 }
168  
169 # The events kept, taken; or the first.
170 10 ▶sub events ($self) { return splice @{ $self->{events} } }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
171 1 ▶sub fetch_one_event ($self) { return shift @{ $self->{events} } }
Subroutine fetch_one_event: called CC=1 SCAR=0.0
Pod documented
172  
173 3 1;
174  
175 __END__
« lib/Peta/MIDI/Device.pm ↑ Summary lib/Peta/MP3/Encode.pm »