cog/Plugins/CueSheet/CueSheetMetadataReader.m

97 lines
3.0 KiB
Matlab
Raw Normal View History

2007-10-13 01:36:42 +00:00
//
// 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"
2007-10-13 01:36:42 +00:00
@implementation CueSheetMetadataReader
+ (NSArray *)fileTypes {
2007-10-13 01:36:42 +00:00
return [CueSheetDecoder fileTypes];
}
+ (NSArray *)mimeTypes {
2007-10-14 18:56:23 +00:00
return [CueSheetDecoder mimeTypes];
}
+ (float)priority {
return 16.0f;
}
+ (NSDictionary *)metadataForURL:(NSURL *)url {
if(![url isFileURL]) {
2007-10-13 01:36:42 +00:00
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];
NSString *sheet = [fileMetadata objectForKey:@"cuesheet"];
if(!sheet || ![sheet length]) sheet = [alsoMetadata objectForKey:@"cuesheet"];
if([sheet length]) {
cuesheet = [CueSheet cueSheetWithString:sheet withFilename:[url path]];
embedded = YES;
}
} else
cuesheet = [CueSheet cueSheetWithFile:[url path]];
if(!cuesheet) {
return fileMetadata;
}
2007-10-13 01:36:42 +00:00
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];
2007-10-13 01:36:42 +00:00
}
}
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];
}
2007-10-13 01:36:42 +00:00
@end