Skip to content

Commit

Permalink
Fix thinkos, support global-accidental with simple tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Mar 2, 2013
1 parent 8d20fde commit c6e0e71
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
6 changes: 3 additions & 3 deletions lib/ABC/Grammar.pm
Expand Up @@ -91,14 +91,14 @@ grammar ABC::Grammar


token tune_file { \s* [<tune> \s*]+ } token tune_file { \s* [<tune> \s*]+ }


token clef { [ ["clef=" [<clef-note> | <clef-name>]] | <clef-name>] <clef-line>? ["+8" | "-8"]? [\w+ <clef-middle>]? } token clef { [ ["clef=" [<clef-note> | <clef-name>]] | <clef-name>] <clef-line>? ["+8" | "-8"]? [\h+ <clef-middle>]? }
token clef-note { "G" | "C" | "F" | "P" } token clef-note { "G" | "C" | "F" | "P" }
token clef-name { "treble" | "alto" | "tenor" | "baritone" | "bass" | "mezzo" | "soprano" | "perc" | "none" } token clef-name { "treble" | "alto" | "tenor" | "baritone" | "bass" | "mezzo" | "soprano" | "perc" | "none" }
token clef-line { <[1..5]> } token clef-line { <[1..5]> }
token clef-middle { "middle=" <basenote> <octave> } token clef-middle { "middle=" <basenote> <octave> }


token key { [<key-def> [\w+ <clef>]?] | <clef> | "HP" | "Hp" } token key { [<key-def> [\h+ <clef>]?] | <clef> | "HP" | "Hp" }
token key-def { <basenote> ["#" | "b"]? [<mode>]? [\w+ <global-accidental>]* } token key-def { <basenote> <chord_accidental>? <mode>? [\h+ <global-accidental>]* }
token mode { <minor> | <major> | <lydian> | <ionian> | <mixolydian> | <dorian> | <aeolian> | <phrygian> | <locrian> } token mode { <minor> | <major> | <lydian> | <ionian> | <mixolydian> | <dorian> | <aeolian> | <phrygian> | <locrian> }
token minor { "m" ["in" ["o" ["r"]?]?]? } # m, min, mino, minor - all modes are case insensitive token minor { "m" ["in" ["o" ["r"]?]?]? } # m, min, mino, minor - all modes are case insensitive
token major { "maj" ["o" ["r"]?]? } token major { "maj" ["o" ["r"]?]? }
Expand Down
58 changes: 30 additions & 28 deletions lib/ABC/Utils.pm
Expand Up @@ -31,41 +31,36 @@ package ABC::Utils {
'A' => 3, 'A' => 3,
'E' => 4, 'E' => 4,
'B' => 5, 'B' => 5,
'F#' => 6,
'C#' => 7,
'F' => -1, 'F' => -1,
'Bb' => -2,
'Eb' => -3,
'Ab' => -4,
'Db' => -5,
'Gb' => -6,
'Cb' => -7
); );

# say :$key_signature_name.perl;


my $match = ABC::Grammar.parse($key_signature_name, :rule<key_sig>); my $match = ABC::Grammar.parse($key_signature_name, :rule<key>);
# say :$match.perl; # say :$match.perl;
die "Illegal key signature\n" unless $match; die "Illegal key signature\n" unless $match;
my $lookup = $match<basenote>.uc ~ ($match[0] // ""); fail unless $match<key-def>;
say $match<key-def>.perl;
my $lookup = $match<key-def><basenote>.uc;
# say :$lookup.perl; # say :$lookup.perl;
my $sharps = %keys{$lookup}; my $sharps = %keys{$match<key-def><basenote>.uc};

if $match<key-def><chord_accidental> {
# say :$sharps.perl; given ~$match<key-def><chord_accidental> {
when "#" { $sharps += 7; }
when "b" { $sharps -= 7; }
}
}


if ($match[1].defined) { if $match<key-def><mode> {
given ~($match[1]) { given $match<key-def><mode>[0] {
when "" { } when so .<major> { }
when /^maj/ { } when so .<ionian> { }
when /^ion/ { } when so .<mixolydian> { $sharps -= 1; }
when /^mix/ { $sharps -= 1; } when so .<dorian> { $sharps -= 2; }
when /^dor/ { $sharps -= 2; } when so .<minor> { $sharps -= 3; }
when /^m/ { $sharps -= 3; } when so .<aeolian> { $sharps -= 3; }
when /^aeo/ { $sharps -= 3; } when so .<phrygian> { $sharps -= 4; }
when /^phr/ { $sharps -= 4; } when so .<locrian> { $sharps -= 5; }
when /^loc/ { $sharps -= 5; } when so .<lydian> { $sharps += 1; }
when /^lyd/ { $sharps += 1; } default { die "Unknown mode $_ requested"; }
default { die "Unknown mode {$match[1]} requested"; }
} }
} }


Expand All @@ -76,6 +71,13 @@ package ABC::Utils {
when 1..7 { for ^$sharps -> $i { %hash{@sharp_notes[$i]} = "^"; } } when 1..7 { for ^$sharps -> $i { %hash{@sharp_notes[$i]} = "^"; } }
when -7..-1 { for ^(-$sharps) -> $i { %hash{@sharp_notes[6-$i]} = "_"; } } when -7..-1 { for ^(-$sharps) -> $i { %hash{@sharp_notes[6-$i]} = "_"; } }
} }

if $match<key-def><global-accidental> {
for $match<key-def><global-accidental> -> $ga {
say $ga<basenote>.uc, " huh ", ~$ga<accidental>;
%hash{$ga<basenote>.uc} = ~$ga<accidental>;
}
}


return %hash; return %hash;
} }
Expand Down
7 changes: 7 additions & 0 deletions t/02-key.t
Expand Up @@ -60,6 +60,13 @@ use ABC::Utils;
is %key<B>, "^", "B is sharp"; is %key<B>, "^", "B is sharp";
} }


{
my %key = key_signature("C ^f _b");
is %key.elems, 2, "C ^f _b has two thingees";
is %key<F>, "^", "F is sharp";
is %key<B>, "_", "B is flat";
}

{ {
my %key = key_signature("C#m"); my %key = key_signature("C#m");
is apply_key_signature(%key, ABC::Grammar.parse("f", :rule<pitch>)), "^f", "f => ^f"; is apply_key_signature(%key, ABC::Grammar.parse("f", :rule<pitch>)), "^f", "f => ^f";
Expand Down

0 comments on commit c6e0e71

Please sign in to comment.