blib/lib/MIDI/Util.pm

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