cog/Plugins/Shorten/ShortenDecoder.mm

129 lines
2.4 KiB
Plaintext
Raw Normal View History

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.
//
#import "ShortenDecoder.h"
2006-06-03 20:26:19 +00:00
@implementation ShortenDecoder
2006-06-03 20:26:19 +00:00
- (BOOL)open:(id<CogSource>)source
2006-06-03 20:26:19 +00:00
{
NSURL *url = [source url];
2007-10-13 07:51:42 +00:00
[source close];
if (![[url scheme] isEqualToString:@"file"])
return NO;
2006-06-03 20:26:19 +00:00
decoder = new shn_reader;
if (!decoder)
{
return NO;
}
decoder->open([[url path] UTF8String], true);
2006-06-03 20:26:19 +00:00
bufferSize = decoder->shn_get_buffer_block_size(NUM_DEFAULT_BUFFER_BLOCKS);
2006-06-03 20:26:19 +00:00
bool seekTable;
decoder->file_info(NULL, &channels, &frequency, NULL, &bitsPerSample, &seekTable);
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.
// 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
{
amountRead = decoder->read(buf, amountToRead);
} while(amountRead == -1 && totalRead == 0);
2007-05-16 01:30:28 +00:00
// if (amountRead <= 0) {
// return totalRead;
// }
2007-05-16 01:30:28 +00:00
totalRead += amountRead;
// 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);*/
}
- (NSDictionary *)properties
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:channels],@"channels",
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
[NSNumber numberWithFloat:frequency],@"sampleRate",
[NSNumber numberWithDouble:length],@"length",
[NSNumber numberWithBool:seekable ],@"seekable",
@"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
}
2006-06-03 20:26:19 +00:00
@end