2006-06-03 20:26:19 +00:00
//
// ShnFile.mm
// Cog
//
// Created by Vincent Spader on 6/6/05.
// Copyright 2005 Vincent Spader All rights reserved.
//
2007-02-24 20:36:27 +00:00
#import "ShortenDecoder.h"
2006-06-03 20:26:19 +00:00
2007-02-24 20:36:27 +00:00
@implementation ShortenDecoder
2006-06-03 20:26:19 +00:00
2007-03-04 18:46:44 +00:00
- (BOOL)open:(id<CogSource>)source
2006-06-03 20:26:19 +00:00
{
2007-03-04 18:46:44 +00:00
NSURL *url = [source url];
2007-10-13 07:51:42 +00:00
[source close];
2007-03-04 18:46:44 +00:00
if (![[url scheme] isEqualToString:@"file"])
return NO;
2006-06-03 20:26:19 +00:00
decoder = new shn_reader;
if (!decoder)
{
return NO;
}
2007-02-24 20:36:27 +00:00
decoder->open([[url path] UTF8String], true);
2006-06-03 20:26:19 +00:00
2007-05-16 22:56:54 +00:00
bufferSize = decoder->shn_get_buffer_block_size(NUM_DEFAULT_BUFFER_BLOCKS);
2006-06-03 20:26:19 +00:00
2007-05-27 15:11:30 +00:00
bool seekTable;
2007-05-27 14:48:01 +00:00
decoder->file_info(NULL, &channels, &frequency, NULL, &bitsPerSample, &seekTable);
2007-05-27 15:11:30 +00:00
seekable = seekTable == true ? YES : NO;
2008-03-30 15:43:28 +00:00
totalFrames = (decoder->shn_get_song_length() * frequency)/1000.0;
2006-06-03 20:26:19 +00:00
decoder->go();
2007-05-11 01:33:05 +00:00
[self willChangeValueForKey:@"properties"];
[self didChangeValueForKey:@"properties"];
2006-06-03 20:26:19 +00:00
return YES;
}
2008-03-30 15:43:28 +00:00
- (int)readAudio:(void *)buf frames:(UInt32)frames
2006-06-03 20:26:19 +00:00
{
2008-03-30 15:43:28 +00:00
int bytesPerFrame = channels * (bitsPerSample/8);
int amountRead;
2007-05-16 01:30:28 +00:00
2008-03-30 15:43:28 +00:00
//For some reason a busy loop is causing pops when output is set to 48000. Probably CPU starvation, since the SHN decoder seems to use a multithreaded nonblocking approach.
do
{
amountRead = decoder->read(buf, frames * bytesPerFrame);
} while(amountRead == -1);
2007-05-11 01:33:05 +00:00
2008-03-30 15:43:28 +00:00
return amountRead/bytesPerFrame;
2006-06-03 20:26:19 +00:00
}
2008-03-30 15:43:28 +00:00
- (long)seek:(long)sample
2006-06-03 20:26:19 +00:00
{
2008-03-30 15:43:28 +00:00
unsigned int sec = sample/frequency;
2006-06-03 20:26:19 +00:00
decoder->seek(sec);
2008-03-30 15:43:28 +00:00
return sample;
2006-06-03 20:26:19 +00:00
}
- (void)close
{
if(decoder)
{
decoder->exit();
delete decoder;
decoder = NULL;
}
/*if (shn_cleanup_decoder(handle))
shn_unload(handle);*/
}
2007-02-24 20:36:27 +00:00
- (NSDictionary *)properties
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:channels],@"channels",
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
[NSNumber numberWithFloat:frequency],@"sampleRate",
2008-03-30 15:43:28 +00:00
[NSNumber numberWithDouble:totalFrames],@"totalFrames",
2007-05-27 15:11:30 +00:00
[NSNumber numberWithBool:seekable ],@"seekable",
2007-02-24 20:36:27 +00:00
@"little",@"endian",
nil];
}
+ (NSArray *)fileTypes
{
return [NSArray arrayWithObject:@"shn"];
}
2007-10-14 18:39:58 +00:00
+ (NSArray *)mimeTypes
{
2008-03-30 15:43:28 +00:00
return [NSArray arrayWithObjects:@"application/x-shorten", nil]; //This is basically useless, since we cant stream shorten yet
2007-10-14 18:39:58 +00:00
}
2007-02-24 20:36:27 +00:00
2006-06-03 20:26:19 +00:00
@end