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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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;
|
|
|
|
FILE *fd = caf->_inFd;
|
2006-05-13 04:50:06 +00:00
|
|
|
|
2006-05-12 00:06:50 +00:00
|
|
|
fseek(fd, inPosition, SEEK_SET);
|
2006-05-13 04:50:06 +00:00
|
|
|
|
2006-05-12 00:06:50 +00:00
|
|
|
*actualCount = fread(buffer, 1, requestCount, fd);
|
|
|
|
|
|
|
|
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;
|
|
|
|
FILE *fd = caf->_inFd;
|
2006-05-13 04:50:06 +00:00
|
|
|
|
|
|
|
|
2006-05-12 01:13:00 +00:00
|
|
|
if (caf->_fileSize != 0)
|
|
|
|
{
|
|
|
|
return caf->_fileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
long curPos;
|
|
|
|
|
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-13 04:50:06 +00:00
|
|
|
|
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;
|
|
|
|
|
|
|
|
#ifdef _USE_WRAPPER_
|
|
|
|
fclose(_inFd);
|
|
|
|
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-13 04:50:06 +00:00
|
|
|
|
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-12 00:06:50 +00:00
|
|
|
_inFd = fopen(filename, "r");
|
|
|
|
if (!_inFd)
|
|
|
|
{
|
|
|
|
NSLog(@"Error operning file: %s", filename);
|
2006-05-07 13:19:23 +00:00
|
|
|
return NO;
|
|
|
|
}
|
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-13 04:50:06 +00:00
|
|
|
err = AudioFileOpenWithCallbacks(self, readFunc, writeFunc, getSizeFunc, setSizeFunc, 0, &_audioID);
|
|
|
|
if(noErr != err)
|
|
|
|
{
|
|
|
|
FSRef ref;
|
|
|
|
fclose(_inFd);
|
|
|
|
|
|
|
|
err = FSPathMakeRef((const UInt8 *)filename, &ref, NULL);
|
|
|
|
if(noErr != err) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = AudioFileOpen(&ref, fsRdPerm, 0, &_audioID);
|
|
|
|
if(noErr != err) {
|
|
|
|
NSLog(@"Error opening AudioFile: %i", (char *)&err);
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// 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-13 04:50:06 +00:00
|
|
|
size = sizeof(totalFrames);
|
|
|
|
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-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;
|
|
|
|
}
|
2006-05-21 20:58:21 +00:00
|
|
|
NSLog(@"BITRATE: %lli %lli %lf", totalBytes, totalFrames, asbd.mSampleRate);
|
|
|
|
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?
|
|
|
|
bitRate = 0;
|
|
|
|
#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-05-13 04:50:06 +00:00
|
|
|
totalSize = totalFrames * channels * (bitsPerSample / 8);
|
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-12 00:06:50 +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-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-12 00:34:56 +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
|