2005-06-02 18:16:43 +00:00
|
|
|
//
|
|
|
|
// MPEGFile.m
|
|
|
|
// zyVorbis
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 1/30/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 "MPEGFile.h"
|
|
|
|
|
|
|
|
|
|
|
|
@implementation MPEGFile
|
|
|
|
|
|
|
|
- (BOOL)open:(const char *)filename
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
// DBLog(@"Opening: %s!!!!", filename);
|
|
|
|
|
|
|
|
err = DecMPA_CreateUsingFile(&decoder, filename, DECMPA_VERSION);
|
|
|
|
if (err != DECMPA_OK)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
DecMPA_SetParam(decoder, DECMPA_PARAM_OUTPUT, DECMPA_OUTPUT_INT16);
|
|
|
|
|
|
|
|
long n;
|
|
|
|
DecMPA_DecodeNoData(decoder, &n);
|
|
|
|
// DBLog(@"Woot: %i", n);
|
|
|
|
DecMPA_OutputFormat outputFormat;
|
|
|
|
err = DecMPA_GetOutputFormat(decoder, &outputFormat);
|
2006-04-18 17:00:29 +00:00
|
|
|
|
2005-06-02 18:16:43 +00:00
|
|
|
if (err != DECMPA_OK)
|
|
|
|
return NO;
|
|
|
|
|
|
|
|
frequency = outputFormat.nFrequency;
|
|
|
|
channels = outputFormat.nChannels;
|
|
|
|
bitsPerSample = 16;
|
|
|
|
|
2006-04-18 17:00:29 +00:00
|
|
|
#ifdef __BIG_ENDIAN__
|
2005-06-02 18:16:43 +00:00
|
|
|
isBigEndian = YES;
|
2006-04-18 17:00:29 +00:00
|
|
|
#else
|
|
|
|
isBigEndian = NO;
|
|
|
|
#endif
|
2005-06-02 18:16:43 +00:00
|
|
|
|
|
|
|
long duration;
|
|
|
|
DecMPA_GetDuration(decoder, &duration);
|
|
|
|
totalSize = (long int)(duration*(double)frequency/1000.0*channels*bitsPerSample/8);
|
|
|
|
|
|
|
|
DecMPA_MPEGHeader mpegHeader;
|
|
|
|
DecMPA_GetMPEGHeader(decoder, &mpegHeader);
|
|
|
|
// int totalRate = mpegHeader.nBitRateKbps;
|
|
|
|
// int num = 0;
|
|
|
|
/*
|
|
|
|
while (DecMPA_DecodeNoData(decoder, &n) == DECMPA_OK)
|
|
|
|
{
|
|
|
|
DecMPA_GetMPEGHeader(decoder, &mpegHeader);
|
|
|
|
totalRate += mpegHeader.nBitRateKbps;
|
|
|
|
DBLog(@"%i %i %i", num, mpegHeader.nBitRateIndex, mpegHeader.nBitRateKbps);
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
err = DecMPA_GetMPEGHeader(decoder, &mpegHeader);
|
|
|
|
*/
|
|
|
|
bitRate = mpegHeader.nBitRateKbps;
|
|
|
|
// DBLog(@"Bitrate? %i", mpegHeader.);
|
|
|
|
// DBLog(@"Mpeg file opened.");
|
|
|
|
err = DecMPA_SeekToTime(decoder, 0);
|
|
|
|
if (err != DECMPA_OK)
|
|
|
|
return NO;
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)readInfo:(const char *)filename
|
|
|
|
{
|
2005-06-29 15:28:20 +00:00
|
|
|
[self open:filename];
|
|
|
|
|
|
|
|
return YES;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (int)fillBuffer:(void *)buf ofSize:(UInt32)size
|
|
|
|
{
|
|
|
|
long numread;
|
|
|
|
long total = 0;
|
|
|
|
|
|
|
|
DecMPA_Decode(decoder, &((char *)buf)[total], size - total, &numread);
|
|
|
|
while (total != size && numread > 0)
|
|
|
|
{
|
|
|
|
total += numread;
|
|
|
|
|
|
|
|
DecMPA_Decode(decoder, &((char *)buf)[total], size - total, &numread);
|
|
|
|
}
|
2006-04-18 17:00:29 +00:00
|
|
|
/*
|
|
|
|
int n;
|
2005-06-02 18:16:43 +00:00
|
|
|
for (n = 0; n < total/2; n++)
|
|
|
|
{
|
2006-04-18 17:00:29 +00:00
|
|
|
((UInt16 *)buf)[n] = CFSwapInt16BigToHost(((UInt16 *)buf)[n]);
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
2006-04-18 17:00:29 +00:00
|
|
|
*/
|
2005-06-02 18:16:43 +00:00
|
|
|
currentPosition += total;
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)close
|
|
|
|
{
|
|
|
|
if (decoder)
|
|
|
|
DecMPA_Destroy(decoder);
|
|
|
|
decoder = NULL;
|
|
|
|
}
|
|
|
|
|
2005-06-05 18:52:35 +00:00
|
|
|
- (double)seekToTime:(double)milliseconds
|
2005-06-02 18:16:43 +00:00
|
|
|
{
|
|
|
|
DecMPA_SeekToTime(decoder, (unsigned long)milliseconds);
|
2005-06-06 17:47:29 +00:00
|
|
|
|
2005-06-05 18:52:35 +00:00
|
|
|
return milliseconds;
|
2005-06-02 18:16:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|