2006-06-19 00:39:41 +00:00
|
|
|
//
|
|
|
|
// MADFile.m
|
|
|
|
// Cog
|
|
|
|
//
|
2006-09-04 18:46:18 +00:00
|
|
|
// Created by Vincent Spader on 6/17/06.
|
|
|
|
// Copyright 2006 Vincent Spader. All rights reserved.
|
2006-06-19 00:39:41 +00:00
|
|
|
//
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
#import "MADDecoder.h"
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation MADDecoder
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
#define LAME_HEADER_SIZE ((8 * 5) + 4 + 4 + 8 + 32 + 16 + 16 + 4 + 4 + 8 + 12 + 12 + 8 + 8 + 2 + 3 + 11 + 32 + 32 + 32)
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
// From vbrheadersdk:
|
|
|
|
// ========================================
|
|
|
|
// A Xing header may be present in the ancillary
|
|
|
|
// data field of the first frame of an mp3 bitstream
|
|
|
|
// The Xing header (optionally) contains
|
|
|
|
// frames total number of audio frames in the bitstream
|
|
|
|
// bytes total number of bytes in the bitstream
|
|
|
|
// toc table of contents
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
// toc (table of contents) gives seek points
|
|
|
|
// for random access
|
|
|
|
// the ith entry determines the seek point for
|
|
|
|
// i-percent duration
|
|
|
|
// seek point in bytes = (toc[i]/256.0) * total_bitstream_bytes
|
|
|
|
// e.g. half duration seek point = (toc[50]/256.0) * total_bitstream_bytes
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
#define FRAMES_FLAG 0x0001
|
|
|
|
#define BYTES_FLAG 0x0002
|
|
|
|
#define TOC_FLAG 0x0004
|
|
|
|
#define VBR_SCALE_FLAG 0x0008
|
2006-09-02 16:09:20 +00:00
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
//Scan file quickly
|
|
|
|
- (BOOL)scanFile
|
2006-09-02 16:09:20 +00:00
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
struct mad_stream stream;
|
|
|
|
struct mad_frame frame;
|
2006-09-02 16:09:20 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
int framesDecoded = 0;
|
|
|
|
int bytesToRead, bytesRemaining;
|
|
|
|
int samplesPerMPEGFrame = 0;
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
int id3_length = 0;
|
2008-02-20 01:09:15 +00:00
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
mad_stream_init (&stream);
|
|
|
|
mad_frame_init (&frame);
|
|
|
|
|
2007-03-03 00:33:36 +00:00
|
|
|
[_source seek:0 whence:SEEK_END];
|
|
|
|
_fileSize = [_source tell];
|
|
|
|
[_source seek:0 whence:SEEK_SET];
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
for (;;) {
|
|
|
|
if(NULL == stream.buffer || MAD_ERROR_BUFLEN == stream.error) {
|
|
|
|
if(stream.next_frame) {
|
|
|
|
bytesRemaining = stream.bufend - stream.next_frame;
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
memmove(_inputBuffer, stream.next_frame, bytesRemaining);
|
|
|
|
|
|
|
|
bytesToRead = INPUT_BUFFER_SIZE - bytesRemaining;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
bytesToRead = INPUT_BUFFER_SIZE,
|
|
|
|
bytesRemaining = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read raw bytes from the MP3 file
|
|
|
|
int bytesRead = [_source read:_inputBuffer + bytesRemaining amount: bytesToRead];
|
|
|
|
|
|
|
|
if (bytesRead == 0)
|
|
|
|
{
|
|
|
|
memset(_inputBuffer + bytesRemaining + bytesRead, 0, MAD_BUFFER_GUARD);
|
|
|
|
bytesRead += MAD_BUFFER_GUARD;
|
|
|
|
inputEOF = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
mad_stream_buffer(&stream, _inputBuffer, bytesRead + bytesRemaining);
|
|
|
|
stream.error = MAD_ERROR_NONE;
|
|
|
|
}
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
if (mad_frame_decode(&frame, &stream) == -1)
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
if (MAD_RECOVERABLE(stream.error))
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
// Prevent ID3 tags from reporting recoverable frame errors
|
|
|
|
const uint8_t *buffer = stream.this_frame;
|
|
|
|
unsigned buflen = stream.bufend - stream.this_frame;
|
|
|
|
|
|
|
|
if(10 <= buflen && 0x49 == buffer[0] && 0x44 == buffer[1] && 0x33 == buffer[2]) {
|
|
|
|
id3_length = (((buffer[6] & 0x7F) << (3 * 7)) | ((buffer[7] & 0x7F) << (2 * 7)) |
|
|
|
|
((buffer[8] & 0x7F) << (1 * 7)) | ((buffer[9] & 0x7F) << (0 * 7)));
|
|
|
|
|
|
|
|
// Add 10 bytes for ID3 header
|
|
|
|
id3_length += 10;
|
|
|
|
|
|
|
|
mad_stream_skip(&stream, id3_length);
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
else if (stream.error == MAD_ERROR_BUFLEN && inputEOF)
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
break;
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
else if (stream.error == MAD_ERROR_BUFLEN)
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
continue;
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
else
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"Unrecoverable error: %s", mad_stream_errorstr(&stream));
|
2006-06-19 00:39:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
framesDecoded++;
|
|
|
|
|
|
|
|
if (framesDecoded == 1)
|
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
sampleRate = frame.header.samplerate;
|
2007-10-18 02:33:12 +00:00
|
|
|
channels = MAD_NCHANNELS(&frame.header);
|
|
|
|
|
|
|
|
if(MAD_FLAG_LSF_EXT & frame.header.flags || MAD_FLAG_MPEG_2_5_EXT & frame.header.flags) {
|
|
|
|
switch(frame.header.layer) {
|
|
|
|
case MAD_LAYER_I: samplesPerMPEGFrame = 384; break;
|
|
|
|
case MAD_LAYER_II: samplesPerMPEGFrame = 1152; break;
|
|
|
|
case MAD_LAYER_III: samplesPerMPEGFrame = 576; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch(frame.header.layer) {
|
|
|
|
case MAD_LAYER_I: samplesPerMPEGFrame = 384; break;
|
|
|
|
case MAD_LAYER_II: samplesPerMPEGFrame = 1152; break;
|
|
|
|
case MAD_LAYER_III: samplesPerMPEGFrame = 1152; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned ancillaryBitsRemaining = stream.anc_bitlen;
|
|
|
|
|
|
|
|
if(32 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
uint32_t magic = mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
ancillaryBitsRemaining -= 32;
|
|
|
|
|
|
|
|
if('Xing' == magic || 'Info' == magic) {
|
|
|
|
unsigned i;
|
|
|
|
uint32_t flags = 0, frames = 0, bytes = 0, vbrScale = 0;
|
|
|
|
|
|
|
|
if(32 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
flags = mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
ancillaryBitsRemaining -= 32;
|
|
|
|
|
|
|
|
// 4 byte value containing total frames
|
|
|
|
if(FRAMES_FLAG & flags) {
|
|
|
|
if(32 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
frames = mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
ancillaryBitsRemaining -= 32;
|
|
|
|
|
|
|
|
// Determine number of samples, discounting encoder delay and padding
|
|
|
|
// Our concept of a frame is the same as CoreAudio's- one sample across all channels
|
2007-11-24 20:16:27 +00:00
|
|
|
totalFrames = frames * samplesPerMPEGFrame;
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"TOTAL READ FROM XING");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4 byte value containing total bytes
|
|
|
|
if(BYTES_FLAG & flags) {
|
|
|
|
if(32 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bytes = mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
ancillaryBitsRemaining -= 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 100 bytes containing TOC information
|
|
|
|
if(TOC_FLAG & flags) {
|
|
|
|
if(8 * 100 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(i = 0; i < 100; ++i)
|
|
|
|
/*_xingTOC[i] = */ mad_bit_read(&stream.anc_ptr, 8);
|
|
|
|
|
|
|
|
ancillaryBitsRemaining -= (8* 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4 byte value indicating encoded vbr scale
|
|
|
|
if(VBR_SCALE_FLAG & flags) {
|
|
|
|
if(32 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
vbrScale = mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
ancillaryBitsRemaining -= 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
framesDecoded = frames;
|
|
|
|
|
|
|
|
_foundXingHeader = YES;
|
|
|
|
|
|
|
|
// Loook for the LAME header next
|
|
|
|
// http://gabriel.mp3-tech.org/mp3infotag.html
|
|
|
|
if(32 > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
magic = mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
|
|
|
|
ancillaryBitsRemaining -= 32;
|
|
|
|
|
|
|
|
if('LAME' == magic) {
|
|
|
|
|
|
|
|
if(LAME_HEADER_SIZE > ancillaryBitsRemaining)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*unsigned char versionString [5 + 1];
|
|
|
|
memset(versionString, 0, 6);*/
|
|
|
|
|
|
|
|
for(i = 0; i < 5; ++i)
|
|
|
|
/*versionString[i] =*/ mad_bit_read(&stream.anc_ptr, 8);
|
|
|
|
|
|
|
|
/*uint8_t infoTagRevision =*/ mad_bit_read(&stream.anc_ptr, 4);
|
|
|
|
/*uint8_t vbrMethod =*/ mad_bit_read(&stream.anc_ptr, 4);
|
|
|
|
|
|
|
|
/*uint8_t lowpassFilterValue =*/ mad_bit_read(&stream.anc_ptr, 8);
|
|
|
|
|
|
|
|
/*float peakSignalAmplitude =*/ mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
/*uint16_t radioReplayGain =*/ mad_bit_read(&stream.anc_ptr, 16);
|
|
|
|
/*uint16_t audiophileReplayGain =*/ mad_bit_read(&stream.anc_ptr, 16);
|
|
|
|
|
|
|
|
/*uint8_t encodingFlags =*/ mad_bit_read(&stream.anc_ptr, 4);
|
|
|
|
/*uint8_t athType =*/ mad_bit_read(&stream.anc_ptr, 4);
|
|
|
|
|
|
|
|
/*uint8_t lameBitrate =*/ mad_bit_read(&stream.anc_ptr, 8);
|
|
|
|
|
|
|
|
_startPadding = mad_bit_read(&stream.anc_ptr, 12);
|
|
|
|
_endPadding = mad_bit_read(&stream.anc_ptr, 12);
|
|
|
|
|
|
|
|
_startPadding += 528 + 1; //MDCT/filterbank delay
|
2007-11-24 20:16:27 +00:00
|
|
|
totalFrames += 528 + 1;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
|
|
|
/*uint8_t misc =*/ mad_bit_read(&stream.anc_ptr, 8);
|
|
|
|
|
|
|
|
/*uint8_t mp3Gain =*/ mad_bit_read(&stream.anc_ptr, 8);
|
|
|
|
|
|
|
|
/*uint8_t unused =*/mad_bit_read(&stream.anc_ptr, 2);
|
|
|
|
/*uint8_t surroundInfo =*/ mad_bit_read(&stream.anc_ptr, 3);
|
|
|
|
/*uint16_t presetInfo =*/ mad_bit_read(&stream.anc_ptr, 11);
|
|
|
|
|
|
|
|
/*uint32_t musicGain =*/ mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
|
|
|
|
/*uint32_t musicCRC =*/ mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
|
|
|
|
/*uint32_t tagCRC =*/ mad_bit_read(&stream.anc_ptr, 32);
|
|
|
|
|
|
|
|
ancillaryBitsRemaining -= LAME_HEADER_SIZE;
|
|
|
|
|
|
|
|
_foundLAMEHeader = YES;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
totalFrames = (double)frame.header.samplerate * ((_fileSize - id3_length) / (frame.header.bitrate / 8.0));
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"Guestimating total samples");
|
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
break;
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
|
|
|
}
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2008-02-20 01:09:15 +00:00
|
|
|
bitrate = ((double)((_fileSize - id3_length)*8)/1000.0) * (sampleRate/(double)totalFrames);
|
2006-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
mad_frame_finish (&frame);
|
|
|
|
mad_stream_finish (&stream);
|
|
|
|
|
2007-03-04 00:17:05 +00:00
|
|
|
bitsPerSample = 16;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
bytesPerFrame = (bitsPerSample/8) * channels;
|
|
|
|
|
2007-03-03 00:33:36 +00:00
|
|
|
[_source seek:0 whence:SEEK_SET];
|
2007-11-24 20:16:27 +00:00
|
|
|
inputEOF = NO;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
NSLog(@"Mad properties: %@", [self properties]);
|
2007-10-18 02:33:12 +00:00
|
|
|
|
|
|
|
return YES;
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-03 00:33:36 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)source
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-03-03 00:33:36 +00:00
|
|
|
[source retain];
|
|
|
|
[_source release];
|
|
|
|
_source = source;
|
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
/* First the structures used by libmad must be initialized. */
|
|
|
|
mad_stream_init(&_stream);
|
|
|
|
mad_frame_init(&_frame);
|
|
|
|
mad_synth_init(&_synth);
|
2006-06-19 01:41:15 +00:00
|
|
|
|
2007-03-04 00:17:05 +00:00
|
|
|
_firstFrame = YES;
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"OPEN: %i", _firstFrame);
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
inputEOF = NO;
|
2007-03-04 00:17:05 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
if (![_source seekable])
|
|
|
|
{
|
|
|
|
//Decode the first frame to get the channels, samplerate, etc.
|
|
|
|
int r;
|
|
|
|
do {
|
|
|
|
r = [self decodeMPEGFrame];
|
2008-02-19 19:59:35 +00:00
|
|
|
NSLog(@"Decoding first frame: %i", r);
|
2007-11-24 20:16:27 +00:00
|
|
|
} while (r == 0);
|
|
|
|
|
|
|
|
return (r == -1 ? NO : YES);
|
2007-03-04 00:17:05 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
return [self scanFile];
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scale PCM data
|
|
|
|
*/
|
|
|
|
static inline signed int scale (mad_fixed_t sample)
|
|
|
|
{
|
|
|
|
BOOL hard_limit = YES;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
|
|
|
double scale = 1.0;
|
2006-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
/* hard-limit (clipping-prevention) */
|
|
|
|
if (hard_limit)
|
|
|
|
{
|
|
|
|
/* convert to double before computation, to avoid mad_fixed_t wrapping */
|
|
|
|
double x = mad_f_todouble(sample) * scale;
|
|
|
|
static const double k = 0.5; // -6dBFS
|
|
|
|
if (x > k)
|
|
|
|
{
|
|
|
|
x = tanh((x - k) / (1-k)) * (1-k) + k;
|
|
|
|
}
|
|
|
|
else if(x < -k)
|
|
|
|
{
|
|
|
|
x = tanh((x + k) / (1-k)) * (1-k) - k;
|
|
|
|
}
|
|
|
|
sample = x * (MAD_F_ONE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
sample *= scale;
|
|
|
|
|
|
|
|
int n_bits_to_loose = MAD_F_FRACBITS + 1 - 16;
|
|
|
|
|
|
|
|
/* round */
|
|
|
|
/* add half of the bits_to_loose range to round */
|
|
|
|
sample += (1L << (n_bits_to_loose - 1));
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
/* clip */
|
|
|
|
/* make sure we are between -1 and 1 */
|
|
|
|
if (sample >= MAD_F_ONE)
|
|
|
|
{
|
|
|
|
sample = MAD_F_ONE - 1;
|
|
|
|
}
|
|
|
|
else if (sample < -MAD_F_ONE)
|
|
|
|
{
|
|
|
|
sample = -MAD_F_ONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* quantize */
|
|
|
|
/*
|
|
|
|
* Turn our mad_fixed_t into an integer.
|
|
|
|
* Shift all but 16-bits of the fractional part
|
|
|
|
* off the right hand side and shift an extra place
|
|
|
|
* to get the sign bit.
|
|
|
|
*/
|
|
|
|
sample >>= n_bits_to_loose;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
return sample;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)writeOutput
|
|
|
|
{
|
2007-10-18 02:33:12 +00:00
|
|
|
unsigned int startingSample = 0;
|
|
|
|
unsigned int sampleCount = _synth.pcm.length;
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
//NSLog(@"Position: %li/%li", _framesDecoded, totalFrames);
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"<%i, %i>", _startPadding, _endPadding);
|
|
|
|
|
|
|
|
//NSLog(@"Counts: %i, %i", startingSample, sampleCount);
|
|
|
|
if (_foundLAMEHeader) {
|
2007-11-24 20:16:27 +00:00
|
|
|
if (_framesDecoded < _startPadding) {
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"Skipping start.");
|
2007-11-24 20:16:27 +00:00
|
|
|
startingSample = _startPadding - _framesDecoded;
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
if (_framesDecoded > totalFrames - _endPadding + startingSample) {
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"End of file. Not writing.");
|
|
|
|
return;
|
|
|
|
}
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
if (_framesDecoded + (sampleCount - startingSample) > totalFrames - _endPadding) {
|
|
|
|
sampleCount = totalFrames - _endPadding - _framesDecoded + startingSample;
|
|
|
|
//NSLog(@"End of file. %li", totalFrames - _endPadding - _framesDecoded);
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
if (startingSample > sampleCount) {
|
2007-11-24 20:16:27 +00:00
|
|
|
_framesDecoded += _synth.pcm.length;
|
2007-10-18 02:33:12 +00:00
|
|
|
//NSLog(@"Skipping entire sample");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//NSLog(@"Revised: %i, %i", startingSample, sampleCount);
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
_framesDecoded += _synth.pcm.length;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
_outputFrames = (sampleCount - startingSample);
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
if (_outputBuffer)
|
|
|
|
free(_outputBuffer);
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
_outputBuffer = (unsigned char *) malloc (_outputFrames * bytesPerFrame * sizeof (char));
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
unsigned int i, j;
|
|
|
|
unsigned int stride = channels * 2;
|
|
|
|
for (j = 0; j < channels; j++)
|
|
|
|
{
|
|
|
|
/* output sample(s) in 16-bit signed little-endian PCM */
|
|
|
|
mad_fixed_t const *channel = _synth.pcm.samples[j];
|
|
|
|
unsigned char *outputPtr = _outputBuffer + (j * 2);
|
|
|
|
|
|
|
|
for (i = startingSample; i < sampleCount; i++)
|
|
|
|
{
|
|
|
|
signed short sample = scale(channel[i]);
|
|
|
|
|
|
|
|
outputPtr[0] = sample>>8;
|
|
|
|
outputPtr[1] = sample & 0xff;
|
|
|
|
outputPtr += stride;
|
|
|
|
}
|
|
|
|
}
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
- (int)decodeMPEGFrame
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
if (_stream.buffer == NULL || _stream.error == MAD_ERROR_BUFLEN)
|
2007-10-18 02:33:12 +00:00
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
int inputToRead;
|
|
|
|
int inputRemaining;
|
|
|
|
|
|
|
|
if (_stream.next_frame != NULL)
|
|
|
|
{
|
|
|
|
inputRemaining = _stream.bufend - _stream.next_frame;
|
|
|
|
|
|
|
|
memmove(_inputBuffer, _stream.next_frame, inputRemaining);
|
|
|
|
|
|
|
|
inputToRead = INPUT_BUFFER_SIZE - inputRemaining;
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
else
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
inputToRead = INPUT_BUFFER_SIZE;
|
|
|
|
inputRemaining = 0;
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
int inputRead = [_source read:_inputBuffer + inputRemaining amount:INPUT_BUFFER_SIZE - inputRemaining];
|
|
|
|
if (inputRead == 0)
|
|
|
|
{
|
|
|
|
memset(_inputBuffer + inputRemaining + inputRead, 0, MAD_BUFFER_GUARD);
|
|
|
|
inputRead += MAD_BUFFER_GUARD;
|
|
|
|
inputEOF = YES;
|
2007-03-04 00:17:05 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
mad_stream_buffer(&_stream, _inputBuffer, inputRead + inputRemaining);
|
|
|
|
_stream.error = MAD_ERROR_NONE;
|
|
|
|
//NSLog(@"Read stream.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mad_frame_decode(&_frame, &_stream) == -1) {
|
|
|
|
if (MAD_RECOVERABLE (_stream.error))
|
2007-10-18 02:33:12 +00:00
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
const uint8_t *buffer = _stream.this_frame;
|
|
|
|
unsigned buflen = _stream.bufend - _stream.this_frame;
|
|
|
|
uint32_t id3_length = 0;
|
|
|
|
|
|
|
|
//No longer need ID3Tag framework
|
|
|
|
if(10 <= buflen && 0x49 == buffer[0] && 0x44 == buffer[1] && 0x33 == buffer[2]) {
|
|
|
|
id3_length = (((buffer[6] & 0x7F) << (3 * 7)) | ((buffer[7] & 0x7F) << (2 * 7)) |
|
|
|
|
((buffer[8] & 0x7F) << (1 * 7)) | ((buffer[9] & 0x7F) << (0 * 7)));
|
|
|
|
|
|
|
|
// Add 10 bytes for ID3 header
|
|
|
|
id3_length += 10;
|
|
|
|
|
|
|
|
mad_stream_skip(&_stream, id3_length);
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
NSLog(@"recoverable error");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (MAD_ERROR_BUFLEN == _stream.error && inputEOF)
|
|
|
|
{
|
|
|
|
NSLog(@"EOF");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
else if (MAD_ERROR_BUFLEN == _stream.error)
|
|
|
|
{
|
2008-02-19 19:59:35 +00:00
|
|
|
//NSLog(@"Bufferlen");
|
2007-11-24 20:16:27 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//NSLog(@"Unrecoverable stream error: %s", mad_stream_errorstr(&_stream));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//NSLog(@"Decoded buffer.");
|
|
|
|
mad_synth_frame (&_synth, &_frame);
|
|
|
|
//NSLog(@"first frame: %i", _firstFrame);
|
|
|
|
if (_firstFrame)
|
|
|
|
{
|
|
|
|
_firstFrame = NO;
|
|
|
|
|
|
|
|
if (![_source seekable]) {
|
|
|
|
sampleRate = _frame.header.samplerate;
|
|
|
|
channels = MAD_NCHANNELS(&_frame.header);
|
|
|
|
bitsPerSample = 16;
|
|
|
|
bytesPerFrame = (bitsPerSample/8) * channels;
|
|
|
|
|
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
}
|
|
|
|
//NSLog(@"FIRST FRAME!!! %i %i", _foundXingHeader, _foundLAMEHeader);
|
|
|
|
if (_foundXingHeader) {
|
|
|
|
//NSLog(@"Skipping xing header.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (int)readAudio:(void *)buffer frames:(UInt32)frames
|
|
|
|
{
|
|
|
|
int framesRead = 0;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
int framesRemaining = frames - framesRead;
|
|
|
|
int framesToCopy = (_outputFrames > framesRemaining ? framesRemaining : _outputFrames);
|
|
|
|
|
|
|
|
if (framesToCopy) {
|
|
|
|
memcpy(buffer + (framesRead * bytesPerFrame), _outputBuffer, framesToCopy*bytesPerFrame);
|
|
|
|
framesRead += framesToCopy;
|
|
|
|
|
|
|
|
if (framesToCopy != _outputFrames) {
|
|
|
|
memmove(_outputBuffer, _outputBuffer + (framesToCopy*bytesPerFrame), (_outputFrames - framesToCopy)*bytesPerFrame);
|
2007-10-18 02:33:12 +00:00
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
|
|
|
|
_outputFrames -= framesToCopy;
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
if (framesRead == frames)
|
|
|
|
break;
|
|
|
|
|
|
|
|
int r = [self decodeMPEGFrame];
|
2008-02-19 19:59:35 +00:00
|
|
|
//NSLog(@"Decoding frame: %i", r);
|
2007-11-24 20:16:27 +00:00
|
|
|
if (r == 0) //Recoverable error.
|
|
|
|
continue;
|
|
|
|
else if (r == -1) //Unrecoverable error
|
|
|
|
break;
|
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
[self writeOutput];
|
|
|
|
//NSLog(@"Wrote output");
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
|
|
|
|
//NSLog(@"Read: %i/%i", bytesRead, size);
|
2007-11-24 20:16:27 +00:00
|
|
|
return framesRead;
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
2007-10-13 07:09:46 +00:00
|
|
|
if (_source)
|
|
|
|
{
|
|
|
|
[_source close];
|
|
|
|
[_source release];
|
|
|
|
_source = nil;
|
|
|
|
}
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2007-10-13 07:51:42 +00:00
|
|
|
if (_outputBuffer)
|
|
|
|
{
|
|
|
|
free(_outputBuffer);
|
|
|
|
_outputBuffer = NULL;
|
|
|
|
}
|
2006-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
mad_synth_finish(&_synth);
|
|
|
|
mad_frame_finish(&_frame);
|
|
|
|
mad_stream_finish(&_stream);
|
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
- (long)seek:(long)frame
|
2006-06-19 00:39:41 +00:00
|
|
|
{
|
2007-11-24 20:16:27 +00:00
|
|
|
if (frame > totalFrames)
|
|
|
|
frame = totalFrames;
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
unsigned long new_position = ((double)frame / totalFrames) * _fileSize;
|
2007-03-03 00:33:36 +00:00
|
|
|
[_source seek:new_position whence:SEEK_SET];
|
2006-06-19 00:39:41 +00:00
|
|
|
|
2007-10-18 02:33:12 +00:00
|
|
|
mad_stream_buffer(&_stream, NULL, 0);
|
|
|
|
|
|
|
|
//Gapless busted after seek. Mp3 just doesn't have sample-accurate seeking. Maybe xing toc?
|
2007-11-24 20:16:27 +00:00
|
|
|
_framesDecoded = frame;
|
2007-10-18 02:33:12 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
return frame;
|
2006-06-19 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels],@"channels",
|
|
|
|
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
|
2007-11-24 20:16:27 +00:00
|
|
|
[NSNumber numberWithFloat:sampleRate],@"sampleRate",
|
2007-02-24 20:36:27 +00:00
|
|
|
[NSNumber numberWithInt:bitrate],@"bitrate",
|
2007-11-24 20:16:27 +00:00
|
|
|
[NSNumber numberWithLong:totalFrames],@"totalFrames",
|
2007-05-27 15:11:30 +00:00
|
|
|
[NSNumber numberWithBool:[_source seekable]], @"seekable",
|
2007-02-24 20:36:27 +00:00
|
|
|
@"big", @"endian",
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
2007-03-03 00:33:36 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
|
|
|
return [NSArray arrayWithObjects:@"mp3",nil];
|
|
|
|
}
|
|
|
|
|
2007-10-14 18:12:15 +00:00
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
2007-10-14 18:56:23 +00:00
|
|
|
return [NSArray arrayWithObjects:@"audio/mpeg", @"audio/x-mp3", nil];
|
2007-10-14 18:12:15 +00:00
|
|
|
}
|
|
|
|
|
2006-06-19 00:39:41 +00:00
|
|
|
@end
|
|
|
|
|