forked from rrooding/TagLib.framework
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTagLib.m
128 lines (96 loc) · 4.07 KB
/
TagLib.m
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
//
// TagLib.m
// TagLibBundle
//
// A simple Obj-C wrapper around the C functions that TagLib exposes
//
// Created by Nick Ludlam on 21/07/2010.
//
#import "TagLib.h"
#include "tag_c.h"
// Required in order to be able to use #require in Ruby to load this bundle
void Init_TagLib(void) { }
@implementation TagLib
@synthesize validTags, validAudioProperties;
@synthesize path;
@synthesize title;
@synthesize artist;
@synthesize album;
@synthesize comment;
@synthesize genre;
@synthesize track;
@synthesize year;
@synthesize length, sampleRate, bitRate;
- (id)initWithFileAtPath:(NSString *)filePath {
if (self = [super init]) {
// Initialisation as per the TagLib example C code
TagLib_File *file;
TagLib_Tag *tag;
// We want UTF8 strings out of TagLib
taglib_set_strings_unicode(TRUE);
file = taglib_file_new([filePath cStringUsingEncoding:NSUTF8StringEncoding]);
self.path = filePath;
if (file != NULL) {
tag = taglib_file_tag(file);
if (tag != NULL) {
// Collect title, artist, album, comment, genre, track and year in turn.
// Sanity check them for presence, and length
self.validTags = YES;
if (taglib_tag_title(tag) != NULL &&
strlen(taglib_tag_title(tag)) > 0) {
self.title = [NSString stringWithCString:taglib_tag_title(tag)
encoding:NSUTF8StringEncoding];
}
if (taglib_tag_artist(tag) != NULL &&
strlen(taglib_tag_artist(tag)) > 0) {
self.artist = [NSString stringWithCString:taglib_tag_artist(tag)
encoding:NSUTF8StringEncoding];
}
if (taglib_tag_album(tag) != NULL &&
strlen(taglib_tag_album(tag)) > 0) {
self.album = [NSString stringWithCString:taglib_tag_album(tag)
encoding:NSUTF8StringEncoding];
}
if (taglib_tag_comment(tag) != NULL &&
strlen(taglib_tag_comment(tag)) > 0) {
self.comment = [NSString stringWithCString:taglib_tag_comment(tag)
encoding:NSUTF8StringEncoding];
}
if (taglib_tag_genre(tag) != NULL &&
strlen(taglib_tag_genre(tag)) > 0) {
self.genre = [NSString stringWithCString:taglib_tag_genre(tag)
encoding:NSUTF8StringEncoding];
}
// Year and track are uints
if (taglib_tag_year(tag) > 0) {
self.year = [NSNumber numberWithUnsignedInt:taglib_tag_year(tag)];
}
if (taglib_tag_track(tag) > 0) {
self.track = [NSNumber numberWithUnsignedInt:taglib_tag_track(tag)];
}
} else {
self.validTags = NO;
}
const TagLib_AudioProperties *properties = taglib_file_audioproperties(file);
if (properties != NULL) {
self.validAudioProperties = YES;
if (taglib_audioproperties_length(properties) > 0) {
self.length = [NSNumber numberWithInt:taglib_audioproperties_length(properties)];
}
} else {
self.validAudioProperties = NO;
}
// Free up our used memory so far
taglib_tag_free_strings();
taglib_file_free(file);
}
}
return self;
}
- (NSString *)description {
return [NSString stringWithFormat:@"%@ - Path: %@", [super description], path];
}
- (NSString *)inspect {
return [self description];
}
@end