script/midi-dump

filter: Statementstmt 90.4% 218 / 241 Branchbran 64.2% 54 / 84 Conditioncond 37.2% 16 / 43 MC/DCmcdc 17.3% 4 / 23 Subroutinesub 86.6% 26 / 30 Podpod 66.6% 2 / 3 totaltotal 75.4% 320 / 424 CC 62 SCAR 22.0
CC
62
Cov
78%
dump
16.9
dump
16.4
app_options
16.0
CRAP 9.1
↑ Summary lib/MIDI/Tweaks.pm »
1 #!/usr/local/bin/perl -w
2  
3 # Author : Johan Vromans
4 # Created On : Wed Jun 11 16:02:35 2008
5 # Last Modified By: Johan Vromans
6 # Last Modified On: Thu Apr 20 13:57:58 2017
7 # Update Count : 298
8 # Status : Unknown, Use with caution!
9  
10 ################ Common stuff ################
11  
12 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 18 ▶use MIDI::Tweaks ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 18 our $VERSION = $MIDI::Tweaks::VERSION;
17  
18 # Package name.
19 18 my $my_package = 'Sciurix';
20 # Program name and version.
21 18 my $my_name = "midi-dump";
22  
23 ################ Command line parameters ################
24  
25 18 ▶use Getopt::Long 2.13;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Command line options.
28 18 my $rgscale = 0;
29 18 my $verbose = 0; # verbose processing
30  
31 # Development options (not shown with -help).
32 18 my $debug = 0; # debugging
33 18 my $trace = 0; # trace (show process)
34 18 my $test = 0; # test mode.
35  
36 ################ Presets ################
37  
38 18 ▶my $TMPDIR = $ENV{TMPDIR} || $ENV{TEMP} || '/usr/tmp';
Condition40%2/5
$ENV{'TMPDIR'} || $ENV{'TEMP'} || '/usr/tmp'
l!l
018
$ENV{"TMPDIR"} or $ENV{"TEMP"}
l!l&&r!l&&!r
0018
MC/DC0%0/2
$ENV{'TMPDIR'} || $ENV{'TEMP'} || '/usr/tmp'
$ENV{"TMPDIR"} $ENV{"TEMP"}
Truth table33%1/3
A or B
A= $ENV{"TMPDIR"}B= $ENV{"TEMP"}
ABresult
1X1
011
000
39  
40 ################ The Process ################
41  
42 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 18 ▶use MIDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 18 ▶use constant EV_TYPE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 18 ▶use constant EV_TIME => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 18 ▶use constant EV_NOTE_PITCH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 18 ▶use constant EV_NOTE_VELO => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 18 ▶use constant EV_MARKER_NAME => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # Note that qw() does not like # in the strings. Use split().
53 18 my @keys = (split(/ /, "C G D A E B F# C# Cb Gb Db Ab Eb Bb F"));
54 18 my @snotes = (split(/ /, "C C# D D# E F F# G G# A A# B"));
55 18 my @fnotes = (split(/ /, "C Db D Eb E F Gb G Ab A Bb B"));
56 18 my $notes = \@snotes;
57 18 my $ticks; # ticks per 1/4 note.
58  
59 18 ▶unless ( caller() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller()) 18 0 18
60 # Process command line options.
61 18 app_options();
62  
63 # Post-processing.
64 18 ▶ $trace |= ($debug || $test);
Condition33%1/3
$debug || $test
l!l&&r!l&&!r
0018
MC/DC0%0/2
$debug || $test
$debug $test
Truth table33%1/3
A || B
A= $debugB= $test
ABresult
1X1
011
000
65  
66 18 midi_dump(new MIDI::Opus ({from_file => $ARGV[0]}));
67  
68 # delta = time (in ticks)
69 # ticks = # ticks per quarter note.
70 # time_signature nn dd cc bb
71 # nn/dd -> numerator / 2log denomitator. 6/3 -> 6/2**3 -> 6/8.
72 # cc = # MIDI clocks per metronome tick.
73 # bb = the number of notated 32nd notes in a MIDI quarter note (24 MIDI clocks).
74 }
75  
76 18 ▶sub midi_dump {
Subroutine midi_dump: called CC=2 SCAR=7.0
77 18 my ($op) = @_;
78  
79 18 $ticks = $op->ticks; # ticks per 1/4 note
80 18 ▶ $rgscale = 960/$ticks if $rgscale;
Branch: 1 branch
BranchTrueFalseTotal
if $rgscale 0 18 18
81  
82 # Defaults time signature according to the MIDI spec.
83 18 init_time_signature(1);
84 18 set_time_signature(0, 4, 2, 24, 8);
85  
86 18 $op->dump({ dump_tracks => 1, flat => 0});
87 }
88  
89 0 ▶sub midi_rgscale {
Subroutine midi_rgscale: not called CC=1 SCAR=6.9
90 0 $rgscale = shift;
91 }
92  
93 ################ Convenience routines ################
94  
95 988 ▶sub is_note_event {
Subroutine is_note_event: called CC=1 SCAR=0.0
96 988 my ($e) = shift;
97 988 $e->[EV_TYPE] =~ /^note_o(n|ff)$/;
98 }
99  
100 964 ▶sub is_note_on {
Subroutine is_note_on: called CC=2 SCAR=6.9
101 964 my ($e) = shift;
102 964 ▶ $e->[EV_TYPE] eq 'note_on' && $e->[EV_NOTE_VELO];
Branch: 1 branch
BranchTrueFalseTotal
$e->[0] eq "note_on" and $e->[4] 482 482 964
103 }
104  
105 0 ▶sub is_note_off {
Subroutine is_note_off: not called CC=3 SCAR=24.8
106 0 my ($e) = shift;
107 0 ▶ $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] 0 0 0
Condition0%0/6
$e->[0] eq 'note_off' || $e->[0] eq 'note_on' && !$e->[4]
l!l&&r!l&&!r
000
$e->[0] eq "note_on" and not $e->[4]
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
108 || $e->[EV_TYPE] eq 'note_on' && !$e->[EV_NOTE_VELO];
109 }
110  
111 ################ MIDI::* overriding subroutines ################
112  
113 18 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
114  
115 18 my $etime; # cumulative event time
116  
117 1109 ▶sub MIDI::Event::dump {
Subroutine dump: called CC=14 SCAR=27.9
Pod undocumented
118 0 ▶ my @event = ref($_[0]) ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1109 1109
119 1109 ▶ $event[1] *= $rgscale if $rgscale;
Branch: 1 branch
BranchTrueFalseTotal
if $rgscale 0 1109 1109
120  
121 1109 my $t = "[" . MIDI::_dump_quote(@event) . "],";
122 1109 $etime += $event[EV_TIME];
123 1109 check_time_signature($etime);
124  
125 1109 my $extra = "";
126  
127 1109 ▶ if ( $event[EV_TYPE] eq 'time_signature' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($event[0] eq 'time_signature') { } 45 1064 1109
elsif ($event[0] eq 'key_signature') { } 0 1064 1064
elsif ($event[0] eq 'set_tempo') { } 24 1040 1064
elsif ($event[0] eq 'patch_change') { } 18 1022 1040
elsif ($event[0] eq 'control_change') { } 34 988 1022
elsif (is_note_event(\@event)) { } 964 24 988
128 45 set_time_signature($etime, @event[2..5]);
129 45 $extra = sprintf("Time = %d/%d, Click = %d, NoteQ = %d",
130 $event[2], 2**($event[3]), $event[4], $event[5]);
131 }
132  
133 elsif ( $event[EV_TYPE] eq 'key_signature' ) {
134 0 ▶ $extra = sprintf("Key = %s%s",
Branch: 1 branch
BranchTrueFalseTotal
$event[3] eq 'minor' ? : 0 0 0
135 $keys[$event[2]], $event[3] eq 'minor' ? "m" : "");
136 0 ▶ $notes = $event[2] < 0 ? \@fnotes : \@snotes;
Branch: 1 branch
BranchTrueFalseTotal
$event[2] < 0 ? : 0 0 0
137 }
138  
139 elsif ( $event[EV_TYPE] eq 'set_tempo' ) {
140 # Number of microseconds per quarter note.
141 24 my $qpm = sprintf("%.2f", 60000000 / $event[2]);
142 24 $qpm =~ s/[,.]00$//;
143 24 $extra = "Tempo: q = " . $qpm;
144 }
145  
146 elsif ( $event[EV_TYPE] eq 'patch_change' ) {
147 $extra = sprintf("Channel %d, patch = %s",
148 18 $event[2]+1, $MIDI::number2patch{$event[3]});
149 }
150  
151 elsif ( $event[EV_TYPE] eq 'control_change' ) {
152 34 ▶ if ( defined $MIDI::controllers[$event[3]] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $MIDI::controllers[$event[3]]) 34 0 34
153 34 $extra = $MIDI::controllers[$event[3]] . ' = '
154 . $event[4];
155 }
156 }
157  
158 elsif ( is_note_event(\@event) ) {
159 964 ▶ $extra = is_note_on(\@event) ? "On: " : "Off: ";
Branch: 1 branch
BranchTrueFalseTotal
is_note_on(\@event) ? : 482 482 964
160 964 $extra .= sprintf("%s%d",
161 $notes->[$event[3]%12], int($event[3]/12)-2);
162 }
163  
164 1109 ▶ $extra = " ".$extra if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 1085 24 1109
165 1109 printf(" %-40s #%8d %s%s\n",
166 $t, $etime, scalar(rg_timestamp($etime)), $extra);
167 }
168  
169 # This is copied from MIDI::Track, with one change (see #JV comment).
170 36 ▶sub MIDI::Track::dump { # dump a track's contents
Subroutine dump: called CC=4 SCAR=14.0
Pod documented
171 36 my $this = $_[0];
172 36 ▶ my $options_r = ref($_[1]) eq 'HASH' ? $_[1] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] eq 'HASH' ? : 36 0 36
173 36 my $type = $this->type;
174  
175 36 my $indent = ' ';
176 36 my @events = $this->events;
177 print(
178 $indent, "MIDI::Track->new({\n",
179 $indent, " 'type' => ", &MIDI::_dump_quote($type), ",\n",
180 defined($this->{'data'}) ?
181 ( $indent, " 'data' => ",
182 36 ▶ &MIDI::_dump_quote($this->{'data'}), ",\n" )
Branch: 1 branch
BranchTrueFalseTotal
defined $this->{'data'} ? : 0 36 36
183 : (),
184 $indent, " 'events' => [ # ", scalar(@events), " events.\n",
185 );
186  
187 36 init_time_signature(); # JV
188  
189 36 foreach my $event (@events) {
190 1109 &MIDI::Event::dump(@$event);
191 # was: print( $indent, " [", &MIDI::_dump_quote(@$event), "],\n" );
192 }
193 36 print( "$indent ]\n$indent}),\n$indent\n" );
194 36 return;
195 }
196  
197 # This is copied from MIDI::Opus, with one change (see #JV comment).
198 18 ▶sub MIDI::Opus::dump { # method; read-only
Subroutine dump: called CC=10 SCAR=28.3
Pod documented
199 18 my $this = $_[0];
200 18 my %info = $this->info();
201 18 ▶ my $options_r = ref($_[1]) eq 'HASH' ? $_[1] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] eq 'HASH' ? : 18 0 18
202  
203 18 ▶ if($options_r->{'flat'}) { # Super-barebones dump mode
Branch: 1 branch
BranchTrueFalseTotal
if ($options_r->{"flat"}) 0 18 18
204 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
205 0 foreach my $track ($this->tracks) {
206 0 foreach my $event (@{ $track->events_r }) {
207 0 print( join($d, @$event), "\n" );
208 }
209 }
210 0 return;
211 }
212  
213 print "MIDI::Opus->new({\n",
214 18 " 'format' => ", &MIDI::_dump_quote($this->{'format'}), ",\n";
215 18 ▶ if ( $rgscale ) { # JV
Branch: 1 branch
BranchTrueFalseTotal
if ($rgscale) { } 0 18 18
216 0 print " 'ticks' => 960,\t\t# was: ", &MIDI::_dump_quote($this->{'ticks'}), "\n";
217 }
218 else {
219 18 print " 'ticks' => ", &MIDI::_dump_quote($this->{'ticks'}), ",\n";
220 }
221  
222 18 my @tracks = $this->tracks;
223 18 ▶ if( $options_r->{'dump_tracks'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options_r->{'dump_tracks'}) { } 18 0 18
224 18 print " 'tracks' => [ # ", scalar(@tracks), " tracks...\n\n";
225 18 foreach my $x (0 .. $#tracks) {
226 36 my $track = $tracks[$x];
227 36 print " # Track \#", $x+1, " ...\n"; # JV
228 36 ▶ if(ref($track)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $track) { } 36 0 36
229 36 $track->dump($options_r);
230 } else {
231 0 print " # \[$track\] is not a reference!!\n";
232 }
233 }
234 18 print " ]\n";
235 } else {
236 0 print " 'tracks' => [ ], # ", scalar(@tracks), " tracks (not dumped)\n";
237 }
238 18 print "});\n";
239 18 return 1;
240 }
241  
242 18 ▶use warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 ################ Rosegarden compliant time subroutines ################
245  
246 # From: "Chris Cannam" <cannam@all-day-breakfast.com>
247 # Subject: Re: 33840 -> 024-02-08-00
248 # Date: Thu, 12 Jun 2008 15:14:14 +0100
249 #
250 # On Thu, Jun 12, 2008 at 2:27 PM, D. Michael McIntyre
251 # <michael.mcintyre@rosegardenmusic.com> wrote:
252 # > On Thursday 12 June 2008, Johan Vromans wrote:
253 # >> Any pointers to where this is documented?
254 # >
255 # > I never documented it because I don't really understand it myself. I just
256 # > muddle along semi-randomly.
257 # >
258 # > Hopefully Chris can offer the real explanation.
259 #
260 # Oh no! The pressure is on! Will I be able to remember what it's all
261 # intended to mean?
262 #
263 # OK, these four values MMM-BB-XX-YY are referred to in the code as
264 # "bar", "beat", "fraction", and "remainder".
265 #
266 # "Bar" is the bar or measure number as you would expect.
267 #
268 # "Beat" is the count of beats within the bar. The total number of
269 # beats is defined by the time signature, although it is not always
270 # equal to the numerator of the time sig (e.g. in 6/8 it is 2).
271 #
272 # "Fraction" is the count of hemidemisemiquavers (sixty-fourth notes)
273 # within the beat. In 4/4 there are 16 of these per beat, but for other
274 # time signatures that will vary.
275 #
276 # "Remainder" is the count of "Rosegarden's basic note timing units"
277 # within the hemidemisemiquaver. The base timing unit is 1/960 of a
278 # crotchet (quarter note), so there are 60 of these for each increment
279 # of the fraction counter.
280 #
281 # So, it's bars; beats (according to time signature); 64th-notes; and
282 # then whatever finer timing Rosegarden may happen to have a record of.
283  
284 # From: "Chris Cannam" <cannam@all-day-breakfast.com>
285 # Subject: Re: [Rosegarden-user] 33840 -> 024-02-08-00
286 # Date: Thu, 12 Jun 2008 20:40:00 +0100
287 #
288 # On Thu, Jun 12, 2008 at 8:21 PM, Johan Vromans <jvromans@squirrel.nl> wrote:
289 # > I create a piece in 6/8, and generate midi, this will have tick = 480
290 #
291 # Ah, introducing MIDI export complicates the matter.
292 #
293 # The timebase used for MIDI export is different from the internal
294 # resolution. The original intention, I imagine, was to make the MIDI
295 # export timebase configurable, but as it happens it is currently
296 # hardcoded to 480 ppq -- only half the internal resolution.
297 #
298 # > Since it is 6/8, there are 2 beats per measure, so the number of ticks
299 # > per measure is 2 * 480 = 960.
300 #
301 # Again, this is not the same in MIDI export. MIDI timebase is always
302 # per quarter note, not per beat, and there are three quarter notes in a
303 # 6/8 bar, which is where your 1440 comes from.
304 #
305 # Rosegarden doesn't "think" in MIDI time (or MIDI anything much) internally.
306 #
307 # >> "Fraction" is the count of hemidemisemiquavers (sixty-fourth notes)
308 # >> within the beat. In 4/4 there are 16 of these per beat, but for other
309 # >> time signatures that will vary.
310 # >
311 # > In 6/8, this will be 24, right?
312 #
313 # Right -- as far as display in the event list (or transport window in
314 # musical time mode) is concerned.
315  
316 my $bpm; # beats per measure
317 18 my $tpm; # ticks per measure
318 18 my $bnd; # duration of beat note
319 18 my $frac; # number of 1/64 notes per beat
320  
321 18 my $lts_bar; # bar of last time sig change
322 18 my $lts_time; # time of last time sig change
323  
324 18 my @tsigs; # track of time signatures
325 18 my $csig; # current time signature
326  
327 54 ▶sub init_time_signature {
Subroutine init_time_signature: called CC=2 SCAR=6.9
328 54 my $reset = shift;
329 54 $lts_bar = 0;
330 54 $lts_time = 0;
331 54 $etime = 0;
332 54 $csig = 0;
333 54 ▶ @tsigs = () if $reset;
Branch: 1 branch
BranchTrueFalseTotal
if $reset 18 36 54
334 }
335  
336 1109 ▶sub check_time_signature {
Subroutine check_time_signature: called CC=4 SCAR=13.9
337 1109 my $et = shift;
338  
339 1109 while ( 1 ) {
340 1170 ▶ return if $csig >= @tsigs;
Branch: 1 branch
BranchTrueFalseTotal
if $csig >= @tsigs 793 377 1170
341 377 my ($t, $nn, $dd, $cc, $bb) = @{$tsigs[$csig]};
342 377 ▶ return if $et < $t;
Branch: 1 branch
BranchTrueFalseTotal
if $et < $t 316 61 377
343 61 set_time_signature($t, $nn, $dd, $cc, $bb);
344 61 ▶ printf("%s#%8d %s Time = %d/%d, Click = %d, NoteQ = %d\n",
Branch: 1 branch
BranchTrueFalseTotal
if $csig 25 36 61
345 " " x 50, $t,
346 scalar(rg_timestamp($t)),
347 $nn, 2**$dd, $cc, $bb) if $csig;
348 61 $csig++;
349 }
350 0 return 1;
351 }
352  
353 124 ▶sub set_time_signature {
Subroutine set_time_signature: called CC=10 SCAR=23.2
354 124 my ($t, $nn, $dd, $cc, $bb) = @_;
355  
356 # Register where the time sig happened.
357 124 my @a = rg_duration($t-$lts_time);
358 # By definition, time sig implies a new bar.
359 124 ▶ $a[0]++ if $a[1] || $a[2] || $a[3];
Branch: 1 branch
BranchTrueFalseTotal
if $a[1] or $a[2] or $a[3] 2 122 124
Condition66%4/6
$a[1] or $a[2] or $a[3]
l!l&&r!l&&!r
20122
$a[1] or $a[2]
l!l&&r!l&&!r
20122
MC/DC33%1/3
$a[1] or $a[2] or $a[3]
$a[1] $a[2] $a[3]
Truth table50%2/4
A or B or C
A= $a[1]B= $a[2]C= $a[3]
ABCresult
1XX1
01X1
0011
0000
360 124 $lts_bar += $a[0];
361 124 $lts_time = $t;
362  
363 124 ▶ if ( $csig < @tsigs && $tsigs[$csig]->[0] == $t ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csig < @tsigs and $tsigs[$csig][0] == $t) { } 61 63 124
Condition66%2/3
$csig < @tsigs and $tsigs[$csig][0] == $t
!ll&&!rl&&r
63061
MC/DC50%1/2
$csig < @tsigs and $tsigs[$csig][0] == $t
$csig < @tsigs $tsigs[$csig][0] == $t
Truth table66%2/3
A and B
A= $csig < @tsigsB= $tsigs[$csig][0] == $t
ABresult
0X0
100
111
364 61 $tsigs[$csig] = [$t, $nn, $dd, $cc, $bb];
365 }
366 else {
367 63 push(@tsigs, [$t, $nn, $dd, $cc, $bb]);
368 63 $csig++;
369 }
370  
371 124 ▶ if ( $nn > 5 && $nn % 3 == 0 ) { # compound signature
Branch: 1 branch
BranchTrueFalseTotal
if ($nn > 5 and $nn % 3 == 0) { } 21 103 124
Condition100%3/3
$nn > 5 and $nn % 3 == 0
!ll&&!rl&&r
94921
MC/DC100%2/2
$nn > 5 and $nn % 3 == 0
$nn > 5 $nn % 3 == 0
Truth table100%3/3
A and B
A= $nn > 5B= $nn % 3 == 0
ABresult
0X0
100
111
372 21 $bpm = $nn / 3;
373 21 ▶ $tpm = ($nn / 2**$dd) * 4 * ($rgscale ? 960 : $ticks); # midi always ticks qn
Branch: 1 branch
BranchTrueFalseTotal
$rgscale ? : 0 21 21
374 21 $bnd = $tpm / $bpm;
375 21 $frac = 3 * (64 / 2**$dd);
376 }
377 else { # simple time signature
378 103 $bpm = $nn;
379 103 $bpm *= 2**($dd-2) * ($cc / 24);
380 103 ▶ $tpm = $bpm * ($rgscale ? 960 : $ticks) * ($cc / 24);
Branch: 1 branch
BranchTrueFalseTotal
$rgscale ? : 0 103 103
381 103 $bnd = $tpm / $bpm;
382 103 $frac = 64 / 2**$dd;
383 }
384 # warn("time sig: bpm = $bpm, tpm = $tpm, frac = $frac, bnd = $bnd\n");
385 }
386  
387 1258 ▶sub rg_duration {
Subroutine rg_duration: called CC=4 SCAR=14.1
388 1258 my ($t) = @_;
389  
390 # The time signature must be issued in the first track, at time 0.
391 1258 ▶ return ( 0, 0, 0, 0 ) unless $t; # time = 0
Branch: 1 branch
BranchTrueFalseTotal
unless $t 286 972 1258
392 972 ▶ return ( 0, 0, 0, 0 ) unless $tpm; # no time sig seen
Branch: 1 branch
BranchTrueFalseTotal
unless $tpm 0 972 972
393  
394 # 1408 = 1*1024 + 1*256 + 8*16 + 0
395  
396 972 my $bar = int($t / $tpm);
397 972 $t %= $tpm;
398  
399 972 my $beat = int($t / $bnd);
400 972 $t %= $bnd;
401  
402 972 my $fraction = int(($t / $bnd) * $frac);
403  
404 972 $t = sprintf("%.0f", (((($t / $bnd) * $frac) - $fraction) * 60));
405  
406 972 ▶ return ($bar, $beat, $fraction, $t) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 972 0 972
407 0 sprintf("%03d-%02d-%02d-%02.0f", $bar, $beat, $fraction, $t);
408 }
409  
410 1134 ▶sub rg_timestamp {
Subroutine rg_timestamp: called CC=3 SCAR=11.2
411 1134 ▶ return ( 0, 0, 0, 0 ) unless $tpm; # no time sig seen
Branch: 1 branch
BranchTrueFalseTotal
unless $tpm 0 1134 1134
412  
413 1134 my @ret = rg_duration($_[0]-$lts_time, $_[1], $_[2], $_[3]);
414 1134 $ret[0] += $lts_bar;
415  
416 # Measures and beats count from 1.
417 1134 $ret[0]++;
418 1134 $ret[1]++;
419  
420 1134 ▶ return @ret if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1134 1134
421 1134 sprintf("%03d-%02d-%02d-%02.0f", @ret);
422 }
423  
424 ################ Command line subroutines ################
425  
426 18 ▶sub app_options {
Subroutine app_options: called CC=8 SCAR=27.7
427 18 my $help = 0; # handled locally
428 18 my $ident = 0; # handled locally
429  
430 # Process options, if any.
431  
432 18 ▶ if ( !GetOptions
Branch: 1 branch
BranchTrueFalseTotal
if (not &GetOptions("rgscale", \$rgscale, "ident", \$ident, "verbose", \$verbose, "trace", \$trace, "help|?", \$help, "debug", \$debug) or $help) 0 18 18
Condition33%1/3
not &GetOptions("rgscale", \$rgscale, "ident", \$ident, "verbose", \$verbose, "trace", \$trace, "help|?", \$help, "debug", \$debug) or $help
l!l&&r!l&&!r
0018
MC/DC0%0/2
not &GetOptions("rgscale", \$rgscale, "ident", \$ident, "verbose", \$verbose, "trace", \$trace, "help|?", \$help, "debug", \$debug) or $help
not &GetOptions("rgscale", \$rgscale, "ident", \$ident, "verbose", \$verbose, "trace", \$trace, "help|?", \$help, "debug", \$debug) $help
Truth table33%1/3
not(A) or B
A= not &GetOptions("rgscale", \$rgscale, "ident", \$ident, "verbose", \$verbose, "trace", \$trace, "help|?", \$help, "debug", \$debug)B= $help
ABresult
1X1
011
000
433 (
434 'rgscale' => \$rgscale,
435 'ident' => \$ident,
436 'verbose' => \$verbose,
437 'trace' => \$trace,
438 'help|?' => \$help,
439 'debug' => \$debug,
440 ) or $help )
441 {
442 0 app_usage(2);
443 }
444 18 ▶ app_ident() if $ident;
Branch: 1 branch
BranchTrueFalseTotal
if $ident 0 18 18
445 18 ▶ app_usage(1) unless @ARGV == 1 && -f $ARGV[0] && -r _ && -s _;
Branch: 1 branch
BranchTrueFalseTotal
unless @ARGV == 1 and -f $ARGV[0] and -r _ and -s _ 0 18 18
Condition33%3/9
@ARGV == 1 and -f $ARGV[0] and -r _ and -s _
!ll&&!rl&&r
0018
@ARGV == 1 and -f $ARGV[0] and -r _
!ll&&!rl&&r
0018
@ARGV == 1 and -f $ARGV[0]
!ll&&!rl&&r
0018
MC/DC0%0/4
@ARGV == 1 and -f $ARGV[0] and -r _ and -s _
@ARGV == 1 -f $ARGV[0] -r _ -s _
Truth table20%1/5
A and B and C and D
A= @ARGV == 1B= -f $ARGV[0]C= -r _D= -s _
ABCDresult
0XXX0
10XX0
110X0
11100
11111
446 }
447  
448 0 ▶sub app_ident {
Subroutine app_ident: not called CC=1 SCAR=6.9
449 0 print STDERR ("This is $my_package [$my_name $VERSION]\n");
450 }
451  
452 0 ▶sub app_usage {
Subroutine app_usage: not called CC=3 SCAR=24.8
453 0 my ($exit) = @_;
454 0 app_ident();
455 0 print STDERR <<EndOfUsage;
456 Usage: $0 [options] file
457 --help this message
458 --ident show identification
459 --verbose verbose information
460 EndOfUsage
461 0 ▶ exit $exit if defined $exit && $exit != 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $exit and $exit != 0 0 0 0
Condition0%0/3
defined $exit and $exit != 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $exit and $exit != 0
defined $exit $exit != 0
Truth table0%0/3
A and B
A= defined $exitB= $exit != 0
ABresult
0X0
100
111
462 }
463  
464 1;
465  
466 =head1 NAME
467  
468 midi-dump - Dump contents of MIDI files
469  
470 =head1 SYNOPSIS
471  
472 midi-dump [options] file
473  
474 Options:
475  
476 --rgscale scale ticks to RoseGarden convention
477 --ident show identification
478 --help brief help message
479 --verbose verbose information
480  
481 =head1 OPTIONS
482  
483 =over 8
484  
485 =item B<--rgscale>
486  
487 RoseGarden always uses a tick unit of 960. When B<--rgscale> is used,
488 all times are scaled to 960 tick units.
489  
490 =item B<--verbose>
491  
492 More verbose information (default).
493  
494 =item B<--version>
495  
496 Print a version identification to standard output and exits.
497  
498 =item B<--help>
499  
500 Print a brief help message to standard output and exits.
501  
502 =item B<--ident>
503  
504 Prints a program identification.
505  
506 =item I<file>
507  
508 Input file, which must be a valid MIDI files.
509  
510 =back
511  
512 =head1 DESCRIPTION
513  
514 B<This program> will read the given MIDI file and writes a readable
515 and understandable representation of the contents to standard output.
516  
517 The format of the output is a Perl structure similiar to the one
518 produced by the dump function of the L<MIDI> module. The output has
519 been enhanced with additional information.
520  
521 For example:
522  
523 MIDI::Opus->new({
524 'format' => 1,
525 'ticks' => 256,
526 'tracks' => [ # 5 tracks...
527  
528 # Track #1 ...
529 MIDI::Track->new({
530 'type' => 'MTrk',
531 'events' => [ # 3 events.
532 ['time_signature', 0, 4, 2, 24, 8], # 0 001-01-00-00 Time = 4/4, Click = 24, NoteQ = 8
533 ['key_signature', 0, 0, 0], # 0 001-01-00-00 Key = C
534 ['set_tempo', 0, 600000], # 0 001-01-00-00 Tempo: q = 100
535 ]
536 }),
537
538 # Track #2 ...
539 MIDI::Track->new({
540 'type' => 'MTrk',
541 'events' => [ # 346 events.
542 ['control_change', 0, 0, 0, 0], # 0 001-01-00-00
543 ['control_change', 0, 0, 32, 0], # 0 001-01-00-00
544 ['patch_change', 0, 0, 52], # 0 001-01-00-00
545 ['lyric', 128, '1.If '], # 128 001-01-08-00
546 ['note_on', 0, 0, 70, 68], # 128 001-01-08-00 A#3 on
547 ['note_off', 128, 0, 70, 0], # 256 001-02-00-00 A#3 off
548 ['lyric', 0, 'ev'], # 256 001-02-00-00
549 ['note_on', 0, 0, 70, 75], # 256 001-02-00-00 A#3 on
550 ['note_off', 128, 0, 70, 0], # 384 001-02-08-00 A#3 off
551 ...
552 ['note_off', 256, 1, 62, 0], # 31616 031-04-08-00 D3 off
553 ['note_off', 1152, 1, 60, 0], # 32768 033-01-00-00 C3 off
554 ]
555 }),
556
557 ]
558 });
559  
560 The added information is at the right side, after the C<< # >> mark.
561 It consists of the accumulated time so far, a timestamp in the form
562 measure-beat-fraction-remainder (as used e.g. by the Rosegarden
563 program), and some event specific details.
564  
565 The output is a valid Perl structure, that can be loaded to create a
566 new MIDI::Opus object. See L<MIDI> for details.
567  
568 =head1 REQUIREMENTS
569  
570 L<MIDI> 0.80 or later.
571  
572 =head1 AUTHOR
573  
574 Johan Vromans <jvromans@squirrel.nl>
575  
576 =head1 COPYRIGHT
577  
578 This programs is Copyright 2008 Squirrel Consultancy.
579  
580 This program is free software; you can redistribute it and/or modify
581 it under the terms of the Perl Artistic License or the GNU General
582 Public License as published by the Free Software Foundation; either
583 version 2 of the License, or (at your option) any later version.
584  
585 =cut
586  
587 18 ▶INIT {
Subroutine INIT: called CC=6 SCAR=17.9
588 18 $MIDI::controllers[ 0] = "Bank Select";
589 18 $MIDI::controllers[ 1] = "Modulation";
590 18 $MIDI::controllers[ 2] = "Breath Controller";
591  
592 18 $MIDI::controllers[ 4] = "Foot Controller";
593 18 $MIDI::controllers[ 5] = "Portamento Time";
594 18 $MIDI::controllers[ 6] = "Data Entry (MSB)";
595 18 $MIDI::controllers[ 7] = "Main Volume";
596 18 $MIDI::controllers[ 8] = "Balance";
597  
598 18 $MIDI::controllers[ 10] = "Pan";
599 18 $MIDI::controllers[ 11] = "Expression Controller";
600 18 $MIDI::controllers[ 12] = "Effect Control 1";
601 18 $MIDI::controllers[ 13] = "Effect Control 2";
602  
603 18 $MIDI::controllers[ 15+$_ ] = "General-Purpose Controller $_" for 1..4;
604  
605 18 $MIDI::controllers[ 32+$_ ] = "LSB for controller $_" for 0 .. 31;
606  
607 18 $MIDI::controllers[ 64] = "Damper pedal (sustain)";
608 18 $MIDI::controllers[ 65] = "Portamento";
609 18 $MIDI::controllers[ 66] = "Sostenuto";
610 18 $MIDI::controllers[ 67] = "Soft Pedal";
611 18 $MIDI::controllers[ 68] = "Legato Footswitch";
612 18 $MIDI::controllers[ 69] = "Hold 2";
613 18 $MIDI::controllers[ 70] = "Sound Controller 1 (default: Timber Variation)";
614 18 $MIDI::controllers[ 71] = "Sound Controller 2 (default: Timber/Harmonic Content)";
615 18 $MIDI::controllers[ 72] = "Sound Controller 3 (default: Release Time)";
616 18 $MIDI::controllers[ 73] = "Sound Controller 4 (default: Attack Time)";
617  
618 18 $MIDI::controllers[ 68+$_ ] = "Sound Controller $_" for 6 .. 10;
619  
620 18 $MIDI::controllers[ 75+$_ ] = "General-Purpose Controller $_" for 5 .. 8;
621  
622 18 $MIDI::controllers[ 84] = "Portamento Control";
623  
624 18 $MIDI::controllers[ 91] = "Effects 1 Depth (formerly External Effects Depth)";
625 18 $MIDI::controllers[ 92] = "Effects 2 Depth (formerly Tremolo Depth)";
626 18 $MIDI::controllers[ 93] = "Effects 3 Depth (formerly Chorus Depth)";
627 18 $MIDI::controllers[ 94] = "Effects 4 Depth (formerly Celeste Detune)";
628 18 $MIDI::controllers[ 95] = "Effects 5 Depth (formerly Phaser Depth)";
629 18 $MIDI::controllers[ 96] = "Data Increment";
630 18 $MIDI::controllers[ 97] = "Data Decrement";
631 18 $MIDI::controllers[ 98] = "Non-Registered Parameter Number (LSB)";
632 18 $MIDI::controllers[ 99] = "Non-Registered Parameter Number (MSB)";
633 18 $MIDI::controllers[100] = "Registered Parameter Number (LSB)";
634 18 $MIDI::controllers[101] = "Registered Parameter Number (MSB)";
635  
636 18 $MIDI::controllers[120+$_] = "Mode Message $_" for 1 .. 7;
637 }
↑ Summary lib/MIDI/Tweaks.pm »