| 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 |  | 16142 | use strict; | 
|  | 6 |  |  |  |  | 13 |  | 
|  | 6 |  |  |  |  | 135 |  | 
| 13 | 6 |  |  | 6 |  | 26 | use warnings; | 
|  | 6 |  |  |  |  | 12 |  | 
|  | 6 |  |  |  |  | 196 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | our $VERSION = "1.005"; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 6 |  |  | 6 |  | 31 | use Carp; | 
|  | 6 |  |  |  |  | 20 |  | 
|  | 6 |  |  |  |  | 389 |  | 
| 18 | 6 |  |  | 6 |  | 1606 | use App::Music::PlayTab::NoteMap qw(note_to_key key_to_note); | 
|  | 6 |  |  |  |  | 16 |  | 
|  | 6 |  |  |  |  | 2563 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | my $debug; | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | sub new { | 
| 23 | 699 |  |  | 699 | 0 | 1166 | my $pkg = shift; | 
| 24 | 699 |  | 33 |  |  | 2511 | $pkg = ref($pkg) || $pkg; | 
| 25 | 699 |  |  |  |  | 1792 | bless {}, $pkg; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub parse { | 
| 29 | 699 |  |  | 699 | 0 | 30086 | my ($self, $note) = @_; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # Parse note name and return internal code. | 
| 32 | 699 | 50 |  |  |  | 2089 | $self = $self->new unless ref($self); | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # Trim. | 
| 35 | 699 |  |  |  |  | 1577 | $note =~ s/\s+$//; | 
| 36 | 699 |  |  |  |  | 1657 | $self->{unparsed} = $note; | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | # Get out if relative scale specifier. | 
| 39 | 699 | 50 |  |  |  | 1725 | return '*' if $note eq '*';	# TODO: Why? | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 699 |  |  |  |  | 1036 | my $res; | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # Try sharp notes, e.g. Ais, C#. | 
| 44 | 699 | 100 |  |  |  | 3129 | if ( $note =~ /^([a-g])(is|\#)$/i ) { | 
|  |  | 100 |  |  |  |  |  | 
|  |  | 50 |  |  |  |  |  | 
| 45 | 65 |  |  |  |  | 198 | $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 |  |  |  |  | 316 | $res = (note_to_key($1) - 1) % 12; | 
| 50 | 107 |  |  |  |  | 229 | $self->{useflat} = 1; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  | # Try plain note, e.g. A, C. | 
| 53 |  |  |  |  |  |  | elsif ( $note =~ /^([a-g])$/i ) { | 
| 54 | 527 |  |  |  |  | 1443 | $res = note_to_key($1); | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # No more tries. | 
| 58 | 699 | 50 |  |  |  | 1716 | unless ( defined $res ) { | 
| 59 | 0 |  |  |  |  | 0 | croak("Unrecognized note name \"$note\""); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Return. | 
| 63 | 699 |  |  |  |  | 1254 | $self->{key} = $res; | 
| 64 | 699 |  |  |  |  | 1780 | $self; | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | sub transpose { | 
| 68 | 275 |  |  | 275 | 0 | 781 | my ($self, $xp) = @_; | 
| 69 | 275 | 100 |  |  |  | 613 | return $self unless $xp; | 
| 70 | 269 |  |  |  |  | 530 | $self->{key} = ($self->{key} + $xp) % 12; | 
| 71 | 269 |  |  |  |  | 592 | $self->{useflat} = $xp < 0; | 
| 72 | 269 |  |  |  |  | 519 | $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 | 1582 | my $self = shift; | 
| 82 | 819 |  |  |  |  | 2495 | App::Music::PlayTab::NoteMap::key_to_note($self->{key}, $self->{useflat}); | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  |  | 
| 85 |  |  |  |  |  |  | sub ps { | 
| 86 | 261 |  |  | 261 | 0 | 5276 | my $self = shift; | 
| 87 | 261 |  |  |  |  | 538 | my $res = $self->name; | 
| 88 | 261 | 100 |  |  |  | 906 | if ( $res =~ /(.)b/ ) { | 
|  |  | 100 |  |  |  |  |  | 
| 89 | 37 |  |  |  |  | 140 | $res = '('.$1.') root flat'; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  | elsif ( $res =~ /(.)#/ ) { | 
| 92 | 25 |  |  |  |  | 114 | $res = '('.$1.') root sharp'; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | else { | 
| 95 | 199 |  |  |  |  | 439 | $res = '('.$res.') root'; | 
| 96 |  |  |  |  |  |  | } | 
| 97 | 261 |  |  |  |  | 604 | $res; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | 1; | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | __END__ |