lib/MIDI/Tweaks.pm

filter: Statementstmt 94.7% 396 / 418 Branchbran 70.4% 124 / 176 Conditioncond 64.8% 48 / 74 MC/DCmcdc 42.5% 17 / 40 Subroutinesub 96.5% 56 / 58 Podpod 27.7% 5 / 18 totaltotal 82.4% 646 / 784 CC 132 SCAR 21.8
CC
132
Cov
85%
split_hml
19.2
check_sanity
17.0
dump
14.7
CRAP 8.8
« script/midi-dump ↑ Summary
1 #! perl
2  
3 package MIDI::Tweaks;
4  
5 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 MIDI::Tweaks - Enhancements to MIDI.pm.
11  
12 =cut
13  
14 40 our $VERSION = '1.01';
15  
16 40 ▶use MIDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # These are valid for all events.
20 40 ▶use constant EV_TYPE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 40 ▶use constant EV_TIME => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 # These are for events that apply to a channel only.
23 40 ▶use constant EV_CHAN => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 # These are for note events.
25 40 ▶use constant EV_NOTE_PITCH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use constant EV_NOTE_VELO => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 # These if for track_name events.
28 40 ▶use constant EV_MARKER_NAME => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 # Drum channel
31 40 ▶use constant MIDI_CHAN_PERCUSSION => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 40 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 40 our @EXPORT;
36 40 our @EXPORT_OK;
37  
38 40 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
39 40 @EXPORT = qw(EV_TYPE EV_TIME EV_CHAN EV_NOTE_PITCH EV_NOTE_VELO EV_MARKER_NAME);
40 40 @EXPORT_OK = qw(is_note_event is_note_on is_note_off is_channel_event);
41 }
42  
43 =head1 SYNOPSIS
44  
45 This module implements a number of MIDI tweaks using the Sean Burke's
46 MIDI module.
47  
48 # Read midi data.
49 my $op = new MIDI::Tweaks::Opus ({ from_file => "orig.mid" });
50  
51 # Reset all volume controls.
52 $_->change_volume({ value => 100 }) foreach $op->tracks;
53  
54 # Slowdown a bit.
55 $op->change_tempo({ ratio => 0.9 });
56  
57 # Prepare the individual tracks.
58 my $track0 = $op->tracks_r->[0];
59 my $acc = $op->tracks_r->[1]->change_velocity({ value => 30 });
60 my $solo = $op->tracks_r->[2]->change_velocity({ value => 110 });
61 my $high = $op->tracks_r->[3]->change_velocity({ value => 100 });
62 my $low = $op->tracks_r->[4]->change_velocity({ value => 100 });
63  
64 # $low contains the middle + lower parts. Split.
65 (my $mid, $low) = $low->split_hilo;
66  
67 # Produce a midi for low voice only.
68 $op->tracks($track0, $acc, $low);
69 $op->write_to_file("low.mid");
70  
71  
72 Warning: This module is still under development. The interface to the
73 methods may change when new features are added.
74  
75 Two scripts are provided when installing this module:
76  
77 midi-tweak: applies some tweaks to MIDI files
78  
79 midi-dump: dumps contents of a MIDI file in an understandable format
80  
81 =head1 CONSTANTS
82  
83 The following constants will be exported by default.
84  
85 =head2 EV_TYPE
86  
87 The offset in an event (array ref) of the type information.
88  
89 =head2 EV_TIME
90  
91 The offset in an event (array ref) of the delta time.
92  
93 =head2 EV_CHAN
94  
95 The offset in an event (array ref) of the channel.
96  
97 =head2 EV_NOTE_PITCH
98  
99 The offset in a note event of the pitch.
100  
101 =head2 EV_NOTE_VELO
102  
103 The offset in a note event of the velocity.
104  
105 =head2 EV_MARKER_NAME
106  
107 The offset in a marker event of the name.
108  
109 =head1 FUNCTIONS
110  
111 The following functions can be exported on demand.
112  
113 =head2 MIDI::Tweaks::is_note_event
114  
115 Function. Takes an event (array reference) as argument.
116 Returns true if the event is a 'note on' or 'note off' event.
117  
118 =cut
119  
120 315 ▶sub is_note_event {
Subroutine is_note_event: called CC=1 SCAR=0.0
Pod documented
121 315 my ($e) = shift;
122 315 $e->[EV_TYPE] =~ /^note_o(n|ff)$/;
123 }
124  
125 =head2 MIDI::Tweaks::is_note_on
126  
127 Function. Takes an event (array reference) as argument.
128 Returns true if the event is a 'note on' event with a non-zero velocity.
129  
130 =cut
131  
132 4528 ▶sub is_note_on {
Subroutine is_note_on: called CC=2 SCAR=6.9
Pod documented
133 4528 my ($e) = shift;
134 4528 ▶ $e->[EV_TYPE] eq 'note_on' && $e->[EV_NOTE_VELO];
Branch: 1 branch
BranchTrueFalseTotal
$e->[0] eq "note_on" and $e->[4] 4383 145 4528
135 }
136  
137 =head2 MIDI::Tweaks::is_note_off
138  
139 Function. Takes an event (array reference) as argument.
140 Returns true if the event is a 'note off' event, or a 'note on' event
141 with zero velocity.
142  
143 =cut
144  
145 2337 ▶sub is_note_off {
Subroutine is_note_off: called CC=3 SCAR=11.0
Pod documented
146 2337 my ($e) = shift;
147 2337 ▶ $e->[EV_TYPE] eq 'note_off'
Branch: 1 branch
BranchTrueFalseTotal
$e->[0] eq "note_off" or $e->[0] eq "note_on" and not $e->[4] 17 2320 2337
Condition100%6/6
$e->[0] eq 'note_off' || $e->[0] eq 'note_on' && !$e->[4]
l!l&&r!l&&!r
172158162
$e->[0] eq "note_on" and not $e->[4]
!ll&&!rl&&r
137252158
MC/DC100%3/3
$e->[0] eq 'note_off' || $e->[0] eq 'note_on' && !$e->[4]
$e->[0] eq 'note_off' $e->[0] eq "note_on" not $e->[4]
Truth table100%4/4
A || B and not(C)
A= $e->[0] eq 'note_off'B= $e->[0] eq "note_on"C= not $e->[4]
ABCresult
1XX1
0111
00X0
0100
148 || $e->[EV_TYPE] eq 'note_on' && !$e->[EV_NOTE_VELO];
149 }
150  
151 =head2 MIDI::Tweaks::is_channel_event
152  
153 Function. Takes an event (array reference) as argument.
154 Returns true if the event aqpplies to specific channel.
155  
156 =cut
157  
158 40 my $evpat;
159 40 ▶INIT {
Subroutine INIT: called CC=1 SCAR=0.0
160 40 $evpat = qr/^
161 note_off
162 | note_on
163 | key_after_touch
164 | control_change
165 | patch_change
166 | channel_after_touch
167 | pitch_wheel_change
168 $/x;
169 }
170  
171 5654 ▶sub is_channel_event {
Subroutine is_channel_event: called CC=1 SCAR=0.0
Pod documented
172 5654 my ($e) = shift;
173 5654 $e->[EV_TYPE] =~ $evpat;
174 }
175  
176 =head1 OPUS METHODS
177  
178 =cut
179  
180 package MIDI::Tweaks::Opus;
181  
182 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
183 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
184 40 ▶use base qw(MIDI::Opus);
Subroutine BEGIN: called CC=1 SCAR=0.0
185 40 ▶use MIDI::Tweaks;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
187  
188 =head2 MIDI::Tweaks::Opus::new
189  
190 Method. Does whatever MIDI::Opus::new does, but checks for sanity and
191 produces an Opus with absolute time stamps.
192  
193 The options hash may contain a key C<require_sanity> that controls the
194 level of sanity checking:
195  
196 0: no checking
197 1: normal checking
198 warn: normal checking, but warn instead of die
199  
200 =cut
201  
202 23 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
203 23 my $pkg = shift;
204  
205 23 ▶ my $args = $_[0] ? { %{$_[0]} } : {};
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 23 0 23
206 23 my $require_sanity = delete($args->{require_sanity});
207 23 ▶ $require_sanity = 1 unless defined $require_sanity;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $require_sanity 23 0 23
208  
209 23 my $op = $pkg->SUPER::new($args);
210  
211 23 $op->delta2time;
212 23 ▶ $op->check_sanity({ strict => $require_sanity }) if $require_sanity;
Branch: 1 branch
BranchTrueFalseTotal
if $require_sanity 23 0 23
213  
214 21 return $op;
215 }
216  
217 =head2 MIDI::Tweaks::Opus::write_to_handle
218  
219 Method. Copies the Opus, converts the time stamps to delta times and
220 passes the result to MIDI::Opus::write_to_handle.
221  
222 Note that this method is used internally by write_to_file.
223  
224 =cut
225  
226 2 ▶sub write_to_handle {
Subroutine write_to_handle: called CC=1 SCAR=0.0
227 2 my $op = shift->copy;
228 2 $op->time2delta;
229 2 $op->SUPER::write_to_handle(@_);
230 }
231  
232 =head2 MIDI::Tweaks::Opus::dump
233  
234 Method. Copies the Opus, converts the time stamps to delta times and
235 passes the result to MIDI::Opus::dump.
236  
237 =cut
238  
239 23 ▶sub dump {
Subroutine dump: called CC=1 SCAR=0.0
240 23 my $op = shift->copy;
241 23 $op->time2delta;
242 23 $op->SUPER::dump(@_);
243 }
244  
245 =head2 MIDI::Tweaks::Opus::check_sanity
246  
247 Method, internal. Verifies that the MIDI data obeys certain criteria
248 that make it suitable for tweaking. In particular, there must be a
249 one-to-one relationship between tracks and channels.
250  
251 This method is called internally by the MIDI::Tweaks::Opus::new method.
252  
253 =cut
254  
255 24 ▶sub check_sanity {
Subroutine check_sanity: called CC=17 SCAR=28.3
256 24 my ($self, $args) = @_;
257 24 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
240
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
258  
259 24 my $strict = 1;
260 24 ▶ if ( $args->{strict} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"strict"}) 23 1 24
261 23 $strict = delete $args->{strict}; # 1, or 'warn'
262 }
263  
264 24 my @channel_seen;
265 my $fail;
266 24 my $tn = 1;
267 24 foreach my $track ( $self->tracks ) {
268 51 my $chan;
269 my $noteon;
270 51 foreach ( $track->events ) {
271 5634 ▶ next unless MIDI::Tweaks::is_channel_event($_);
Branch: 1 branch
BranchTrueFalseTotal
unless MIDI::Tweaks::is_channel_event($_) 1341 4293 5634
272 4293 ▶ if ( defined $chan ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $chan) { } 4260 33 4293
273 4260 ▶ if ( $_->[EV_CHAN] != $chan ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[2] != $chan) 2 4258 4260
274 2 carp("Sanity check: track $tn controls channels ",
275 $chan+1,
276 " and ",
277 $_->[EV_CHAN]+1);
278 2 $fail++;
279 }
280 }
281 else {
282 33 $chan = $_->[EV_CHAN];
283 33 ▶ if ( $channel_seen[$chan] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($channel_seen[$chan]) 1 32 33
284 1 carp("Sanity check: channel ",
285 $chan+1,
286 " is controlled by tracks ",
287 $channel_seen[$chan],
288 " and $tn");
289 1 $fail++;
290 }
291 33 $channel_seen[$chan] = $tn;
292 }
293 4293 ▶ if ( MIDI::Tweaks::is_note_on($_) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (MIDI::Tweaks::is_note_on($_)) { } 2095 2198 4293
elsif (MIDI::Tweaks::is_note_off($_)) { } 2095 103 2198
294 2095 ▶ if ( defined $noteon->[$_->[EV_NOTE_PITCH]] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$noteon[$_->[3]]) { } 1 2094 2095
295 1 carp("Sanity warning: track $tn, time $_->[EV_TIME], "
296 . "note $_->[EV_NOTE_PITCH] already on (since "
297 . $noteon->[$_->[EV_NOTE_PITCH]] . ")");
298 }
299 else {
300 2094 $noteon->[$_->[EV_NOTE_PITCH]] = $_->[EV_TIME];
301 }
302 }
303 elsif ( MIDI::Tweaks::is_note_off($_) ) {
304 2095 ▶ if ( defined $noteon->[$_->[EV_NOTE_PITCH]] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$noteon[$_->[3]]) { } 2093 2 2095
305 2093 $noteon->[$_->[EV_NOTE_PITCH]] = undef;
306 }
307 else {
308 2 carp("Sanity warning: track $tn, time $_->[EV_TIME], "
309 . "note $_->[EV_NOTE_PITCH] not on");
310 }
311 }
312 }
313 51 foreach my $i ( 0 .. $#{$noteon} ) {
314 2449 ▶ next unless defined $noteon->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $noteon->[$i] 2448 1 2449
315 1 carp("Sanity check: track $tn, "
316 . "unfinished note $i (on since $noteon->[$i])");
317 1 $fail++;
318 }
319 51 $tn++;
320 }
321 24 ▶ if ( $fail ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fail) 2 22 24
322 2 ▶ return if $strict eq 'warn';
Branch: 1 branch
BranchTrueFalseTotal
if $strict eq "warn" 0 2 2
323 2 croak("Sanity check failed");
324 }
325 22 return 1;
326 }
327  
328 =head2 MIDI::Tweaks::Opus::delta2time
329  
330 Method, internal. Modifies the Opus by changing the delta times of all
331 events of all tracks to an absolute time value.
332  
333 This method is called internally by the MIDI::Tweaks::Opus::new method.
334  
335 THIS MAKES THE OPUS NO LONGER DIRECTLY VALID FOR MIDI. When this
336 method has been applied to an Opus it should be undone later by a call
337 to time2delta. This is handled transparently by the
338 MIDI::Tweaks::Opus::write_to_file and MIDI::Tweaks::Opus::dump
339 methods.
340  
341 =cut
342  
343 24 ▶sub delta2time {
Subroutine delta2time: called CC=2 SCAR=6.9
344 24 my ($self) = @_;
345 24 foreach my $track ( $self->tracks ) {
346 51 $track->delta2time;
347 }
348 }
349  
350 =head2 MIDI::Tweaks::Opus::time2delta
351  
352 Method, internal. Modifies the Opus by making all time events relative
353 (delta times).
354  
355 This method undoes the effect of a previous delta2time, making the
356 Opus valid MIDI data again.
357  
358 This method is called internally by MIDI::Tweaks::Opus::write_to_file and
359 MIDI::Tweaks::Opus::dump methods.
360  
361 =cut
362  
363 26 ▶sub time2delta {
Subroutine time2delta: called CC=2 SCAR=6.9
364 26 my ($self) = @_;
365 26 foreach my $track ( $self->tracks ) {
366 65 $track->time2delta;
367 }
368 }
369  
370 =head2 MIDI::Tweaks::Opus::change_pitch
371  
372 Method. One argument, the options hash.
373  
374 Modifies the pitch of the Opus.
375  
376 This method just calls MIDI::Track::change_pitch on all tracks. See
377 L<MIDI::Track::change_pitch> for details. It skips the track
378 associated with channel 9 which is typically associated with
379 percussion.
380  
381 =cut
382  
383 4 ▶sub change_pitch {
Subroutine change_pitch: called CC=3 SCAR=11.1
384 4 my $self = shift;
385 4 foreach my $track ( $self->tracks ) {
386 8 ▶ next if $track->channel == MIDI::Tweaks::MIDI_CHAN_PERCUSSION; # skip drums
Branch: 1 branch
BranchTrueFalseTotal
if $track->channel == 10 0 8 8
387 8 $track->change_pitch(@_);
388 }
389 }
390  
391 =head2 MIDI::Tweaks::Opus::change_tempo
392  
393 Method. One argument, the options hash.
394  
395 Modifies the tempo settings of the Opus.
396  
397 The options has must contain either C<< value => number >> or C<<
398 ratio => number >>. In the first case, the tempo is set to the
399 specified value (beats per minute). In the second case, the tempo is
400 changed according to the ratio.
401  
402 =cut
403  
404 2 ▶sub change_tempo {
Subroutine change_tempo: called CC=2 SCAR=6.9
405 2 my $self = shift;
406 2 foreach my $track ( $self->tracks ) {
407 4 $track->change_tempo(@_);
408 }
409 }
410  
411 # We need to override MIDI::Opus::dump for this to work...
412  
413 40 ▶no warnings qw(redefine once);
Subroutine BEGIN: called CC=1 SCAR=0.0
414  
415 23 ▶sub MIDI::Opus::dump { # method; read-only
Subroutine dump: called CC=9 SCAR=26.8
Pod documented
416 23 my $this = $_[0];
417 23 my %info = $this->info();
418 23 ▶ my $options_r = ref($_[1]) eq 'HASH' ? $_[1] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] eq 'HASH' ? : 23 0 23
419  
420 23 ▶ if($options_r->{'flat'}) { # Super-barebones dump mode
Branch: 1 branch
BranchTrueFalseTotal
if ($options_r->{"flat"}) 0 23 23
421 0 ▶ my $d = $options_r->{'delimiter'} || "\t";
Condition0%0/2
$options_r->{'delimiter'} || "\t"
l!l
00
MC/DC0%0/1
$options_r->{'delimiter'} || "\t"
$options_r->{'delimiter'}
Truth table0%0/2
A
A= $options_r->{'delimiter'}
Aresult
11
00
422 0 foreach my $track ($this->tracks) {
423 0 foreach my $event (@{ $track->events_r }) {
424 0 print( join($d, @$event), "\n" );
425 }
426 }
427 0 return;
428 }
429  
430 # This is the only change to the original code: replace the
431 # hard-wired class name MIDI::Opus with ref($this), so derived
432 # classes work.
433 # WAS: print "MIDI::Opus->new({\n",
434 # IS NOW:
435 print ref($this), "->new({\n",
436 # End of change.
437 " 'format' => ", &MIDI::_dump_quote($this->{'format'}), ",\n",
438 23 " 'ticks' => ", &MIDI::_dump_quote($this->{'ticks'}), ",\n";
439  
440 23 my @tracks = $this->tracks;
441 23 ▶ if( $options_r->{'dump_tracks'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_r->{'dump_tracks'}) { } 23 0 23
442 23 print " 'tracks' => [ # ", scalar(@tracks), " tracks...\n\n";
443 23 foreach my $x (0 .. $#tracks) {
444 56 my $track = $tracks[$x];
445 56 print " # Track \#$x ...\n";
446 56 ▶ if(ref($track)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $track) { } 56 0 56
447 56 $track->dump($options_r);
448 } else {
449 0 print " # \[$track\] is not a reference!!\n";
450 }
451 }
452 23 print " ]\n";
453 } else {
454 0 print " 'tracks' => [ ], # ", scalar(@tracks), " tracks (not dumped)\n";
455 }
456 23 print "});\n";
457 23 return 1;
458 }
459  
460 =head1 TRACK METHODS
461  
462 =cut
463  
464 # We cannot use package MIDI::Track, since that is owbed by MIDI.pm.
465 package MIDI::Tweaks;
466  
467 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
468 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
469 40 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
470  
471 =head2 MIDI::Track::name
472  
473 Method. Returns the first track name as designated by an 'track name' event.
474 If none was found, returns undef.
475  
476 =cut
477  
478 0 ▶sub MIDI::Track::name {
Subroutine name: not called CC=3 SCAR=24.8
Pod undocumented
479 0 my $track = shift;
480 0 foreach my $e ( $track->events ) {
481 0 ▶ return $e->[EV_MARKER_NAME]
Branch: 1 branch
BranchTrueFalseTotal
if $e->[0] eq "track_name" 0 0 0
482 if $e->[EV_TYPE] eq 'track_name';
483 }
484 0 return;
485 }
486  
487 =head2 MIDI::Track::channel
488  
489 Method. Returns the channel controlled by this track.
490 If none was found, returns zero.
491  
492 Note that channels are numbered from one, as per MIDI standard.
493  
494 =cut
495  
496 8 ▶sub MIDI::Track::channel {
Subroutine channel: called CC=3 SCAR=11.0
Pod undocumented
497 8 my $track = shift;
498 8 foreach my $e ( $track->events ) {
499 20 ▶ next unless MIDI::Tweaks::is_channel_event($e);
Branch: 1 branch
BranchTrueFalseTotal
unless is_channel_event($e) 16 4 20
500 4 return $e->[EV_CHAN] + 1;
501 }
502 4 return 0;
503 }
504  
505 =head2 MIDI::Track::delta2time
506  
507 Method, internal. Modifies the track by changing the delta times of
508 all events to an absolute time value.
509  
510 THIS MAKES THE TRACK NO LONGER VALID FOR MIDI. When this method has
511 been applied to a track it should be undone later by a call to
512 time2delta.
513  
514 =cut
515  
516 51 ▶sub MIDI::Track::delta2time {
Subroutine delta2time: called CC=4 SCAR=15.2
Pod undocumented
517 51 my ($self, $force) = @_;
518  
519 51 ▶ if ( $self->{_tweaky_abstime} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_tweaky_abstime"}) 0 51 51
520 0 ▶ croak("MIDI::Track::delta2time: Already abstime")
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
521 unless $force;
522 }
523  
524 51 my $time = 0; # time until now
525 51 foreach my $e ( $self->events ) {
526 5634 $time = $e->[EV_TIME] += $time;
527 }
528  
529 51 $self->{_tweaky_abstime} = 1;
530  
531 # For convenience:
532 51 return $self;
533 }
534  
535 =head2 MIDI::Track::time2delta
536  
537 Method, internal. Modifies the track by making all time events
538 relative (delta times).
539  
540 This method undoes the effect of a previous delta2time, making the
541 track valid MIDI data again.
542  
543 =cut
544  
545 65 ▶sub MIDI::Track::time2delta {
Subroutine time2delta: called CC=5 SCAR=18.4
Pod undocumented
546 65 my ($self, $force) = @_;
547  
548 65 ▶ unless ( $self->{_tweaky_abstime} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_tweaky_abstime"}) 0 65 65
549 0 ▶ croak("MIDI::Track::delta2time: Already delta time")
Branch: 1 branch
BranchTrueFalseTotal
unless $force 0 0 0
550 unless $force;
551 }
552  
553 65 my $time = 0; # time until now
554 65 foreach my $e ( $self->events ) {
555 0 ▶ carp("NEGATIVE DELTA \@ $time: @{[$e->[EV_TIME]-$time]}\n")
Branch: 1 branch
BranchTrueFalseTotal
if $e->[1] < $time 0 7128 7128
556 if $e->[EV_TIME] < $time;
557 # Make time relative.
558 7128 ($time, $e->[EV_TIME]) = ($e->[EV_TIME], $e->[EV_TIME]-$time);
559 }
560  
561 65 delete $self->{_tweaky_abstime};
562  
563 # For convenience:
564 65 return $self;
565 }
566  
567 =head2 MIDI::Track::has_deltatime
568  
569 Method, internal. Returns true if the track events have delta time
570 stamps.
571  
572 This method is not fail safe, i.e., it can return thw wrong result if
573 a track does not have sensible events.
574  
575 =cut
576  
577 3 ▶sub MIDI::Track::has_deltatime {
Subroutine has_deltatime: called CC=3 SCAR=11.0
Pod undocumented
578 3 my ($self) = @_;
579 3 my $time = 0;
580 3 foreach my $e ( $self->events ) {
581 699 ▶ return 1 if $e->[EV_TIME] < $time;
Branch: 1 branch
BranchTrueFalseTotal
if $e->[1] < $time 1 698 699
582 698 $time = $e->[EV_TIME];
583 }
584 2 return;
585 }
586  
587 =head2 MIDI::Track::mapper
588  
589 Method. One or two arguments.
590 First argument is optional: an options hash.
591 Second, or only, argument must be a code ref.
592  
593 Applies to code ref to all events of
594 the track. Returns the track (for convenience);
595  
596 Note that if the code ref modifies the event, this actually modifies
597 the track. If this is not desired, copy it first, or use
598 C<< copy => 1 >> in the options hash.
599  
600 The code ref gets two arguments, the event (an array ref), and the
601 remainder of the options hash.
602  
603 Examples:
604  
605 $track->mapper(sub { print $_->[0] });
606 $new = $track->mapper({ copy => 1 },
607 sub { $_->[1] += 10 });
608  
609 =cut
610  
611 16 ▶sub MIDI::Track::mapper {
Subroutine mapper: called CC=5 SCAR=16.2
Pod undocumented
612 16 my $track = shift;
613  
614 16 my $opts = {};
615 16 ▶ $opts = {%{shift()}} if ref($_[0]) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 16 0 16
616  
617 16 my $mapper = shift;
618 16 ▶ croak("MIDI::Track::mapper requires a CODE argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $mapper eq "CODE" 0 16 16
619 unless ref($mapper) eq 'CODE';
620  
621 16 ▶ $track = $track->copy if delete $opts->{copy};
Branch: 1 branch
BranchTrueFalseTotal
if delete $opts->{"copy"} 1 15 16
622  
623 16 foreach ( $track->events ) {
624 325 $mapper->($_, $opts);
625 }
626  
627 16 $track;
628 }
629  
630 =head2 MIDI::Track::change_pitch
631  
632 Method. One argument, the options hash.
633  
634 Changes the pitch of each 'note on' event according to the options.
635  
636 The options has must contain C<< int => number >>. The number
637 indicates the number of half-tones the pitch should be raised. A
638 negative number will lower the pitch.
639 Any remaining options are passed to the mapper function.
640  
641 Note that key signatures will be changed as well.
642  
643 =cut
644  
645 8 ▶sub MIDI::Track::change_pitch {
Subroutine change_pitch: called CC=5 SCAR=16.3
Pod undocumented
646 8 my ($track, $args) = @_;
647 8 ▶ croak("MIDI::Track::change_pitch requires a HASH argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 8 8
648 unless ref($args) eq 'HASH';
649 8 $args = {%$args};
650  
651 8 my $mapper_func;
652 # C Db D Es E F Gb G As A Bb B
653 8 my @k = ( 0, -5, 2, -3, 4, -1, -6, 1, -4, 3, -2, 5);
654 8 my %k; $k{$k[$_]} = $_ for 0 .. $#k;
655  
656 8 ▶ if ( $args->{int} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"int"}) 8 0 8
657 8 my $value = int(delete $args->{int});
658  
659 136 ▶ $mapper_func = sub {
Subroutine __ANON__: called CC=9 SCAR=22.3
660 136 ▶ if ( MIDI::Tweaks::is_note_event($_[0]) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_note_event($_[0])) 72 64 136
661 72 $_[0]->[EV_NOTE_PITCH] += $value;
662 72 ▶ croak("MIDI::Track::change_pitch: transposed pitch out of range")
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][3] >= 0 and $_[0][3] <= 127 0 72 72
Condition33%1/3
$_[0][3] >= 0 and $_[0][3] <= 127
!ll&&!rl&&r
0072
MC/DC0%0/2
$_[0][3] >= 0 and $_[0][3] <= 127
$_[0][3] >= 0 $_[0][3] <= 127
Truth table33%1/3
A and B
A= $_[0][3] >= 0B= $_[0][3] <= 127
ABresult
0X0
100
111
663 unless $_[0]->[EV_NOTE_PITCH] >= 0 && $_[0]->[EV_NOTE_PITCH] <= 127;
664 72 return;
665 }
666 64 ▶ if ( $_[0]->[0] eq 'key_signature' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0][0] eq "key_signature") 4 60 64
667 # Warning: ugly code ahead.
668 # This is expected to be run only a few times.
669 # Don't spent much effort on elegance and optimizing.
670 4 my $f = $_[0]->[2]; # current #sharps
671 4 ▶ $f -= 12 if $f >= 6; # normalize
Branch: 1 branch
BranchTrueFalseTotal
if $f >= 6 0 4 4
672 4 ▶ $f += 12 if $f < -6;
Branch: 1 branch
BranchTrueFalseTotal
if $f < -6 0 4 4
673 4 $f = $k{$f}; # get note
674 4 $f += $value; # transpose
675 4 ▶ $f -= 12 while $f >= 12; # normalize
Branch: 1 branch
BranchTrueFalseTotal
$f >= 12 and do { $f -= 12 } 1 4 5
676 4 ▶ $f += 12 while $f < 0;
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 and do { $f += 12 } 1 4 5
677 4 $_[0]->[2] = $k[$f]; # get #sharps
678 4 return;
679 }
680 8 };
681 }
682  
683 8 ▶ croak("MIDI::Track::change_pitch: Missing 'value' or 'ratio' option")
Branch: 1 branch
BranchTrueFalseTotal
unless $mapper_func 0 8 8
684 unless $mapper_func;
685  
686 8 $track->mapper($args, $mapper_func);
687 }
688  
689 =head2 MIDI::Track::change_velocity
690  
691 Method. One argument, the options hash.
692  
693 Changes the velocity of each 'note on' event according to the options.
694  
695 The options has must contain either C<< value => number >> or
696 C<< ratio => number >>. In the first case, the velocity is set to the
697 specified value (which must be a number between 0 and 127). In the
698 second case, the velocity is changed according to the ratio.
699  
700 Any remaining options are passed to the mapper function.
701  
702 Note that setting the velocity to zero effectively turns the 'note on'
703 events into 'note off' events.
704  
705 Also note that tracks usually have an initial 'control_change' event
706 that controls the overall volume for a channel. Use change_volume to
707 change this setting.
708  
709 =cut
710  
711 3 ▶sub MIDI::Track::change_velocity {
Subroutine change_velocity: called CC=7 SCAR=20.0
Pod undocumented
712 3 my ($track, $args) = @_;
713 3 ▶ croak("MIDI::Track::change_velocity requires a HASH argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 3 3
714 unless ref($args) eq 'HASH';
715 3 $args = {%$args};
716  
717 3 my $mapper_func;
718  
719 3 ▶ if ( $args->{value} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'value'}) { } 2 1 3
elsif ($args->{'ratio'}) { } 1 0 1
720 2 my $value = int(delete $args->{value});
721 2 ▶ croak("MIDI::Track::change_velocity: value should be between 0 and 127")
Branch: 1 branch
BranchTrueFalseTotal
unless $value >= 0 and $value <= 127 0 2 2
Condition33%1/3
$value >= 0 and $value <= 127
!ll&&!rl&&r
002
MC/DC0%0/2
$value >= 0 and $value <= 127
$value >= 0 $value <= 127
Truth table33%1/3
A and B
A= $value >= 0B= $value <= 127
ABresult
0X0
100
111
722 unless $value >= 0 && $value <= 127;
723  
724 58 ▶ $mapper_func = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
725 58 ▶ return unless MIDI::Tweaks::is_note_on($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless is_note_on($_[0]) 40 18 58
726 18 $_[0]->[EV_NOTE_VELO] = $value;
727 2 };
728 }
729 elsif ( $args->{ratio} ) {
730 1 my $ratio = delete $args->{ratio};
731 29 ▶ $mapper_func = sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
732 29 ▶ return unless MIDI::Tweaks::is_note_on($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless is_note_on($_[0]) 20 9 29
733 9 $_[0]->[EV_NOTE_VELO] = int($_[0]->[EV_NOTE_VELO] * $ratio);
734 9 ▶ $_[0]->[EV_NOTE_VELO] = 127 if $_[0]->[EV_NOTE_VELO] > 127;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][4] > 127 0 9 9
735 1 };
736 }
737  
738 3 ▶ croak("MIDI::Track::change_velocity: Missing 'value' or 'ratio' option")
Branch: 1 branch
BranchTrueFalseTotal
unless $mapper_func 0 3 3
739 unless $mapper_func;
740  
741 3 $track->mapper($args, $mapper_func);
742 }
743  
744 =head2 MIDI::Track::change_tempo
745  
746 Method. One argument, the options hash.
747  
748 Changes the tempo of a trackaccording to the options.
749  
750 The options has must contain either C<< value => number >> or C<<
751 ratio => number >>. In the first case, each occurence of a tempo event
752 is changed to the specified value. In the second case, the tempo is
753 changed according to the ratio.
754  
755 Any remaining options are passed to the mapper function.
756  
757 Note that usually track 0 controls the tempi for an opus.
758  
759 =cut
760  
761 4 ▶sub MIDI::Track::change_tempo {
Subroutine change_tempo: called CC=5 SCAR=16.2
Pod undocumented
762 4 my ($track, $args) = @_;
763 4 ▶ croak("MIDI::Track::change_tempo requires a HASH argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 4 4
764 unless ref($args) eq 'HASH';
765 4 $args = {%$args};
766  
767 4 my $mapper_func;
768  
769 4 ▶ if ( $args->{value} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'value'}) { } 2 2 4
elsif ($args->{'ratio'}) { } 2 0 2
770 2 my $value = int(60000000 / int(delete $args->{value}));
771  
772 36 ▶ $mapper_func = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
773 36 ▶ return unless $_[0]->[0] eq 'set_tempo';
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] eq "set_tempo" 33 3 36
774 3 $_[0]->[2] = $value;
775 2 };
776 }
777 elsif ( $args->{ratio} ) {
778 2 my $ratio = delete $args->{ratio};
779 36 ▶ $mapper_func = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
780 36 ▶ return unless $_[0]->[0] eq 'set_tempo';
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] eq "set_tempo" 33 3 36
781 3 $_[0]->[2] = int($_[0]->[2] / $ratio);
782 2 };
783 }
784  
785 4 ▶ croak("MIDI::Track::change_tempo: Missing 'value' or 'ratio' option")
Branch: 1 branch
BranchTrueFalseTotal
unless $mapper_func 0 4 4
786 unless $mapper_func;
787  
788 4 $track->mapper($args, $mapper_func);
789 }
790  
791 =head2 MIDI::Track::change_volume
792  
793 Method. One argument, the options hash.
794  
795 Changes the volume of the channel.
796  
797 The options has must contain either C<< value => number >> or
798 C<< ratio => number >>. In the first case, the volume is set to the
799 specified value (which must be a number between 0 and 127). In the
800 second case, the volume is changed according to the ratio.
801  
802 Any remaining options are passed to the mapper function.
803  
804 =cut
805  
806 1 ▶sub MIDI::Track::change_volume {
Subroutine change_volume: called CC=7 SCAR=25.1
Pod undocumented
807 1 my ($track, $args) = @_;
808 1 ▶ croak("MIDI::Track::change_volume requires a HASH argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 1 1
809 unless ref($args) eq 'HASH';
810 1 $args = {%$args};
811  
812 1 my $mapper_func;
813  
814 1 ▶ if ( $args->{value} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args->{'value'}) { } 0 1 1
elsif ($args->{'ratio'}) { } 1 0 1
815 0 my $value = int(delete $args->{value});
816 0 ▶ croak("MIDI::Track::change_volume: value should be between 0 and 127")
Branch: 1 branch
BranchTrueFalseTotal
unless $value >= 0 and $value <= 127 0 0 0
Condition0%0/3
$value >= 0 and $value <= 127
!ll&&!rl&&r
000
MC/DC0%0/2
$value >= 0 and $value <= 127
$value >= 0 $value <= 127
Truth table0%0/3
A and B
A= $value >= 0B= $value <= 127
ABresult
0X0
100
111
817 unless $value >= 0 && $value <= 127;
818  
819 0 ▶ $mapper_func = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
820 return unless
821 0 ▶ $_[0]->[0] eq 'control_change'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] eq "control_change" and $_[0][3] == 7 0 0 0
Condition0%0/3
$_[0][0] eq "control_change" and $_[0][3] == 7
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0][0] eq "control_change" and $_[0][3] == 7
$_[0][0] eq "control_change" $_[0][3] == 7
Truth table0%0/3
A and B
A= $_[0][0] eq "control_change"B= $_[0][3] == 7
ABresult
0X0
100
111
822 && $_[0]->[3] == 7;
823 0 $_[0]->[4] = $value;
824 0 };
825 }
826 elsif ( $args->{ratio} ) {
827 1 my $ratio = delete $args->{ratio};
828 30 ▶ $mapper_func = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
829 return unless
830 30 ▶ $_[0]->[0] eq 'control_change'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0][0] eq "control_change" and $_[0][3] == 7 29 1 30
Condition100%3/3
$_[0][0] eq "control_change" and $_[0][3] == 7
!ll&&!rl&&r
2721
MC/DC100%2/2
$_[0][0] eq "control_change" and $_[0][3] == 7
$_[0][0] eq "control_change" $_[0][3] == 7
Truth table100%3/3
A and B
A= $_[0][0] eq "control_change"B= $_[0][3] == 7
ABresult
0X0
100
111
831 && $_[0]->[3] == 7;
832 1 $_[0]->[4] = int($_[0]->[4] * $ratio);
833 1 ▶ $_[0]->[4] = 127 if $_[0]->[4] > 127;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][4] > 127 0 1 1
834 1 };
835 }
836  
837 1 ▶ croak("MIDI::Track::change_volume: Missing 'value' or 'ratio' option")
Branch: 1 branch
BranchTrueFalseTotal
unless $mapper_func 0 1 1
838 unless $mapper_func;
839  
840 1 $track->mapper($args, $mapper_func);
841 }
842  
843 =head2 MIDI::Track::split_pitch
844  
845 Method. One argument, the options hash.
846  
847 The track is split into two tracks, depending on whether the pitch of
848 a note event is lower than a preset value. Non-note events are copied
849 to both tracks.
850  
851 The options hash may contain C<< pitch => number >> to specify the
852 pitch value to split on. All notes whose pitches are less than the
853 split value are copied to the lower track, all other notes are copied
854 to the upper track.
855  
856 Default value is 56. This is a suitable value to split a single MIDI
857 track containing a piano part into left hand and right hand tracks.
858  
859 All events are copied, and the track is not modified.
860  
861 This method returns a list, the higher track and the lower track.
862  
863 =cut
864  
865 1 ▶sub MIDI::Track::split_pitch {
Subroutine split_pitch: called CC=12 SCAR=25.6
Pod undocumented
866 1 my ($track, $args) = @_;
867 1 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
10
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
868 1 ▶ croak("MIDI::Track::split_pitch requires a HASH argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 1 1
869 unless ref($args) eq 'HASH';
870 1 $args = {%$args};
871  
872 1 ▶ my $split ||= 56;
Condition50%1/2
my $split ||= 56
l!l
01
MC/DC0%0/1
my $split ||= 56
my $split
Truth table50%1/2
A
A= my $split
Aresult
11
00
873  
874 1 ▶ if ( $args->{pitch} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"pitch"}) 1 0 1
875 1 $split = delete $args->{pitch};
876 1 ▶ croak("MIDI::Track::split_pitch: split value should be between 0 and 127")
Branch: 1 branch
BranchTrueFalseTotal
unless $split >= 0 and $split <= 127 0 1 1
Condition33%1/3
$split >= 0 and $split <= 127
!ll&&!rl&&r
001
MC/DC0%0/2
$split >= 0 and $split <= 127
$split >= 0 $split <= 127
Truth table33%1/3
A and B
A= $split >= 0B= $split <= 127
ABresult
0X0
100
111
877 unless $split >= 0 && $split <= 127;
878 }
879  
880 1 ▶ croak("MIDI::Track::split_pitch: unknown options: ".
Branch: 1 branch
BranchTrueFalseTotal
if %$args 0 1 1
881 join(" ", sort keys %$args)) if %$args;
882  
883 croak("MIDI::Track::split_pitch: FATAL: track has delta times")
884 1 ▶ unless $track->{_tweaky_abstime};
Branch: 1 branch
BranchTrueFalseTotal
unless $track->{"_tweaky_abstime"} 0 1 1
885  
886 1 my @hi;
887 my @lo;
888  
889 1 foreach ( $track->events ) {
890 29 ▶ unless ( MIDI::Tweaks::is_note_event($_) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_note_event($_)) 11 18 29
891 # Copy.
892 11 push(@hi, [@$_]);
893 11 push(@lo, [@$_]);
894 11 next;
895 }
896  
897 18 ▶ if ( $_->[EV_NOTE_PITCH] >= $split ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[3] >= $split) { } 12 6 18
898 12 push(@hi, [@$_]);
899 }
900 else {
901 6 push(@lo, [@$_]);
902 }
903 }
904  
905 1 my $hi = MIDI::Track->new;
906 1 $hi->type($track->type);
907 1 $hi->events(@hi);
908 1 $hi->{_tweaky_abstime} = 1;
909  
910 1 my $lo = MIDI::Track->new;
911 1 $lo->type($track->type);
912 1 $lo->events(@lo);
913 1 $lo->{_tweaky_abstime} = 1;
914  
915 1 return ( $hi, $lo );
916 }
917  
918 =head2 MIDI::Track::split_hilo
919  
920 Method. No arguments.
921  
922 The track is split into two tracks, high and low.
923  
924 If there are two 'note on' (or 'note off') events at the same time,
925 the event with the highest pitch gets copied to the high track and the
926 other to the low track. If there's only one note, or if it is not a
927 note event, it gets copied to both tracks.
928  
929 All events are copied, and the track is not modified.
930  
931 This method returns a list (high track, low track).
932  
933 NOTE: This process assumes that if there are two notes, they start and
934 end at the same time.
935  
936 NOTE: This process discards all non-note events from the resultant
937 tracks. Sorry.
938  
939 =cut
940  
941 3 ▶sub MIDI::Track::split_hilo {
Subroutine split_hilo: called CC=13 SCAR=25.7
Pod undocumented
942 3 my ($track) = @_;
943  
944 croak("MIDI::Track::split_hilo: FATAL: track has delta times")
945 3 ▶ unless $track->{_tweaky_abstime};
Branch: 1 branch
BranchTrueFalseTotal
unless $track->{"_tweaky_abstime"} 0 3 3
946  
947 3 my @hi;
948 my @lo;
949 3 ▶ my $eqtimes = sub { $_[0]->[EV_TIME] == $_[1]->[EV_TIME] };
Subroutine __ANON__: called CC=1 SCAR=0.0
950  
951 3 my @events = $track->events;
952 3 ▶ while ( @events ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@events) 94 3 97
953 94 my $this_event = shift(@events);
954 94 my $next_event = $events[0];
955  
956 # Skip lyrics.
957 94 ▶ next if $this_event->[EV_TYPE] =~ /^lyric$/;
Branch: 1 branch
BranchTrueFalseTotal
if $this_event->[0] =~ /^lyric$/ 24 70 94
958  
959 # Assert we're still in phase.
960 70 ▶ unless ( @hi == @lo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@hi == @lo) 0 70 70
961 0 croak("!t1 = ", scalar(@hi), " events\n",
962 "!t2 = ", scalar(@lo), " events\n");
963 }
964  
965 70 ▶ unless ( MIDI::Tweaks::is_note_event($this_event)
Branch: 1 branch
BranchTrueFalseTotal
unless (is_note_event($this_event) and @events and &$eqtimes($this_event, $next_event)) 36 34 70
Condition100%6/6
is_note_event($this_event) and @events and &$eqtimes($this_event, $next_event)
!ll&&!rl&&r
201634
is_note_event($this_event) and @events
!ll&&!rl&&r
18250
MC/DC100%3/3
is_note_event($this_event) and @events and &$eqtimes($this_event, $next_event)
is_note_event($this_event) @events &$eqtimes($this_event, $next_event)
Truth table100%4/4
A and B and C
A= is_note_event($this_event)B= @eventsC= &$eqtimes($this_event, $next_event)
ABCresult
0XX0
10X0
1100
1111
966 && @events && $eqtimes->($this_event, $next_event) ) {
967 # Copy.
968 36 push(@hi, [@$this_event]);
969 36 push(@lo, [@$this_event]);
970 36 next;
971 }
972  
973 34 ▶ if ( MIDI::Tweaks::is_note_on($this_event)
Branch: 1 branch
BranchTrueFalseTotal
if (is_note_on($this_event) and is_note_on($next_event) or is_note_off($this_event) and is_note_off($next_event)) { } 20 14 34
Condition77%7/9
is_note_on($this_event) and is_note_on($next_event) or is_note_off($this_event) and is_note_off($next_event)
l!l&&r!l&&!r
101014
is_note_on($this_event) and is_note_on($next_event)
!ll&&!rl&&r
24010
is_note_off($this_event) and is_note_off($next_event)
!ll&&!rl&&r
01410
MC/DC50%2/4
is_note_on($this_event) and is_note_on($next_event) or is_note_off($this_event) and is_note_off($next_event)
is_note_on($this_event) is_note_on($next_event) is_note_off($this_event) is_note_off($next_event)
Truth table42%3/7
A and B or C and D
A= is_note_on($this_event)B= is_note_on($next_event)C= is_note_off($this_event)D= is_note_off($next_event)
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
974 && MIDI::Tweaks::is_note_on($next_event)
975 or
976 MIDI::Tweaks::is_note_off($this_event)
977 && MIDI::Tweaks::is_note_off($next_event) ) {
978  
979 # Remove from events.
980 20 shift(@events);
981  
982 20 ▶ if ( $this_event->[EV_NOTE_PITCH] > $next_event->[EV_NOTE_PITCH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_event->[3] > $next_event->[3]) { } 12 8 20
983 12 push(@hi, [@$this_event]);
984 12 push(@lo, [@$next_event]);
985 }
986 else {
987 8 push(@hi, [@$next_event]);
988 8 push(@lo, [@$this_event]);
989 }
990 }
991 else {
992 # Not a multi-note, copy.
993 14 push(@hi, [@$this_event]);
994 14 push(@lo, [@$this_event]);
995 }
996 }
997  
998 3 my $hi = MIDI::Track->new;
999 3 $hi->type($track->type);
1000 3 $hi->events(@hi);
1001 3 $hi->{_tweaky_abstime} = 1;
1002  
1003 3 my $lo = MIDI::Track->new;
1004 3 $lo->type($track->type);
1005 3 $lo->events(@lo);
1006 3 $lo->{_tweaky_abstime} = 1;
1007  
1008 3 return ( $hi, $lo );
1009 }
1010  
1011 =head2 MIDI::Track::split_hml
1012  
1013 Method. No arguments.
1014  
1015 The track is split into three tracks, high, middle and low.
1016  
1017 If there are three 'note on' (or 'note off') events at the same time,
1018 the event with the highest pitch gets copied to the high track, the
1019 event with the lowest pitch gets copied to the low track, and the
1020 other to the middle track.
1021  
1022 If there are two 'note on' (or 'note off') events at the same time,
1023 the event with the highest pitch gets copied to the high track and the
1024 other to the middle and low tracks.
1025  
1026 If there's only one note event at that time, or if it is not a note
1027 event, it gets copied to all tracks.
1028  
1029 All events are copied, and the track is not modified.
1030  
1031 This method returns a list (high track, middle track, low track).
1032  
1033 NOTE: This process assumes that if there are two or three notes, they
1034 start and end at the same time.
1035  
1036 NOTE: This process discards all non-note events from the resultant
1037 tracks. Sorry.
1038  
1039 =cut
1040  
1041 3 ▶sub MIDI::Track::split_hml {
Subroutine split_hml: called CC=19 SCAR=29.6
Pod undocumented
1042 3 my ($track) = @_;
1043  
1044 croak("MIDI::Track::split_hml: FATAL: track has delta times")
1045 3 ▶ unless $track->{_tweaky_abstime};
Branch: 1 branch
BranchTrueFalseTotal
unless $track->{"_tweaky_abstime"} 0 3 3
1046  
1047 3 my @hi;
1048 my @md;
1049 3 my @lo;
1050 3 ▶ my $eqtimes = sub { $_[0]->[EV_TIME] == $_[1]->[EV_TIME] };
Subroutine __ANON__: called CC=1 SCAR=0.0
1051  
1052 3 my @events = $track->events;
1053 3 my $time = 0;
1054  
1055 3 ▶ while ( @events ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@events) 104 3 107
1056 104 my $this_event = shift(@events);
1057 104 my $next_event = $events[0];
1058 104 ▶ next if $this_event->[EV_TYPE] =~ /^lyric$/;
Branch: 1 branch
BranchTrueFalseTotal
if $this_event->[0] =~ /^lyric$/ 27 77 104
1059  
1060 77 ▶ unless ( @hi == @md && @md == @lo ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@hi == @md and @md == @lo) 0 77 77
Condition33%1/3
@hi == @md and @md == @lo
!ll&&!rl&&r
0077
MC/DC0%0/2
@hi == @md and @md == @lo
@hi == @md @md == @lo
Truth table33%1/3
A and B
A= @hi == @mdB= @md == @lo
ABresult
0X0
100
111
1061 0 croak("!t1 = ", scalar(@hi), " events\n",
1062 "!t2 = ", scalar(@md), " events\n",
1063 "!t3 = ", scalar(@lo), " events\n");
1064 }
1065  
1066 77 $time = $this_event->[EV_TIME];
1067  
1068 77 ▶ if ( MIDI::Tweaks::is_note_event($this_event) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_note_event($this_event)) { } 60 17 77
1069 # Check if there's a note already at this time.
1070 60 ▶ if ( MIDI::Tweaks::is_note_on($this_event)
Branch: 2 branches
BranchTrueFalseTotal
if (is_note_on($this_event) and @events and is_note_on($next_event) and &$eqtimes($this_event, $next_event)) { } 11 49 60
elsif (is_note_off($this_event) and @events and is_note_off($next_event) and &$eqtimes($this_event, $next_event)) { } 11 38 49
Condition77%7/9
is_note_on($this_event) and @events and is_note_on($next_event) and &$eqtimes($this_event, $next_event)
!ll&&!rl&&r
49011
is_note_on($this_event) and @events and is_note_on($next_event)
!ll&&!rl&&r
301911
is_note_on($this_event) and @events
!ll&&!rl&&r
30030
MC/DC50%2/4
is_note_on($this_event) and @events and is_note_on($next_event) and &$eqtimes($this_event, $next_event)
is_note_on($this_event) @events is_note_on($next_event) &$eqtimes($this_event, $next_event)
Truth table60%3/5
A and B and C and D
A= is_note_on($this_event)B= @eventsC= is_note_on($next_event)D= &$eqtimes($this_event, $next_event)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1071 && @events && MIDI::Tweaks::is_note_on($next_event)
1072 && $eqtimes->($this_event, $next_event) ) {
1073 11 ▶ if ( MIDI::Tweaks::is_note_on($events[1])
Branch: 1 branch
BranchTrueFalseTotal
if (is_note_on($events[1]) and &$eqtimes($this_event, $events[1])) { } 3 8 11
Condition66%2/3
is_note_on($events[1]) and &$eqtimes($this_event, $events[1])
!ll&&!rl&&r
803
MC/DC50%1/2
is_note_on($events[1]) and &$eqtimes($this_event, $events[1])
is_note_on($events[1]) &$eqtimes($this_event, $events[1])
Truth table66%2/3
A and B
A= is_note_on($events[1])B= &$eqtimes($this_event, $events[1])
ABresult
0X0
100
111
1074 && $eqtimes->($this_event, $events[1]) ) {
1075 # Remove next from events.
1076 3 shift(@events);
1077 # Store higher in hi, lower in md, etc.
1078 # (also removes afternext from events)
1079 my @a = sort {
1080 9 $b->[EV_NOTE_PITCH] <=> $a->[EV_NOTE_PITCH]
1081 3 } ( [@$this_event], [@$next_event], [@{shift(@events)}] );
1082 3 push(@hi, $a[0]);
1083 3 push(@md, $a[1]);
1084 3 push(@lo, $a[2]);
1085 }
1086 else {
1087 # Remove next from events.
1088 8 shift(@events);
1089 my @a = sort {
1090 8 $b->[EV_NOTE_PITCH] <=> $a->[EV_NOTE_PITCH]
1091 } ( [@$this_event], [@$next_event] );
1092 8 push(@hi, $a[0]);
1093 8 push(@md, $a[1]);
1094 8 push(@lo, $a[1]);
1095 }
1096 11 $hi[-1]->[EV_TIME] = $time;
1097 11 $md[-1]->[EV_TIME] = $time;
1098 11 $lo[-1]->[EV_TIME] = $time;
1099 }
1100 ▶ elsif ( MIDI::Tweaks::is_note_off($this_event)
Condition88%8/9
is_note_off($this_event) and @events and is_note_off($next_event) and &$eqtimes($this_event, $next_event)
!ll&&!rl&&r
38011
is_note_off($this_event) and @events and is_note_off($next_event)
!ll&&!rl&&r
211711
is_note_off($this_event) and @events
!ll&&!rl&&r
19228
MC/DC75%3/4
is_note_off($this_event) and @events and is_note_off($next_event) and &$eqtimes($this_event, $next_event)
is_note_off($this_event) @events is_note_off($next_event) &$eqtimes($this_event, $next_event)
Truth table80%4/5
A and B and C and D
A= is_note_off($this_event)B= @eventsC= is_note_off($next_event)D= &$eqtimes($this_event, $next_event)
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1101 && @events && MIDI::Tweaks::is_note_off($next_event)
1102 && $eqtimes->($this_event, $next_event) ) {
1103 11 ▶ if ( MIDI::Tweaks::is_note_off($events[1])
Branch: 1 branch
BranchTrueFalseTotal
if (is_note_off($events[1]) and &$eqtimes($this_event, $events[1])) { } 3 8 11
Condition66%2/3
is_note_off($events[1]) and &$eqtimes($this_event, $events[1])
!ll&&!rl&&r
803
MC/DC50%1/2
is_note_off($events[1]) and &$eqtimes($this_event, $events[1])
is_note_off($events[1]) &$eqtimes($this_event, $events[1])
Truth table66%2/3
A and B
A= is_note_off($events[1])B= &$eqtimes($this_event, $events[1])
ABresult
0X0
100
111
1104 && $eqtimes->($this_event, $events[1]) ) {
1105 # Remove next from events.
1106 3 shift(@events);
1107 # Store higher in hi, lower in md, etc.
1108 # (also removes afternext from events)
1109 my @a = sort {
1110 9 $b->[EV_NOTE_PITCH] <=> $a->[EV_NOTE_PITCH]
1111 3 } ( [@$this_event], [@$next_event], [@{shift(@events)}] );
1112 3 push(@hi, $a[0]);
1113 3 push(@md, $a[1]);
1114 3 push(@lo, $a[2]);
1115 }
1116 else {
1117 # Remove next from events.
1118 8 shift(@events);
1119 my @a = sort {
1120 8 $b->[EV_NOTE_PITCH] <=> $a->[EV_NOTE_PITCH]
1121 } ( [@$this_event], [@$next_event] );
1122 8 push(@hi, $a[0]);
1123 8 push(@md, $a[1]);
1124 8 push(@lo, $a[1]);
1125 }
1126 11 $hi[-1]->[EV_TIME] = $time;
1127 11 $md[-1]->[EV_TIME] = $time;
1128 11 $lo[-1]->[EV_TIME] = $time;
1129 }
1130 else {
1131 # Not a multi-note, copy.
1132 38 push(@hi, [@$this_event]);
1133 38 push(@md, [@$this_event]);
1134 38 push(@lo, [@$this_event]);
1135 }
1136 }
1137 else {
1138 # Not a note, copy.
1139 17 push(@hi, [@$this_event]);
1140 17 push(@md, [@$this_event]);
1141 17 push(@lo, [@$this_event]);
1142 }
1143 }
1144  
1145 3 my $hi = MIDI::Track->new;
1146 3 $hi->type($track->type);
1147 3 $hi->events(@hi);
1148 3 $hi->{_tweaky_abstime} = 1;
1149  
1150 3 my $md = MIDI::Track->new;
1151 3 $md->type($track->type);
1152 3 $md->events(@md);
1153 3 $md->{_tweaky_abstime} = 1;
1154  
1155 3 my $lo = MIDI::Track->new;
1156 3 $lo->type($track->type);
1157 3 $lo->events(@lo);
1158 3 $lo->{_tweaky_abstime} = 1;
1159  
1160 3 return ( $hi, $md, $lo );
1161 }
1162  
1163 package main;
1164  
1165 =head1 AUTHOR
1166  
1167 Johan Vromans, C<< <jvromans at squirrel.nl> >>
1168  
1169 =head1 BUGS
1170  
1171 Please report any bugs or feature requests to C<bug-midi-tweaks at
1172 rt.cpan.org>, or through the web interface at
1173 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=MIDI-Tweaks>. I will
1174 be notified, and then you'll automatically be notified of progress on
1175 your bug as I make changes.
1176  
1177 =head1 SEE ALSO
1178  
1179 L<MIDI>, L<MIDI::Opus>, L<midi-dump>, L<midi-tweak>.
1180  
1181 =head1 SUPPORT
1182  
1183 You can find documentation for this module with the perldoc command.
1184  
1185 perldoc MIDI::Tweaks
1186  
1187 Development of this module is hosted on GitHub:
1188 L<https://github.com/sciurius/perl-MIDI-Tweaks>. Feel free to fork and
1189 contribute.
1190  
1191 =head1 ACKNOWLEDGEMENTS
1192  
1193 =head1 COPYRIGHT & LICENSE
1194  
1195 Copyright 2008,2017 Johan Vromans, Squirrel Consultancy. All rights reserved.
1196  
1197 This program is free software; you can redistribute it and/or modify it
1198 under the same terms as Perl itself.
1199  
1200 =cut
1201  
1202 40 1;
« script/midi-dump ↑ Summary