2007-10-09 02:25:40 +00:00
|
|
|
//
|
|
|
|
// CueSheet.m
|
|
|
|
// CueSheet
|
|
|
|
//
|
|
|
|
// Created by Zaphod Beeblebrox on 10/8/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "CueSheet.h"
|
|
|
|
#import "CueSheetTrack.h"
|
|
|
|
|
2007-10-20 15:54:42 +00:00
|
|
|
#import "Plugin.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-10-09 02:25:40 +00:00
|
|
|
@implementation CueSheet
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (id)cueSheetWithFile:(NSString *)filename {
|
2016-05-05 20:05:39 +00:00
|
|
|
return [[CueSheet alloc] initWithFile:filename];
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (id)cueSheetWithString:(NSString *)cuesheet withFilename:(NSString *)filename {
|
|
|
|
return [[CueSheet alloc] initWithString:cuesheet withFilename:filename];
|
2022-01-15 00:46:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSURL *)urlForPath:(NSString *)path relativeTo:(NSString *)baseFilename {
|
2007-10-15 22:19:14 +00:00
|
|
|
NSRange protocolRange = [path rangeOfString:@"://"];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(protocolRange.location != NSNotFound) {
|
2007-10-10 01:59:25 +00:00
|
|
|
return [NSURL URLWithString:path];
|
|
|
|
}
|
|
|
|
|
2007-10-15 22:19:14 +00:00
|
|
|
NSMutableString *unixPath = [path mutableCopy];
|
2007-10-10 01:59:25 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// Get the fragment
|
2007-10-15 22:57:30 +00:00
|
|
|
NSString *fragment = @"";
|
2007-10-19 03:06:52 +00:00
|
|
|
NSScanner *scanner = [NSScanner scannerWithString:unixPath];
|
|
|
|
NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"#1234567890"];
|
2022-02-07 05:49:27 +00:00
|
|
|
while(![scanner isAtEnd]) {
|
2007-10-19 03:06:52 +00:00
|
|
|
NSString *possibleFragment;
|
|
|
|
[scanner scanUpToString:@"#" intoString:nil];
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if([scanner scanCharactersFromSet:characterSet intoString:&possibleFragment] && [scanner isAtEnd]) {
|
2007-10-19 03:06:52 +00:00
|
|
|
fragment = possibleFragment;
|
|
|
|
[unixPath deleteCharactersInRange:NSMakeRange([scanner scanLocation] - [possibleFragment length], [possibleFragment length])];
|
|
|
|
break;
|
|
|
|
}
|
2007-10-15 22:57:30 +00:00
|
|
|
}
|
2007-10-15 22:19:14 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![unixPath hasPrefix:@"/"]) {
|
|
|
|
// Only relative paths would have windows backslashes.
|
2007-10-15 22:57:30 +00:00
|
|
|
[unixPath replaceOccurrencesOfString:@"\\" withString:@"/" options:0 range:NSMakeRange(0, [unixPath length])];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-15 22:57:30 +00:00
|
|
|
NSString *basePath = [[[baseFilename stringByStandardizingPath] stringByDeletingLastPathComponent] stringByAppendingString:@"/"];
|
2007-10-15 22:19:14 +00:00
|
|
|
|
2007-10-15 22:57:30 +00:00
|
|
|
[unixPath insertString:basePath atIndex:0];
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
// Append the fragment
|
|
|
|
NSURL *url = [NSURL URLWithString:[[[NSURL fileURLWithPath:unixPath] absoluteString] stringByAppendingString:fragment]];
|
2008-08-26 17:45:16 +00:00
|
|
|
return url;
|
2007-10-15 22:19:14 +00:00
|
|
|
}
|
2007-10-10 01:59:25 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)parseFile:(NSString *)filename {
|
|
|
|
NSStringEncoding encoding;
|
|
|
|
NSError *error = nil;
|
|
|
|
NSString *contents = [NSString stringWithContentsOfFile:filename usedEncoding:&encoding error:&error];
|
|
|
|
if(error) {
|
|
|
|
error = nil;
|
|
|
|
contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&error];
|
|
|
|
}
|
|
|
|
if(error) {
|
|
|
|
error = nil;
|
|
|
|
contents = [NSString stringWithContentsOfFile:filename encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000) error:&error];
|
|
|
|
}
|
|
|
|
if(error) {
|
|
|
|
error = nil;
|
|
|
|
contents = [NSString stringWithContentsOfFile:filename encoding:NSWindowsCP1251StringEncoding error:&error];
|
|
|
|
}
|
|
|
|
if(error) {
|
|
|
|
error = nil;
|
|
|
|
contents = [NSString stringWithContentsOfFile:filename encoding:NSISOLatin1StringEncoding error:&error];
|
|
|
|
}
|
|
|
|
if(error || !contents) {
|
|
|
|
ALog(@"Could not open file...%@ %@ %@", filename, contents, error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self parseString:contents withFilename:filename];
|
2022-01-15 00:46:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)parseString:(NSString *)contents withFilename:(NSString *)filename {
|
2007-10-10 01:59:25 +00:00
|
|
|
NSMutableArray *entries = [[NSMutableArray alloc] init];
|
|
|
|
|
|
|
|
NSString *track = nil;
|
|
|
|
NSString *path = nil;
|
2007-10-13 01:07:19 +00:00
|
|
|
NSString *artist = nil;
|
|
|
|
NSString *album = nil;
|
|
|
|
NSString *title = nil;
|
|
|
|
NSString *genre = nil;
|
|
|
|
NSString *year = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
float albumGain = 0.0f;
|
|
|
|
float albumPeak = 0.0f;
|
|
|
|
float trackGain = 0.0f;
|
|
|
|
float trackPeak = 0.0f;
|
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
BOOL trackAdded = NO;
|
|
|
|
|
|
|
|
NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
|
|
|
|
|
|
|
|
NSScanner *scanner = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
for(NSString *line in [contents componentsSeparatedByString:@"\n"]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
scanner = [[NSScanner alloc] initWithString:line];
|
|
|
|
|
|
|
|
NSString *command;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&command]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
// FILE "filename.shn" WAVE
|
|
|
|
if([command isEqualToString:@"FILE"]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
trackAdded = NO;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanString:@"\"" intoString:nil]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// Read in the path
|
|
|
|
if(![scanner scanUpToString:@"\"" intoString:&path]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
// TRACK 01 AUDIO
|
|
|
|
else if([command isEqualToString:@"TRACK"]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
trackAdded = NO;
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&track]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
NSString *type = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&type] || ![type isEqualToString:@"AUDIO"]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
// INDEX 01 00:00:10
|
|
|
|
// Note that time is written in Minutes:Seconds:Frames, where frames are 1/75 of a second
|
|
|
|
else if([command isEqualToString:@"INDEX"]) {
|
|
|
|
if(trackAdded) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(!path) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *index = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&index] || [index intValue] != 1) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
[scanner scanCharactersFromSet:whitespace intoString:nil];
|
|
|
|
|
|
|
|
NSString *time = nil;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&time]) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
NSArray *msf = [time componentsSeparatedByString:@":"];
|
2022-02-07 05:49:27 +00:00
|
|
|
size_t count = [msf count];
|
|
|
|
if(count != 1 && count != 3) {
|
2007-10-10 01:59:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
double seconds;
|
|
|
|
BOOL timeInSamples = NO;
|
|
|
|
|
|
|
|
if(count == 1) {
|
|
|
|
seconds = [[msf objectAtIndex:0] floatValue];
|
|
|
|
timeInSamples = YES;
|
|
|
|
} else
|
|
|
|
seconds = (60 * [[msf objectAtIndex:0] intValue]) + [[msf objectAtIndex:1] intValue] + ([[msf objectAtIndex:2] floatValue] / 75);
|
|
|
|
|
|
|
|
if(track == nil) {
|
2007-10-10 01:59:25 +00:00
|
|
|
track = @"01";
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// Need to add basePath, and convert to URL
|
2007-10-10 01:59:25 +00:00
|
|
|
[entries addObject:
|
2022-02-07 05:49:27 +00:00
|
|
|
[CueSheetTrack trackWithURL:[self urlForPath:path relativeTo:filename]
|
|
|
|
track:track
|
|
|
|
time:seconds
|
|
|
|
timeInSamples:timeInSamples
|
|
|
|
artist:artist
|
|
|
|
album:album
|
|
|
|
title:title
|
|
|
|
genre:genre
|
|
|
|
year:year
|
|
|
|
albumGain:albumGain
|
|
|
|
albumPeak:albumPeak
|
|
|
|
trackGain:trackGain
|
|
|
|
trackPeak:trackPeak]];
|
2007-10-10 01:59:25 +00:00
|
|
|
trackAdded = YES;
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if([command isEqualToString:@"PERFORMER"]) {
|
|
|
|
if(![scanner scanString:@"\"" intoString:nil]) {
|
2007-10-13 01:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// Read in the path
|
|
|
|
if(![scanner scanUpToString:@"\"" intoString:&artist]) {
|
2007-10-13 01:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if([command isEqualToString:@"TITLE"]) {
|
|
|
|
NSString *titleDest;
|
|
|
|
|
|
|
|
if(![scanner scanString:@"\"" intoString:nil]) {
|
2007-10-13 01:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// Read in the path
|
|
|
|
if(![scanner scanUpToString:@"\"" intoString:&titleDest]) {
|
2007-10-13 01:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(!path) // Have not come across a file yet.
|
|
|
|
album = titleDest;
|
|
|
|
else
|
|
|
|
title = titleDest;
|
|
|
|
} else if([command isEqualToString:@"REM"]) // Additional metadata sometimes stored in comments
|
2007-10-13 01:07:19 +00:00
|
|
|
{
|
|
|
|
NSString *type;
|
2022-02-07 05:49:27 +00:00
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&type]) {
|
2007-10-13 16:31:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if([type isEqualToString:@"GENRE"]) {
|
|
|
|
// DLog(@"GENRE!");
|
|
|
|
if([scanner scanString:@"\"" intoString:nil]) {
|
|
|
|
// DLog(@"QUOTED");
|
|
|
|
if(![scanner scanUpToString:@"\"" intoString:&genre]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"FAILED TO SCAN");
|
2007-10-13 16:21:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
} else {
|
|
|
|
// DLog(@"UNQUOTED");
|
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&genre]) {
|
2007-10-13 16:21:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if([type isEqualToString:@"DATE"]) {
|
|
|
|
// DLog(@"DATE!");
|
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&year]) {
|
2007-10-13 16:21:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
} else if([type hasPrefix:@"REPLAYGAIN_"]) {
|
|
|
|
NSString *rgTag = nil;
|
|
|
|
if(![scanner scanUpToCharactersFromSet:whitespace intoString:&rgTag])
|
|
|
|
continue;
|
|
|
|
if([type hasPrefix:@"REPLAYGAIN_ALBUM_"]) {
|
|
|
|
if([type hasSuffix:@"GAIN"])
|
|
|
|
albumGain = [rgTag floatValue];
|
|
|
|
else if([type hasSuffix:@"PEAK"])
|
|
|
|
albumPeak = [rgTag floatValue];
|
|
|
|
} else if([type hasPrefix:@"REPLAYGAIN_TRACK_"]) {
|
|
|
|
if([type hasSuffix:@"GAIN"])
|
|
|
|
trackGain = [rgTag floatValue];
|
|
|
|
else if([type hasSuffix:@"PEAK"])
|
|
|
|
trackPeak = [rgTag floatValue];
|
|
|
|
}
|
2007-10-13 01:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
2007-10-10 01:59:25 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
tracks = [entries copy];
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)initWithFile:(NSString *)filename {
|
2007-10-09 02:25:40 +00:00
|
|
|
self = [super init];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(self) {
|
2007-10-10 01:59:25 +00:00
|
|
|
[self parseFile:filename];
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-09 02:25:40 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (id)initWithString:(NSString *)cuesheet withFilename:(NSString *)filename {
|
|
|
|
self = [super init];
|
|
|
|
if(self) {
|
|
|
|
[self parseString:cuesheet withFilename:filename];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2022-01-15 00:46:35 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSArray *)tracks {
|
2007-10-09 02:25:40 +00:00
|
|
|
return tracks;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (CueSheetTrack *)track:(NSString *)fragment {
|
|
|
|
for(CueSheetTrack *t in tracks) {
|
|
|
|
if([[t track] isEqualToString:fragment]) {
|
2007-10-09 02:25:40 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2007-10-09 02:25:40 +00:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|