Stream/Decoder.pm

filter: Statementstmt 95.4% 126 / 132 Branchbran 88.4% 46 / 52 Conditioncond 86.9% 20 / 23 MC/DCmcdc 76.9% 10 / 13 Subroutinesub 91.3% 21 / 23 Podpod 77.7% 7 / 9 totaltotal 91.2% 230 / 252 CC 40 SCAR 21.4
CC
40
Cov
93%
decode
18.0
__ANON__
10.3
__ANON__
7.0
CRAP 8.5
« Stream/EventFactory.pm ↑ Summary Stream/Tables.pm »
1 13 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use Feature::Compat::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: MIDI bytestream decoder
6  
7 package MIDI::Stream::Decoder;
8 1 ▶class MIDI::Stream::Decoder :isa( MIDI::Stream );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 13 our $VERSION = '0.005';
12  
13 13 ▶use Time::HiRes qw/ gettimeofday tv_interval /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 13 ▶use Carp qw/ carp croak /;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 ▶use MIDI::Stream::Tables qw/ is_cc is_realtime message_length combine_bytes /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 13 ▶use MIDI::Stream::FIFO;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 13 ▶use MIDI::Stream::EventFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 13 field $retain_events :param = 1;
21  
22 13 field $enable_14bit_cc :param = 0;
23 13 field @cc;
24  
25 13 field $callback :param = sub { @_ };
26 13 field $filter_cb = {};
27  
28 13 field $clock_ppqn :param = 24;
29 13 field $clock_samples :param = 24;
30 13 field $clock_fifo = MIDI::Stream::FIFO->new( length => $clock_samples );
31 13 field $round_tempo :param = 0;
32  
33 13 field @events;
34 13 field @pending_event;
35 13 field $message_length;
36  
37 ▶my $_expand_cc = method( $event ) {
Subroutine __ANON__: called CC=7 SCAR=19.5
38 171 ▶ return $event unless is_cc( $event->[ 0 ] );
Branch: 1 branch
BranchTrueFalseTotal
unless is_cc($event->[0]) 124 47 171
39 47 ▶ return $event unless $enable_14bit_cc;
Branch: 1 branch
BranchTrueFalseTotal
unless $enable_14bit_cc 29 18 47
40 18 ▶ return $event if $event->[ 1 ] > 0x3f;
Branch: 1 branch
BranchTrueFalseTotal
if $event->[1] > 63 4 14 18
41 14 ▶ return $event if $event->[ 2 ] > 0x7f;
Branch: 1 branch
BranchTrueFalseTotal
if $event->[2] > 127 0 14 14
42  
43 14 ▶ if ( $event->[ 1 ] & 0x20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event->[1] & 32) 8 6 14
44 8 my $msb = $cc[ $event->[ 1 ] & ~0x20 ];
45 8 ▶ return unless defined $msb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msb 0 8 8
46 8 $event->[ 2 ] = combine_bytes( $event->[ 2 ], $msb );
47 8 $event->[ 1 ] &= ~0x20;
48 8 return $event;
49 }
50  
51 6 $cc[ $event->[ 1 ] ] = $event->[ 2 ];
52 6 return;
53 13 };
54  
55 ▶my $_sample_clock = method() {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 22 state $t = [ gettimeofday ];
57 22 $clock_fifo->add( tv_interval( $t ) );
58 22 $t = [ gettimeofday ];
59 13 };
60  
61 171 ▶my $_push_event = method( $event = undef ) {
Subroutine __ANON__: called CC=10 SCAR=23.3
62 171 state $t = [ gettimeofday ];
63 # Do not use a reference to @pending_event!
64 # Contents will have changed by the time you get round to using it.
65 171 ▶ $event //= [ @pending_event ];
Condition100%2/2
$event //= [@pending_event]
l!l
34137
MC/DC100%1/1
$event //= [@pending_event]
$event
Truth table100%2/2
A
A= $event
Aresult
11
00
66 171 $event = $self->$_expand_cc( $event );
67 171 ▶ return unless $event;
Branch: 1 branch
BranchTrueFalseTotal
unless $event 6 165 171
68 165 my $dt = tv_interval( $t );
69 165 $t = [ gettimeofday ];
70  
71 165 my $stream_event = MIDI::Stream::EventFactory->event( $dt, $event );
72  
73 165 ▶ if ( !$stream_event ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stream_event) 0 165 165
74 0 carp( "Ignoring unknown status $event->[0]" );
75 0 return;
76 }
77  
78 165 ▶ push @events, $stream_event if $retain_events;
Branch: 1 branch
BranchTrueFalseTotal
if $retain_events 165 0 165
79  
80 165 ▶ my @callbacks = ( $filter_cb->{ all } // [] )->@*;
Condition50%1/2
$filter_cb->{"all"} // []
l!l
0165
MC/DC0%0/1
$filter_cb->{"all"} // []
$filter_cb->{"all"}
Truth table50%1/2
A
A= $filter_cb->{"all"}
Aresult
11
00
81 165 ▶ push @callbacks, ( $filter_cb->{ $stream_event->name } // [] )->@*;
Condition100%2/2
$$filter_cb{$stream_event->name} // []
l!l
10155
MC/DC100%1/1
$$filter_cb{$stream_event->name} // []
$$filter_cb{$stream_event->name}
Truth table100%2/2
A
A= $$filter_cb{$stream_event->name}
Aresult
11
00
82  
83 165 for my $cb ( @callbacks ) {
84 13 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 10 ▶ last if $cb->( $stream_event ) eq $self->stop;
Branch: 1 branch
BranchTrueFalseTotal
if &$cb($stream_event) eq $self->stop 2 8 10
86 }
87  
88 165 $callback->( $stream_event );
89 13 };
90  
91 225 ▶my $_reset_pending_event = method( $status = undef ) {
Subroutine __ANON__: called CC=3 SCAR=11.0
92 225 @pending_event = ();
93 225 ▶ push @pending_event, $status if defined $status;
Branch: 1 branch
BranchTrueFalseTotal
if defined $status 222 3 225
94 225 $message_length = message_length( $status );
95 13 };
96  
97  
98 ▶method decode( @bytestrings ) {
Subroutine decode: called CC=18 SCAR=28.9
Pod documented
99 52 my $bytestring = join '', @bytestrings;
100 52 my @bytes = unpack 'C*', $bytestring;
101 52 my $status;
102  
103 BYTE:
104 52 ▶ while ( @bytes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bytes) 429 52 481
105  
106 # Status byte - start/end of message
107 429 ▶ if ( $bytes[0] & 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes[0] & 128) 131 298 429
108 131 my $status = shift @bytes;
109  
110 # Sample the clock to determine tempo ASAP
111 131 ▶ $status == 0xf8 && $self->$_sample_clock();
Branch: 1 branch
BranchTrueFalseTotal
$status == 248 and $self->$_sample_clock 22 109 131
112  
113 # End-of-Xclusive
114 131 ▶ if ( $status == 0xf7 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 247) 4 127 131
115 4 ▶ carp( "EOX received for non-SysEx message - ignoring!") && next BYTE
Branch: 1 branch
BranchTrueFalseTotal
unless $pending_event[0] == 240 1 3 4
Condition60%3/5
$pending_event[0] == 240 || carp('EOX received for non-SysEx message - ignoring!') && (next BYTE)
l!l&&r!l&&!r
310
carp("EOX received for non-SysEx message - ignoring!") and next BYTE
!ll
01
MC/DC0%0/2
$pending_event[0] == 240 || carp('EOX received for non-SysEx message - ignoring!') && (next BYTE)
$pending_event[0] == 240 carp("EOX received for non-SysEx message - ignoring!")
Truth table66%2/3
A || B
A= $pending_event[0] == 240B= carp("EOX received for non-SysEx message - ignoring!")
ABresult
1X1
011
000
116 unless $pending_event[0] == 0xf0;
117 3 $self->$_push_event();
118 3 $self->$_reset_pending_event();
119 3 next BYTE;
120 }
121  
122 # Real-Time messages can appear within other messages.
123 127 ▶ if ( is_realtime( $status ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_realtime($status)) 31 96 127
124 # Push unless we have an undefined realtime status
125 31 ▶ $self->$_push_event( [ $status ] ) unless $status == 0xf9 || $status == 0xfd;
Branch: 1 branch
BranchTrueFalseTotal
unless $status == 249 or $status == 253 29 2 31
Condition100%3/3
$status == 249 or $status == 253
l!l&&r!l&&!r
1129
MC/DC100%2/2
$status == 249 or $status == 253
$status == 249 $status == 253
Truth table100%3/3
A or B
A= $status == 249B= $status == 253
ABresult
1X1
011
000
126 31 next BYTE;
127 }
128  
129 # Any non-Real-Time status byte ends a SysEx
130 # Push the pending sysex and proceed ...
131 96 ▶ if ( @pending_event && $pending_event[0] == 0xf0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending_event and $pending_event[0] == 240) 1 95 96
Condition100%3/3
@pending_event and $pending_event[0] == 240
!ll&&!rl&&r
18771
MC/DC100%2/2
@pending_event and $pending_event[0] == 240
@pending_event $pending_event[0] == 240
Truth table100%3/3
A and B
A= @pending_eventB= $pending_event[0] == 240
ABresult
0X0
100
111
132 1 $self->$_push_event();
133 }
134  
135 # Undefined system statuses which should reset running status -
136 # a full message needs to be received after this
137 96 ▶ if ( $status == 0xf4 || $status == 0xf5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status == 244 or $status == 245) 2 94 96
Condition100%3/3
$status == 244 or $status == 245
l!l&&r!l&&!r
1194
MC/DC100%2/2
$status == 244 or $status == 245
$status == 244 $status == 245
Truth table100%3/3
A or B
A= $status == 244B= $status == 245
ABresult
1X1
011
000
138 2 @pending_event = ();
139 2 next BYTE;
140 }
141  
142 # Should now be able to push any single-byte statuses,
143 # e.g. Tune request
144 94 ▶ if ( message_length( $status ) == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (message_length($status) == 1) 5 89 94
145 5 @pending_event = ();
146 5 $self->$_push_event( [ $status ] );
147 5 next BYTE;
148 }
149  
150 89 $self->$_reset_pending_event( $status );
151 89 next BYTE;
152 } # end if status byte
153  
154 298 my $byte = shift @bytes;
155 298 ▶ next BYTE unless @pending_event;
Branch: 1 branch
BranchTrueFalseTotal
unless @pending_event 4 294 298
156  
157 294 push @pending_event, $byte;
158 294 my $remaining = $message_length - @pending_event;
159  
160 # A complete message denoted by length, not upcoming status bytes
161 294 ▶ if ( $message_length && $remaining <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message_length and $remaining <= 0) 133 161 294
Condition100%3/3
$message_length and $remaining <= 0
!ll&&!rl&&r
38123133
MC/DC100%2/2
$message_length and $remaining <= 0
$message_length $remaining <= 0
Truth table100%3/3
A and B
A= $message_lengthB= $remaining <= 0
ABresult
0X0
100
111
162 133 $self->$_push_event();
163 133 $self->$_reset_pending_event( $pending_event[0] );
164 }
165 } # end while
166  
167 52 scalar @events;
168 }
169  
170  
171 ▶method attach_callback( $event, $callback ) {
Subroutine attach_callback: called CC=3 SCAR=11.0
Pod documented
172 10 ▶ if ( ref $event eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq "ARRAY") 2 8 10
173 2 $self->attach_callback( $_, $callback ) for $event->@*;
174 2 return;
175 }
176 8 push $filter_cb->{ $event }->@*, $callback;
177 }
178  
179  
180 ▶method cancel_event_callback( $event ) {
Subroutine cancel_event_callback: called CC=3 SCAR=14.2
Pod documented
181 1 ▶ if ( ref $event eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $event eq "ARRAY") 0 1 1
182 0 $self->cancel_event_callbacks( $_ ) for $event->@*;
183 0 return;
184 }
185 1 delete $filter_cb->{ $event };
186 }
187  
188  
189 method cancel_callback {
190 0 ▶ undef $callback;
Subroutine cancel_callback: not called CC=1 SCAR=6.9
Pod documented
191 }
192  
193  
194 method events {
195 35 ▶ splice @events;
Subroutine events: called CC=1 SCAR=0.0
Pod documented
196 }
197  
198  
199 method fetch_one_event {
200 3 ▶ shift @events;
Subroutine fetch_one_event: called CC=1 SCAR=0.0
Pod documented
201 }
202  
203  
204 method tempo {
205 1 ▶ my $tempo = 60 / ( $clock_fifo->average * $clock_ppqn );
Subroutine tempo: called CC=2 SCAR=7.2
Pod documented
206 1 ▶ $round_tempo ? sprintf( '%.0f', $tempo ) : $tempo;
Branch: 1 branch
BranchTrueFalseTotal
$round_tempo ? : 1 0 1
207 }
208  
209 0 ▶method continue { MIDI::Stream::Tables::continue() }
Subroutine continue: not called CC=1 SCAR=6.9
Pod undocumented
210 12 ▶method stop { MIDI::Stream::Tables::stop() }
Subroutine stop: called CC=1 SCAR=0.0
Pod undocumented
211  
212 13 1;
213  
214 __END__
« Stream/EventFactory.pm ↑ Summary Stream/Tables.pm »