Permalink
Browse files

Fix thinkos, support global-accidental with simple tests.

  • Loading branch information...
1 parent 8d20fde commit c6e0e71c1f24e7966717aaeb4b8bf00cb1fbfe76 @colomon committed Mar 2, 2013
Showing with 40 additions and 31 deletions.
  1. +3 −3 lib/ABC/Grammar.pm
  2. +30 −28 lib/ABC/Utils.pm
  3. +7 −0 t/02-key.t
View
@@ -91,14 +91,14 @@ grammar ABC::Grammar
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-name { "treble" | "alto" | "tenor" | "baritone" | "bass" | "mezzo" | "soprano" | "perc" | "none" }
token clef-line { <[1..5]> }
token clef-middle { "middle=" <basenote> <octave> }
- token key { [<key-def> [\w+ <clef>]?] | <clef> | "HP" | "Hp" }
- token key-def { <basenote> ["#" | "b"]? [<mode>]? [\w+ <global-accidental>]* }
+ token key { [<key-def> [\h+ <clef>]?] | <clef> | "HP" | "Hp" }
+ token key-def { <basenote> <chord_accidental>? <mode>? [\h+ <global-accidental>]* }
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 major { "maj" ["o" ["r"]?]? }
View
@@ -31,41 +31,36 @@ package ABC::Utils {
'A' => 3,
'E' => 4,
'B' => 5,
- 'F#' => 6,
- 'C#' => 7,
'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;
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;
- my $sharps = %keys{$lookup};
-
- # say :$sharps.perl;
+ my $sharps = %keys{$match<key-def><basenote>.uc};
+ if $match<key-def><chord_accidental> {
+ given ~$match<key-def><chord_accidental> {
+ when "#" { $sharps += 7; }
+ when "b" { $sharps -= 7; }
+ }
+ }
- if ($match[1].defined) {
- given ~($match[1]) {
- when "" { }
- when /^maj/ { }
- when /^ion/ { }
- when /^mix/ { $sharps -= 1; }
- when /^dor/ { $sharps -= 2; }
- when /^m/ { $sharps -= 3; }
- when /^aeo/ { $sharps -= 3; }
- when /^phr/ { $sharps -= 4; }
- when /^loc/ { $sharps -= 5; }
- when /^lyd/ { $sharps += 1; }
- default { die "Unknown mode {$match[1]} requested"; }
+ if $match<key-def><mode> {
+ given $match<key-def><mode>[0] {
+ when so .<major> { }
+ when so .<ionian> { }
+ when so .<mixolydian> { $sharps -= 1; }
+ when so .<dorian> { $sharps -= 2; }
+ when so .<minor> { $sharps -= 3; }
+ when so .<aeolian> { $sharps -= 3; }
+ when so .<phrygian> { $sharps -= 4; }
+ when so .<locrian> { $sharps -= 5; }
+ when so .<lydian> { $sharps += 1; }
+ default { die "Unknown mode $_ requested"; }
}
}
@@ -76,6 +71,13 @@ package ABC::Utils {
when 1..7 { for ^$sharps -> $i { %hash{@sharp_notes[$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;
}
View
@@ -61,6 +61,13 @@ use ABC::Utils;
}
{
+ 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");
is apply_key_signature(%key, ABC::Grammar.parse("f", :rule<pitch>)), "^f", "f => ^f";
is apply_key_signature(%key, ABC::Grammar.parse("C", :rule<pitch>)), "^C", "C => ^C";

0 comments on commit c6e0e71

Please sign in to comment.