From f9f2946bb8dd9484b3adac257c503dce4462e5f3 Mon Sep 17 00:00:00 2001 From: Matthias Neeracher Date: Thu, 24 Jan 2008 01:29:18 +0000 Subject: [PATCH] Smart groove preview --- Sources/VLDocument.h | 6 ++-- Sources/VLDocument.mm | 9 ++++-- Sources/VLGrooveController.h | 1 + Sources/VLGrooveController.mm | 5 +-- Sources/VLMMADocument.mm | 60 ++++++++++++++++++++--------------- Sources/VLMMAWriter.cpp | 6 +++- Sources/VLMMAWriter.h | 7 +++- Sources/VLSheetView.h | 2 +- Sources/VLSheetView.mm | 5 +++ 9 files changed, 66 insertions(+), 35 deletions(-) diff --git a/Sources/VLDocument.h b/Sources/VLDocument.h index 49ec105..6692a97 100644 --- a/Sources/VLDocument.h +++ b/Sources/VLDocument.h @@ -21,7 +21,8 @@ enum { kVLPlayAccompaniment = 1, kVLPlayMelody = 2, kVLPlayMetronome = 4, - kVLPlayCountIn = 8 + kVLPlayCountIn = 8, + kVLPlayGroovePreview = 32768 }; @interface VLDocument : NSDocument @@ -46,6 +47,7 @@ enum { VLPDFWindow * pdfWin; VLKeyValueUndo* undo; PDFDocument * printDoc; + NSRange previewRange; } - (VLSong *) song; @@ -59,6 +61,7 @@ enum { - (void) setTimeNum:(int)num denom:(int)denom inSections:(NSRange)sections; - (void) setDivisions:(int)divisions inSections:(NSRange)sections; - (void) setGroove:(NSString *)groove inSections:(NSRange)sections; +- (void) playWithGroove:(NSString *)groove inSections:(NSRange)sections; - (void) setRepeatVolta:(int)repeatVolta; @@ -81,7 +84,6 @@ enum { - (void) didChangeSong; - (void) addObserver:(id)observer; - (VLLogWindow *)logWin; -- (void) playWithGroove:(NSString *)groove; @end diff --git a/Sources/VLDocument.mm b/Sources/VLDocument.mm index fbb4356..e13b29f 100644 --- a/Sources/VLDocument.mm +++ b/Sources/VLDocument.mm @@ -448,14 +448,17 @@ [self fileURLWithExtension:@"mid"]])); } -- (void) playWithGroove:(NSString *)groove +- (void) playWithGroove:(NSString *)groove inSections:(NSRange)sections { NSString * savedGroove = songGroove; - songGroove = groove; [validTmpFiles removeObjectForKey:@"mma"]; [validTmpFiles removeObjectForKey:@"mid"]; + songGroove = groove; + previewRange = sections; + playElements |= kVLPlayGroovePreview; [self play:groove]; - songGroove = savedGroove; + playElements &= ~kVLPlayGroovePreview; + songGroove = savedGroove; [validTmpFiles removeObjectForKey:@"mma"]; [validTmpFiles removeObjectForKey:@"mid"]; } diff --git a/Sources/VLGrooveController.h b/Sources/VLGrooveController.h index 244c9f9..985b351 100644 --- a/Sources/VLGrooveController.h +++ b/Sources/VLGrooveController.h @@ -23,6 +23,7 @@ NSDictionary * fSubStyles; NSArray * fSubStyleList; NSPredicate * fSubStyleFilter; + VLSheetView * fView; VLDocument * fDocument; } diff --git a/Sources/VLGrooveController.mm b/Sources/VLGrooveController.mm index 03630df..36e73dc 100644 --- a/Sources/VLGrooveController.mm +++ b/Sources/VLGrooveController.mm @@ -23,6 +23,7 @@ [[NSPredicate predicateWithFormat: @"!(SELF matches[c] '.*(Intro|End)\\\\d*$')"] retain]; + fView = view; fDocument = [view document]; [NSApp beginSheet: [self window] @@ -43,7 +44,7 @@ - (IBAction) togglePlay:(id)sender { if ([sender state]) - [fDocument playWithGroove:[[fBrowser selectedCellInColumn:1] stringValue]]; + [fView playWithGroove:[[fBrowser selectedCellInColumn:1] stringValue]]; else [fDocument stop:sender]; } @@ -57,7 +58,7 @@ { [fDocument stop:self]; if (returnCode == NSAlertFirstButtonReturn) - [(VLSheetView *)contextInfo setGroove:[[fBrowser selectedCellInColumn:1] stringValue]]; + [fView setGroove:[[fBrowser selectedCellInColumn:1] stringValue]]; [[self window] orderOut:self]; } diff --git a/Sources/VLMMADocument.mm b/Sources/VLMMADocument.mm index 760cc9c..18bf59c 100644 --- a/Sources/VLMMADocument.mm +++ b/Sources/VLMMADocument.mm @@ -15,37 +15,47 @@ - (NSData *)mmaDataWithError:(NSError **)outError { - char buf[32]; - NSBundle * bndl = [NSBundle mainBundle]; - VLMMAWriter writer; - writer.Visit(*song); - const VLProperties & prop = song->fProperties.front(); + char buf[32]; + NSBundle * bndl = [NSBundle mainBundle]; + VLMMAWriter writer(playElements & kVLPlayGroovePreview, + previewRange.location, + previewRange.location+previewRange.length); + writer.Visit(*song); std::string mmaFile = std::string("// Generated by VocalEasel ") + (const char *)[[bndl objectForInfoDictionaryKey:@"CFBundleVersion"] UTF8String] - + "\n\n" - + "Solo Voice AltoSax\n" - + "Solo Volume fff\n"; + + "\n\n"; sprintf(buf, "Tempo %d\n", [songTempo intValue]); mmaFile += buf; - if (playElements & kVLPlayCountIn) - switch ([[self songTime] intValue]) { - case 0x404: - mmaFile += "Groove Metronome2-4\nz\nz\n"; - break; - case 0x304: - case 0x608: - mmaFile += "Groove Metronome3\nz\nz\n"; - break; - default: - // Can't handle these yet - break; - } - if (!(playElements & kVLPlayAccompaniment)) - mmaFile += "AllTracks Off\nSolo On\n"; - if (!(playElements & kVLPlayMelody)) - mmaFile += "Solo Off\n"; + if (playElements & kVLPlayGroovePreview) { + // + // Override all other flags + // + mmaFile += "Groove "; + mmaFile += [songGroove UTF8String]; + mmaFile += "\nSolo Off\n"; + } else { + if (playElements & kVLPlayCountIn) + switch ([[self songTime] intValue]) { + case 0x404: + mmaFile += "Groove Metronome2-4\nz\nz\n"; + break; + case 0x304: + case 0x608: + mmaFile += "Groove Metronome3\nz\nz\n"; + break; + default: + // Can't handle these yet + break; + } + if (!(playElements & kVLPlayAccompaniment)) + mmaFile += "AllTracks Off\nSolo On\n"; + if (playElements & kVLPlayMelody) + mmaFile += "Solo Voice AltoSax\nSolo Volume fff\n"; + else + mmaFile += "Solo Off\n"; + } mmaFile += '\n'+writer.Measures(); return [[NSString stringWithUTF8String:mmaFile.c_str()] diff --git a/Sources/VLMMAWriter.cpp b/Sources/VLMMAWriter.cpp index dd337fd..16291f8 100644 --- a/Sources/VLMMAWriter.cpp +++ b/Sources/VLMMAWriter.cpp @@ -24,6 +24,10 @@ void VLMMAWriter::Visit(VLSong & song) void VLMMAWriter::VisitMeasure(size_t m, VLProperties & p, VLMeasure & meas) { + if (fPreview) + if (meas.fPropIdx < fBeginSection || meas.fPropIdx >= fEndSection) + return; // Skip this measure + char buf[64]; if (p.fKey != fKey) { @@ -31,7 +35,7 @@ void VLMMAWriter::VisitMeasure(size_t m, VLProperties & p, VLMeasure & meas) sprintf(buf, "KeySig %d%c\n", labs(fKey), fKey>=0 ? '#' : '&'); fMeasures += buf; } - if (p.fGroove != fGroove) { + if (!fPreview && p.fGroove != fGroove) { fGroove = p.fGroove; fMeasures += "Groove " + fGroove + '\n'; } diff --git a/Sources/VLMMAWriter.h b/Sources/VLMMAWriter.h index 6270f91..5079719 100644 --- a/Sources/VLMMAWriter.h +++ b/Sources/VLMMAWriter.h @@ -12,7 +12,9 @@ class VLMMAWriter: public VLSongVisitor { public: - VLMMAWriter() {} + VLMMAWriter(bool preview, int beginSection, int endSection) + : fPreview(preview), fBeginSection(beginSection), fEndSection(endSection) + {} virtual void Visit(VLSong & song); virtual void VisitMeasure(size_t m, VLProperties & p, VLMeasure & meas); @@ -24,6 +26,7 @@ private: std::string fMeasures; VLSong * fSong; + bool fPreview; bool fUseSharps; bool fTied; bool fInitial; @@ -32,6 +35,8 @@ private: std::string fAccum; int fKey; std::string fGroove; + int fBeginSection; + int fEndSection; }; // Local Variables: diff --git a/Sources/VLSheetView.h b/Sources/VLSheetView.h index 4cce221..4c66c5a 100644 --- a/Sources/VLSheetView.h +++ b/Sources/VLSheetView.h @@ -128,7 +128,7 @@ enum VLRecalc { - (VLRegion) findRegionForEvent:(NSEvent *) event; - (void) setGroove:(NSString *)groove; -- (void) setGrooveMenu:(NSString *)groove; +- (void) playWithGroove:(NSString *)groove; @end diff --git a/Sources/VLSheetView.mm b/Sources/VLSheetView.mm index 2284944..e4fec1e 100644 --- a/Sources/VLSheetView.mm +++ b/Sources/VLSheetView.mm @@ -1054,6 +1054,11 @@ static int8_t sSharpAcc[] = { [[self document] setGroove:groove inSections:[self sectionsInSelection]]; } +- (void)playWithGroove:(NSString *)groove +{ + [[self document] playWithGroove:groove inSections:[self sectionsInSelection]]; +} + - (IBAction)editDisplayOptions:(id)sender { VLSheetWindow * wc = [[self window] windowController];