VocalEasel/Sources/VLDocument.mm

574 lines
15 KiB
Plaintext
Raw Permalink Normal View History

2006-09-11 02:49:56 +00:00
//
2007-04-27 06:41:34 +00:00
// File: VLDocument.mm - VocalEasel document
2006-09-11 02:49:56 +00:00
//
2007-04-27 06:41:34 +00:00
// Author(s):
//
// (MN) Matthias Neeracher
//
2018-02-19 00:59:23 +00:00
// Copyright © 2005-2018 Matthias Neeracher
2006-09-11 02:49:56 +00:00
//
#import "VLDocument.h"
2006-10-22 07:16:29 +00:00
#import "VLXMLDocument.h"
#import "VLLilypondDocument.h"
2006-11-04 08:15:34 +00:00
#import "VLMMADocument.h"
2006-11-12 11:37:36 +00:00
#import "VLMIDIDocument.h"
2008-05-29 18:54:30 +00:00
#import "VLAIFFDocument.h"
#import "VLMP3Document.h"
2007-04-18 08:25:43 +00:00
#import "VLPDFDocument.h"
#import "VLPListDocument.h"
#import "VLPDFWindow.h"
#import "VLLogWindow.h"
#import "VLSheetWindow.h"
2006-11-13 04:26:09 +00:00
#import "VLSoundOut.h"
2008-05-29 18:54:30 +00:00
#import "VLMIDIWriter.h"
2006-09-11 02:49:56 +00:00
2007-04-18 08:25:43 +00:00
#import <Quartz/Quartz.h>
@interface PDFDocument (PDFKitSecretsIKnow)
- (NSPrintOperation *)getPrintOperationForPrintInfo:(NSPrintInfo *)printInfo autoRotate:(BOOL)doRotate;
@end
2006-12-04 07:04:24 +00:00
@interface VLSongWrapper : NSObject {
VLSong * wrappedSong;
}
+ (VLSongWrapper *)wrapperWithSong:(VLSong *)song;
- (VLSong *)song;
@end
@implementation VLSongWrapper
- (id)initWithSong:(VLSong *)song
{
if (self = [super init])
wrappedSong = new VLSong(*song);
return self;
}
- (void) dealloc
{
delete wrappedSong;
[super dealloc];
}
+ (VLSongWrapper *)wrapperWithSong:(VLSong *)song
{
return [[[VLSongWrapper alloc] initWithSong:song] autorelease];
}
- (VLSong *)song
{
return wrappedSong;
}
@end
@implementation VLDocument
2006-10-03 17:52:54 +00:00
2011-09-04 19:48:57 +00:00
@synthesize songTempo, playElements;
2011-08-28 21:05:19 +00:00
+ (BOOL)autosavesInPlace
{
return YES;
}
- (id)init
2006-10-03 17:52:54 +00:00
{
self = [super init];
if (self) {
song = new VLSong;
lilypondTemplate = @"default";
songTitle = @"";
songLyricist = @"";
songComposer = @"";
songArranger = @"";
2006-11-04 08:15:34 +00:00
songGroove = @"Swing";
playElements = kVLPlayAccompaniment|kVLPlayMelody|kVLPlayCountIn;
2011-09-04 19:48:57 +00:00
songTempo = 120.0f;
2008-07-06 11:07:41 +00:00
baseTempo = 120.0f;
2008-01-26 16:38:30 +00:00
chordSize = 6.0f;
lyricSize = 0.0f;
staffSize = 20.0f;
2011-07-24 03:32:43 +00:00
topPadding = 2.0f;
titlePadding = 4.0f;
staffPadding = 3.0f;
chordPadding = 1.5f;
lyricPadding = 1.0f;
sheetWin = nil;
2011-09-03 20:34:53 +00:00
tmpURL = nil;
vcsWrapper = nil;
repeatVolta = 2;
2007-01-21 11:34:40 +00:00
brandNew = true;
2011-09-11 21:27:53 +00:00
musicSequence = nil;
2008-05-29 18:54:30 +00:00
playRate = 1.0;
validTmpFiles = [[NSMutableDictionary alloc] initWithCapacity:10];
2006-12-04 07:04:24 +00:00
[self setHasUndoManager:YES];
undo =
[[VLKeyValueUndo alloc] initWithOwner:self
keysAndNames: [NSDictionary dictionaryWithObjectsAndKeys:
@"", @"songTitle",
@"", @"songLyricist",
@"", @"songComposer",
@"", @"songArranger",
@"", @"songGroove",
@"", @"songTempo",
nil]];
staffMetrics =
[[VLKeyValueUndo alloc] initWithOwner:self
keysAndNames:[NSDictionary dictionaryWithObjectsAndKeys:
@"", @"chordSize",
@"", @"lyricSize",
@"", @"staffSize",
@"", @"topPadding",
@"", @"titlePadding",
@"", @"staffPadding",
@"", @"chordPadding",
@"", @"lyricPadding",
nil]
update:^(NSString *keyPath) {
[validTmpFiles removeObjectForKey:@"ly"];
[validTmpFiles removeObjectForKey:@"pdf"];
}];
}
return self;
2006-10-03 17:52:54 +00:00
}
- (void)updateChangeCount:(NSDocumentChangeType)changeType
{
2011-09-11 21:27:53 +00:00
musicSequence = nil;
[validTmpFiles removeAllObjects];
[super updateChangeCount:changeType];
}
2007-04-16 05:35:52 +00:00
- (void) close
{
2017-11-24 04:52:47 +00:00
VLSoundOut::Instance()->Stop(false);
[super close];
2007-04-16 05:35:52 +00:00
}
- (void) dealloc
2006-10-03 17:52:54 +00:00
{
2011-09-04 19:48:57 +00:00
VLSoundOut::Instance()->Stop(false);
delete song;
2006-10-03 17:52:54 +00:00
[lilypondTemplate release];
[songTitle release];
[songLyricist release];
[songComposer release];
[songArranger release];
[vcsWrapper release];
[staffMetrics release];
2006-12-04 07:04:24 +00:00
[undo release];
2007-04-16 05:35:52 +00:00
2011-09-03 20:34:53 +00:00
if (tmpURL) {
[[NSFileManager defaultManager] removeItemAtURL:tmpURL error:nil];
[tmpURL release];
2006-11-10 08:09:18 +00:00
}
[super dealloc];
2006-10-03 17:52:54 +00:00
}
- (void)makeWindowControllers
2006-09-11 02:49:56 +00:00
{
sheetWin = [[VLSheetWindow alloc] initWithWindowNibName: @"VLDocument"];
[self addWindowController: sheetWin];
[sheetWin setShouldCloseDocument:YES];
[sheetWin release];
2006-09-11 02:49:56 +00:00
}
- (VLSong *) song
{
return song;
}
2008-03-30 19:13:17 +00:00
- (void) setSongTitle:(NSString *)newTitle
{
if (newTitle != songTitle) {
[songTitle release];
songTitle = [newTitle retain];
}
[[self windowControllers] makeObjectsPerformSelector:
@selector(synchronizeWindowTitleWithDocumentName)];
}
2006-09-11 02:49:56 +00:00
- (NSNumber *) songKey
{
const VLProperties & prop = song->fProperties.front();
return [NSNumber numberWithInt: (prop.fKey << 8) | (prop.fMode & 0xFF)];
}
- (void) setKey:(int)key transpose:(BOOL)transpose inSections:(NSRange)sections
2006-09-11 02:49:56 +00:00
{
2006-12-04 07:04:24 +00:00
[self willChangeSong];
2007-05-06 05:07:39 +00:00
[self willChangeValueForKey:@"songKey"];
while (sections.length-- > 0)
song->ChangeKey(sections.location++, key>>8, key & 0xFF, transpose);
2007-05-06 05:07:39 +00:00
[self didChangeValueForKey:@"songKey"];
[self didChangeSong];
2006-09-11 02:49:56 +00:00
}
- (NSNumber *) songTime
{
const VLProperties & prop = song->fProperties.front();
return [NSNumber numberWithInt: (prop.fTime.fNum << 8) | prop.fTime.fDenom];
}
- (void) setTimeNum:(int)num denom:(int)denom inSections:(NSRange)sections
2006-09-11 02:49:56 +00:00
{
2006-12-04 07:04:24 +00:00
[self willChangeSong];
2007-05-06 05:07:39 +00:00
[self willChangeValueForKey:@"songTime"];
while (sections.length-- > 0)
song->ChangeTime(sections.location++, VLFraction(num, denom));
2007-05-06 05:07:39 +00:00
[self didChangeValueForKey:@"songTime"];
[self didChangeSong];
2006-09-11 02:49:56 +00:00
}
- (NSNumber *) songDivisions
{
const VLProperties & prop = song->fProperties.front();
return [NSNumber numberWithInt: prop.fDivisions];
}
- (void) setDivisions:(int)divisions inSections:(NSRange)sections
2006-09-11 02:49:56 +00:00
{
2006-12-04 07:04:24 +00:00
[self willChangeSong];
2007-05-06 05:07:39 +00:00
[self willChangeValueForKey:@"songDivisions"];
while (sections.length-- > 0)
song->ChangeDivisions(sections.location++, divisions);
2007-05-06 05:07:39 +00:00
[self didChangeValueForKey:@"songDivisions"];
[self didChangeSong];
2006-09-11 02:49:56 +00:00
}
2011-09-04 19:48:57 +00:00
- (void) setSongTempo:(float)tempo
2008-07-06 11:07:41 +00:00
{
2011-09-04 19:48:57 +00:00
if (tempo == songTempo)
2008-07-06 11:08:20 +00:00
return;
2011-09-04 19:48:57 +00:00
songTempo = tempo;
2008-07-06 11:07:41 +00:00
if (VLSoundOut::Instance()->Playing())
VLSoundOut::Instance()->SetPlayRate(playRate*tempo/baseTempo);
}
2008-01-23 01:20:09 +00:00
- (void) setGroove:(NSString *)groove inSections:(NSRange)sections
{
const char * grv = [groove UTF8String];
[self willChangeSong];
[self willChangeValueForKey:@"songGroove"];
while (sections.length-- > 0)
song->fProperties[sections.location++].fGroove = grv;
[self didChangeValueForKey:@"songGroove"];
[self didChangeSong];
}
2008-04-12 21:33:43 +00:00
- (void) changeOctave:(BOOL)up inSections:(NSRange)sections
{
[self willChangeSong];
while (sections.length-- > 0)
song->ChangeOctave(sections.location++, up);
[self didChangeSong];
}
- (void)setPlayElements:(int)elements
{
2011-09-04 23:38:01 +00:00
[self willChangeValueForKey:@"playElements"];
playElements = elements;
2011-09-04 23:38:01 +00:00
if (!(playElements & (kVLPlayMelody|kVLPlayAccompaniment)))
playElements |= kVLPlayAccompaniment;
if ((playElements & (kVLPlayMelody|kVLPlayGroovePreview)) != kVLPlayMelody)
VLSoundOut::Instance()->SetMelodyState(VLSoundOut::kMelodyMute);
else if (!(playElements & (kVLPlayAccompaniment|kVLPlayGroovePreview)))
VLSoundOut::Instance()->SetMelodyState(VLSoundOut::kMelodySolo);
else
VLSoundOut::Instance()->SetMelodyState(VLSoundOut::kMelodyRegular);
[self didChangeValueForKey:@"playElements"];
}
- (int) repeatVolta
{
return repeatVolta;
}
2007-01-21 11:34:40 +00:00
- (bool) brandNew
{
return brandNew && ![self isDocumentEdited];
}
- (void) setRepeatVolta:(int)volta
{
repeatVolta = volta;
}
2011-09-03 20:34:53 +00:00
- (NSURL *) tmpURL
2006-11-10 08:09:18 +00:00
{
2011-09-03 20:34:53 +00:00
if (!tmpURL) {
NSString * tmpPath = [NSString stringWithFormat:@"/var/tmp/VocalEasel.%08x", self];
tmpURL = [[NSURL alloc] initFileURLWithPath:tmpPath];
[[NSFileManager defaultManager] createDirectoryAtURL:tmpURL withIntermediateDirectories:NO attributes:nil error:nil];
2006-11-10 08:09:18 +00:00
}
2011-09-03 20:34:53 +00:00
return tmpURL;
2006-11-10 08:09:18 +00:00
}
2011-09-03 20:34:53 +00:00
- (NSURL *) workURL
2006-11-10 08:09:18 +00:00
{
2011-09-03 20:34:53 +00:00
if (NSURL * url = [self fileURL]) // Prefer our wrapper directory
return url;
2006-11-10 08:09:18 +00:00
else
2011-09-03 20:34:53 +00:00
return [self tmpURL];
2006-11-10 08:09:18 +00:00
}
- (NSString *) baseName
{
2011-09-03 20:34:53 +00:00
return [[[self workURL] lastPathComponent] stringByDeletingPathExtension];
2006-11-10 08:09:18 +00:00
}
- (NSURL *) fileURLWithExtension:(NSString*)extension
{
2011-09-03 20:34:53 +00:00
return [[[self workURL] URLByAppendingPathComponent:[self baseName]]
URLByAppendingPathExtension:extension];
2006-11-10 08:09:18 +00:00
}
- (BOOL)saveToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation error:(NSError **)outError
{
NSFileWrapper * preservedVCSWrapper = nil;
switch (saveOperation) {
case NSSaveToOperation:
case NSAutosaveOperation:
preservedVCSWrapper = vcsWrapper;
[preservedVCSWrapper retain];
// Fall through
case NSSaveAsOperation:
[vcsWrapper release];
vcsWrapper = nil;
// Fall through
case NSSaveOperation:
break;
}
BOOL res = [super saveToURL:absoluteURL ofType:typeName
forSaveOperation:saveOperation error:outError];
if (!vcsWrapper)
vcsWrapper = preservedVCSWrapper;
if ([typeName isEqual:VLNativeType])
[validTmpFiles removeAllObjects];
return res;
}
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError **)outError
2006-10-22 07:16:29 +00:00
{
if ([typeName isEqual:VLNativeType]) {
return [self XMLFileWrapperWithError:outError flat:NO];
} else if ([typeName isEqual:VLMusicXMLType]) {
return [self XMLFileWrapperWithError:outError flat:YES];
} else if ([typeName isEqual:VLLilypondType]) {
return [self lilypondFileWrapperWithError:outError];
} else if ([typeName isEqual:VLMMAType]) {
2006-11-04 08:15:34 +00:00
return [self mmaFileWrapperWithError:outError];
} else if ([typeName isEqual:VLMIDIType]) {
2006-11-12 11:37:36 +00:00
return [self midiFileWrapperWithError:outError];
} else if ([typeName isEqual:VLAIFFType]) {
2008-05-29 18:54:30 +00:00
return [self aiffFileWrapperWithError:outError];
} else if ([typeName isEqual:VLMP3Type]) {
2008-05-29 18:54:30 +00:00
return [self mp3FileWrapperWithError:outError];
} else if ([typeName isEqual:VLPDFType]) {
return [self pdfFileWrapperWithError:outError];
2006-10-23 07:42:53 +00:00
} else {
if (outError)
*outError = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSPersistentStoreInvalidTypeError
userInfo:nil];
2006-10-22 07:16:29 +00:00
return nil;
2006-10-23 07:42:53 +00:00
}
2006-10-22 07:16:29 +00:00
}
- (BOOL)readFromFileWrapper:(NSFileWrapper *)wrapper ofType:(NSString *)typeName error:(NSError **)outError
2006-10-22 07:16:29 +00:00
{
2007-01-21 11:34:40 +00:00
brandNew = false;
//
// On opening a document, close all unchanged empty documents
//
NSEnumerator * docs = [[[NSDocumentController sharedDocumentController]
documents] objectEnumerator];
while (VLDocument * doc = [docs nextObject])
if ([doc brandNew])
[[doc windowControllers]
makeObjectsPerformSelector:@selector(close)];
if ([typeName isEqual:VLNativeType] || [typeName isEqual:VLMusicXMLType]) {
return [self readFromXMLFileWrapper:wrapper error:outError];
} else if ([typeName isEqual:VLLilypondType] || [typeName isEqual:VLBIABType]) {
if ([self readFromFileWrapper:wrapper withFilter:typeName error:outError]) {
[self setFileURL:nil];
return YES;
} else
return NO;
2006-10-23 07:42:53 +00:00
} else {
if (outError)
*outError = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSPersistentStoreInvalidTypeError
userInfo:nil];
2006-10-22 07:16:29 +00:00
return NO;
2006-10-23 07:42:53 +00:00
}
2006-10-22 07:16:29 +00:00
}
- (void) changedFileWrapper
{
2011-09-03 20:34:53 +00:00
if (NSURL * url = [self workURL]) {
NSDate * modDate;
if ([url getResourceValue:&modDate forKey:NSURLAttributeModificationDateKey error:nil])
[self setFileModificationDate:modDate];
}
}
- (void) createTmpFileWithExtension:(NSString*)ext ofType:(NSString*)type
{
if (![validTmpFiles objectForKey:ext]) {
NSError * err;
if ([self writeToURL:[self fileURLWithExtension:ext]
ofType:type error:&err]
) {
[validTmpFiles setObject:type forKey:ext];
[self changedFileWrapper];
}
}
}
2006-11-12 11:37:36 +00:00
- (NSTask *) taskWithLaunchPath:(NSString *)launch arguments:(NSArray *)args;
{
NSTask * task = [[NSTask alloc] init];
2011-09-03 20:34:53 +00:00
NSString * path = [[self workURL] path];
2006-11-12 11:37:36 +00:00
NSPipe * pipe = [NSPipe pipe];
[task setCurrentDirectoryPath: path];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
[task setArguments: args];
[task setLaunchPath: launch];
[NSThread detachNewThreadSelector:@selector(logFromFileHandle:) toTarget:[sheetWin logWin]
2006-11-12 11:37:36 +00:00
withObject:[pipe fileHandleForReading]];
return task;
}
2011-09-04 19:48:57 +00:00
- (void) playSong
2006-11-13 04:26:09 +00:00
{
2011-09-11 21:27:53 +00:00
if (musicSequence) {
void (^finalizer)() = [sheetWin willPlaySequence:musicSequence];
2008-07-05 13:56:51 +00:00
VLSoundOut::Instance()->PlaySequence(NULL);
2011-09-11 21:27:53 +00:00
finalizer();
2008-07-05 13:56:51 +00:00
} else {
[self createTmpFileWithExtension:@"mid" ofType:VLMIDIType];
2008-07-05 13:56:51 +00:00
2011-09-11 21:27:53 +00:00
NewMusicSequence(&musicSequence);
2008-07-05 13:56:51 +00:00
2011-09-11 21:27:53 +00:00
MusicSequenceFileLoad(musicSequence, (CFURLRef)[self fileURLWithExtension:@"mid"],
2018-02-19 00:59:23 +00:00
kMusicSequenceFile_MIDIType, 0);
2008-07-05 13:56:51 +00:00
size_t countIn = 0;
if (playElements & kVLPlayCountIn)
switch ([[self songTime] intValue]) {
case 0x404:
case 0x304:
case 0x608:
countIn = 2;
}
2011-09-11 21:27:53 +00:00
VLMIDIWriter annotate(musicSequence, countIn);
2008-07-05 13:56:51 +00:00
annotate.Visit(*song);
2008-05-29 18:54:30 +00:00
2011-09-04 19:48:57 +00:00
baseTempo = songTempo;
2011-09-11 21:27:53 +00:00
void (^finalizer)() = [sheetWin willPlaySequence:musicSequence];
VLSoundOut::Instance()->SetPlayRate(playRate);
VLSoundOut::Instance()->PlaySequence(musicSequence);
finalizer();
2011-09-04 23:38:01 +00:00
}
[self setPlayElements:[self playElements]];
2006-11-13 04:26:09 +00:00
}
2012-08-18 22:30:50 +00:00
- (void) endSong
{
musicSequence = nil;
}
2008-01-24 01:29:18 +00:00
- (void) playWithGroove:(NSString *)groove inSections:(NSRange)sections
2007-04-23 05:46:37 +00:00
{
NSString * savedGroove = songGroove;
[validTmpFiles removeObjectForKey:@"mma"];
[validTmpFiles removeObjectForKey:@"mid"];
2011-09-11 21:27:53 +00:00
musicSequence = nil;
2008-01-24 01:29:18 +00:00
songGroove = groove;
previewRange = sections;
playElements |= kVLPlayGroovePreview;
2011-09-04 19:48:57 +00:00
[self playSong];
2008-01-24 01:29:18 +00:00
playElements &= ~kVLPlayGroovePreview;
songGroove = savedGroove;
2007-04-23 05:46:37 +00:00
[validTmpFiles removeObjectForKey:@"mma"];
[validTmpFiles removeObjectForKey:@"mid"];
2011-09-11 21:27:53 +00:00
musicSequence = nil;
2007-04-23 05:46:37 +00:00
}
2007-04-18 08:25:43 +00:00
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings
error:(NSError **)outError
{
[self createTmpFileWithExtension:@"pdf" ofType:VLPDFType];
2011-09-04 00:06:12 +00:00
PDFDocument * printDoc = [[PDFDocument alloc] initWithURL:[self fileURLWithExtension:@"pdf"]];
[printDoc autorelease];
2007-04-18 08:25:43 +00:00
NSPrintOperation *printOperation = [printDoc getPrintOperationForPrintInfo:[self printInfo] autoRotate:NO];
// Specify that the print operation can run in a separate thread. This will cause the print progress panel to appear as a sheet on the document window.
[printOperation setCanSpawnSeparateThread:YES];
// Set any print settings that might have been specified in a Print Document Apple event.
[[[printOperation printInfo] dictionary] addEntriesFromDictionary:printSettings];
return printOperation;
}
2006-12-04 07:04:24 +00:00
- (void) willChangeSong
{
[self willChangeValueForKey:@"song"];
[[self undoManager] registerUndoWithTarget:self
selector:@selector(restoreSong:)
object:[VLSongWrapper wrapperWithSong:song]];
}
- (void) didChangeSong
{
[self didChangeValueForKey:@"song"];
[self updateChangeCount:NSChangeDone];
}
- (void) restoreSong:(VLSongWrapper *)savedSong
{
[self willChangeSong];
[self willChangeValueForKey:@"songKey"];
[self willChangeValueForKey:@"songTime"];
[self willChangeValueForKey:@"songDivisions"];
[self willChangeValueForKey:@"songGroove"];
2006-12-04 07:04:24 +00:00
song->swap(*[savedSong song]);
[self didChangeValueForKey:@"songKey"];
[self didChangeValueForKey:@"songTime"];
[self didChangeValueForKey:@"songDivisions"];
[self didChangeValueForKey:@"songGroove"];
2006-12-04 07:04:24 +00:00
[self didChangeSong];
}
2008-03-24 22:47:29 +00:00
- (NSString *) displayName
{
if ([songTitle isEqual:@""])
return [super displayName];
else
return songTitle;
}
2006-09-11 02:49:56 +00:00
@end
2008-07-05 14:04:01 +00:00