Show
Ignore:
Timestamp:
12/08/08 16:34:57 (6 years ago)
Author:
bayashi
Message:

add 'scale' method & add some tests

Location:
lang/perl/Music-Chord-Note/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/Music-Chord-Note/trunk/Changes

    r25526 r26114  
    11Revision history for Music-Chord-Note 
    22 
     30.0.3  mon Dec 08 16:16:16 2008 
     4       add scale func & test 
     5        
    360.0.2  mon Dec 01 12:12:12 2008 
    47       fixed chord list. 
  • lang/perl/Music-Chord-Note/trunk/META.yml

    r25526 r26114  
    11--- 
    22name: Music-Chord-Note 
    3 version: 0.0.2 
     3version: 0.0.3 
    44author: 
    55  - 'Dai Okabayashi <bayashi@cpan.org>' 
     
    1414  Music::Chord::Note: 
    1515    file: lib/Music/Chord/Note.pm 
    16     version: 0.0.2 
     16    version: 0.0.3 
    1717generated_by: Module::Build version 0.3 
    1818meta-spec: 
  • lang/perl/Music-Chord-Note/trunk/lib/Music/Chord/Note.pm

    r25526 r26114  
    55use Carp qw( croak ); 
    66 
    7 our $VERSION = '0.0.2'; 
     7our $VERSION = '0.0.3'; 
    88 
    99my @tone_list = ('C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B', 
     
    7979}; 
    8080 
    81 sub new { 
     81sub new 
     82{ 
    8283    my $class = shift; 
    8384    bless {}, $class; 
    8485} 
    8586 
    86 sub chord { 
     87sub chord 
     88{ 
    8789    my ($self, $chord_name) = @_; 
     90 
    8891    croak "No CHORD_NAME!" unless $chord_name; 
    8992    my ($tonic, $kind) = ($chord_name =~ /([A-G][b#]?)(.+)?/); 
    90     croak("unknown chord $chord_name") unless defined $tonic; 
     93    croak "unknown chord $chord_name" unless defined $tonic; 
    9194    $kind = 'base' unless $kind; 
    9295    my $scalic = $scalic_value->{$tonic}; 
    93     croak("undefined kind of chord $kind($chord_name)") unless defined $base_chord_list->{$kind}; 
     96    croak "undefined kind of chord $kind($chord_name)" 
     97        unless defined $base_chord_list->{$kind}; 
     98 
    9499    my @keys; 
    95     for my $scale ( split(/\,/, $base_chord_list->{$kind}) ){ 
     100    for my $scale ( split /\,/, $base_chord_list->{$kind} ){ 
    96101        my $note = $scale + $scalic; 
    97102        $note = int($note % 24) + 12 if $note > 23; 
    98         push(@keys, $tone_list[$note]); 
     103        push @keys, $tone_list[$note]; 
    99104    } 
     105 
    100106    return @keys; 
     107} 
     108 
     109sub scale 
     110{ 
     111    my $self = shift; 
     112    my $note = shift || return; 
     113 
     114    $note =~ s/^([a-g])/uc($1)/e; 
     115    croak "wrong note ($note)" if $note !~ /^[A-G](?:[#b])?$/; 
     116 
     117    return $scalic_value->{$note}; 
    101118} 
    102119 
     
    121138    print "@tone"; # C E G B 
    122139 
     140    my $note = $cn->scale('D#'); 
     141 
     142    print "$note"; # 3 
     143 
    123144 
    124145=head1 METHOD 
     
    133154 
    134155get tone list from chord name 
     156 
     157=item scale($note) 
     158 
     159get scalic value from C (C=0, B=11) 
    135160 
    136161=back 
  • lang/perl/Music-Chord-Note/trunk/t/01.rehearsal.t

    r25526 r26114  
    11use strict; 
    22 
    3 use Test::More tests => 22; 
     3use Test::More tests => 31; 
    44 
    55use Music::Chord::Note; 
     
    5252 
    5353 
     54my $no_note = $cn->scale(''); 
     55is($no_note, undef, "No Note"); 
    5456 
     57eval { my $wrong_note = $cn->scale('H'); }; 
     58like($@, qr/wrong note/, "Wrong Note H"); 
     59 
     60eval { my $wrong_note = $cn->scale('C+'); }; 
     61like($@, qr/wrong note/, "Wrong Note C+"); 
     62 
     63eval { my $wrong_note = $cn->scale('D-'); }; 
     64like($@, qr/wrong note/, "Wrong Note D-"); 
     65 
     66my $sv = $cn->scale('C'); 
     67is($sv, 0, 'Scalic Value C'); 
     68 
     69my $sv1 = $cn->scale('A'); 
     70is($sv1, 9, 'Scalic Value A'); 
     71 
     72my $sv2 = $cn->scale('A#'); 
     73is($sv2, 10, 'Scalic Value A#'); 
     74 
     75my $sv3 = $cn->scale('Eb'); 
     76is($sv3, 3, 'Scalic Value Eb'); 
     77 
     78my $sv4 = $cn->scale('gb'); 
     79is($sv4, 6, 'Scalic Value gb');