Skip to content
Browse files

Refactor a tad in preparation for better command line interface.

  • Loading branch information...
1 parent b8de2c9 commit 6b394ee8361eeea703751d7740116b12933eaa29 @colomon committed May 2, 2012
Showing with 38 additions and 35 deletions.
  1. +38 −35 bin/abc2ly.pl
View
73 bin/abc2ly.pl
@@ -133,15 +133,15 @@
}
}
-sub HeaderToLilypond(ABC::Header $header) {
- say "\\header \{";
+sub HeaderToLilypond(ABC::Header $header, $out) {
+ $out.say: "\\header \{";
my @titles = $header.get("T")>>.value;
- say " piece = \"{ @titles[0] }\"";
+ $out.say: " piece = \"{ @titles[0] }\"";
my @composers = $header.get("C")>>.value;
- say " composer = \"{ @composers[0] }\"" if ?@composers;
+ $out.say: " composer = \"{ @composers[0] }\"" if ?@composers;
- say "}";
+ $out.say: "}";
}
class TuneConvertor {
@@ -188,7 +188,7 @@ (ABC::Header $header)
$result ~ $lilypond-bar;
}
- method SectionToLilypond(@elements) {
+ method SectionToLilypond(@elements, $out) {
my $notes = "";
my $lilypond = "";
my $duration = 0;
@@ -307,14 +307,14 @@ (ABC::Header $header)
}
}
- say "\{";
+ $out.say: "\{";
$notes ~= self.WrapBar($lilypond, $duration);
- say $notes;
- say " \}";
+ $out.say: $notes;
+ $out.say: " \}";
}
- method BodyToLilypond(@elements) {
- say "\{";
+ method BodyToLilypond(@elements, $out) {
+ $out.say: "\{";
print $.context.key-to-string;
printf $.context.meter-to-string;
@@ -329,39 +329,39 @@ (ABC::Header $header)
|| @elements[$i].value eq ':|:' | ':|' | '::' {
print "\\repeat volta 2 "; # 2 is abitrarily chosen here!
}
- self.SectionToLilypond(@elements[$start-of-section ..^ $i]);
+ self.SectionToLilypond(@elements[$start-of-section ..^ $i], $out);
$start-of-section = $i + 1;
# if @elements[$i].value eq '||' {
# say '\\bar "||"';
# }
if @elements[$i].value eq '|]' {
- say '\\bar "|."';
+ $out.say: '\\bar "|."';
}
}
if @elements[$i].key eq "nth_repeat" {
my $final-bar = False;
- say "\\alternative \{";
+ $out.say: "\\alternative \{";
my $endings = 0;
loop (; $i < +@elements; $i++) {
# say @elements[$i].WHAT;
if @elements[$i].key eq "barline"
&& @elements[$i].value ne "|" {
- self.SectionToLilypond(@elements[$start-of-section ..^ $i]);
+ self.SectionToLilypond(@elements[$start-of-section ..^ $i], $out);
$start-of-section = $i + 1;
$final-bar = True if @elements[$i].value eq '|]';
last if ++$endings == 2;
}
}
if $endings == 1 {
- self.SectionToLilypond(@elements[$start-of-section ..^ $i]);
+ self.SectionToLilypond(@elements[$start-of-section ..^ $i], $out);
$start-of-section = $i + 1;
$final-bar = True if $i < +@elements && @elements[$i].value eq '|]';
}
- say "\}";
+ $out.say: "\}";
if $final-bar {
- say '\\bar "|."';
+ $out.say: '\\bar "|."';
}
}
@@ -373,34 +373,37 @@ (ABC::Header $header)
}
self.SectionToLilypond(@elements[$start-of-section ..^ +@elements]);
if @elements[*-1].value eq '|]' {
- say '\\bar "|."';
+ $out.say: '\\bar "|."';
}
}
- say "\}";
+ $out.say: "\}";
}
}
-my $match = ABC::Grammar.parse($*IN.slurp, :rule<tune_file>, :actions(ABC::Actions.new));
+sub TuneStreamToLilypondStream($in, $out) {
+ my $match = ABC::Grammar.parse($in.slurp, :rule<tune_file>, :actions(ABC::Actions.new));
-say '\\version "2.12.3"';
-say "#(set-default-paper-size \"{$paper-size}\")";
-
-for @( $match.ast ) -> $tune {
- $*ERR.say: "Working on { $tune.header.get-first-value("T") // $tune.header.get-first-value("X") }";
- say "\\score \{";
+ $out.say: '\\version "2.12.3"';
+ $out.say: "#(set-default-paper-size \"{$paper-size}\")";
- # say ~$tune.music;
+ for @( $match.ast ) -> $tune {
+ $*ERR.say: "Working on { $tune.header.get-first-value("T") // $tune.header.get-first-value("X") }";
+ $out.say: "\\score \{";
+
+ # say ~$tune.music;
- my $key = $tune.header.get("K")[0].value;
- my $meter = $tune.header.get("M")[0].value;
- my $length = $tune.header.get("L") ?? $tune.header.get("L")[0].value !! "1/8";
+ my $key = $tune.header.get("K")[0].value;
+ my $meter = $tune.header.get("M")[0].value;
+ my $length = $tune.header.get("L") ?? $tune.header.get("L")[0].value !! "1/8";
- my $convertor = TuneConvertor.new($key, $meter, $length);
- $convertor.BodyToLilypond($tune.music);
- HeaderToLilypond($tune.header);
+ my $convertor = TuneConvertor.new($key, $meter, $length);
+ $convertor.BodyToLilypond($tune.music, $out);
+ HeaderToLilypond($tune.header, $out);
- say "}\n\n";
+ $out.say: "}\n\n";
+ }
}
+TuneStreamToLilypondStream($*IN, $*OUT);

0 comments on commit 6b394ee

Please sign in to comment.
Something went wrong with that request. Please try again.