cog/Plugins/WavPack/WavPackDecoder.m

92 lines
1.7 KiB
Matlab
Raw Normal View History

2005-06-06 17:47:29 +00:00
//
// WavPackFile.m
// Cog
//
2005-07-02 21:02:06 +00:00
// Created by Vincent Spader on 6/6/05.
// Copyright 2005 Vincent Spader All rights reserved.
2005-06-06 17:47:29 +00:00
//
#import "WavPackDecoder.h"
2005-06-06 17:47:29 +00:00
@implementation WavPackDecoder
2005-06-06 17:47:29 +00:00
- (BOOL)open:(NSURL *)url
2005-06-06 17:47:29 +00:00
{
int open_flags = 0;
char error[80];
wpc = WavpackOpenFileInput([[url path] UTF8String], error, open_flags, 0);
2005-06-06 17:47:29 +00:00
if (!wpc)
return NO;
channels = WavpackGetNumChannels(wpc);
2006-04-21 20:43:47 +00:00
bitsPerSample = WavpackGetBitsPerSample(wpc);
2005-06-29 15:28:20 +00:00
frequency = WavpackGetSampleRate(wpc);
2005-06-06 17:47:29 +00:00
length = ((double)WavpackGetNumSamples(wpc) * 1000.0)/frequency;
2005-06-06 17:47:29 +00:00
2006-06-19 00:39:41 +00:00
bitrate = (int)(WavpackGetAverageBitrate(wpc, TRUE)/1000.0);
2005-06-06 17:47:29 +00:00
2005-06-29 15:28:20 +00:00
return YES;
2005-06-06 17:47:29 +00:00
}
- (int)fillBuffer:(void *)buf ofSize:(UInt32)size
{
int numsamples;
int n;
2006-04-21 20:43:47 +00:00
void *sampleBuf = malloc(size*2);
2005-06-06 17:47:29 +00:00
2006-04-21 20:43:47 +00:00
numsamples = size/(bitsPerSample/8)/channels;
2005-06-06 17:47:29 +00:00
// DBLog(@"NUM SAMPLES: %i %i", numsamples, size);
2006-04-21 20:43:47 +00:00
n = WavpackUnpackSamples(wpc, sampleBuf, numsamples);
2005-06-06 17:47:29 +00:00
2005-06-29 15:28:20 +00:00
int i;
2006-04-21 20:43:47 +00:00
for (i = 0; i < n*channels; i++)
2005-06-29 15:28:20 +00:00
{
2006-04-21 20:43:47 +00:00
((UInt16 *)buf)[i] = ((UInt32 *)sampleBuf)[i];
2005-06-29 15:28:20 +00:00
}
2006-04-21 20:43:47 +00:00
n *= (bitsPerSample/8)*channels;
2005-06-29 15:28:20 +00:00
2006-04-28 23:19:14 +00:00
free(sampleBuf);
2005-06-06 17:47:29 +00:00
return n;
}
- (double)seekToTime:(double)milliseconds
{
int sample;
2006-04-25 20:20:11 +00:00
sample = frequency*(milliseconds/1000.0);
2005-06-06 17:47:29 +00:00
WavpackSeekSample(wpc, sample);
return milliseconds;
}
- (void)close
{
WavpackCloseFile(wpc);
}
- (NSDictionary *)properties
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:channels],@"channels",
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
[NSNumber numberWithInt:bitrate],@"bitrate",
[NSNumber numberWithFloat:frequency],@"sampleRate",
[NSNumber numberWithDouble:length],@"length",
@"host",@"endian",
nil];
}
+ (NSArray *)fileTypes
{
return [NSArray arrayWithObject:@"wv"];
}
2005-06-06 17:47:29 +00:00
@end