File Coverage

blib/lib/App/Music/PlayTab/Note.pm
Criterion Covered Total %
statement 43 46 93.4
branch 14 18 77.7
condition 1 3 33.3
subroutine 9 10 90.0
pod 0 6 0.0
total 67 83 80.7


line stmt bran cond sub pod time code
1             #! perl
2              
3             # Author : Johan Vromans
4             # Created On : Wed Aug 22 22:33:31 2007
5             # Last Modified By: Johan Vromans
6             # Last Modified On: Tue Apr 19 16:25:06 2011
7             # Update Count : 6
8             # Status : Unknown, Use with caution!
9              
10             package App::Music::PlayTab::Note;
11              
12 6     6   17355 use strict;
  6         8  
  6         134  
13 6     6   16 use warnings;
  6         7  
  6         193  
14              
15             our $VERSION = "1.005";
16              
17 6     6   24 use Carp;
  6         55  
  6         440  
18 6     6   1718 use App::Music::PlayTab::NoteMap qw(note_to_key key_to_note);
  6         8  
  6         2834  
19              
20             my $debug;
21              
22             sub new {
23 699     699 0 506 my $pkg = shift;
24 699   33     1748 $pkg = ref($pkg) || $pkg;
25 699         1038 bless {}, $pkg;
26             }
27              
28             sub parse {
29 699     699 0 15313 my ($self, $note) = @_;
30              
31             # Parse note name and return internal code.
32 699 50       1371 $self = $self->new unless ref($self);
33              
34             # Trim.
35 699         847 $note =~ s/\s+$//;
36 699         901 $self->{unparsed} = $note;
37              
38             # Get out if relative scale specifier.
39 699 50       996 return '*' if $note eq '*'; # TODO: Why?
40              
41 699         454 my $res;
42              
43             # Try sharp notes, e.g. Ais, C#.
44 699 100       2182 if ( $note =~ /^([a-g])(is|\#)$/i ) {
    100          
    50          
45 65         118 $res = (note_to_key($1) + 1) % 12;
46             }
47             # Try flat notes, e.g. Es, Bes, Db.
48             elsif ( $note =~ /^([a-g])(e?s|b)$/i ) {
49 107         193 $res = (note_to_key($1) - 1) % 12;
50 107         123 $self->{useflat} = 1;
51             }
52             # Try plain note, e.g. A, C.
53             elsif ( $note =~ /^([a-g])$/i ) {
54 527         866 $res = note_to_key($1);
55             }
56              
57             # No more tries.
58 699 50       955 unless ( defined $res ) {
59 0         0 croak("Unrecognized note name \"$note\"");
60             }
61              
62             # Return.
63 699         624 $self->{key} = $res;
64 699         1081 $self;
65             }
66              
67             sub transpose {
68 275     275 0 380 my ($self, $xp) = @_;
69 275 100       350 return $self unless $xp;
70 269         275 $self->{key} = ($self->{key} + $xp) % 12;
71 269         323 $self->{useflat} = $xp < 0;
72 269         261 $self;
73             }
74              
75             sub key {
76 0     0 0 0 my $self = shift;
77 0         0 $self->{key};
78             }
79              
80             sub name {
81 819     819 0 747 my $self = shift;
82 819         1643 App::Music::PlayTab::NoteMap::key_to_note($self->{key}, $self->{useflat});
83             }
84              
85             sub ps {
86 261     261 0 3249 my $self = shift;
87 261         341 my $res = $self->name;
88 261 100       646 if ( $res =~ /(.)b/ ) {
    100          
89 37         79 $res = '('.$1.') root flat';
90             }
91             elsif ( $res =~ /(.)#/ ) {
92 25         64 $res = '('.$1.') root sharp';
93             }
94             else {
95 199         266 $res = '('.$res.') root';
96             }
97 261         415 $res;
98             }
99              
100             1;
101              
102             __END__