blib/lib/MIDI/Util.pm

filter: Statementstmt 82.7% 134 / 162 Branchbran 65.5% 38 / 58 Conditioncond 11.7% 2 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 88.0% 22 / 25 Podpod 100% 14 / 14 totaltotal 73.4% 210 / 286 CC 40 SCAR 26.9
CC
40
Cov
73%
play_fluidsynth
56.0
reverse_dump
30.0
play_timidity
20.0
CRAP 14.8
↑ Summary
1 package MIDI::Util;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: MIDI and music utilities
5  
6 2 our $VERSION = '0.1310';
7  
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  
11 2 ▶use File::Slurper qw(write_text);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MIDI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use MIDI::Simple ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Music::Tempo qw(bpm_to_ms);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Music::Scales qw(%original_modes);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use constant TICKS => 96; # nb: used internally when a score is not given
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 our @EXPORT = qw(
22 midi_dump
23 reverse_dump
24 midi_format
25 set_chan_patch
26 set_time_signature
27 setup_score
28 dura_size
29 ticks
30 timidity_conf
31 play_timidity
32 play_fluidsynth
33 get_microseconds
34 score2events
35 scale_names
36 TICKS
37 );
38  
39  
40 1 ▶sub setup_score {
Subroutine setup_score: called CC=2 SCAR=6.9
Pod documented
41 1 my %args = (
42 lead_in => 4,
43 volume => 120,
44 bpm => 100,
45 channel => 0,
46 patch => 0,
47 octave => 4,
48 signature => '4/4',
49 @_,
50 );
51  
52 1 my $score = MIDI::Simple->new_score();
53  
54 1 set_time_signature($score, $args{signature});
55  
56 1 $score->set_tempo( bpm_to_ms($args{bpm}) * 1000 );
57  
58 1 $score->Channel(9);
59 1 $score->n( 'qn', 42 ) for 1 .. $args{lead_in};
60  
61 1 $score->Volume($args{volume});
62 1 $score->Channel($args{channel});
63 1 $score->Octave($args{octave});
64 1 $score->patch_change( $args{channel}, $args{patch} );
65  
66 1 return $score;
67 }
68  
69  
70 1 ▶sub set_chan_patch {
Subroutine set_chan_patch: called CC=3 SCAR=11.4
Pod documented
71 1 my ( $score, $channel, $patch ) = @_;
72  
73 1 ▶ $channel //= 0;
Condition50%1/2
$channel //= 0
l!l
10
MC/DC0%0/1
$channel //= 0
$channel
Truth table50%1/2
A
A= $channel
Aresult
11
00
74  
75 1 ▶ $score->patch_change( $channel, $patch )
Branch: 1 branch
BranchTrueFalseTotal
if defined $patch 1 0 1
76 if defined $patch;
77  
78 1 $score->noop( 'c' . $channel );
79 }
80  
81  
82 15 ▶sub midi_dump {
Subroutine midi_dump: called CC=16 SCAR=27.7
Pod documented
83 15 my ($key) = @_;
84  
85 15 ▶ if ( lc $key eq 'volume' ) {
Branch: 15 branches
BranchTrueFalseTotal
if (lc $key eq 'volume') { } 1 14 15
elsif (lc $key eq 'length') { } 1 13 14
elsif (lc $key eq 'ticks') { } 1 12 13
elsif (lc $key eq 'note') { } 1 11 12
elsif (lc $key eq 'note2number') { } 1 10 11
elsif (lc $key eq 'number2note') { } 1 9 10
elsif (lc $key eq 'patch2number') { } 1 8 9
elsif (lc $key eq 'number2patch') { } 1 7 8
elsif (lc $key eq 'notenum2percussion') { } 1 6 7
elsif (lc $key eq 'percussion2notenum') { } 1 5 6
elsif (lc $key eq 'all_events') { } 1 4 5
elsif (lc $key eq 'midi_events') { } 1 3 4
elsif (lc $key eq 'meta_events') { } 1 2 3
elsif (lc $key eq 'text_events') { } 1 1 2
elsif (lc $key eq 'nontext_meta_events') { } 1 0 1
86 return {
87 10 map { $_ => $MIDI::Simple::Volume{$_} }
88 1 sort { $MIDI::Simple::Volume{$a} <=> $MIDI::Simple::Volume{$b} }
89 keys %MIDI::Simple::Volume
90 };
91 }
92 elsif ( lc $key eq 'length' ) {
93 return {
94 20 map { $_ => $MIDI::Simple::Length{$_} }
95 1 sort { $MIDI::Simple::Length{$a} <=> $MIDI::Simple::Length{$b} }
96 keys %MIDI::Simple::Length
97 };
98 }
99 elsif ( lc $key eq 'ticks' ) {
100 return {
101 20 map { $_ => $MIDI::Simple::Length{$_} * TICKS }
102 1 sort { $MIDI::Simple::Length{$a} <=> $MIDI::Simple::Length{$b} }
103 keys %MIDI::Simple::Length
104 };
105 }
106 elsif ( lc $key eq 'note' ) {
107 return {
108 27 map { $_ => $MIDI::Simple::Note{$_} }
109 1 sort { $MIDI::Simple::Note{$a} <=> $MIDI::Simple::Note{$b} }
110 keys %MIDI::Simple::Note
111 };
112 }
113 elsif ( lc $key eq 'note2number' ) {
114 return {
115 128 map { $_ => $MIDI::note2number{$_} }
116 1 sort { $MIDI::note2number{$a} <=> $MIDI::note2number{$b} }
117 keys %MIDI::note2number
118 };
119 }
120 elsif ( lc $key eq 'number2note' ) {
121 return {
122 128 map { $_ => $MIDI::number2note{$_} }
123 1 sort { $a <=> $b }
124 keys %MIDI::number2note
125 };
126 }
127 elsif ( lc $key eq 'patch2number' ) {
128 return {
129 128 map { $_ => $MIDI::patch2number{$_} }
130 1 sort { $MIDI::patch2number{$a} <=> $MIDI::patch2number{$b} }
131 keys %MIDI::patch2number
132 };
133 }
134 elsif ( lc $key eq 'number2patch' ) {
135 return {
136 128 map { $_ => $MIDI::number2patch{$_} }
137 1 sort { $a <=> $b }
138 keys %MIDI::number2patch
139 };
140 }
141 elsif ( lc $key eq 'notenum2percussion' ) {
142 return {
143 47 map { $_ => $MIDI::notenum2percussion{$_} }
144 1 sort { $a <=> $b }
145 keys %MIDI::notenum2percussion
146 };
147 }
148 elsif ( lc $key eq 'percussion2notenum' ) {
149 return {
150 47 map { $_ => $MIDI::percussion2notenum{$_} }
151 1 sort { $MIDI::percussion2notenum{$a} <=> $MIDI::percussion2notenum{$b} }
152 keys %MIDI::percussion2notenum
153 };
154 }
155 elsif ( lc $key eq 'all_events' ) {
156 1 return \@MIDI::Event::All_events;
157 }
158 elsif ( lc $key eq 'midi_events' ) {
159 1 return \@MIDI::Event::MIDI_events;
160 }
161 elsif ( lc $key eq 'meta_events' ) {
162 1 return \@MIDI::Event::Meta_events;
163 }
164 elsif ( lc $key eq 'text_events' ) {
165 1 return \@MIDI::Event::Text_events;
166 }
167 elsif ( lc $key eq 'nontext_meta_events' ) {
168 1 return \@MIDI::Event::Nontext_meta_events;
169 }
170 else {
171 0 return [];
172 }
173 }
174  
175  
176 0 ▶sub reverse_dump {
Subroutine reverse_dump: not called CC=5 SCAR=34.0
Pod documented
177 0 my ($name, $precision) = @_;
178  
179 0 ▶ $precision //= -1;
Condition0%0/2
$precision //= -1
l!l
00
MC/DC0%0/1
$precision //= -1
$precision
Truth table0%0/2
A
A= $precision
Aresult
11
00
180  
181 0 my %by_value;
182  
183 0 my $dump = midi_dump($name); # dumps an arrayref
184  
185 0 for my $key (keys %$dump) {
186 my $val = $name eq 'length' && $precision >= 0
187 ? sprintf('%.*f', $precision, $dump->{$key})
188 0 ▶ : $dump->{$key};
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'length' && $precision >= 0 ? : 0 0 0
Condition0%0/3
$name eq "length" and $precision >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq "length" and $precision >= 0
$name eq "length" $precision >= 0
Truth table0%0/3
A and B
A= $name eq "length"B= $precision >= 0
ABresult
0X0
100
111
189 0 $by_value{$val} = $key;
190 }
191  
192 0 return \%by_value;
193 }
194  
195  
196 1 ▶sub midi_format {
Subroutine midi_format: called CC=4 SCAR=13.9
Pod documented
197 1 my (@notes) = @_;
198 1 my $flag = 1;
199 1 ▶ if ($notes[0] =~ /^(\d)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notes[0] =~ /^(\d)$/) 0 1 1
200 0 $flag = shift @notes;
201 }
202 1 my @formatted;
203 1 for my $note (@notes) {
204 4 $note =~ s/C##/D/;
205 4 $note =~ s/D##/E/;
206 4 $note =~ s/F##/G/;
207 4 $note =~ s/G##/A/;
208  
209 4 $note =~ s/Dbb/C/;
210 4 $note =~ s/Ebb/D/;
211 4 $note =~ s/Abb/G/;
212 4 $note =~ s/Bbb/A/;
213  
214 4 $note =~ s/E#/F/;
215 4 $note =~ s/B#/C/;
216  
217 4 $note =~ s/Cb/B/;
218 4 $note =~ s/Fb/E/;
219  
220 4 ▶ if ($flag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flag) 4 0 4
221 4 $note =~ s/#/s/;
222 4 $note =~ s/b/f/;
223 }
224  
225 4 push @formatted, $note;
226 }
227 1 return @formatted;
228 }
229  
230  
231 1 ▶sub set_time_signature {
Subroutine set_time_signature: called CC=3 SCAR=11.7
Pod documented
232 1 my ($score, $signature) = @_;
233 1 my ($beats, $divisions) = split /\//, $signature;
234 1 ▶ $score->time_signature(
Branch: 2 branches
BranchTrueFalseTotal
$divisions == 8 ? : 0 1 1
$divisions == 8 ? : 0 1 1
235 $beats,
236 ($divisions == 8 ? 3 : 2),
237 ($divisions == 8 ? 24 : 18 ),
238 8
239 );
240 }
241  
242  
243 4 ▶sub dura_size {
Subroutine dura_size: called CC=3 SCAR=11.0
Pod documented
244 4 my ($duration, $ppqn) = @_;
245 4 ▶ $ppqn ||= TICKS;
Condition50%1/2
$ppqn ||= 96
l!l
04
MC/DC0%0/1
$ppqn ||= 96
$ppqn
Truth table50%1/2
A
A= $ppqn
Aresult
11
00
246 4 my $size = 0;
247 4 ▶ if ($duration =~ /^d(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($duration =~ /^d(\d+)$/) { } 2 2 4
248 2 $size = sprintf '%0.f', $1 / $ppqn;
249 }
250 else {
251 2 $size = $MIDI::Simple::Length{$duration};
252 }
253 4 return $size;
254 }
255  
256  
257 1 ▶sub ticks {
Subroutine ticks: called CC=1 SCAR=0.0
Pod documented
258 1 my ($score) = @_;
259 1 return ${ $score->{Tempo} };
260 }
261  
262  
263 2 ▶sub timidity_conf {
Subroutine timidity_conf: called CC=2 SCAR=6.9
Pod documented
264 2 my ($soundfont, $config_file) = @_;
265 2 my $config = "soundfont $soundfont\n";
266 2 ▶ write_text($config_file, $config) if $config_file;
Branch: 1 branch
BranchTrueFalseTotal
if $config_file 1 1 2
267 2 return $config;
268 }
269  
270  
271 0 ▶sub play_timidity {
Subroutine play_timidity: not called CC=4 SCAR=30.0
Pod documented
272 0 my ($score, $midi, $soundfont, $config) = @_;
273 0 $score->write_score($midi);
274 0 my @cmd;
275 0 ▶ if ($soundfont) {
Branch: 1 branch
BranchTrueFalseTotal
if ($soundfont) { } 0 0 0
276 0 ▶ $config ||= 'timidity-midi-util.cfg';
Condition0%0/2
$config ||= "timidity-midi-util.cfg"
l!l
00
MC/DC0%0/1
$config ||= "timidity-midi-util.cfg"
$config
Truth table0%0/2
A
A= $config
Aresult
11
00
277 0 timidity_conf($soundfont, $config);
278 0 @cmd = ('timidity', '-c', $config, '-Od', $midi);
279 }
280 else {
281 0 @cmd = ('timidity', '-Od', $midi);
282 }
283 0 ▶ system(@cmd) == 0 or die "system(@cmd) failed: $?";
Branch: 1 branch
BranchTrueFalseTotal
system(@cmd) == 0 or die "system(@cmd) failed: $?" 0 0 0
284 }
285  
286  
287 0 ▶sub play_fluidsynth {
Subroutine play_fluidsynth: not called CC=7 SCAR=40.3
Pod documented
288 0 my ($score, $midi, $soundfont, $config) = @_;
289 0 $score->write_score($midi);
290 0 ▶ if (!$config && $^O eq 'darwin') {
Branch: 3 branches
BranchTrueFalseTotal
if (not $config and $^O eq 'darwin') { } 0 0 0
elsif (not $config and $^O eq 'MSWin32') { } 0 0 0
elsif (not $config) { } 0 0 0
Condition0%0/3
not $config and $^O eq "darwin"
!ll&&!rl&&r
000
MC/DC0%0/2
not $config and $^O eq "darwin"
not $config $^O eq "darwin"
Truth table0%0/3
not(A) and B
A= not $configB= $^O eq "darwin"
ABresult
0X0
100
111
291 0 $config = [qw(-a coreaudio -m coremidi -i)];
292 }
293 ▶ elsif (!$config && $^O eq 'MSWin32') {
Condition0%0/3
not $config and $^O eq "MSWin32"
!ll&&!rl&&r
000
MC/DC0%0/2
not $config and $^O eq "MSWin32"
not $config $^O eq "MSWin32"
Truth table0%0/3
not(A) and B
A= not $configB= $^O eq "MSWin32"
ABresult
0X0
100
111
294 0 $config = [];
295 }
296 elsif (!$config) { # linux
297 0 $config = [qw(-a alsa -m alsa_seq -i)];
298 }
299 0 my @cmd;
300 0 @cmd = ('fluidsynth', @$config, $soundfont, $midi);
301 0 ▶ system(@cmd) == 0 or die "system(@cmd) failed: $?";
Branch: 1 branch
BranchTrueFalseTotal
system(@cmd) == 0 or die "system(@cmd) failed: $?" 0 0 0
302 }
303  
304  
305 1 ▶sub get_microseconds {
Subroutine get_microseconds: called CC=1 SCAR=0.0
Pod documented
306 1 my ($score) = @_;
307 1 ▶ my $tempo = first { $_->[0] eq 'set_tempo' } @{ $score->{Score} };
Subroutine __ANON__: called CC=1 SCAR=0.0
308 1 return $tempo->[2] / ${ $score->{Tempo} };
309 }
310  
311  
312 1 ▶sub score2events {
Subroutine score2events: called CC=1 SCAR=0.0
Pod documented
313 1 my ($score) = @_;
314 1 return MIDI::Score::score_r_to_events_r($score->{Score});
315 }
316  
317  
318 1 ▶sub scale_names {
Subroutine scale_names: called CC=1 SCAR=0.0
Pod documented
319 1 return [ sort keys %original_modes ];
320 }
321  
322 2 1;
323  
324 __END__
↑ Summary