MIDI.pm

filter: Statementstmt 100% 250 / 250 Branchbran 95.8% 140 / 146 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 24 / 24 Podpod 100% 16 / 16 totaltotal 98.6% 430 / 436 CC 74 SCAR 22.1
CC
74
Cov
98%
midi_text
22.0
_dpv
8.0
midi_header
8.0
CRAP 9.1
« MIDI/ConfigData.pm ↑ Summary
1 # -*- Perl -*-
2 #
3 # A module to test MIDI file contents.
4  
5 package Test2::Tools::MIDI;
6 2 our $VERSION = '0.02';
7 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Test2::API 'context';
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 our @EXPORT = qw(
15 midi_aftertouch midi_channel_aftertouch midi_control_change midi_eof
16 midi_footer midi_header midi_note_off midi_note_on midi_patch
17 midi_pitch_wheel midi_skip midi_skip_dtime midi_tempo midi_text midi_track
18 );
19  
20 61 ▶sub _failure ($$\@) {
Subroutine _failure: called CC=2 SCAR=6.9
21 61 my $result = 1;
22 61 ▶ if ( @{ $_[2] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$_[2];}) { } 24 37 61
23 $_[0]->fail( $_[1], join ' ',
24 24 map { "$_->[0] [$_->[1],$_->[2]]" } @{ $_[2] } );
25 24 $result = 0;
26 } else {
27 37 $_[0]->pass( $_[1] );
28 }
29 61 $_[0]->release;
30 61 return $result;
31 }
32  
33 # note_on, note_off and a few others share this pattern, though you may
34 # need to squint a little to make the "controller" and "value" of
35 # control_change fit the pitch and velocity fields
36 17 ▶sub _dpv ($$$$$$$) {
Subroutine _dpv: called CC=8 SCAR=20.8
37 17 my ( $fh, $dtime, $channel, $pitch, $velocity, $want_code, $name ) =
38 @_;
39  
40 17 my @failure;
41 17 my $q = read_vlq($fh);
42 17 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 4 13 17
43 4 push @failure, [ dtime => $q, $dtime ];
44 }
45 17 my $amount = read $fh, my $track, 3;
46 17 ▶ confess "$name read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 17 17
47 17 ▶ if ( $amount != 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 3) 1 16 17
48 1 push @failure, [ length => $amount, 3 ];
49 1 goto FAIL_DPV;
50 }
51 16 my ( $pa, $re, $ci ) = unpack CCC => $track;
52 16 my ( $ch, $code ) = ( $pa & 0xF, $pa & 0xF0 );
53 16 ▶ if ( $ch != $channel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch != $channel) 4 12 16
54 4 push @failure, [ channel => $ch, $channel ];
55 }
56 16 ▶ if ( $code != $want_code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != $want_code) 2 14 16
57 2 push @failure, [ code => $ch, $want_code ];
58 }
59 16 ▶ if ( $re != $pitch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re != $pitch) 4 12 16
60 4 push @failure, [ pitch => $re, $pitch ];
61 }
62 16 ▶ if ( $ci != $velocity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ci != $velocity) 4 12 16
63 4 push @failure, [ velocity => $ci, $velocity ];
64 }
65 FAIL_DPV:
66 17 _failure( context(), $name, @failure );
67 }
68  
69 1 ▶sub midi_aftertouch ($$$$$) {
Subroutine midi_aftertouch: called CC=1 SCAR=0.0
Pod documented
70 1 push @_, 0xA0, 'MIDI key_after_touch';
71 1 goto &_dpv;
72 }
73  
74 3 ▶sub midi_channel_aftertouch ($$$$) {
Subroutine midi_channel_aftertouch: called CC=7 SCAR=19.5
Pod documented
75 3 my ( $fh, $dtime, $channel, $velocity ) = @_;
76 3 my @failure;
77 3 my $q = read_vlq($fh);
78 3 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 1 2 3
79 1 push @failure, [ dtime => $q, $dtime ];
80 }
81 3 my $amount = read $fh, my $track, 2;
82 3 ▶ confess "midi_channel_aftertouch read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 3 3
83 3 ▶ if ( $amount != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 2) 1 2 3
84 1 push @failure, [ length => $amount, 2 ];
85 1 goto FAIL_CHAFT;
86 }
87 2 my ( $pa, $velo ) = unpack CC => $track;
88 2 my ( $ch, $code ) = ( $pa & 0xF, $pa & 0xF0 );
89 2 ▶ if ( $ch != $channel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch != $channel) 1 1 2
90 1 push @failure, [ channel => $ch, $channel ];
91 }
92 2 ▶ if ( $code != 0xD0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 208) 1 1 2
93 1 push @failure, [ code => $code, 0xD0 ];
94 }
95 2 ▶ if ( $velo != $velocity ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($velo != $velocity) 1 1 2
96 1 push @failure, [ velocity => $velo, $velocity ];
97 }
98 FAIL_CHAFT:
99 3 _failure( context(), 'MIDI channel_aftertouch', @failure );
100 }
101  
102 1 ▶sub midi_control_change ($$$$$) {
Subroutine midi_control_change: called CC=1 SCAR=0.0
Pod documented
103 1 push @_, 0xB0, 'MIDI control_change';
104 1 goto &_dpv;
105 }
106  
107 2 ▶sub midi_eof ($) {
Subroutine midi_eof: called CC=2 SCAR=6.9
Pod documented
108 2 my $eof = eof $_[0];
109 2 my $ctx = context();
110 2 my $result = 1;
111 2 ▶ if ($eof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eof) { } 1 1 2
112 1 $ctx->pass('MIDI EOF');
113 } else {
114 1 $ctx->fail('MIDI EOF');
115 1 $result = 0;
116 }
117 2 $ctx->release;
118 2 return $result;
119 }
120  
121 3 ▶sub midi_footer ($$) {
Subroutine midi_footer: called CC=5 SCAR=16.1
Pod documented
122 3 my ( $fh, $dtime ) = @_;
123 3 my @failure;
124 3 my $q = read_vlq($fh);
125 3 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 1 2 3
126 1 push @failure, [ dtime => $q, $dtime ];
127 }
128 3 my $amount = read $fh, my $footer, 3;
129 3 ▶ confess "midi_footer read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 3 3
130 3 ▶ if ( $amount != 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 3) 1 2 3
131 1 push @failure, [ length => $amount, 3 ];
132 1 goto FAIL_FOOTER;
133 }
134 2 my $expect = "\xFF\x2F\x00";
135 2 ▶ if ( $footer ne $expect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($footer ne $expect) 1 1 2
136 push @failure,
137 1 [ footer => map { sprintf '%vx', $_ } $footer, $expect ];
138 }
139 FAIL_FOOTER:
140 3 _failure( context(), 'MIDI footer', @failure );
141 }
142  
143 5 ▶sub midi_header ($$$$) {
Subroutine midi_header: called CC=8 SCAR=20.8
Pod documented
144 5 my ( $fh, $want_format, $want_tracks, $want_division ) = @_;
145  
146 5 my $amount = read $fh, my $header, 14;
147 5 ▶ confess "midi_header read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 1 4 5
148 4 my @failure;
149 4 ▶ if ( $amount != 14 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 14) 1 3 4
150 1 push @failure, [ byte_count => $amount, 14 ];
151 1 goto FAIL_HEADER;
152 }
153 3 my ( $mthd, $header_len, $format, $tracks, $division ) =
154 unpack a4Nnnn => $header;
155 3 ▶ if ( $mthd ne 'MThd' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mthd ne "MThd") 1 2 3
156 1 push @failure, [ id => $mthd, 'MThd' ];
157 }
158 3 ▶ if ( $header_len != 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header_len != 6) 1 2 3
159 1 push @failure, [ header_length => $header_len, 6 ];
160 }
161 3 ▶ if ( $format != $want_format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format != $want_format) 1 2 3
162 1 push @failure, [ format => $format, $want_format ];
163 }
164 3 ▶ if ( $tracks != $want_tracks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tracks != $want_tracks) 1 2 3
165 1 push @failure, [ tracks => $tracks, $want_tracks ];
166 }
167 3 ▶ if ( $division != $want_division ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($division != $want_division) 1 2 3
168 1 push @failure, [ division => $division, $want_division ];
169 }
170 FAIL_HEADER:
171 4 _failure( context(), 'MIDI header', @failure );
172 }
173  
174 8 ▶sub midi_note_off ($$$$$) {
Subroutine midi_note_off: called CC=1 SCAR=0.0
Pod documented
175 8 push @_, 0x80, 'MIDI note_off';
176 8 goto &_dpv;
177 }
178  
179 7 ▶sub midi_note_on ($$$$$) {
Subroutine midi_note_on: called CC=1 SCAR=0.0
Pod documented
180 7 push @_, 0x90, 'MIDI note_on';
181 7 goto &_dpv;
182 }
183  
184 # TODO probably need to support some more events around about here
185  
186 3 ▶sub midi_patch ($$$$) {
Subroutine midi_patch: called CC=7 SCAR=19.5
Pod documented
187 3 my ( $fh, $dtime, $channel, $want_patch ) = @_;
188 3 my @failure;
189 3 my $q = read_vlq($fh);
190 3 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 1 2 3
191 1 push @failure, [ dtime => $q, $dtime ];
192 }
193 3 my $amount = read $fh, my $track, 2;
194 3 ▶ confess "midi_patch read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 3 3
195 3 ▶ if ( $amount != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 2) 1 2 3
196 1 push @failure, [ length => $amount, 2 ];
197 1 goto FAIL_PATCH;
198 }
199 2 my ( $pa, $patch ) = unpack CC => $track;
200 2 my ( $ch, $code ) = ( $pa & 0xF, $pa & 0xF0 );
201 2 ▶ if ( $ch != $channel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch != $channel) 1 1 2
202 1 push @failure, [ channel => $ch, $channel ];
203 }
204 2 ▶ if ( $code != 0xC0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 192) 1 1 2
205 1 push @failure, [ code => $code, 0xC0 ];
206 }
207 2 ▶ if ( $patch != $want_patch ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($patch != $want_patch) 1 1 2
208 1 push @failure, [ patch => $patch, $want_patch ];
209 }
210 FAIL_PATCH:
211 3 _failure( context(), 'MIDI patch', @failure );
212 }
213  
214 3 ▶sub midi_pitch_wheel ($$$$) {
Subroutine midi_pitch_wheel: called CC=6 SCAR=17.9
Pod documented
215 3 my ( $fh, $dtime, $channel, $wheel ) = @_;
216 3 my @failure;
217 3 my $q = read_vlq($fh);
218 3 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 1 2 3
219 1 push @failure, [ dtime => $q, $dtime ];
220 }
221 3 my $amount = read $fh, my $track, 3;
222 3 ▶ if ( $amount != 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 3) 1 2 3
223 1 push @failure, [ length => $amount, 3 ];
224 1 goto FAIL_WHEEL;
225 }
226 2 my ( $pa, $high, $low ) = unpack CCC => $track;
227 2 my ( $ch, $code ) = ( $pa & 0xF, $pa & 0xF0 );
228 2 ▶ if ( $ch != $channel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch != $channel) 1 1 2
229 1 push @failure, [ channel => $ch, $channel ];
230 }
231 2 ▶ if ( $code != 0xE0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 224) 1 1 2
232 1 push @failure, [ code => $code, 0xE0 ];
233 }
234 2 my $value = $high | ( $low << 7 ) - 0x2000;
235 2 ▶ if ( $value != $wheel ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value != $wheel) 1 1 2
236 1 push @failure, [ wheel => $value, $wheel ];
237 }
238 FAIL_WHEEL:
239 3 _failure( context(), 'MIDI pitch_wheel', @failure );
240 }
241  
242 4 ▶sub midi_skip ($$) {
Subroutine midi_skip: called CC=3 SCAR=11.0
Pod documented
243 4 my ( $fh, $size ) = @_;
244 4 my $amount = read $fh, my ($unused), $size;
245 4 ▶ confess "midi_skip read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 1 3 4
246 3 my @failure;
247 3 ▶ if ( $amount != $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != $size) 1 2 3
248 1 push @failure, [ byte_count => $amount, $size ];
249 }
250 3 _failure( context(), 'MIDI skip', @failure );
251 }
252  
253 1 ▶sub midi_skip_dtime ($$) {
Subroutine midi_skip_dtime: called CC=1 SCAR=0.0
Pod documented
254 1 read_vlq( $_[0] );
255 1 goto &midi_skip;
256 }
257  
258 4 ▶sub midi_tempo ($$$) {
Subroutine midi_tempo: called CC=6 SCAR=17.9
Pod documented
259 4 my ( $fh, $dtime, $tempo_want ) = @_;
260 4 my @failure;
261 4 my $q = read_vlq($fh);
262 4 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 1 3 4
263 1 push @failure, [ dtime => $q, $dtime ];
264 }
265 4 my $amount = read $fh, my $track, 6;
266 4 ▶ confess "midi_tempo read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 4 4
267 4 ▶ if ( $amount != 6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 6) 1 3 4
268 1 push @failure, [ length => $amount, 6 ];
269 1 goto FAIL_TEMPO;
270 }
271 3 my ( $code, $high, $low ) = unpack Z3Cn => $track;
272 3 my $expect = "\xFF\x51\x03";
273 3 ▶ if ( $code ne $expect ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code ne $expect) 1 2 3
274 push @failure,
275 1 [ tempo_code => map { sprintf '%vx', $_ } $code, $expect ];
276 }
277 3 my $tempo = ( $high << 16 ) | $low;
278 3 ▶ if ( $tempo != $tempo_want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tempo != $tempo_want) 1 2 3
279 1 push @failure, [ tempo => $tempo, $tempo_want ];
280 }
281 FAIL_TEMPO:
282 4 _failure( context(), 'MIDI tempo', @failure );
283 }
284  
285 19 ▶sub midi_text ($$$$) {
Subroutine midi_text: called CC=22 SCAR=30.9
Pod documented
286 19 my ( $fh, $dtime, $type, $want_string ) = @_;
287 19 my $code;
288 19 ▶ if ( $type eq 'text' ) {
Branch: 15 branches
BranchTrueFalseTotal
if ($type eq 'text') { } 4 15 19
elsif ($type eq 'copyright') { } 1 14 15
elsif ($type eq 'name') { } 1 13 14
elsif ($type eq 'instrument') { } 1 12 13
elsif ($type eq 'lyric') { } 1 11 12
elsif ($type eq 'marker') { } 1 10 11
elsif ($type eq 'cue') { } 1 9 10
elsif ($type eq 'text8') { } 1 8 9
elsif ($type eq 'text9') { } 1 7 8
elsif ($type eq 'texta') { } 1 6 7
elsif ($type eq 'textb') { } 1 5 6
elsif ($type eq 'textc') { } 1 4 5
elsif ($type eq 'textd') { } 1 3 4
elsif ($type eq 'texte') { } 1 2 3
elsif ($type eq 'textf') { } 1 1 2
289 4 $code = "\xFF\x01";
290 } elsif ( $type eq 'copyright' ) {
291 1 $code = "\xFF\x02";
292 } elsif ( $type eq 'name' ) {
293 1 $code = "\xFF\x03";
294 } elsif ( $type eq 'instrument' ) {
295 1 $code = "\xFF\x04";
296 } elsif ( $type eq 'lyric' ) {
297 1 $code = "\xFF\x05";
298 } elsif ( $type eq 'marker' ) {
299 1 $code = "\xFF\x06";
300 } elsif ( $type eq 'cue' ) {
301 1 $code = "\xFF\x07";
302 } elsif ( $type eq 'text8' ) {
303 1 $code = "\xFF\x08";
304 } elsif ( $type eq 'text9' ) {
305 1 $code = "\xFF\x09";
306 } elsif ( $type eq 'texta' ) {
307 1 $code = "\xFF\x0A";
308 } elsif ( $type eq 'textb' ) {
309 1 $code = "\xFF\x0B";
310 } elsif ( $type eq 'textc' ) {
311 1 $code = "\xFF\x0C";
312 } elsif ( $type eq 'textd' ) {
313 1 $code = "\xFF\x0D";
314 } elsif ( $type eq 'texte' ) {
315 1 $code = "\xFF\x0E";
316 } elsif ( $type eq 'textf' ) {
317 1 $code = "\xFF\x0F";
318 } else {
319 1 confess "unknown type '$type'";
320 }
321 18 my @failure;
322 18 my $q = read_vlq($fh);
323 18 ▶ if ( $q != $dtime ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q != $dtime) 1 17 18
324 1 push @failure, [ dtime => $q, $dtime ];
325 }
326 18 my $amount = read $fh, my $track, 2;
327 18 ▶ confess "midi_text read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 0 18 18
328 18 ▶ if ( $amount != 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 2) 1 17 18
329 1 push @failure, [ code_length => $amount, 2 ];
330 1 goto FAIL_TEXT;
331 }
332 17 ▶ if ( $track ne $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track ne $code) 1 16 17
333 push @failure,
334 1 [ text_code => map { sprintf '%vx', $_ } $track, $code ];
335 }
336 17 my $string_length = read_vlq($fh);
337 17 $amount = read $fh, $track, $string_length;
338 17 ▶ if ( $amount != $string_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != $string_length) 1 16 17
339 1 push @failure, [ text_length => $amount, $string_length ];
340 1 goto FAIL_TEXT;
341 }
342 16 ▶ if ( $track ne $want_string ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track ne $want_string) 1 15 16
343 1 push @failure, [ text => $track, $want_string ];
344 }
345 FAIL_TEXT:
346 18 _failure( context(), "MIDI text_$type", @failure );
347 }
348  
349 4 ▶sub midi_track ($$) {
Subroutine midi_track: called CC=5 SCAR=16.1
Pod documented
350 4 my ( $fh, $want_length ) = @_;
351  
352 4 my $amount = read $fh, my $track, 8;
353 4 ▶ confess "midi_track read $!" unless defined $amount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $amount 1 3 4
354 3 my @failure;
355 3 ▶ if ( $amount != 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($amount != 8) 1 2 3
356 1 push @failure, [ byte_count => $amount, 8 ];
357 1 goto FAIL_TRACK;
358 }
359 2 my ( $mtrk, $track_len ) = unpack a4N => $track;
360 2 ▶ if ( $mtrk ne 'MTrk' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mtrk ne "MTrk") 1 1 2
361 1 push @failure, [ id => $mtrk, 'MTrk' ];
362 }
363 2 ▶ if ( $track_len != $want_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($track_len != $want_length) 1 1 2
364 1 push @failure, [ track_length => $track_len, $want_length ];
365 1 goto FAIL_TRACK;
366 }
367 FAIL_TRACK:
368 3 _failure( context(), 'MIDI track', @failure );
369 }
370  
371 72 ▶sub read_vlq ($) {
Subroutine read_vlq: called CC=5 SCAR=16.1
Pod documented
372 72 my $q = 0;
373 72 while (1) {
374 86 my $r = read $_[0], my $byte, 1;
375 86 ▶ confess "read_vlq read $!" unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 1 85 86
376 85 ▶ confess "read_vlq eof" if $r == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $r == 0 1 84 85
377 84 my $n = unpack C => $byte;
378 84 $q = ( $q << 7 ) | ( $n & 0x7f );
379 84 ▶ if ( $n < 0x80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 128) 70 14 84
380 70 ▶ confess "read_vlq range $q" if $q > 0xFFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $q > 268435455 1 69 70
381 69 return $q;
382 }
383 }
384 }
385  
386 2 1;
387 __END__
« MIDI/ConfigData.pm ↑ Summary