120 lines
3.6 KiB
Objective-C
120 lines
3.6 KiB
Objective-C
//
|
|
// CueSheetMetadataReader.m
|
|
// CueSheet
|
|
//
|
|
// Created by Vincent Spader on 10/12/07.
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
//
|
|
|
|
#import "CueSheetMetadataReader.h"
|
|
#import "CueSheetDecoder.h"
|
|
|
|
#import "CueSheet.h"
|
|
|
|
#import "AudioMetadataReader.h"
|
|
#import "NSDictionary+Merge.h"
|
|
|
|
@implementation CueSheetMetadataReader
|
|
|
|
+ (NSArray *)fileTypes {
|
|
return [CueSheetDecoder fileTypes];
|
|
}
|
|
|
|
+ (NSArray *)mimeTypes {
|
|
return [CueSheetDecoder mimeTypes];
|
|
}
|
|
|
|
+ (float)priority {
|
|
return 16.0f;
|
|
}
|
|
|
|
+ (NSDictionary *)metadataForURL:(NSURL *)url {
|
|
if(![url isFileURL]) {
|
|
return nil;
|
|
}
|
|
|
|
BOOL embedded = NO;
|
|
CueSheet *cuesheet = nil;
|
|
NSDictionary *fileMetadata;
|
|
|
|
Class audioMetadataReader = NSClassFromString(@"AudioMetadataReader");
|
|
|
|
NSString *ext = [url pathExtension];
|
|
if([ext caseInsensitiveCompare:@"cue"] != NSOrderedSame) {
|
|
// Embedded cuesheet check
|
|
fileMetadata = [audioMetadataReader metadataForURL:url skipCue:YES];
|
|
|
|
NSDictionary *alsoMetadata = [NSClassFromString(@"AudioPropertiesReader") propertiesForURL:url skipCue:YES];
|
|
|
|
id sheet = [fileMetadata objectForKey:@"cuesheet"];
|
|
NSString *sheetString = nil;
|
|
if(sheet) {
|
|
if([sheet isKindOfClass:[NSArray class]]) {
|
|
NSArray *sheetContainer = sheet;
|
|
if([sheetContainer count]) {
|
|
sheetString = sheetContainer[0];
|
|
}
|
|
} else if([sheet isKindOfClass:[NSString class]]) {
|
|
sheetString = sheet;
|
|
}
|
|
}
|
|
if(!sheetString || ![sheetString length]) {
|
|
sheet = [alsoMetadata objectForKey:@"cuesheet"];
|
|
if(sheet) {
|
|
if([sheet isKindOfClass:[NSArray class]]) {
|
|
NSArray *sheetContainer = sheet;
|
|
if([sheetContainer count]) {
|
|
sheetString = sheetContainer[0];
|
|
}
|
|
} else if([sheet isKindOfClass:[NSString class]]) {
|
|
sheetString = sheet;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(sheetString && [sheetString length]) {
|
|
cuesheet = [CueSheet cueSheetWithString:sheetString withFilename:[url path]];
|
|
embedded = YES;
|
|
}
|
|
} else
|
|
cuesheet = [CueSheet cueSheetWithFile:[url path]];
|
|
|
|
if(!cuesheet) {
|
|
return fileMetadata;
|
|
}
|
|
|
|
NSArray *tracks = [cuesheet tracks];
|
|
for(CueSheetTrack *track in tracks) {
|
|
if([[url fragment] isEqualToString:[track track]]) {
|
|
// Class supplied by CogAudio, which is guaranteed to be present
|
|
if(!embedded)
|
|
fileMetadata = [audioMetadataReader metadataForURL:[track url] skipCue:YES];
|
|
|
|
NSDictionary *cuesheetMetadata = [CueSheetMetadataReader processDataForTrack:track];
|
|
|
|
return [cuesheetMetadata dictionaryByMergingWith:fileMetadata];
|
|
}
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
+ (NSDictionary *)processDataForTrack:(CueSheetTrack *)track {
|
|
NSMutableDictionary *cuesheetMetadata = [[NSMutableDictionary alloc] init];
|
|
|
|
if([track artist]) [cuesheetMetadata setValue:[track artist] forKey:@"artist"];
|
|
if([track album]) [cuesheetMetadata setValue:[track album] forKey:@"album"];
|
|
if([track title]) [cuesheetMetadata setValue:[track title] forKey:@"title"];
|
|
if([[track track] intValue]) [cuesheetMetadata setValue:@([[track track] intValue]) forKey:@"track"];
|
|
if([track genre]) [cuesheetMetadata setValue:[track genre] forKey:@"genre"];
|
|
if([[track year] intValue]) [cuesheetMetadata setValue:@([[track year] intValue]) forKey:@"year"];
|
|
if([track albumGain]) [cuesheetMetadata setValue:@([track albumGain]) forKey:@"replaygain_album_gain"];
|
|
if([track albumPeak]) [cuesheetMetadata setValue:@([track albumPeak]) forKey:@"replaygain_album_peak"];
|
|
if([track trackGain]) [cuesheetMetadata setValue:@([track trackGain]) forKey:@"replaygain_track_gain"];
|
|
if([track trackPeak]) [cuesheetMetadata setValue:@([track trackPeak]) forKey:@"replaygain_track_peak"];
|
|
|
|
return [NSDictionary dictionaryWithDictionary:cuesheetMetadata];
|
|
}
|
|
|
|
@end
|