cog/Plugins/Shorten/ShortenDecoder.mm

113 lines
2.2 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;
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;
}
- (int)readAudio:(void *)buf frames:(UInt32)frames
2006-06-03 20:26:19 +00:00
{
int bytesPerFrame = channels * (bitsPerSample/8);
int amountRead;
2007-05-16 01:30: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
return amountRead/bytesPerFrame;
2006-06-03 20:26:19 +00:00
}
- (long)seek:(long)sample
2006-06-03 20:26:19 +00:00
{
unsigned int sec = sample/frequency;
2006-06-03 20:26:19 +00:00
decoder->seek(sec);
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);*/
}
- (NSDictionary *)properties
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:channels],@"channels",
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
[NSNumber numberWithFloat:frequency],@"sampleRate",
[NSNumber numberWithDouble:totalFrames],@"totalFrames",
[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, since we cant stream shorten yet
2007-10-14 18:39:58 +00:00
}
+ (float)priority
{
return 1.0;
}
2006-06-03 20:26:19 +00:00
@end