cog/Audio/Decoders/CoreAudioFile.m

325 lines
7.6 KiB
Matlab
Raw Normal View History

2006-05-07 13:19:23 +00:00
/*
* $Id$
*
* Copyright (C) 2006 Stephen F. Booth <me@sbooth.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
2006-05-29 22:02:59 +00:00
#include <unistd.h>
2006-05-07 13:19:23 +00:00
#import "CoreAudioFile.h"
@interface CoreAudioFile (Private)
2006-05-13 04:50:06 +00:00
- (BOOL) readInfoFromExtAudioFileRef;
2006-05-07 13:19:23 +00:00
@end
@implementation CoreAudioFile
2006-05-13 04:50:06 +00:00
#ifdef _USE_WRAPPER_
2006-05-12 00:06:50 +00:00
OSStatus readFunc(void * inRefCon, SInt64 inPosition, ByteCount requestCount, void *buffer, ByteCount* actualCount)
{
2006-05-12 01:13:00 +00:00
CoreAudioFile *caf = (CoreAudioFile *)inRefCon;
2006-05-29 22:02:59 +00:00
int fd = caf->_inFd;
2006-05-13 04:50:06 +00:00
2006-05-29 22:02:59 +00:00
// fseek(fd, inPosition, SEEK_SET);
// NSLog(@"Requesting %u", requestCount);
// NSLog(@"Currently at %lli", inPosition);
2006-05-13 04:50:06 +00:00
2006-05-29 22:02:59 +00:00
*actualCount = pread(fd, buffer, requestCount, inPosition+caf->_startOffset);
2006-05-12 00:06:50 +00:00
if (*actualCount <= 0)
2006-05-13 04:50:06 +00:00
{
2006-05-12 00:06:50 +00:00
return -1000; //Error?
2006-05-13 04:50:06 +00:00
}
2006-05-12 00:06:50 +00:00
return noErr;
}
SInt64 getSizeFunc(void *inRefCon)
{
2006-05-12 01:13:00 +00:00
CoreAudioFile *caf = (CoreAudioFile *)inRefCon;
2006-05-29 22:02:59 +00:00
int fd = caf->_inFd;
2006-05-12 01:13:00 +00:00
if (caf->_fileSize != 0)
{
return caf->_fileSize;
}
2006-05-29 22:02:59 +00:00
/* long curPos;
2006-05-12 01:13:00 +00:00
2006-05-12 00:06:50 +00:00
curPos = ftell(fd);
fseek(fd, 0, SEEK_END);
2006-05-13 04:50:06 +00:00
2006-05-12 01:13:00 +00:00
caf->_fileSize = ftell(fd);
2006-05-12 00:06:50 +00:00
fseek(fd, curPos, SEEK_SET);
2006-05-29 22:02:59 +00:00
*/
caf->_fileSize = lseek(fd, 0, SEEK_END) - caf->_startOffset;
NSLog(@"SIZE at %lli", caf->_fileSize);
NSLog(@"ERROR: %i = %i %i %i", errno, EBADF, ESPIPE, EINVAL);
2006-05-12 01:13:00 +00:00
return caf->_fileSize;
2006-05-12 00:06:50 +00:00
}
OSStatus setSizeFunc(void * inRefCon, SInt64 inSize)
{
2006-05-13 04:50:06 +00:00
NSLog(@"setsize FUNC");
2006-05-12 00:06:50 +00:00
return -1000; //Not supported at the moment
}
OSStatus writeFunc(void * inRefCon, SInt64 inPosition, ByteCount requestCount, const void *buffer, ByteCount* actualCount)
{
2006-05-13 04:50:06 +00:00
NSLog(@"WRITE FUNC");
2006-05-12 00:06:50 +00:00
return -1000; //Not supported at the moment
}
2006-05-12 01:13:00 +00:00
#endif
2006-05-12 00:06:50 +00:00
2006-05-07 13:19:23 +00:00
- (BOOL) open:(const char *)filename
{
2006-05-12 01:13:00 +00:00
return [self readInfo:filename];
2006-05-07 13:19:23 +00:00
}
- (void) close
{
2006-05-13 04:50:06 +00:00
OSStatus err;
2006-05-29 22:02:59 +00:00
2006-05-13 04:50:06 +00:00
#ifdef _USE_WRAPPER_
2006-05-29 22:02:59 +00:00
if (_inFd)
close(_inFd);
2006-05-13 04:50:06 +00:00
AudioFileClose(_audioID);
#endif
2006-05-12 01:13:00 +00:00
2006-05-13 04:50:06 +00:00
err = ExtAudioFileDispose(_in);
if(noErr != err) {
NSLog(@"Error closing ExtAudioFile");
2006-05-12 01:13:00 +00:00
}
2006-05-07 13:19:23 +00:00
}
- (BOOL) readInfo:(const char *)filename
{
OSStatus err;
2006-05-29 22:02:59 +00:00
AudioFileTypeID type = 0;
NSString *ext;
2006-05-13 16:50:52 +00:00
#ifdef _USE_WRAPPER_
2006-05-12 01:13:00 +00:00
// Open the input file
2006-05-29 22:02:59 +00:00
_inFd = open(filename, O_RDONLY, 0777);
if (_inFd < 0)
2006-05-12 00:06:50 +00:00
{
NSLog(@"Error operning file: %s", filename);
2006-05-07 13:19:23 +00:00
return NO;
}
2006-05-29 22:02:59 +00:00
_startOffset = 0;
2006-05-12 01:13:00 +00:00
2006-05-29 22:02:59 +00:00
ext = [[NSString stringWithCString:filename encoding:NSASCIIStringEncoding] pathExtension];
//Find first sync frame for MP3
if([ext caseInsensitiveCompare:@"mp3"] == NSOrderedSame) {
size_t bytesRead;
uint8_t buf[2];
type = kAudioFileMP3Type;
for(;;) {
bytesRead = read(_inFd, buf, 2);
if(2 != bytesRead) {
NSLog(@"Error finding mp3 sync frame");
close(_inFd);
return NO;
}
// found some kind of data
if(0x00 != buf[0] || 0x00 != buf[1]) {
_startOffset = lseek(_inFd, 0, SEEK_CUR) - 2;
NSLog(@"Found sync frame at: %llx", _startOffset);
break;
}
}
}
else if([ext caseInsensitiveCompare:@"aac"] == NSOrderedSame) {
type = kAudioFileAAC_ADTSType;
}
else if([ext caseInsensitiveCompare:@"m4a"] == NSOrderedSame) {
type = kAudioFileM4AType;
}
else if([ext caseInsensitiveCompare:@"mp4"] == NSOrderedSame) {
type = kAudioFileMPEG4Type;
}
2006-05-12 01:13:00 +00:00
//Using callbacks with fopen, ftell, fseek, fclose, because the default pread hangs when accessing the same file from multiple threads.
2006-05-29 22:02:59 +00:00
err = AudioFileOpenWithCallbacks(self, readFunc, writeFunc, getSizeFunc, setSizeFunc, type, &_audioID);
2006-05-13 04:50:06 +00:00
if(noErr != err)
{
2006-05-29 22:02:59 +00:00
NSLog(@"Error opening with callbacks, falling back: %s", (char *)&err);
2006-05-13 04:50:06 +00:00
FSRef ref;
2006-05-29 22:02:59 +00:00
close(_inFd);
_inFd = 0;
2006-05-13 04:50:06 +00:00
err = FSPathMakeRef((const UInt8 *)filename, &ref, NULL);
if(noErr != err) {
return NO;
}
2006-05-29 22:02:59 +00:00
err = AudioFileOpen(&ref, fsRdPerm, type, &_audioID);
2006-05-13 04:50:06 +00:00
if(noErr != err) {
2006-05-29 22:02:59 +00:00
NSLog(@"Error opening AudioFile: %s", (char *)&err);
2006-05-13 04:50:06 +00:00
return NO;
}
}
err = ExtAudioFileWrapAudioFileID(_audioID, NO, &_in);
2006-05-07 13:19:23 +00:00
if(noErr != err) {
return NO;
}
2006-05-13 04:50:06 +00:00
2006-05-12 01:13:00 +00:00
#else
2006-05-13 04:50:06 +00:00
FSRef ref;
2006-05-29 22:02:59 +00:00
2006-05-13 04:50:06 +00:00
// Open the input file
2006-05-12 01:13:00 +00:00
err = FSPathMakeRef((const UInt8 *)filename, &ref, NULL);
if(noErr != err) {
return NO;
}
2006-05-07 13:19:23 +00:00
2006-05-13 04:50:06 +00:00
err = ExtAudioFileOpen(&ref, &_in);
2006-05-12 01:13:00 +00:00
if(noErr != err) {
2006-05-29 22:02:59 +00:00
NSLog(@"Error opening file: %s", &err);
2006-05-12 01:13:00 +00:00
return NO;
}
2006-05-13 04:50:06 +00:00
#endif
return [self readInfoFromExtAudioFileRef];
2006-05-07 13:19:23 +00:00
}
2006-05-13 04:50:06 +00:00
- (BOOL) readInfoFromExtAudioFileRef
2006-05-07 13:19:23 +00:00
{
OSStatus err;
UInt32 size;
2006-05-13 04:50:06 +00:00
SInt64 totalFrames;
2006-05-07 13:19:23 +00:00
AudioStreamBasicDescription asbd;
2006-05-29 22:02:59 +00:00
2006-05-07 13:19:23 +00:00
// Get input file information
size = sizeof(asbd);
2006-05-13 04:50:06 +00:00
err = ExtAudioFileGetProperty(_in, kExtAudioFileProperty_FileDataFormat, &size, &asbd);
2006-05-07 13:19:23 +00:00
if(err != noErr) {
2006-05-13 04:50:06 +00:00
err = ExtAudioFileDispose(_in);
2006-05-07 13:19:23 +00:00
return NO;
}
2006-05-29 22:02:59 +00:00
2006-05-13 04:50:06 +00:00
size = sizeof(totalFrames);
2006-09-02 16:09:20 +00:00
err = ExtAudioFileGetProperty(_in, kExtAudioFileProperty_FileLengthFrames, &size, &totalFrames);
2006-05-12 00:06:50 +00:00
if(err != noErr) {
2006-05-13 04:50:06 +00:00
err = ExtAudioFileDispose(_in);
2006-05-12 00:06:50 +00:00
return NO;
}
2006-05-29 22:02:59 +00:00
2006-05-13 16:50:52 +00:00
#ifdef _USE_WRAPPER_
SInt64 totalBytes;
size = sizeof(totalBytes);
err = AudioFileGetProperty(_audioID, kAudioFilePropertyAudioDataByteCount, &size, &totalBytes);
if(err != noErr) {
[self close];
return NO;
}
NSLog(@"BITRATE: %lli %lli %lf", totalBytes, totalFrames, asbd.mSampleRate);
2006-06-19 00:39:41 +00:00
bitrate = round(((totalBytes*8.0)/((double)(totalFrames)/asbd.mSampleRate))/1000.0);
2006-05-13 16:50:52 +00:00
#else
//Is there a way to get bitrate with extAudioFile?
2006-06-19 00:39:41 +00:00
bitrate = 0;
2006-05-13 16:50:52 +00:00
#endif
2006-05-07 13:19:23 +00:00
// Set our properties
bitsPerSample = asbd.mBitsPerChannel;
channels = asbd.mChannelsPerFrame;
frequency = asbd.mSampleRate;
2006-05-12 00:34:56 +00:00
2006-05-07 13:19:23 +00:00
// mBitsPerChannel will only be set for lpcm formats
if(0 == bitsPerSample) {
bitsPerSample = 16;
}
2006-09-02 16:09:20 +00:00
totalSize = totalFrames * channels * (bitsPerSample / 8);
2006-05-29 22:02:59 +00:00
2006-05-12 00:06:50 +00:00
// Set output format
2006-05-13 04:50:06 +00:00
AudioStreamBasicDescription result;
2006-05-07 13:19:23 +00:00
bzero(&result, sizeof(AudioStreamBasicDescription));
result.mFormatID = kAudioFormatLinearPCM;
result.mFormatFlags = kAudioFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsBigEndian;
result.mSampleRate = frequency;
result.mChannelsPerFrame = channels;
result.mBitsPerChannel = bitsPerSample;
result.mBytesPerPacket = channels * (bitsPerSample / 8);
result.mFramesPerPacket = 1;
result.mBytesPerFrame = channels * (bitsPerSample / 8);
2006-05-29 22:02:59 +00:00
2006-05-13 04:50:06 +00:00
err = ExtAudioFileSetProperty(_in, kExtAudioFileProperty_ClientDataFormat, sizeof(result), &result);
if(noErr != err) {
err = ExtAudioFileDispose(_in);
2006-05-12 00:06:50 +00:00
return NO;
}
2006-05-07 13:19:23 +00:00
2006-05-13 04:50:06 +00:00
// Further properties
isBigEndian = YES;
isUnsigned = NO;
2006-05-29 22:02:59 +00:00
2006-05-07 13:19:23 +00:00
return YES;
}
- (int) fillBuffer:(void *)buf ofSize:(UInt32)size
{
2006-05-13 04:50:06 +00:00
OSStatus err;
AudioBufferList bufferList;
UInt32 frameCount;
// Set up the AudioBufferList
bufferList.mNumberBuffers = 1;
bufferList.mBuffers[0].mNumberChannels = channels;
bufferList.mBuffers[0].mData = buf;
bufferList.mBuffers[0].mDataByteSize = size;
2006-05-07 13:19:23 +00:00
// Read a chunk of PCM input (converted from whatever format)
2006-05-13 04:50:06 +00:00
frameCount = (size / (channels * (bitsPerSample / 8)));
err = ExtAudioFileRead(_in, &frameCount, &bufferList);
2006-05-07 13:19:23 +00:00
if(err != noErr) {
return 0;
}
2006-05-13 04:50:06 +00:00
return frameCount * (channels * (bitsPerSample / 8));
2006-05-07 13:19:23 +00:00
}
- (double) seekToTime:(double)milliseconds
{
2006-05-13 04:50:06 +00:00
OSStatus err;
2006-05-29 22:02:59 +00:00
2006-05-13 04:50:06 +00:00
err = ExtAudioFileSeek(_in, ((milliseconds / 1000.f) * frequency));
if(noErr != err) {
return -1.f;
}
2006-05-07 13:19:23 +00:00
2006-05-13 04:50:06 +00:00
return milliseconds;
2006-05-07 13:19:23 +00:00
}
@end