blib/lib/Music/ScaleNote.pm

filter: Statementstmt 100% 84 / 84 Branchbran 65.3% 17 / 26 Conditioncond 63.1% 24 / 38 MC/DCmcdc 25.0% 6 / 24 Subroutinesub 100% 19 / 19 Podpod 100% 2 / 2 totaltotal 78.7% 152 / 193 CC 29 SCAR 23.2
CC
29
Cov
84%
get_offset
21.1
step
9.5
_build__scale
4.0
CRAP 10.2
↑ Summary
1 package Music::ScaleNote;
2 2 our $AUTHORITY = 'cpan:GENE';
3  
4 # ABSTRACT: Manipulate the position of a note in a scale
5  
6 2 our $VERSION = '0.0905';
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # use Data::Dumper::Compact qw( ddc );
12 2 ▶use List::SomeUtils qw( first_index );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use MIDI::Util qw(midi_format);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Music::Note ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Music::Scales qw( get_scale_nums );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 has scale_note => (
20 is => 'ro',
21 3 ▶ default => sub { 'C' },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 2 );
23  
24  
25 has scale_name => (
26 is => 'ro',
27 3 ▶ default => sub { 'major' },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 );
29  
30 2 has _scale => (
31 is => 'lazy',
32 builder => '_build__scale',
33 );
34 4 ▶sub _build__scale {
Subroutine _build__scale: called CC=4 SCAR=13.9
35 4 my ($self) = @_;
36 4 my @base = get_scale_nums( $self->scale_name );
37 4 my @scale;
38 4 for my $i (0 .. 10) {
39 44 for my $degree (@base) {
40 264 push @scale, $degree + ($i * 12)
41 }
42 }
43 4 ▶ print "@scale\n" if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 4 4
44 4 return \@scale;
45 }
46  
47  
48 has note_format => (
49 is => 'ro',
50 3 ▶ default => sub { 'midinum' },
Subroutine __ANON__: called CC=1 SCAR=0.0
51 2 );
52  
53  
54 has offset => (
55 is => 'ro',
56 7 ▶ isa => sub { die 'Not a negative or positive integer' unless $_[0] =~ /^-?\d+$/ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] =~ /^-?\d+$/ 0 7 7
57 7 ▶ default => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
58 2 );
59  
60  
61 has flat => (
62 is => 'ro',
63 7 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
64 2 );
65  
66  
67 has verbose => (
68 is => 'ro',
69 7 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
70 2 );
71  
72  
73 11 ▶sub get_offset {
Subroutine get_offset: called CC=17 SCAR=30.5
Pod documented
74 11 my ( $self, %args ) = @_;
75  
76 11 ▶ my $name = $args{note_name} || $self->scale_note;
Condition66%2/3
$args{'note_name'} || $self->scale_note
l!l&&r!l&&!r
830
MC/DC0%0/2
$args{'note_name'} || $self->scale_note
$args{'note_name'} $self->scale_note
Truth table66%2/3
A || B
A= $args{'note_name'}B= $self->scale_note
ABresult
1X1
011
000
77 11 ▶ my $format = $args{note_format} || $self->note_format;
Condition66%2/3
$args{'note_format'} || $self->note_format
l!l&&r!l&&!r
380
MC/DC0%0/2
$args{'note_format'} || $self->note_format
$args{'note_format'} $self->note_format
Truth table66%2/3
A || B
A= $args{'note_format'}B= $self->note_format
ABresult
1X1
011
000
78 11 ▶ my $offset = $args{offset} || $self->offset;
Condition66%2/3
$args{'offset'} || $self->offset
l!l&&r!l&&!r
650
MC/DC0%0/2
$args{'offset'} || $self->offset
$args{'offset'} $self->offset
Truth table66%2/3
A || B
A= $args{'offset'}B= $self->offset
ABresult
1X1
011
000
79 11 ▶ my $flat = $args{flat} || $self->flat;
Condition66%2/3
$args{'flat'} || $self->flat
l!l&&r!l&&!r
1010
MC/DC50%1/2
$args{'flat'} || $self->flat
$args{'flat'} $self->flat
Truth table66%2/3
A || B
A= $args{'flat'}B= $self->flat
ABresult
1X1
011
000
80  
81 11 ▶ croak 'note_name, note_format or offset not provided'
Branch: 1 branch
BranchTrueFalseTotal
unless $name or $format or $offset 0 11 11
Condition33%2/6
$name or $format or $offset
l!l&&r!l&&!r
1100
$name or $format
l!l&&r!l&&!r
1100
MC/DC0%0/3
$name or $format or $offset
$name $format $offset
Truth table25%1/4
A or B or C
A= $nameB= $formatC= $offset
ABCresult
1XX1
01X1
0011
0000
82 unless $name || $format || $offset;
83  
84 11 my $note = Music::Note->new( $name, $format );
85  
86 11 ▶ croak "Note not defined for $name and $format!" unless $note->format($format) eq $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $note->format($format) eq $name 1 10 11
87  
88 10 my $octave = $note->octave;
89  
90 10 ▶ $note->en_eq('flat') if $flat && $note->format('isobase') =~ /#/;
Branch: 1 branch
BranchTrueFalseTotal
if $flat and $note->format("isobase") =~ /#/ 0 10 10
Condition66%2/3
$flat and $note->format("isobase") =~ /#/
!ll&&!rl&&r
910
MC/DC0%0/2
$flat and $note->format("isobase") =~ /#/
$flat $note->format("isobase") =~ /#/
Truth table66%2/3
A and B
A= $flatB= $note->format("isobase") =~ /#/
ABresult
0X0
100
111
91  
92 10 ▶ printf "Given note: %s, Format: %s, ISO: %s, Offset: %d\n",
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 10 10
93 $name, $format, $note->format('ISO'), $offset
94 if $self->verbose;
95  
96 10 ▶ my $posn = first_index { $note->format('midinum') == $_ } @{ $self->_scale };
Subroutine __ANON__: called CC=1 SCAR=0.0
97 10 ▶ if ( $posn >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($posn >= 0) { } 9 1 10
98 9 ▶ printf "\tPosition: %d, Offset position: %d\n", $posn, $posn + $offset
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 9 9
99 if $self->verbose;
100 }
101 else {
102 1 croak 'Scale position not defined!';
103 }
104  
105 9 my $n = $self->_scale->[ $posn + $offset ];
106 9 $note = Music::Note->new( $n, 'midinum' );
107  
108 9 ▶ $note->en_eq('flat') if $flat && $note->format('ISO') =~ /#/;
Branch: 1 branch
BranchTrueFalseTotal
if $flat and $note->format("ISO") =~ /#/ 1 8 9
Condition66%2/3
$flat and $note->format("ISO") =~ /#/
!ll&&!rl&&r
801
MC/DC50%1/2
$flat and $note->format("ISO") =~ /#/
$flat $note->format("ISO") =~ /#/
Truth table66%2/3
A and B
A= $flatB= $note->format("ISO") =~ /#/
ABresult
0X0
100
111
109  
110 9 ▶ printf "\tOctave: %d, ISO: %s, Formatted: %s\n",
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 9 9
111 $octave, $note->format('ISO'), $note->format($format)
112 if $self->verbose;
113  
114 9 return $note;
115 }
116  
117  
118 7 ▶sub step {
Subroutine step: called CC=9 SCAR=22.5
Pod documented
119 7 my ( $self, %args ) = @_;
120  
121 7 ▶ my $name = $args{note_name} || $self->scale_note;
Condition33%1/3
$args{'note_name'} || $self->scale_note
l!l&&r!l&&!r
700
MC/DC0%0/2
$args{'note_name'} || $self->scale_note
$args{'note_name'} $self->scale_note
Truth table33%1/3
A || B
A= $args{'note_name'}B= $self->scale_note
ABresult
1X1
011
000
122 7 ▶ my $format = $args{note_format} || $self->note_format;
Condition66%2/3
$args{'note_format'} || $self->note_format
l!l&&r!l&&!r
160
MC/DC0%0/2
$args{'note_format'} || $self->note_format
$args{'note_format'} $self->note_format
Truth table66%2/3
A || B
A= $args{'note_format'}B= $self->note_format
ABresult
1X1
011
000
123 7 ▶ my $steps = $args{steps} || 1;
Condition100%2/2
$args{'steps'} || 1
l!l
61
MC/DC100%1/1
$args{'steps'} || 1
$args{'steps'}
Truth table100%2/2
A
A= $args{'steps'}
Aresult
11
00
124 7 ▶ my $flat = $args{flat} || $self->flat;
Condition66%2/3
$args{'flat'} || $self->flat
l!l&&r!l&&!r
304
MC/DC50%1/2
$args{'flat'} || $self->flat
$args{'flat'} $self->flat
Truth table66%2/3
A || B
A= $args{'flat'}B= $self->flat
ABresult
1X1
011
000
125  
126 7 my $note = Music::Note->new( $name, $format );
127 7 my $num = $note->format('midinum');
128  
129 7 ▶ printf "Given note: %s, ISO: %s, Formatted: %d\n",
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 7 7
130 $name, $note->format('ISO'), $num
131 if $self->verbose;
132  
133 7 $num += $steps;
134 7 $note = Music::Note->new( $num, 'midinum' );
135 7 ▶ $note->en_eq('flat') if $flat && $note->format('isobase') =~ /#/;
Branch: 1 branch
BranchTrueFalseTotal
if $flat and $note->format("isobase") =~ /#/ 2 5 7
Condition100%3/3
$flat and $note->format("isobase") =~ /#/
!ll&&!rl&&r
412
MC/DC100%2/2
$flat and $note->format("isobase") =~ /#/
$flat $note->format("isobase") =~ /#/
Truth table100%3/3
A and B
A= $flatB= $note->format("isobase") =~ /#/
ABresult
0X0
100
111
136  
137 7 ▶ printf "\tNew steps: %d, ISO: %s, Formatted: %s\n",
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 7 7
138 $steps, $note->format('ISO'), $note->format( $self->note_format )
139 if $self->verbose;
140  
141 7 return $note;
142 }
143  
144 2 1;
145  
146 __END__
↑ Summary