From 4afefae1262c1daa9d9deaec7517d29b674fb4d7 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Sun, 6 May 2007 05:07:39 +0000 Subject: [PATCH] Implement division/time change --- Sources/VLDocument.mm | 35 +++---- Sources/VLModel.cpp | 231 ++++++++++++++++++++++++++++++++++++++---- Sources/VLModel.h | 4 +- 3 files changed, 228 insertions(+), 42 deletions(-) diff --git a/Sources/VLDocument.mm b/Sources/VLDocument.mm index 55eff02..641f455 100644 --- a/Sources/VLDocument.mm +++ b/Sources/VLDocument.mm @@ -204,16 +204,10 @@ - (void) setKey:(int)key transpose:(BOOL)transpose { [self willChangeSong]; - - VLProperties & prop = song->fProperties.front(); - - if (transpose) - song->Transpose((7*((key>>8)-prop.fKey) % 12)); - - prop.fKey = key >> 8; - prop.fMode= key & 0xFF; - - [self updateChangeCount:NSChangeDone]; + [self willChangeValueForKey:@"songKey"]; + song->ChangeKey(key>>8, key & 0xFF, transpose); + [self didChangeValueForKey:@"songKey"]; + [self didChangeSong]; } - (NSNumber *) songTime @@ -226,12 +220,10 @@ - (void) setTimeNum:(int)num denom:(int)denom { [self willChangeSong]; - - VLProperties & prop = song->fProperties.front(); - - prop.fTime = VLFraction(num, denom); - - [self updateChangeCount:NSChangeDone]; + [self willChangeValueForKey:@"songTime"]; + song->ChangeTime(VLFraction(num, denom)); + [self didChangeValueForKey:@"songTime"]; + [self didChangeSong]; } - (NSNumber *) songDivisions @@ -244,12 +236,11 @@ - (void) setDivisions:(int)divisions { [self willChangeSong]; - - VLProperties & prop = song->fProperties.front(); - - prop.fDivisions = divisions; - - [self updateChangeCount:NSChangeDone]; + [self willChangeValueForKey:@"songDivisions"]; + [self willChangeSong]; + song->ChangeDivisions(divisions); + [self didChangeValueForKey:@"songDivisions"]; + [self didChangeSong]; } - (int) repeatVolta diff --git a/Sources/VLModel.cpp b/Sources/VLModel.cpp index 10cad58..700e2a8 100644 --- a/Sources/VLModel.cpp +++ b/Sources/VLModel.cpp @@ -852,7 +852,7 @@ VLSong::VLSong(bool initialize) void VLSong::AddMeasure() { - VLFraction dur = fProperties[0].fTime; + VLFraction dur = fProperties.front().fTime; VLLyricsNote rest = VLLyricsNote(VLRest(dur)); VLChord rchord; rchord.fDuration = dur; @@ -1131,22 +1131,6 @@ void VLSong::ExtendNote(size_t measure, VLFraction at) } } -static void TransposePinned(int8_t & pitch, int semi) -{ - if (pitch == VLNote::kNoPitch) - return; - - int pitchInOctave = pitch % 12; - int octave = pitch-pitchInOctave; - pitchInOctave += semi; - if (pitchInOctave < 0) - pitch = octave+pitchInOctave+12; - else if (pitchInOctave > 11) - pitch = octave+pitchInOctave-12; - else - pitch = octave+pitchInOctave; -} - bool VLSong::IsNonEmpty() const { for (size_t measure=0; measure 11) + pitch = octave+pitchInOctave-12; + else + pitch = octave+pitchInOctave; +} + +void VLSong::ChangeKey(int newKey, bool newMode, bool transpose) +{ + VLProperties & prop = fProperties.front(); + int semi = 7*(newKey-prop.fKey) % 12; + prop.fKey = newKey; + prop.fMode = newMode; + if (!transpose) + return; + for (size_t measure=0; measure= newTime) { + if (at < newTime) { + c.fDuration = newTime-at; + newChords.push_back(c); + } + break; + } else { + newChords.push_back(c); + at += c.fDuration; + } + } + fMeasures[measure].fChords.swap(newChords); + } else + fMeasures[measure].fChords.push_back(rchord); + + if (newTime < prop.fTime) { + VLNoteList::iterator i = fMeasures[measure].fMelody.begin(); + VLNoteList::iterator e = fMeasures[measure].fMelody.end(); + VLFraction at(0); + VLNoteList newMelody; + + for (; i!=e; ++i) { + VLLyricsNote n = *i; + if (at+n.fDuration >= newTime) { + if (at < newTime) { + n.fDuration = newTime-at; + newMelody.push_back(n); + } + break; + } else { + newMelody.push_back(n); + at += n.fDuration; + } + } + fMeasures[measure].fMelody.swap(newMelody); + } else + fMeasures[measure].fMelody.push_back(rnote); + } + prop.fTime = newTime; +} + size_t VLSong::EmptyEnding() const { size_t full = fMeasures.size(); @@ -1289,7 +1482,7 @@ void VLSong::LilypondNotes(std::string & notes) const int numEndings = 0; size_t endMeasure = fMeasures.size()-EmptyEnding(); bool pickup = fMeasures[0].NoChords(); - for (size_t measure=0; measure