2007-02-24 20:36:27 +00:00
|
|
|
//
|
|
|
|
// TagLibMetadataReader.m
|
|
|
|
// TagLib
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 2/24/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TagLibMetadataReader.h"
|
2022-02-07 05:49:27 +00:00
|
|
|
#import <taglib/audioproperties.h>
|
2015-01-07 12:09:29 +00:00
|
|
|
#import <taglib/fileref.h>
|
2022-06-17 07:30:08 +00:00
|
|
|
#import <taglib/flac/flacfile.h>
|
|
|
|
#import <taglib/mpc/mpcproperties.h>
|
2022-02-07 05:49:27 +00:00
|
|
|
#import <taglib/mpeg/id3v2/frames/attachedpictureframe.h>
|
|
|
|
#import <taglib/mpeg/id3v2/id3v2tag.h>
|
2015-01-07 12:09:29 +00:00
|
|
|
#import <taglib/mpeg/mpegfile.h>
|
2022-02-07 05:49:27 +00:00
|
|
|
#import <taglib/tag.h>
|
2022-06-17 07:30:08 +00:00
|
|
|
#import <taglib/ogg/vorbis/vorbisfile.h>
|
|
|
|
#import <taglib/ogg/xiphcomment.h>
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2022-06-20 10:35:29 +00:00
|
|
|
#import "SandboxBroker.h"
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation TagLibMetadataReader
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSDictionary *)metadataForURL:(NSURL *)url {
|
|
|
|
if(![url isFileURL]) {
|
2009-03-08 20:04:09 +00:00
|
|
|
return [NSDictionary dictionary];
|
|
|
|
}
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2022-06-20 10:35:29 +00:00
|
|
|
id sandboxBrokerClass = NSClassFromString(@"SandboxBroker");
|
|
|
|
id sandboxBroker = [sandboxBrokerClass sharedSandboxBroker];
|
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
const void *sbHandle = [sandboxBroker beginFolderAccess:url];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2009-03-08 20:04:09 +00:00
|
|
|
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
// if ( !*TagLib::ascii_encoding ) {
|
|
|
|
// NSStringEncoding enc = [NSString defaultCStringEncoding];
|
|
|
|
// CFStringEncoding cfenc = CFStringConvertNSStringEncodingToEncoding(enc);
|
|
|
|
// NSString *ref = (NSString *)CFStringConvertEncodingToIANACharSetName(cfenc);
|
|
|
|
// UInt32 cp = CFStringConvertEncodingToWindowsCodepage(cfenc);
|
|
|
|
//
|
|
|
|
// // Most tags are using windows codepage, so remap OS X codepage to Windows one.
|
|
|
|
//
|
|
|
|
// static struct {
|
|
|
|
// UInt32 from, to;
|
|
|
|
// } codepage_remaps[] = {
|
|
|
|
// { 10001, 932 }, // Japanese Shift-JIS
|
|
|
|
// { 10002, 950 }, // Traditional Chinese
|
|
|
|
// { 10003, 949 }, // Korean
|
|
|
|
// { 10004, 1256 }, // Arabic
|
|
|
|
// { 10005, 1255 }, // Hebrew
|
|
|
|
// { 10006, 1253 }, // Greek
|
|
|
|
// { 10007, 1251 }, // Cyrillic
|
|
|
|
// { 10008, 936 }, // Simplified Chinese
|
|
|
|
// { 10029, 1250 }, // Central European (latin2)
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// int i;
|
|
|
|
// int max = sizeof(codepage_remaps)/sizeof(codepage_remaps[0]);
|
|
|
|
// for ( i=0; i<max; i++ )
|
|
|
|
// if ( codepage_remaps[i].from == cp )
|
|
|
|
// break;
|
|
|
|
// if ( i < max )
|
|
|
|
// sprintf(TagLib::ascii_encoding, "windows-%d", codepage_remaps[i].to);
|
|
|
|
// else
|
|
|
|
// strcpy(TagLib::ascii_encoding, [ref UTF8String]);
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2008-03-03 01:55:25 +00:00
|
|
|
TagLib::FileRef f((const char *)[[url path] UTF8String], false);
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!f.isNull()) {
|
2008-03-03 01:55:25 +00:00
|
|
|
const TagLib::Tag *tag = f.tag();
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(tag) {
|
2021-10-02 02:18:42 +00:00
|
|
|
TagLib::String artist, albumartist, title, album, genre, comment;
|
2022-01-22 05:49:17 +00:00
|
|
|
int year, track, disc;
|
2022-02-07 05:49:27 +00:00
|
|
|
float rgAlbumGain, rgAlbumPeak, rgTrackGain, rgTrackPeak;
|
|
|
|
TagLib::String cuesheet;
|
|
|
|
TagLib::String soundcheck;
|
|
|
|
|
2009-03-08 20:04:09 +00:00
|
|
|
artist = tag->artist();
|
2022-02-07 05:49:27 +00:00
|
|
|
albumartist = tag->albumartist();
|
|
|
|
title = tag->title();
|
|
|
|
;
|
2009-03-08 20:04:09 +00:00
|
|
|
album = tag->album();
|
|
|
|
genre = tag->genre();
|
|
|
|
comment = tag->comment();
|
2022-02-07 05:49:27 +00:00
|
|
|
cuesheet = tag->cuesheet();
|
|
|
|
|
2009-03-08 20:04:09 +00:00
|
|
|
year = tag->year();
|
2022-06-17 13:39:02 +00:00
|
|
|
[dict setObject:@(year) forKey:@"year"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
2009-03-08 20:04:09 +00:00
|
|
|
track = tag->track();
|
2022-06-17 13:39:02 +00:00
|
|
|
[dict setObject:@(track) forKey:@"track"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
disc = tag->disc();
|
2022-06-17 13:39:02 +00:00
|
|
|
[dict setObject:@(disc) forKey:@"disc"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
rgAlbumGain = tag->rgAlbumGain();
|
|
|
|
rgAlbumPeak = tag->rgAlbumPeak();
|
|
|
|
rgTrackGain = tag->rgTrackGain();
|
|
|
|
rgTrackPeak = tag->rgTrackPeak();
|
2022-07-08 13:26:28 +00:00
|
|
|
[dict setObject:@(rgAlbumGain) forKey:@"replaygain_album_gain"];
|
|
|
|
[dict setObject:@(rgAlbumPeak) forKey:@"replaygain_album_peak"];
|
|
|
|
[dict setObject:@(rgTrackGain) forKey:@"replaygain_track_gain"];
|
|
|
|
[dict setObject:@(rgTrackPeak) forKey:@"replaygain_track_peak"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
soundcheck = tag->soundcheck();
|
|
|
|
if(!soundcheck.isEmpty()) {
|
|
|
|
TagLib::StringList tag = soundcheck.split(" ");
|
|
|
|
TagLib::StringList wantedTag;
|
|
|
|
for(int i = 0, count = tag.size(); i < count; i++) {
|
|
|
|
if(tag[i].length() == 8)
|
|
|
|
wantedTag.append(tag[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wantedTag.size() >= 10) {
|
|
|
|
float volume1 = -log10((double)((uint32_t)wantedTag[0].toInt(16)) / 1000) * 10;
|
|
|
|
float volume2 = -log10((double)((uint32_t)wantedTag[1].toInt(16)) / 1000) * 10;
|
|
|
|
float volumeToUse = MIN(volume1, volume2);
|
|
|
|
float volumeScale = pow(10, volumeToUse / 20);
|
2022-06-17 13:39:02 +00:00
|
|
|
[dict setObject:@(volumeScale) forKey:@"volume"];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!artist.isEmpty())
|
2009-03-08 20:04:09 +00:00
|
|
|
[dict setObject:[NSString stringWithUTF8String:artist.toCString(true)] forKey:@"artist"];
|
2008-03-03 01:55:25 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!albumartist.isEmpty())
|
|
|
|
[dict setObject:[NSString stringWithUTF8String:albumartist.toCString(true)] forKey:@"albumartist"];
|
|
|
|
|
|
|
|
if(!album.isEmpty())
|
2009-03-08 20:04:09 +00:00
|
|
|
[dict setObject:[NSString stringWithUTF8String:album.toCString(true)] forKey:@"album"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(!title.isEmpty())
|
2009-03-08 20:04:09 +00:00
|
|
|
[dict setObject:[NSString stringWithUTF8String:title.toCString(true)] forKey:@"title"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(!genre.isEmpty())
|
2009-03-08 20:04:09 +00:00
|
|
|
[dict setObject:[NSString stringWithUTF8String:genre.toCString(true)] forKey:@"genre"];
|
2022-02-07 05:49:27 +00:00
|
|
|
|
|
|
|
if(!cuesheet.isEmpty())
|
|
|
|
[dict setObject:[NSString stringWithUTF8String:cuesheet.toCString(true)] forKey:@"cuesheet"];
|
2022-07-08 13:26:28 +00:00
|
|
|
|
|
|
|
if(!comment.isEmpty())
|
|
|
|
[dict setObject:[NSString stringWithUTF8String:comment.toCString(true)] forKey:@"comment"];
|
2009-03-08 20:04:09 +00:00
|
|
|
}
|
2021-02-07 01:48:49 +00:00
|
|
|
|
2013-10-09 20:53:13 +00:00
|
|
|
// Try to load the image.
|
2022-02-07 05:49:27 +00:00
|
|
|
NSData *image = nil;
|
|
|
|
|
2013-10-12 20:51:36 +00:00
|
|
|
// Try to load the image.
|
2013-10-09 20:53:13 +00:00
|
|
|
// WARNING: HACK
|
|
|
|
TagLib::MPEG::File *mf = dynamic_cast<TagLib::MPEG::File *>(f.file());
|
2022-02-07 05:49:27 +00:00
|
|
|
if(mf) {
|
2013-10-09 20:53:13 +00:00
|
|
|
TagLib::ID3v2::Tag *tag = mf->ID3v2Tag();
|
2022-02-07 05:49:27 +00:00
|
|
|
if(tag) {
|
2013-10-09 20:53:13 +00:00
|
|
|
TagLib::ID3v2::FrameList pictures = mf->ID3v2Tag()->frameListMap()["APIC"];
|
2022-02-07 05:49:27 +00:00
|
|
|
if(!pictures.isEmpty()) {
|
2013-10-09 20:53:13 +00:00
|
|
|
TagLib::ID3v2::AttachedPictureFrame *pic = static_cast<TagLib::ID3v2::AttachedPictureFrame *>(pictures.front());
|
2013-10-12 20:51:36 +00:00
|
|
|
|
|
|
|
image = [NSData dataWithBytes:pic->picture().data() length:pic->picture().size()];
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-08 20:04:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(nil != image) {
|
2009-03-08 20:04:09 +00:00
|
|
|
[dict setObject:image forKey:@"albumArt"];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-08 20:04:09 +00:00
|
|
|
|
2022-06-21 05:10:43 +00:00
|
|
|
[sandboxBroker endFolderAccess:sbHandle];
|
2022-06-20 10:35:29 +00:00
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
return dict;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)fileTypes {
|
|
|
|
// May be a way to get a list of supported formats
|
2022-07-05 07:55:21 +00:00
|
|
|
return @[@"asf", @"wma", @"mpc", @"mp3", @"apl", @"wav", @"aif", @"aiff", @"wv", @"wvp"];
|
2007-10-14 18:56:23 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (NSArray *)mimeTypes {
|
2022-07-05 07:55:21 +00:00
|
|
|
return @[@"audio/x-ms-wma", @"audio/x-musepack", @"audio/mpeg", @"audio/x-mp3", @"audio/x-apl", @"audio/wav", @"audio/aiff", @"audio/x-wavpack"];
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
+ (float)priority {
|
|
|
|
return 1.0f;
|
2015-04-13 07:39:24 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@end
|