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
|
|
|
//
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
#import "MusepackDecoder.h"
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation MusepackDecoder
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
mpc_int32_t ReadProc(mpc_reader *p_reader, void *ptr, mpc_int32_t size)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
|
2007-03-04 18:41:43 +00:00
|
|
|
|
|
|
|
return [[decoder source] read:ptr amount:size];
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
mpc_bool_t SeekProc(mpc_reader *p_reader, mpc_int32_t offset)
|
2007-03-04 18:41:43 +00:00
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
|
2007-07-11 01:20:32 +00:00
|
|
|
|
2007-03-04 18:41:43 +00:00
|
|
|
return [[decoder source] seek:offset whence:SEEK_SET];
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
mpc_int32_t TellProc(mpc_reader *p_reader)
|
2007-03-04 18:41:43 +00:00
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
|
2007-03-04 18:41:43 +00:00
|
|
|
|
|
|
|
return [[decoder source] tell];
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
mpc_int32_t GetSizeProc(mpc_reader *p_reader)
|
2007-03-04 18:41:43 +00:00
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
|
2005-06-02 18:16:43 +00:00
|
|
|
|
2007-03-04 18:41: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
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
mpc_bool_t CanSeekProc(mpc_reader *p_reader)
|
2007-03-04 18:41:43 +00:00
|
|
|
{
|
2016-05-05 20:05:39 +00:00
|
|
|
MusepackDecoder *decoder = (__bridge MusepackDecoder *) p_reader->data;
|
2007-03-04 18:41:43 +00:00
|
|
|
|
|
|
|
return [[decoder source] seekable];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)open:(id<CogSource>)s
|
|
|
|
{
|
|
|
|
[self setSource: s];
|
|
|
|
|
2007-03-04 20:47:49 +00:00
|
|
|
reader.read = ReadProc;
|
|
|
|
reader.seek = SeekProc;
|
|
|
|
reader.tell = TellProc;
|
|
|
|
reader.get_size = GetSizeProc;
|
|
|
|
reader.canseek = CanSeekProc;
|
2016-05-05 20:05:39 +00:00
|
|
|
reader.data = (__bridge void *)(self);
|
2007-03-04 18:41:43 +00:00
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
/* instantiate a demuxer with our reader */
|
|
|
|
demux = mpc_demux_init(&reader);
|
|
|
|
if (!demux)
|
2007-02-24 20:36:27 +00:00
|
|
|
{
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Error initializing decoder.");
|
2007-02-24 20:36:27 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2014-10-20 00:42:04 +00:00
|
|
|
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;
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2008-02-10 16:18:23 +00:00
|
|
|
totalFrames = mpc_streaminfo_get_length_samples(&info);
|
2005-06-29 15:28:20 +00:00
|
|
|
|
2007-03-04 18:41:43 +00:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
- (BOOL)writeToBuffer:(float *)sample_buffer fromBuffer:(const MPC_SAMPLE_FORMAT *)p_buffer frames:(unsigned)frames
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2007-02-24 20:36:27 +00:00
|
|
|
unsigned n;
|
2008-02-10 16:18:23 +00:00
|
|
|
|
2013-10-05 21:15:09 +00:00
|
|
|
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++)
|
|
|
|
{
|
2013-10-05 21:15:09 +00:00
|
|
|
float val;
|
2005-06-02 18:16:43 +00:00
|
|
|
#ifdef MPC_FIXED_POINT
|
2013-10-05 21:15:09 +00:00
|
|
|
val = p_buffer[n] * float_scale;
|
2005-06-02 18:16:43 +00:00
|
|
|
#else
|
2013-10-05 21:15:09 +00:00
|
|
|
val = p_buffer[n];
|
2005-06-02 18:16:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// sample_buffer[n] = CFSwapInt16LittleToHost(val);
|
|
|
|
sample_buffer[n] = val;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// m_data_bytes_written += p_size * (m_bps >> 3);
|
|
|
|
return YES;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2008-02-10 16:18:23 +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];
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2008-02-10 16:18:23 +00:00
|
|
|
int framesRead = 0;
|
2013-10-05 21:15:09 +00:00
|
|
|
int bytesPerFrame = sizeof(float) * 2; //bitsPerSample == 16, channels == 2
|
2008-02-10 16:18:23 +00:00
|
|
|
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
|
|
|
{
|
2008-02-10 16:18:23 +00:00
|
|
|
/* returns the length of the samples*/
|
2014-10-20 00:42:04 +00:00
|
|
|
mpc_frame_info frame;
|
|
|
|
frame.buffer = sampleBuffer;
|
|
|
|
mpc_status err = mpc_demux_decode(demux, &frame);
|
|
|
|
if (frame.bits == -1)
|
2008-02-10 16:18:23 +00:00
|
|
|
{
|
|
|
|
//decode error
|
2014-10-20 00:42:04 +00:00
|
|
|
if (err != MPC_STATUS_OK)
|
|
|
|
DLog(@"Decode error");
|
2008-02-10 16:18:23 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else //status>0 /* status == MPC_FRAME_LENGTH */
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
bufferFrames = frame.samples;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
2008-02-10 16:18:23 +00:00
|
|
|
|
|
|
|
int framesToRead = bufferFrames;
|
|
|
|
if (bufferFrames > frames)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2008-02-10 16:18:23 +00:00
|
|
|
framesToRead = frames;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
2008-02-10 16:18:23 +00:00
|
|
|
|
2014-10-20 00:42:04 +00:00
|
|
|
[self writeToBuffer:((float*)(buf + (framesRead *bytesPerFrame))) fromBuffer:sampleBuffer frames: framesToRead];
|
2008-02-10 16:18:23 +00:00
|
|
|
|
|
|
|
frames -= framesToRead;
|
|
|
|
framesRead += framesToRead;
|
|
|
|
bufferFrames -= framesToRead;
|
|
|
|
|
|
|
|
if (bufferFrames > 0)
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2008-02-10 16:18:23 +00:00
|
|
|
memmove((uint8_t *)sampleBuffer, ((uint8_t *)sampleBuffer) + (framesToRead * bytesPerFrame), bufferFrames * bytesPerFrame);
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-10 16:18:23 +00:00
|
|
|
return framesRead;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
2014-10-20 00:42:04 +00:00
|
|
|
if (demux)
|
|
|
|
{
|
|
|
|
mpc_demux_exit(demux);
|
|
|
|
demux = NULL;
|
|
|
|
}
|
2016-06-19 19:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2008-02-10 16:18:23 +00:00
|
|
|
- (long)seek:(long)sample
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2015-02-08 06:24:42 +00:00
|
|
|
mpc_demux_seek_sample(demux, sample);
|
2005-06-05 18:52:35 +00:00
|
|
|
|
2008-02-10 16:18:23 +00:00
|
|
|
return sample;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2007-03-04 18:41:43 +00:00
|
|
|
- (void)setSource:(id<CogSource>)s
|
|
|
|
{
|
|
|
|
source = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<CogSource>)source
|
|
|
|
{
|
|
|
|
return source;
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
- (NSDictionary *)properties
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2007-02-24 20:36:27 +00:00
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:bitrate], @"bitrate",
|
|
|
|
[NSNumber numberWithFloat:frequency], @"sampleRate",
|
2008-02-10 16:18:23 +00:00
|
|
|
[NSNumber numberWithDouble:totalFrames], @"totalFrames",
|
2013-10-05 21:15:09 +00:00
|
|
|
[NSNumber numberWithInt:32], @"bitsPerSample",
|
|
|
|
[NSNumber numberWithBool:YES], @"floatingPoint",
|
2007-02-24 20:36:27 +00:00
|
|
|
[NSNumber numberWithInt:2], @"channels",
|
2007-05-27 15:11:30 +00:00
|
|
|
[NSNumber numberWithBool:[source seekable]], @"seekable",
|
2007-03-04 20:47:49 +00:00
|
|
|
@"host",@"endian",
|
2007-02-24 20:36:27 +00:00
|
|
|
nil];
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
2007-03-04 18:41:43 +00:00
|
|
|
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
+ (NSArray *)fileTypes
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
2007-02-24 20:36:27 +00:00
|
|
|
return [NSArray arrayWithObject:@"mpc"];
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2007-10-14 18:39:58 +00:00
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"audio/x-musepack", nil];
|
|
|
|
}
|
2007-02-24 20:36:27 +00:00
|
|
|
|
Implemented support for multiple decoders per file name extension, with a floating point priority control per interface. In the event that more than one input is registered to a given extension, and we match that extension, it will be passed off to an instance of the multi-decoder wrapper, which will try opening the file with all of the decoders in order of priority, until either one of them accepts it, or all of them have failed. This paves the way for adding a VGMSTREAM input, so I can give it a very low priority, since it has several formats that are verified by file name extension only. All current inputs have been given a priority of 1.0, except for CoreAudio, which was given a priority of 0.5, because it contains an MP3 and AC3 decoders that I'd rather not use if I don't have to.
2013-10-21 17:54:11 +00:00
|
|
|
+ (float)priority
|
|
|
|
{
|
|
|
|
return 1.0;
|
|
|
|
}
|
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
@end
|