2006-06-03 16:26:19 -04:00
|
|
|
//
|
|
|
|
// ShnFile.mm
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 6/6/05.
|
|
|
|
// Copyright 2005 Vincent Spader All rights reserved.
|
|
|
|
//
|
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
#import "ShortenDecoder.h"
|
2006-06-03 16:26:19 -04:00
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
@implementation ShortenDecoder
|
2006-06-03 16:26:19 -04:00
|
|
|
|
2007-03-04 15:46:44 -03:00
|
|
|
- (BOOL)open:(id<CogSource>)source
|
2006-06-03 16:26:19 -04:00
|
|
|
{
|
2007-03-04 15:46:44 -03:00
|
|
|
NSURL *url = [source url];
|
|
|
|
if (![[url scheme] isEqualToString:@"file"])
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
[source close];
|
|
|
|
|
2006-06-03 16:26:19 -04:00
|
|
|
decoder = new shn_reader;
|
|
|
|
|
|
|
|
if (!decoder)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
decoder->open([[url path] UTF8String], true);
|
2006-06-03 16:26:19 -04:00
|
|
|
|
|
|
|
bufferSize = decoder->shn_get_buffer_block_size(512);
|
|
|
|
|
2007-02-24 17:36:27 -03:00
|
|
|
decoder->file_info(NULL, &channels, &frequency, NULL, &bitsPerSample, NULL);
|
2006-06-03 16:26:19 -04:00
|
|
|
|
|
|
|
length = decoder->shn_get_song_length();
|
|
|
|
|
|
|
|
decoder->go();
|
2007-05-10 21:33:05 -04:00
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
2006-06-03 16:26:19 -04:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)fillBuffer:(void *)buf ofSize:(UInt32)size
|
|
|
|
{
|
2007-05-15 21:30:28 -04:00
|
|
|
long totalRead, amountRead, amountToRead;
|
|
|
|
|
|
|
|
totalRead = 0;
|
|
|
|
|
|
|
|
while (totalRead < size) {
|
|
|
|
amountToRead = size - totalRead;
|
|
|
|
if (amountToRead > bufferSize) {
|
|
|
|
amountToRead = bufferSize;
|
2006-06-03 16:26:19 -04:00
|
|
|
}
|
2007-05-15 21:30:28 -04:00
|
|
|
|
|
|
|
do
|
2006-06-03 16:26:19 -04:00
|
|
|
{
|
2007-05-15 21:30:28 -04:00
|
|
|
amountRead = decoder->read(((char *)buf) + totalRead, amountToRead);
|
|
|
|
} while(amountRead == -1);
|
|
|
|
|
|
|
|
if (amountRead <= 0) {
|
|
|
|
return totalRead;
|
2006-06-03 16:26:19 -04:00
|
|
|
}
|
2007-05-15 21:30:28 -04:00
|
|
|
|
|
|
|
totalRead += amountRead;
|
2006-06-03 16:26:19 -04:00
|
|
|
}
|
2007-05-10 21:33:05 -04:00
|
|
|
|
2007-05-15 21:30:28 -04:00
|
|
|
return totalRead;
|
2006-06-03 16:26:19 -04: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-03-04 15:46:44 -03:00
|
|
|
- (BOOL)seekable
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
2007-02-24 17:36:27 -03:00
|
|
|
|
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels],@"channels",
|
|
|
|
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
|
|
|
|
[NSNumber numberWithFloat:frequency],@"sampleRate",
|
|
|
|
[NSNumber numberWithDouble:length],@"length",
|
|
|
|
@"little",@"endian",
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObject:@"shn"];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-03 16:26:19 -04:00
|
|
|
@end
|