Stream/Decoder.pm

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