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;
2006-06-03 20:26:19 +00:00
length = decoder->shn_get_song_length();
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;
}
- (int)fillBuffer:(void *)buf ofSize:(UInt32)size
{
2007-05-16 01:30:28 +00:00
long totalRead, amountRead, amountToRead;
totalRead = 0;
2007-10-13 07:51:42 +00:00
//For some reason the 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.
2007-05-16 22:56:54 +00:00
// while (totalRead < size) {
2007-05-16 01:30:28 +00:00
amountToRead = size - totalRead;
if (amountToRead > bufferSize) {
amountToRead = bufferSize;
2006-06-03 20:26:19 +00:00
}
2007-05-16 01:30:28 +00:00
do
2006-06-03 20:26:19 +00:00
{
2007-05-16 22:56:54 +00:00
amountRead = decoder->read(buf, amountToRead);
} while(amountRead == -1 && totalRead == 0);
2007-05-16 01:30:28 +00:00
2007-05-16 22:56:54 +00:00
// if (amountRead <= 0) {
// return totalRead;
// }
2007-05-16 01:30:28 +00:00
totalRead += amountRead;
2007-05-16 22:56:54 +00:00
// buf = (void *)((char *)buf + amountRead);
// }
2007-05-11 01:33:05 +00:00
2007-05-16 01:30:28 +00:00
return totalRead;
2006-06-03 20:26:19 +00:00
}
- (double)seekToTime:(double)milliseconds
{
unsigned int sec;
/*if (!shn_seekable(handle))
return -1.0;*/
sec = (int)(milliseconds/1000.0);
//shn_seek(handle, sec);
decoder->seek(sec);
return (sec * 1000.0);
}
- (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",
[NSNumber numberWithDouble:length],@"length",
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
{
return [NSArray arrayWithObjects:@"application/x-shorten", nil]; //This is basically useless
}
2007-02-24 20:36:27 +00:00
2006-06-03 20:26:19 +00:00
@end