blib/lib/SMS/Ringtone/RTTTL/MIDI.pm

filter: Statementstmt 93.3% 42 / 45 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 87.1% 54 / 62 CC 7 SCAR 15.7
CC
7
Cov
85%
rtttl_to_midi
7.5
BEGIN
1.0
BEGIN
1.0
CRAP 4.8
↑ Summary
1 package SMS::Ringtone::RTTTL::MIDI;
2 #### Package information ####
3 # Description and copyright:
4 # See POD (i.e. perldoc SMS::Ringtone::RTTTL::MIDI).
5 ####
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use MIDI::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use SMS::Ringtone::RTTTL::MIDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::String; # Add line "sub BINMODE {}" to this module.
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @ISA = qw(Exporter);
13 1 our @EXPORT = qw(rtttl_to_midi);
14 1 our $VERSION = '0.04';
15  
16 1 1;
17  
18 1 ▶sub rtttl_to_midi {
Subroutine rtttl_to_midi: called CC=7 SCAR=20.2
Pod documented
19 1 my $parser = shift; # SMS::Ringtone::RTTTL::Parser object
20 1 my $patch = shift; # Instrument
21 1 ▶ unless(defined($patch)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $patch) 1 0 1
22 1 $patch = 1; # Piano
23 }
24 1 my $ms = MIDI::Simple->new_score();
25 1 ▶ if (length($parser->get_part_name())) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $parser->get_part_name) 1 0 1
26 1 $ms->track_name($parser->get_part_name());
27 }
28 1 $ms->patch_change(0,$patch-1); # Set instrument on channel 0.
29 1 $ms->set_tempo(60000000 / $parser->get_bpm()); # Microseconds per quarter note
30 1 $ms->noop('fff','c0'); # Set max volume on channel 0.
31 1 foreach my $nref ($parser->get_notes()) {
32 # Convert RTTTL duration to MIDI ticks.
33 22 my $ticks = ($ms->Tempo() * 4)/$nref->[0];
34 22 ▶ if ($nref->[3] == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($nref->[3] == 1) { } 0 22 22
elsif ($nref->[3] == 2) { } 0 22 22
35 0 $ticks *= 1.5;
36 }
37 elsif ($nref->[3] == 2) {
38 0 $ticks *= 1.75;
39 }
40 # Write notes
41 22 ▶ if ($nref->[1] eq 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if ($nref->[1] eq 'P') { } 0 22 22
42 0 $ms->r("d$ticks");
43 }
44 else {
45 22 my $note = uc($nref->[1]);
46 22 $note =~ s/#/s/go;
47 22 $ms->n("d$ticks",$note . $nref->[2]);
48 }
49 }
50 1 my $midi;
51 1 local(*OUT);
52 1 tie *OUT, 'IO::String', \$midi;
53 1 $ms->write_score(\*OUT);
54 1 close(OUT);
55 1 return $midi;
56 }
57  
58 __END__
↑ Summary