cog/Plugins/Shorten/ShortenDecoder.mm

122 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 {
NSURL *url = [source url];
2007-10-13 07:51:42 +00:00
if(![[url scheme] isEqualToString:@"file"])
return NO;
decoder = new shn_reader;
if(!decoder) {
2006-06-03 20:26:19 +00:00
return NO;
}
decoder->open([[url path] UTF8String], true);
bufferSize = decoder->shn_get_buffer_block_size(NUM_DEFAULT_BUFFER_BLOCKS);
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;
}
- (AudioChunk *)readAudio {
long frames = 1024;
long bytesPerFrame = channels * (bitsPerSample / 8);
long amountRead;
2007-05-16 01:30:28 +00:00
id audioChunkClass = NSClassFromString(@"AudioChunk");
AudioChunk *chunk = [[audioChunkClass alloc] initWithProperties:[self properties]];
uint8_t buffer[bytesPerFrame * 1024];
void *buf = (void *)buffer;
// 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
[chunk assignSamples:buf frameCount:amountRead / bytesPerFrame];
return chunk;
2006-06-03 20:26:19 +00:00
}
- (long)seek:(long)sample {
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) {
2006-06-03 20:26:19 +00:00
decoder->exit();
delete decoder;
decoder = NULL;
2006-06-03 20:26:19 +00:00
}
/*if (shn_cleanup_decoder(handle))
shn_unload(handle);*/
2006-06-03 20:26:19 +00:00
}
- (void)dealloc {
[self close];
}
- (NSDictionary *)properties {
return @{ @"channels": @(channels),
@"bitsPerSample": @(bitsPerSample),
@"sampleRate": @(frequency),
@"totalFrames": @(totalFrames),
@"seekable": @(seekable),
@"codec": @"Shorten",
@"endian": @"little",
@"encoding": @"lossless" };
}
- (NSDictionary *)metadata {
return @{};
}
+ (NSArray *)fileTypes {
return @[@"shn"];
}
+ (NSArray *)mimeTypes {
return @[@"application/x-shorten"]; // This is basically useless, since we cant stream shorten yet
2007-10-14 18:39:58 +00:00
}
+ (float)priority {
return 1.0;
}
+ (NSArray *)fileTypeAssociations {
return @[
@[@"Shorten File", @"shn.icns", @"shn"]
];
}
2006-06-03 20:26:19 +00:00
@end