forked from goonj/goonj
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GTrack.m
112 lines (83 loc) · 2.88 KB
/
GTrack.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
/*
File: GTrack.m
Description: A track in the Goonj library/playlist. Stores ID3 tags in
a NSMutableDictionary. In the future, use initWithFile to read in the
tags (implementation).
This file is part of Goonj.
Goonj is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Goonj is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Goonj. If not, see <http://www.gnu.org/licenses/>.
Copyright 2009 Pratul Kalia.
Copyright 2009 Ankur Sethi.
*/
#import "/usr/local/include/taglib/taglib.h"
#import "/usr/local/include/taglib/fileref.h"
#import "/usr/local/include/taglib/tag.h"
#import "GTrack.h"
@implementation GTrack
@dynamic path;
- (id) initWithFile:(NSString *)aURL
{
self = [super init];
if (self) {
properties = [GTrack metadataForFile:aURL];
return self;
}
return nil;
}
- (NSString *) valueForKey:(NSString *)aKey
{
return [properties objectForKey:aKey];
}
- (void) setValue:(NSString *)value forKey:(NSString *)key
{
[properties setValue:value forKey:key];
}
- (NSString *)path
{
return [self valueForKey:@"location"];
}
+ (NSMutableDictionary *) metadataForFile:(NSString *)aURL
{
NSMutableDictionary *metadata = [[NSMutableDictionary alloc] init];
TagLib::FileRef fileRef([aURL cStringUsingEncoding:NSUTF8StringEncoding]);
TagLib::Tag *tag = fileRef.tag();
if (!tag)
return nil;
TagLib::AudioProperties *audioProperties = fileRef.audioProperties();
if (!audioProperties)
return nil;
[metadata setValue:aURL forKey:@"location"];
[metadata setValue:[NSString stringWithCString:tag->title().toCString(true)
encoding:NSUTF8StringEncoding]
forKey:@"name"];
[metadata setValue:[NSString stringWithCString:tag->artist().toCString(true)
encoding:NSUTF8StringEncoding]
forKey:@"artist"];
[metadata setValue:[NSString stringWithCString:tag->album().toCString(true)
encoding:NSUTF8StringEncoding]
forKey:@"album"];
[metadata setValue:[NSString stringWithCString:tag->genre().toCString(true)
encoding:NSUTF8StringEncoding]
forKey:@"genre"];
[metadata setValue:[NSString stringWithCString:tag->comment().toCString(true)
encoding:NSUTF8StringEncoding]
forKey:@"comment"];
int length = audioProperties->length(), minutes = 0, seconds = 0;
while (length > 60) {
minutes++;
length -= 60;
}
seconds = length;
[metadata setValue:[NSString stringWithFormat:@"%d:%02d", minutes, seconds]
forKey:@"time"];
return metadata;
}
@end