File Coverage

blib/lib/POE/Framework/MIDI/Key.pm
Criterion Covered Total %
statement 17 45 37.7
branch 1 8 12.5
condition n/a
subroutine 5 12 41.6
pod 0 8 0.0
total 23 73 31.5


line stmt bran cond sub pod time code
1             # $Id: Key.pm,v 1.2 2004/12/04 18:15:58 root Exp $
2            
3             package POE::Framework::MIDI::Key;
4            
5 2     2   190693 use strict;
  2         13  
  2         77  
6 2     2   10 no strict 'refs';
  2         4  
  2         48  
7 2     2   10 use vars '$VERSION'; $VERSION = '0.02';
  2         3  
  2         117  
8 2     2   2404 use MIDI::Simple;
  2         49421  
  2         1594  
9            
10             # a lexcion of key types and a key for building them from a root note
11             #
12             my $lexicon = {
13             # major scale is tone tone semitone, tone tone tone semitone
14             maj => [0,2,2,1,2,2,2,1], major => [0,2,2,1,2,2,2,1],
15             # declaring it twice is probably dumb, but we need to be able to refer to it by both
16             # key names......there's probaby a more sane way to do this
17             };
18            
19             sub new {
20 1     1 0 394 my ( $self, $class ) = ( {}, shift );
21 1         4 bless $self, $class;
22 1         6 $self->{cfg} = shift;
23             #$self->make_root_numeric;
24 1 50       15 $self->{intervals} = $lexicon->{$self->{cfg}->{name}}
25             or warn "sorry - $self->{cfg}->{name} is not supported yet";
26 1         8 return $self;
27             }
28            
29             # the root note - the tonic of the key
30             sub root {
31 0     0 0   my ( $self, $new_root ) = @_;
32 0 0         $new_root ?
33             $self->{cfg}->{root} = $new_root : return $self->{cfg}->{root}
34             }
35            
36             #chord's name
37             sub name {
38 0     0 0   my ( $self, $new_name ) = @_;
39 0 0         $new_name ?
40             $self->{cfg}->{name} = $new_name : return $self->{cfg}->{name}
41             }
42            
43             sub make_root_numeric {
44 0     0 0   my $self = shift;
45 0 0         if ($self->{cfg}->{root} =~ /\D/) {
46 0           $self->{cfg}->{root} = $self->note_to_number($self->{cfg}->{root});
47             }
48             }
49            
50             sub note_to_number {
51 0     0 0   my ( $self, $note ) = @_;
52 0           my ( $letters, $numbers ) = $note =~ /(\D+)(\d+)/;
53 0           my $position = $MIDI::Simple::Note{$letters};
54 0           my $end_position = $position + ($numbers * 12);
55 0           return $end_position;
56             }
57            
58             # what is the position of this note in the key?
59             sub noteposition {
60 0     0 0   my ( $self, $note ) = @_;
61 0           my $numeric_position = $self->note_to_number($note);
62 0           print $self->root . "\n";
63 0           my $scale = $self->numeric_scale;
64            
65             # we need to generate a "monster scale" that extends this tonal
66             # pattern off to 0 and 127
67 0           for (@$scale) {
68 0           print "$_\n";
69             }
70             }
71            
72             # use the intervals from the lexicon to make the scale based on the
73             # root for this key. For example, if use C4 for the root, (note #)
74             sub numeric_scale {
75 0     0 0   my $self = shift;
76 0           my @scale;
77 0           my $last_note = $self->root;
78 0           for (@{$self->{intervals}}) {
  0            
79 0           push @scale, $last_note + $_;
80 0           $last_note = $scale[-1]; # (negative subscripting)++
81             }
82 0           return \@scale;
83             }
84            
85             sub intervals {
86 0     0 0   my $self = shift;
87 0           return $self->{intervals};
88             }
89            
90            
91            
92            
93             1;
94            
95             __END__