2007-10-12 01:03:12 +00:00
|
|
|
//
|
|
|
|
// DumbFile.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 5/29/06.
|
|
|
|
// Copyright 2006 Vincent Spader. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "DumbDecoder.h"
|
|
|
|
|
|
|
|
@implementation DumbDecoder
|
|
|
|
|
|
|
|
int skipCallback(void *f, long n)
|
|
|
|
{
|
2013-10-04 15:14:47 +00:00
|
|
|
id source = (id)f;
|
2007-10-12 01:03:12 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
if (![source seek:n whence: SEEK_CUR])
|
2007-10-12 01:03:12 +00:00
|
|
|
{
|
|
|
|
return 1; //Non-zero is error
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0; //Zero for success
|
|
|
|
}
|
|
|
|
|
|
|
|
int getCharCallback(void *f)
|
|
|
|
{
|
2013-10-04 15:14:47 +00:00
|
|
|
id source = (id)f;
|
2007-10-12 01:03:12 +00:00
|
|
|
|
2007-10-12 01:46:31 +00:00
|
|
|
unsigned char c;
|
2007-10-12 01:03:12 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
if ([source read:&c amount:1] < 1)
|
2007-10-12 01:03:12 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
long readCallback(char *ptr, long n, void *f)
|
|
|
|
{
|
2013-10-04 15:14:47 +00:00
|
|
|
id source = (id)f;
|
2007-10-12 01:03:12 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
return [source read:ptr amount:n];
|
2007-10-12 01:03:12 +00:00
|
|
|
}
|
|
|
|
|
2013-09-28 03:24:23 +00:00
|
|
|
int seekCallback(void *f, long n)
|
|
|
|
{
|
2013-10-04 15:14:47 +00:00
|
|
|
id source = (id)f;
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
if (![source seekable]) return -1;
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
if ([source seek:n whence:SEEK_SET]) return 0;
|
2013-09-28 03:24:23 +00:00
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
long getsizeCallback(void *f)
|
|
|
|
{
|
2013-10-04 15:14:47 +00:00
|
|
|
id source = (id)f;
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
if (![source seekable]) return 0;
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
long current_offset = [source tell];
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
[source seek:0 whence:SEEK_END];
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
long size = [source tell];
|
2013-09-28 03:24:23 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
[source seek:current_offset whence:SEEK_SET];
|
2013-09-28 03:24:23 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-09-30 00:28:58 +00:00
|
|
|
void oneTimeInit()
|
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
DUMBFILE_SYSTEM dfs = {
|
|
|
|
.open = NULL,
|
|
|
|
.skip = skipCallback,
|
|
|
|
.getc = getCharCallback,
|
|
|
|
.getnc = readCallback,
|
|
|
|
.close = NULL,
|
|
|
|
.seek = seekCallback,
|
|
|
|
.get_size = getsizeCallback
|
|
|
|
};
|
|
|
|
|
2007-10-12 01:03:12 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)s
|
|
|
|
{
|
|
|
|
[self setSource:s];
|
|
|
|
|
2013-09-30 00:28:58 +00:00
|
|
|
DUMBFILE *df;
|
2007-10-12 01:03:12 +00:00
|
|
|
|
2007-10-13 02:20:51 +00:00
|
|
|
// dumb_register_stdfiles();
|
2007-10-12 01:03:12 +00:00
|
|
|
|
2013-10-04 15:14:47 +00:00
|
|
|
df = dumbfile_open_ex(s, &dfs);
|
2007-10-12 01:03:12 +00:00
|
|
|
if (!df)
|
|
|
|
{
|
|
|
|
NSLog(@"EX Failed");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *ext = [[[[s url] path] pathExtension] lowercaseString];
|
2013-09-28 03:24:23 +00:00
|
|
|
duh = dumb_read_any(df, [ext isEqualToString:@"mod"] ? 0 : 1, 0);
|
2007-10-12 01:03:12 +00:00
|
|
|
if (!duh)
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to create duh");
|
2013-09-30 00:28:58 +00:00
|
|
|
dumbfile_close(df);
|
2007-10-12 01:03:12 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2013-09-30 00:28:58 +00:00
|
|
|
dumbfile_close(df);
|
2007-10-12 01:03:12 +00:00
|
|
|
|
|
|
|
length = duh_get_length(duh);
|
|
|
|
|
|
|
|
|
|
|
|
dsr = duh_start_sigrenderer(duh, 0, 2 /* stereo */, 0 /* start from the beginning */);
|
|
|
|
if (!dsr)
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to create dsr");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2013-09-28 03:24:23 +00:00
|
|
|
DUMB_IT_SIGRENDERER * itsr = duh_get_it_sigrenderer( dsr );
|
|
|
|
dumb_it_set_ramp_style( itsr, 2 );
|
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
2007-10-12 02:55:59 +00:00
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
2007-10-12 01:03:12 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:0], @"bitrate",
|
|
|
|
[NSNumber numberWithFloat:44100], @"sampleRate",
|
2008-02-10 15:56:18 +00:00
|
|
|
[NSNumber numberWithDouble:((length / 65.536f)*44.1000)], @"totalFrames",
|
2007-10-12 01:03:12 +00:00
|
|
|
[NSNumber numberWithInt:16], @"bitsPerSample", //Samples are short
|
|
|
|
[NSNumber numberWithInt:2], @"channels", //output from gme_play is in stereo
|
|
|
|
[NSNumber numberWithBool:[source seekable]], @"seekable",
|
|
|
|
@"host", @"endian",
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
2008-02-10 15:56:18 +00:00
|
|
|
- (int)readAudio:(void *)buf frames:(UInt32)frames
|
2007-10-12 01:03:12 +00:00
|
|
|
{
|
|
|
|
if (duh_sigrenderer_get_position(dsr) > length) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-10 15:56:18 +00:00
|
|
|
return duh_render(dsr, 16 /* shorts */, 0 /* not unsigned */, 1.0 /* volume */, 65536.0f / 44100.0f /* 65536 hz? */, frames, buf);
|
2007-10-12 01:03:12 +00:00
|
|
|
}
|
|
|
|
|
2008-02-10 15:56:18 +00:00
|
|
|
- (long)seek:(long)frame
|
2007-10-12 01:03:12 +00:00
|
|
|
{
|
|
|
|
double pos = (double)duh_sigrenderer_get_position(dsr) / 65.536f;
|
2008-02-10 15:56:18 +00:00
|
|
|
double seekPos = frame/44.100;
|
2007-10-12 01:03:12 +00:00
|
|
|
|
|
|
|
if (seekPos < pos) {
|
|
|
|
//Reset. Dumb cannot seek backwards. It's dumb.
|
|
|
|
[self cleanUp];
|
2007-10-12 01:49:36 +00:00
|
|
|
|
|
|
|
[source seek:0 whence:SEEK_SET];
|
2007-10-12 01:03:12 +00:00
|
|
|
[self open:source];
|
|
|
|
|
|
|
|
pos = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int numSamples = (seekPos - pos)/1000 * 44100;
|
|
|
|
|
|
|
|
duh_sigrenderer_generate_samples(dsr, 1.0f, 65536.0f / 44100.0f, numSamples, NULL);
|
|
|
|
|
2008-02-10 15:56:18 +00:00
|
|
|
return frame;
|
2007-10-12 01:03:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)cleanUp
|
|
|
|
{
|
|
|
|
if (dsr) {
|
|
|
|
duh_end_sigrenderer(dsr);
|
|
|
|
dsr = NULL;
|
|
|
|
}
|
|
|
|
if (duh) {
|
|
|
|
unload_duh(duh);
|
|
|
|
duh = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
[self cleanUp];
|
|
|
|
|
|
|
|
if (source) {
|
|
|
|
[source close];
|
|
|
|
[self setSource:nil];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSource:(id<CogSource>)s
|
|
|
|
{
|
|
|
|
[s retain];
|
|
|
|
[source release];
|
|
|
|
source = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<CogSource>)source
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2007-10-14 18:39:58 +00:00
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
2013-09-28 03:24:23 +00:00
|
|
|
return [NSArray arrayWithObjects:@"it", @"xm", @"s3m", @"mod", @"stm", @"ptm", @"mtm", @"669", @"psm", @"am", @"dsm", @"amf", @"okt", @"okta", nil];
|
2007-10-14 18:39:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"audio/x-it", @"audio/x-xm", @"audio/x-s3m", @"audio/x-mod", nil];
|
|
|
|
}
|
|
|
|
|
2007-10-12 01:03:12 +00:00
|
|
|
@end
|