| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 11 | ▶ | my $format = $args{note_format} || $self->note_format; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'note_format'} || $self->note_format
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
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 11 | ▶ | my $offset = $args{offset} || $self->offset; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'offset'} || $self->offset
MC/DC0%0/2
$args{'offset'} || $self->offset
$args{'offset'}
$self->offset
Truth table66%2/3
A || B
A= $args{'offset'}B= $self->offset
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 11 | ▶ | my $flat = $args{flat} || $self->flat; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'flat'} || $self->flat
MC/DC50%1/2
$args{'flat'} || $self->flat
$args{'flat'}
$self->flat
Truth table66%2/3
A || B
A= $args{'flat'}B= $self->flat
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 11 | ▶ | croak 'note_name, note_format or offset not provided' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$name or $format or $offset
$name or $format
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 10 | my $octave = $note->octave; | |||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | 10 | ▶ | $note->en_eq('flat') if $flat && $note->format('isobase') =~ /#/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$flat and $note->format("isobase") =~ /#/
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") =~ /#/
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | ▶ | printf "Given note: %s, Format: %s, ISO: %s, Offset: %d\n", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 9 | ▶ | printf "\tPosition: %d, Offset position: %d\n", $posn, $posn + $offset | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$flat and $note->format("ISO") =~ /#/
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") =~ /#/
| |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | ▶ | printf "\tOctave: %d, ISO: %s, Formatted: %s\n", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 7 | ▶ | my $format = $args{note_format} || $self->note_format; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'note_format'} || $self->note_format
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
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 7 | ▶ | my $steps = $args{steps} || 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'steps'} || 1
MC/DC100%1/1
$args{'steps'} || 1
$args{'steps'}
Truth table100%2/2
A
A= $args{'steps'}
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 7 | ▶ | my $flat = $args{flat} || $self->flat; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'flat'} || $self->flat
MC/DC50%1/2
$args{'flat'} || $self->flat
$args{'flat'}
$self->flat
Truth table66%2/3
A || B
A= $args{'flat'}B= $self->flat
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$flat and $note->format("isobase") =~ /#/
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") =~ /#/
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | 7 | ▶ | printf "\tNew steps: %d, ISO: %s, Formatted: %s\n", | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||