2007-10-20 16:15:02 +00:00
|
|
|
#import "APLDecoder.h"
|
|
|
|
#import "APLFile.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-10-20 16:15:02 +00:00
|
|
|
@implementation APLDecoder
|
|
|
|
|
|
|
|
+ (NSArray *)fileTypes {
|
|
|
|
return [NSArray arrayWithObject:@"apl"];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSArray *)mimeTypes {
|
|
|
|
return [NSArray arrayWithObjects:@"application/x-apl", nil];
|
|
|
|
}
|
|
|
|
|
Implemented support for multiple decoders per file name extension, with a floating point priority control per interface. In the event that more than one input is registered to a given extension, and we match that extension, it will be passed off to an instance of the multi-decoder wrapper, which will try opening the file with all of the decoders in order of priority, until either one of them accepts it, or all of them have failed. This paves the way for adding a VGMSTREAM input, so I can give it a very low priority, since it has several formats that are verified by file name extension only. All current inputs have been given a priority of 1.0, except for CoreAudio, which was given a priority of 0.5, because it contains an MP3 and AC3 decoders that I'd rather not use if I don't have to.
2013-10-21 17:54:11 +00:00
|
|
|
+ (float)priority {
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2007-10-20 16:15:02 +00:00
|
|
|
- (NSDictionary *)properties {
|
|
|
|
NSMutableDictionary *properties = [[decoder properties] mutableCopy];
|
|
|
|
|
|
|
|
//Need to alter length
|
2008-05-09 21:24:49 +00:00
|
|
|
[properties setObject:[NSNumber numberWithLong:trackLength] forKey:@"totalFrames"];
|
2007-10-20 16:15:02 +00:00
|
|
|
return [properties autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)open:(id<CogSource>)s
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
//DLog(@"Loading apl...");
|
2007-10-20 16:15:02 +00:00
|
|
|
if (![[s url] isFileURL])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
NSURL *url = [s url];
|
|
|
|
[s close];
|
|
|
|
|
|
|
|
apl = [APLFile createWithFile:[url path]];
|
|
|
|
[apl retain];
|
|
|
|
|
|
|
|
//Kind of a hackish way of accessing outside classes.
|
|
|
|
source = [NSClassFromString(@"AudioSource") audioSourceForURL:[apl file]];
|
|
|
|
[source retain];
|
|
|
|
|
|
|
|
if (![source open:[apl file]]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Could not open source for file '%@' referenced in apl", [apl file]);
|
2007-10-20 16:15:02 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
decoder = [NSClassFromString(@"AudioDecoder") audioDecoderForSource:source];
|
|
|
|
[decoder retain];
|
|
|
|
|
|
|
|
if (![decoder open:source]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Could not open decoder for source for apl");
|
2007-10-20 16:15:02 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDictionary *properties = [decoder properties];
|
|
|
|
int bitsPerSample = [[properties objectForKey:@"bitsPerSample"] intValue];
|
|
|
|
int channels = [[properties objectForKey:@"channels"] intValue];
|
2008-05-09 21:24:49 +00:00
|
|
|
// float sampleRate = [[properties objectForKey:@"sampleRate"] floatValue];
|
2007-10-20 16:15:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
bytesPerFrame = (bitsPerSample/8) * channels;
|
|
|
|
|
2007-10-20 16:18:44 +00:00
|
|
|
if ([apl endBlock] > [apl startBlock])
|
2008-05-09 21:24:49 +00:00
|
|
|
trackEnd = [apl endBlock]; //([apl endBlock] / sampleRate) * 1000.0;
|
2007-10-20 16:18:44 +00:00
|
|
|
else
|
2008-05-09 21:24:49 +00:00
|
|
|
trackEnd = [[properties objectForKey:@"totalFrames"] doubleValue]; //!!? double?
|
2007-10-20 16:18:44 +00:00
|
|
|
|
2008-05-09 21:24:49 +00:00
|
|
|
trackStart = [apl startBlock];
|
2007-10-20 16:15:02 +00:00
|
|
|
trackLength = trackEnd - trackStart;
|
|
|
|
|
2008-05-09 21:24:49 +00:00
|
|
|
[self seek: 0];
|
2007-10-20 16:15:02 +00:00
|
|
|
|
|
|
|
//Note: Should register for observations of the decoder, but laziness consumes all.
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close {
|
|
|
|
if (decoder) {
|
|
|
|
[decoder close];
|
|
|
|
[decoder release];
|
|
|
|
decoder = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
[source release];
|
|
|
|
source = nil;
|
|
|
|
}
|
|
|
|
if (apl) {
|
|
|
|
[apl release];
|
|
|
|
apl = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-09 21:24:49 +00:00
|
|
|
- (long)seek:(long)frame
|
2007-10-20 16:15:02 +00:00
|
|
|
{
|
2008-05-09 21:24:49 +00:00
|
|
|
if (frame > trackEnd - trackStart) {
|
2007-10-20 16:15:02 +00:00
|
|
|
//need a better way of returning fail.
|
2008-05-09 21:24:49 +00:00
|
|
|
return -1;
|
2007-10-20 16:15:02 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 21:24:49 +00:00
|
|
|
frame += trackStart;
|
2007-10-20 16:15:02 +00:00
|
|
|
|
2008-05-09 21:24:49 +00:00
|
|
|
framePosition = [decoder seek:frame];
|
|
|
|
|
|
|
|
return framePosition;
|
2007-10-20 16:15:02 +00:00
|
|
|
}
|
|
|
|
|
2008-05-09 21:24:49 +00:00
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames
|
2007-10-20 16:15:02 +00:00
|
|
|
{
|
2008-05-09 21:24:49 +00:00
|
|
|
if (framePosition + frames > trackEnd)
|
|
|
|
frames = trackEnd - framePosition;
|
|
|
|
|
|
|
|
if (!frames) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"APL readAudio Returning 0");
|
2007-10-20 16:15:02 +00:00
|
|
|
return 0;
|
2008-05-09 21:24:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int n = [decoder readAudio:buf frames:frames];
|
|
|
|
framePosition += n;
|
2007-10-20 16:15:02 +00:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|