~njh-aelius/maxosx/submit-discid

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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 *  $Id$
 *
 *  Copyright (C) 2005 - 2007 Stephen F. Booth <me@sbooth.org>
 *
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#import "CompactDisc.h"

#import "Drive.h"
#import "LogController.h"

#include <discid/discid.h>
#include <IOKit/storage/IOCDTypes.h>

@implementation CompactDisc

- (id) initWithDeviceName:(NSString *)deviceName;
{
	if((self = [super init])) {
		unsigned				i;
		unsigned				session			= 1;
		unsigned long			discLength		= 150;
		Drive					*drive			= nil;
		TrackDescriptor			*track			= nil;
		NSMutableDictionary		*trackInfo		= nil;
		NSString				*ISRC			= nil;

		_deviceName		= [deviceName retain];

		// To avoid keeping an open file descriptor, read the disc's properties from the drive
		// and store them in our ivars
		drive = [[Drive alloc] initWithDeviceName:[self deviceName]];

		// Is this is a multisession disc?
		if([drive lastSession] - [drive firstSession] > 0)
			[LogController logMessage:NSLocalizedStringFromTable(@"Multisession disc detected", @"Log", @"")];
		
		// Use first session for now
		session			= 1;
		
		// Disc information
		_firstSector	= [drive firstSectorForSession:session];
		_lastSector		= [drive lastSectorForSession:session];

		_leadOut		= [drive leadOutForSession:session];

		_MCN			= [[drive readMCN] retain];
		
		// Iterate through the tracks and get their information
		_tracks			= [[NSMutableArray alloc] init];
		
		for(i = [drive firstTrackForSession:session]; i <= [drive lastTrackForSession:session]; ++i) {

			track = [drive trackNumber:i];
			
			trackInfo = [NSMutableDictionary dictionaryWithCapacity:6];
		
			[trackInfo setObject:[NSNumber numberWithUnsignedInt:i] forKey:@"number"];

			[trackInfo setObject:[NSNumber numberWithUnsignedInt:[drive firstSectorForTrack:i]] forKey:@"firstSector"];
			[trackInfo setObject:[NSNumber numberWithUnsignedInt:[drive lastSectorForTrack:i]] forKey:@"lastSector"];

			[trackInfo setObject:[NSNumber numberWithUnsignedInt:[track channels]] forKey:@"channels"];

			[trackInfo setObject:[NSNumber numberWithBool:[track preEmphasis]] forKey:@"preEmphasis"];
			[trackInfo setObject:[NSNumber numberWithBool:[track copyPermitted]] forKey:@"allowsDigitalCopy"];
			[trackInfo setObject:[NSNumber numberWithBool:[track dataTrack]] forKey:@"dataTrack"];

			ISRC = [drive readISRC:i];
			if(nil != ISRC)
				[trackInfo setObject:ISRC forKey:@"ISRC"];

			[_tracks addObject:trackInfo];
		}
		
		for(i = 0; i < [self countOfTracks]; ++i)
			discLength += [self lastSectorForTrack:i] - [self firstSectorForTrack:i] + 1;
		_length = (unsigned) (60 * (discLength / (60 * 75))) + (unsigned)((discLength / 75) % 60);
		
		[drive release];
				
		return self;
	}
	
	return nil;
}

- (void) dealloc
{
	[_deviceName release];		_deviceName = nil;
	[_tracks release];			_tracks = nil;
	[_MCN release];				_MCN = nil;

	[super dealloc];
}

- (NSString *)		deviceName								{ return [[_deviceName retain] autorelease]; }

- (NSString *)		MCN										{ return [[_MCN retain] autorelease]; }

- (unsigned)		firstSector								{ return _firstSector; }
- (unsigned)		lastSector								{ return _lastSector; }

- (unsigned)		leadOut									{ return _leadOut; }

- (unsigned)		firstSectorForTrack:(unsigned)track		{ return [[[self objectInTracksAtIndex:track] objectForKey:@"firstSector"] unsignedIntValue]; }
- (unsigned)		lastSectorForTrack:(unsigned)track		{ return [[[self objectInTracksAtIndex:track] objectForKey:@"lastSector"] unsignedIntValue]; }

- (unsigned)		channelsForTrack:(unsigned)track		{ return [[[self objectInTracksAtIndex:track] objectForKey:@"channels"] unsignedIntValue]; }

- (BOOL)			trackHasPreEmphasis:(unsigned)track		{ return [[[self objectInTracksAtIndex:track] objectForKey:@"hasPreEmphasis"] boolValue]; }
- (BOOL)			trackAllowsDigitalCopy:(unsigned)track	{ return [[[self objectInTracksAtIndex:track] objectForKey:@"allowsDigitalCopy"] boolValue]; }
- (BOOL)			trackContainsData:(unsigned)track		{ return [[[self objectInTracksAtIndex:track] objectForKey:@"dataTrack"] boolValue]; }

- (NSString *)		ISRCForTrack:(unsigned)track			{ return [[self objectInTracksAtIndex:track] objectForKey:@"ISRC"]; }


- (DiscId *) discIdObj
{
	DiscId *discID = discid_new();
	if(NULL == discID)
		return NULL;
	
	// zero is lead out
	int offsets[100];
	offsets[0] = [self leadOut] + 150;
	
	unsigned i;
	for(i = 0; i < [self countOfTracks]; ++i)
		offsets[1 + i] = [self firstSectorForTrack:i] + 150;
	
	int result = discid_put(discID, 1, [self countOfTracks], offsets);
	if(result) {
		return discID;
	} else {
		discid_free(discID);
		return NULL;
	}
	
}

- (NSString *) discID
{
	NSString *musicBrainzDiscID = nil;
	
	DiscId *discID = [self discIdObj];
	if(NULL == discID)
		return nil;
	
	musicBrainzDiscID = [NSString stringWithCString:discid_get_id(discID) encoding:NSASCIIStringEncoding];
		
	discid_free(discID);
	
	return [[musicBrainzDiscID retain] autorelease];
}

- (NSString *) freeDBDiscID
{
	NSString *freeDBDiscID = nil;
	
	DiscId *discID = [self discIdObj];
	if(NULL == discID)
		return nil;
	
	freeDBDiscID = [NSString stringWithCString:discid_get_freedb_id(discID) encoding:NSASCIIStringEncoding];
	
	discid_free(discID);
	
	return [[freeDBDiscID retain] autorelease];
}

- (NSURL *) submissionURL
{
	NSURL *submissionURL = nil;
	
	DiscId *discID = [self discIdObj];
	if(NULL == discID)
		return nil;
	
	submissionURL = [NSURL URLWithString:[NSString stringWithCString:discid_get_submission_url(discID) encoding:NSASCIIStringEncoding]];
	
	discid_free(discID);
	
	return [[submissionURL retain] autorelease];
}

- (unsigned)		length									{ return _length; }

// KVC
- (unsigned)		countOfTracks							{ return [_tracks count]; }
- (NSDictionary *)	objectInTracksAtIndex:(unsigned)index	{ return [_tracks objectAtIndex:index]; }

@end