From 83438255fd49ecca6544d4908010910b9aa629f2 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Sat, 26 Jan 2008 10:54:00 +0000 Subject: [PATCH] Support changing time/key in lilypond --- Resources/Templates/default.lyt | 2 -- Sources/VLLilypondDocument.mm | 17 ----------------- Sources/VLLilypondWriter.cpp | 27 ++++++++++++++++++++++++++- Sources/VLLilypondWriter.h | 1 + 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/Resources/Templates/default.lyt b/Resources/Templates/default.lyt index 96af19a..774dc83 100644 --- a/Resources/Templates/default.lyt +++ b/Resources/Templates/default.lyt @@ -83,8 +83,6 @@ vlExceptions = #(append <{CHORDS}> } \context Voice = "mel" { - \time <{TIME}> - \key <{KEY}> <{NOTES}> } \lyricsto "mel" \new Lyrics { <{LYRICS}> } diff --git a/Sources/VLLilypondDocument.mm b/Sources/VLLilypondDocument.mm index a806bf2..9141c75 100644 --- a/Sources/VLLilypondDocument.mm +++ b/Sources/VLLilypondDocument.mm @@ -105,17 +105,8 @@ @implementation VLDocument (Lilypond) -const int kMajorOffset = 6; -const int kMinorOffset = 9; - -static const char * sKeyNames[] = { - "ges", "des", "as", "es", "bes", "f", - "c", "g", "d", "a", "e", "b", "fis", "cis", "gis" -}; - - (NSData *)lilypondDataWithError:(NSError **)outError { - const VLProperties & prop = song->fProperties.front(); VLLilypondWriter writer; writer.Visit(*song); NSBundle * bndl = [NSBundle mainBundle]; @@ -137,14 +128,6 @@ static const char * sKeyNames[] = { [bndl objectForInfoDictionaryKey:@"CFBundleVersion"]]; [ly substituteMacro:@"CHORDS" withValue: [NSString stringWithUTF8String:writer.Chords().c_str()]]; - [ly substituteMacro:@"TIME" withValue: - [NSString stringWithFormat:@"%d/%d", - prop.fTime.fNum, prop.fTime.fDenom]]; - [ly substituteMacro:@"KEY" withValue: prop.fMode > 0 - ? [NSString stringWithFormat:@"%s \\major", - sKeyNames[prop.fKey+kMajorOffset]] - : [NSString stringWithFormat:@"%s \\minor", - sKeyNames[prop.fKey+kMinorOffset]]]; [ly substituteMacro:@"NOTES" withValue: [NSString stringWithUTF8String:writer.Melody().c_str()]]; if (size_t stanzas = song->CountStanzas()) diff --git a/Sources/VLLilypondWriter.cpp b/Sources/VLLilypondWriter.cpp index 34f8dc5..624e599 100644 --- a/Sources/VLLilypondWriter.cpp +++ b/Sources/VLLilypondWriter.cpp @@ -22,6 +22,7 @@ void VLLilypondWriter::Visit(VLSong & song) fIndent.clear(); fSeenEnding = 0; fNumEndings = 0; + fLastProp = 0; VisitMeasures(song, false); // @@ -50,6 +51,31 @@ void VLLilypondWriter::VisitMeasure(size_t m, VLProperties & p, VLMeasure & meas fAccum += measNo; fChords+= fAccum + '\n'; + fAccum.clear(); + // + // Generate key/time if changed + // + if (!fLastProp || fLastProp->fTime != p.fTime) { + char time[16]; + sprintf(time, "\\time %d/%d\n", p.fTime.fNum, p.fTime.fDenom); + fAccum += fIndent+time; + } + if (!fLastProp || fLastProp->fKey != p.fKey || fLastProp->fMode != p.fMode) { + const int kMajorOffset = 6; + const int kMinorOffset = 9; + + static const char * sKeyNames[] = { + "ges", "des", "as", "es", "bes", "f", + "c", "g", "d", "a", "e", "b", "fis", "cis", "gis" + }; + char key[16]; + if (p.fMode < 0) + sprintf(key, "\\key %s \\minor\n", sKeyNames[p.fKey+kMinorOffset]); + else + sprintf(key, "\\key %s \\major\n", sKeyNames[p.fKey+kMajorOffset]); + fAccum += fIndent+key; + } + fLastProp = &p; // // Generate structure elements // @@ -57,7 +83,6 @@ void VLLilypondWriter::VisitMeasure(size_t m, VLProperties & p, VLMeasure & meas size_t volta; bool repeat; - fAccum.clear(); if (meas.fBreak == VLMeasure::kNewPage) fAccum += fIndent+"\\pageBreak\n"; else if (meas.fBreak == VLMeasure::kNewSystem) diff --git a/Sources/VLLilypondWriter.h b/Sources/VLLilypondWriter.h index befd759..b77703d 100644 --- a/Sources/VLLilypondWriter.h +++ b/Sources/VLLilypondWriter.h @@ -36,6 +36,7 @@ private: std::string fAccum; std::string fIndent; std::vector fL; + VLProperties * fLastProp; }; // Local Variables: