-
Notifications
You must be signed in to change notification settings - Fork 2
/
MusicFileInfo.mm
172 lines (137 loc) · 4.28 KB
/
MusicFileInfo.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//
// MusicFileInfo.m
// iMusicTags
//
// Created by Kevin Chen on 10-7-23.
// Copyright 2010 Kevin Chen's workstation. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MusicFileInfo.h"
#import <TagLib/taglib.h>
#import <TagLib/tag.h>
#import <TagLib/fileref.h>
#import <TagLib/tstring.h>
#import <TagLib/mpegfile.h>
#import <TagLib/id3v1tag.h>
#import <TagLib/id3v2tag.h>
#import <TagLib/id3v2framefactory.h>
@implementation MusicFileInfo
- (id)initWithUrl:(NSURL *)aUrl
{
return [self initWithUrl:aUrl withEncoding:kCFStringEncodingUTF8];
}
- (id)initWithUrl:(NSURL *)aUrl withEncoding:(CFStringEncoding)anEncoding
{
self = [super init];
if (self) {
fileUrl = aUrl;
guessEncoding = anEncoding;
TagLib::FileRef fileRef([[aUrl path] UTF8String]);
if (fileRef.isNull()) {
return nil;
}
NSString *extension = [aUrl pathExtension];
if ([[extension lowercaseString] isEqualToString:@"mp3"]) {
[self readMPEGInfo:aUrl encoding:anEncoding];
}
}
return self;
}
- (void)writeTags
{
[self writeTags:guessEncoding];
}
- (void)writeTags:(CFStringEncoding)anEncoding
{
NSString *extension = [self.fileUrl pathExtension];
if ([[extension lowercaseString] isEqualToString:@"mp3"]) {
[self writeMPEGInfo:self.fileUrl encoding:anEncoding];
} else {
return;
}
}
- (NSUInteger)hash
{
return [self.fileUrl hash];
}
- (BOOL)isEqual:(id)object
{
if ([object isKindOfClass:[MusicFileInfo class]]) {
return NO;
}
if (nil == [(MusicFileInfo *)object fileUrl]) {
return NO;
}
return [self.fileUrl isEqual:[(MusicFileInfo *)object fileUrl]];
}
- (void)readMPEGInfo:(NSURL *)aUrl encoding:(CFStringEncoding)anEncoding
{
TagLib::MPEG::File file([[aUrl path] UTF8String]);
TagLib::Tag *tag;
if ([self hasID3v2Tag:aUrl]) {
tag = file.ID3v2Tag();
}else {
tag = file.ID3v1Tag();
}
if (kCFStringEncodingUTF8 == anEncoding) {
self.title = [NSString stringWithCString:tag->title().toCString(true)
encoding:NSUTF8StringEncoding];
self.artist = [NSString stringWithCString:tag->artist().toCString(true)
encoding:NSUTF8StringEncoding];
self.album = [NSString stringWithCString:tag->album().toCString(true)
encoding:NSUTF8StringEncoding];
self.genre = [NSString stringWithCString:tag->genre().toCString(true)
encoding:NSUTF8StringEncoding];
} else {
self.title = [NSString stringWithCString:tag->title().toCString()
encoding:CFStringConvertEncodingToNSStringEncoding(anEncoding)];
self.artist = [NSString stringWithCString:tag->artist().toCString()
encoding:CFStringConvertEncodingToNSStringEncoding(anEncoding)];
self.album = [NSString stringWithCString:tag->album().toCString()
encoding:CFStringConvertEncodingToNSStringEncoding(anEncoding)];
self.genre = [NSString stringWithCString:tag->genre().toCString()
encoding:CFStringConvertEncodingToNSStringEncoding(anEncoding)];
}
self.track = tag->track();
self.year = tag->year();
}
- (BOOL)hasID3v2Tag:(NSURL *)aUrl
{
NSError *error;
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingFromURL:aUrl error:&error];
[fileHandle seekToFileOffset:0L];
NSData *data = [fileHandle readDataOfLength:3];
NSString *id3v2id = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if ([id3v2id isEqual:@"ID3"]) {
[fileHandle closeFile];
return YES;
}
[fileHandle closeFile];
return NO;
}
- (void)writeMPEGInfo:(NSURL *)aUrl encoding:(CFStringEncoding)anEncoding
{
TagLib::MPEG::File file([[aUrl path] UTF8String]);
TagLib::Tag *tag;
if ([self hasID3v2Tag:aUrl]) {
tag = file.ID3v2Tag();
}else {
tag = file.ID3v2Tag(true);
}
tag->setTitle(TagLib::String([[self title] UTF8String], TagLib::String::UTF8));
tag->setArtist(TagLib::String([[self artist] UTF8String], TagLib::String::UTF8));
tag->setAlbum(TagLib::String([[self album] UTF8String], TagLib::String::UTF8));
tag->setGenre(TagLib::String([[self genre] UTF8String], TagLib::String::UTF8));
TagLib::ID3v2::FrameFactory *frameFactory = TagLib::ID3v2::FrameFactory::instance();
frameFactory->setDefaultTextEncoding(TagLib::String::UTF8);
file.setID3v2FrameFactory(frameFactory);
file.save();
}
@synthesize title;
@synthesize artist;
@synthesize album;
@synthesize genre;
@synthesize year;
@synthesize track;
@synthesize fileUrl;
@end