2007-10-09 02:25:40 +00:00
|
|
|
//
|
|
|
|
// CueSheetDecoder.m
|
|
|
|
// CueSheet
|
|
|
|
//
|
|
|
|
// Created by Zaphod Beeblebrox on 10/8/07.
|
|
|
|
// Copyright 2007 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "CueSheetDecoder.h"
|
|
|
|
|
|
|
|
#import "CueSheet.h"
|
|
|
|
#import "CueSheetTrack.h"
|
2007-10-10 01:59:25 +00:00
|
|
|
#import "CueSheetContainer.h"
|
2007-10-09 02:25:40 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-10-09 02:25:40 +00:00
|
|
|
@implementation CueSheetDecoder
|
|
|
|
|
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
2007-10-10 01:59:25 +00:00
|
|
|
return [CueSheetContainer fileTypes];
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 18:39:58 +00:00
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
2007-10-14 18:56:23 +00:00
|
|
|
return [CueSheetContainer mimeTypes];
|
2007-10-14 18:39:58 +00:00
|
|
|
}
|
|
|
|
|
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-09 02:25:40 +00:00
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
2007-10-10 01:59:25 +00:00
|
|
|
NSMutableDictionary *properties = [[decoder properties] mutableCopy];
|
|
|
|
|
|
|
|
//Need to alter length
|
2008-03-30 15:43:28 +00:00
|
|
|
[properties setObject:[NSNumber numberWithLong:(trackEnd - trackStart)] forKey:@"totalFrames"];
|
2007-10-10 01:59:25 +00:00
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
return properties;
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)s
|
2007-10-09 02:25:40 +00:00
|
|
|
{
|
2007-10-10 01:59:25 +00:00
|
|
|
if (![[s url] isFileURL]) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSURL *url = [s url];
|
|
|
|
[s close];
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
cuesheet = [CueSheet cueSheetWithFile:[url path]];
|
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
NSArray *tracks = [cuesheet tracks];
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < [tracks count]; i++)
|
2007-10-09 02:25:40 +00:00
|
|
|
{
|
2007-10-10 01:59:25 +00:00
|
|
|
if ([[[tracks objectAtIndex:i] track] isEqualToString:[url fragment]]){
|
|
|
|
track = [tracks objectAtIndex:i];
|
|
|
|
|
|
|
|
//Kind of a hackish way of accessing outside classes.
|
|
|
|
source = [NSClassFromString(@"AudioSource") audioSourceForURL:[track url]];
|
|
|
|
|
|
|
|
if (![source open:[track url]]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Could not open cuesheet source");
|
2007-10-10 01:59:25 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2007-10-14 18:56:23 +00:00
|
|
|
decoder = [NSClassFromString(@"AudioDecoder") audioDecoderForSource:source];
|
2007-10-10 01:59:25 +00:00
|
|
|
|
|
|
|
if (![decoder open:source]) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Could not open cuesheet decoder");
|
2007-10-10 01:59:25 +00:00
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
CueSheetTrack *nextTrack = nil;
|
|
|
|
if (i + 1 < [tracks count]) {
|
|
|
|
nextTrack = [tracks objectAtIndex:i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSDictionary *properties = [decoder properties];
|
|
|
|
int bitsPerSample = [[properties objectForKey:@"bitsPerSample"] intValue];
|
|
|
|
int channels = [[properties objectForKey:@"channels"] intValue];
|
|
|
|
float sampleRate = [[properties objectForKey:@"sampleRate"] floatValue];
|
|
|
|
|
2007-10-15 22:19:14 +00:00
|
|
|
bytesPerFrame = (bitsPerSample/8) * channels;
|
2008-03-30 15:43:28 +00:00
|
|
|
|
|
|
|
trackStart = [track time] * sampleRate;
|
2007-10-10 01:59:25 +00:00
|
|
|
|
|
|
|
if (nextTrack && [[[nextTrack url] absoluteString] isEqualToString:[[track url] absoluteString]]) {
|
2008-03-30 15:43:28 +00:00
|
|
|
trackEnd = [nextTrack time] * sampleRate;
|
2007-10-10 01:59:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-03-30 15:43:28 +00:00
|
|
|
trackEnd = [[properties objectForKey:@"totalFrames"] doubleValue];
|
2007-10-10 01:59:25 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
[self seek: 0];
|
2007-10-19 22:51:01 +00:00
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
//Note: Should register for observations of the decoder
|
2007-10-12 02:55:59 +00:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
2007-10-10 01:59:25 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close {
|
|
|
|
if (decoder) {
|
|
|
|
[decoder close];
|
|
|
|
decoder = nil;
|
|
|
|
}
|
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
source = nil;
|
|
|
|
cuesheet = nil;
|
|
|
|
track = nil;
|
2007-10-11 02:08:29 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 19:57:18 +00:00
|
|
|
- (void)dealloc {
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
- (BOOL)setTrack:(NSURL *)url
|
|
|
|
{
|
2008-03-30 15:43:28 +00:00
|
|
|
//Same file, just next track...this may be unnecessary since frame-based decoding is done now...
|
|
|
|
if ([[[track url] path] isEqualToString:[url path]] && [[[track url] host] isEqualToString:[url host]] && [[url fragment] intValue] == [[track track] intValue] + 1) {
|
2007-10-11 02:08:29 +00:00
|
|
|
NSArray *tracks = [cuesheet tracks];
|
|
|
|
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < [tracks count]; i++) {
|
|
|
|
if ([[[tracks objectAtIndex:i] track] isEqualToString:[url fragment]]){
|
|
|
|
track = [tracks objectAtIndex:i];
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
float sampleRate = [[[decoder properties] objectForKey:@"sampleRate"] floatValue];
|
|
|
|
|
|
|
|
trackStart = [track time] * sampleRate;
|
|
|
|
|
2007-10-11 02:08:29 +00:00
|
|
|
CueSheetTrack *nextTrack = nil;
|
|
|
|
if (i + 1 < [tracks count]) {
|
|
|
|
nextTrack = [tracks objectAtIndex:i + 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextTrack && [[[nextTrack url] absoluteString] isEqualToString:[[track url] absoluteString]]) {
|
2008-03-30 15:43:28 +00:00
|
|
|
trackEnd = [nextTrack time] * sampleRate;
|
2007-10-11 02:08:29 +00:00
|
|
|
}
|
|
|
|
else {
|
2008-03-30 15:43:28 +00:00
|
|
|
trackEnd = [[[decoder properties] objectForKey:@"totalFrames"] longValue];
|
2007-10-11 02:08:29 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"CHANGING TRACK!");
|
2007-10-11 02:08:29 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
- (long)seek:(long)frame
|
2007-10-09 02:25:40 +00:00
|
|
|
{
|
2008-03-30 15:43:28 +00:00
|
|
|
if (frame > trackEnd - trackStart) {
|
2007-10-09 02:25:40 +00:00
|
|
|
//need a better way of returning fail.
|
2008-03-30 15:43:28 +00:00
|
|
|
return -1;
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
frame += trackStart;
|
2007-10-15 22:19:14 +00:00
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
framePosition = [decoder seek:frame];
|
2007-10-11 02:08:29 +00:00
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
return framePosition;
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames
|
2007-10-09 02:25:40 +00:00
|
|
|
{
|
2008-03-30 15:43:28 +00:00
|
|
|
if (framePosition + frames > trackEnd) {
|
2017-09-18 02:21:48 +00:00
|
|
|
frames = (UInt32)(trackEnd - framePosition);
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
if (!frames)
|
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Returning 0");
|
2007-10-10 01:59:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
int n = [decoder readAudio:buf frames:frames];
|
2007-10-09 02:25:40 +00:00
|
|
|
|
2008-03-30 15:43:28 +00:00
|
|
|
framePosition += n;
|
2007-10-10 01:59:25 +00:00
|
|
|
|
|
|
|
return n;
|
2007-10-09 02:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@end
|