cog/Plugins/Musepack/MusepackDecoder.m

233 lines
4.8 KiB
Matlab
Raw Normal View History

2005-06-02 18:16:43 +00:00
//
// MusepackFile.m
// zyVorbis
//
// Created by Vincent Spader on 1/23/05.
2005-07-02 21:02:06 +00:00
// Copyright 2005 Vincent Spader All rights reserved.
2005-06-02 18:16:43 +00:00
//
#import "MusepackDecoder.h"
2005-06-02 18:16:43 +00:00
#import "Logging.h"
@implementation MusepackDecoder
2005-06-02 18:16:43 +00:00
mpc_int32_t ReadProc(mpc_reader *p_reader, void *ptr, mpc_int32_t size)
2005-06-02 18:16:43 +00:00
{
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
return [[decoder source] read:ptr amount:size];
}
mpc_bool_t SeekProc(mpc_reader *p_reader, mpc_int32_t offset)
{
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
2007-07-11 01:20:32 +00:00
return [[decoder source] seek:offset whence:SEEK_SET];
}
mpc_int32_t TellProc(mpc_reader *p_reader)
{
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
return [[decoder source] tell];
}
mpc_int32_t GetSizeProc(mpc_reader *p_reader)
{
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
2005-06-02 18:16:43 +00:00
if ([[decoder source] seekable]) {
long currentPos = [[decoder source] tell];
[[decoder source] seek:0 whence:SEEK_END];
long size = [[decoder source] tell];
[[decoder source] seek:currentPos whence:SEEK_SET];
return size;
}
else {
return 0;
}
}
2005-06-02 18:16:43 +00:00
mpc_bool_t CanSeekProc(mpc_reader *p_reader)
{
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
return [[decoder source] seekable];
}
- (BOOL)open:(id<CogSource>)s
{
[self setSource: s];
reader.read = ReadProc;
reader.seek = SeekProc;
reader.tell = TellProc;
reader.get_size = GetSizeProc;
reader.canseek = CanSeekProc;
reader.data = (__bridge void *)(self);
/* instantiate a demuxer with our reader */
demux = mpc_demux_init(&reader);
if (!demux)
{
DLog(@"Error initializing decoder.");
return NO;
}
mpc_demux_get_info(demux, &info);
2005-06-02 18:16:43 +00:00
2006-06-19 00:39:41 +00:00
bitrate = (int)(info.average_bitrate/1000.0);
2005-06-02 18:16:43 +00:00
frequency = info.sample_freq;
totalFrames = mpc_streaminfo_get_length_samples(&info);
2005-06-29 15:28:20 +00:00
[self willChangeValueForKey:@"properties"];
[self didChangeValueForKey:@"properties"];
2005-06-02 18:16:43 +00:00
return YES;
}
- (BOOL)writeToBuffer:(float *)sample_buffer fromBuffer:(const MPC_SAMPLE_FORMAT *)p_buffer frames:(unsigned)frames
2005-06-02 18:16:43 +00:00
{
unsigned n;
unsigned p_size = frames * 2; //2 = stereo
#ifdef MPC_FIXED_POINT
const float float_scale = 1.0 / MPC_FIXED_POINT_SCALE;
#endif
2005-06-02 18:16:43 +00:00
for (n = 0; n < p_size; n++)
{
float val;
2005-06-02 18:16:43 +00:00
#ifdef MPC_FIXED_POINT
val = p_buffer[n] * float_scale;
2005-06-02 18:16:43 +00:00
#else
val = p_buffer[n];
2005-06-02 18:16:43 +00:00
#endif
// sample_buffer[n] = CFSwapInt16LittleToHost(val);
sample_buffer[n] = val;
}
// m_data_bytes_written += p_size * (m_bps >> 3);
return YES;
2005-06-02 18:16:43 +00:00
}
- (int)readAudio:(void *)buf frames:(UInt32)frames
2005-06-02 18:16:43 +00:00
{
MPC_SAMPLE_FORMAT sampleBuffer[MPC_DECODER_BUFFER_LENGTH];
int framesRead = 0;
int bytesPerFrame = sizeof(float) * 2; //bitsPerSample == 16, channels == 2
while (framesRead < frames)
{
//Fill from buffer, going by bufferFrames
//if still needs more, decode and repeat
if (bufferFrames == 0)
2005-06-02 18:16:43 +00:00
{
/* returns the length of the samples*/
mpc_frame_info frame;
frame.buffer = sampleBuffer;
mpc_status err = mpc_demux_decode(demux, &frame);
if (frame.bits == -1)
{
//decode error
if (err != MPC_STATUS_OK)
DLog(@"Decode error");
return 0;
}
else //status>0 /* status == MPC_FRAME_LENGTH */
{
}
bufferFrames = frame.samples;
2005-06-02 18:16:43 +00:00
}
int framesToRead = bufferFrames;
if (bufferFrames > frames)
2005-06-02 18:16:43 +00:00
{
framesToRead = frames;
2005-06-02 18:16:43 +00:00
}
[self writeToBuffer:((float*)(buf + (framesRead *bytesPerFrame))) fromBuffer:sampleBuffer frames: framesToRead];
frames -= framesToRead;
framesRead += framesToRead;
bufferFrames -= framesToRead;
if (bufferFrames > 0)
2005-06-02 18:16:43 +00:00
{
memmove((uint8_t *)sampleBuffer, ((uint8_t *)sampleBuffer) + (framesToRead * bytesPerFrame), bufferFrames * bytesPerFrame);
2005-06-02 18:16:43 +00:00
}
}
return framesRead;
2005-06-02 18:16:43 +00:00
}
- (void)close
{
if (demux)
{
mpc_demux_exit(demux);
demux = NULL;
}
}
- (void)dealloc
{
[self close];
2005-06-02 18:16:43 +00:00
}
- (long)seek:(long)sample
2005-06-02 18:16:43 +00:00
{
mpc_demux_seek_sample(demux, sample);
return sample;
2005-06-02 18:16:43 +00:00
}
- (void)setSource:(id<CogSource>)s
{
source = s;
}
- (id<CogSource>)source
{
return source;
}
- (NSDictionary *)properties
2005-06-02 18:16:43 +00:00
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:bitrate], @"bitrate",
[NSNumber numberWithFloat:frequency], @"sampleRate",
[NSNumber numberWithDouble:totalFrames], @"totalFrames",
[NSNumber numberWithInt:32], @"bitsPerSample",
[NSNumber numberWithBool:YES], @"floatingPoint",
[NSNumber numberWithInt:2], @"channels",
[NSNumber numberWithBool:[source seekable]], @"seekable",
@"host",@"endian",
nil];
2005-06-02 18:16:43 +00:00
}
+ (NSArray *)fileTypes
2005-06-02 18:16:43 +00:00
{
return [NSArray arrayWithObject:@"mpc"];
2005-06-02 18:16:43 +00:00
}
2007-10-14 18:39:58 +00:00
+ (NSArray *)mimeTypes
{
return [NSArray arrayWithObjects:@"audio/x-musepack", nil];
}
+ (float)priority
{
return 1.0;
}
2005-06-02 18:16:43 +00:00
@end