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>
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
#import "CoreAudioDecoder.h"
|
2006-05-07 13:19:23 +00:00
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@interface CoreAudioDecoder (Private)
|
2006-05-13 04:50:06 +00:00
|
|
|
- (BOOL) readInfoFromExtAudioFileRef;
|
2006-05-07 13:19:23 +00:00
|
|
|
@end
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
@implementation CoreAudioDecoder
|
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
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-03-04 04:36:10 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)source;
|
2006-05-07 13:19:23 +00:00
|
|
|
{
|
|
|
|
OSStatus err;
|
2007-03-04 04:36:10 +00:00
|
|
|
FSRef ref;
|
2007-02-24 20:36:27 +00:00
|
|
|
|
2007-03-04 04:36:10 +00:00
|
|
|
NSURL *url = [source url];
|
2007-10-13 07:51:42 +00:00
|
|
|
[source close]; //There's no room for your kind around here!
|
|
|
|
|
2007-03-04 04:36:10 +00:00
|
|
|
if (![[url scheme] isEqualToString:@"file"])
|
2006-05-07 13:19:23 +00:00
|
|
|
return NO;
|
2007-03-04 04:37:36 +00:00
|
|
|
|
2006-05-29 22:02:59 +00:00
|
|
|
|
2006-05-13 04:50:06 +00:00
|
|
|
// Open the input file
|
2007-02-24 20:36:27 +00:00
|
|
|
err = FSPathMakeRef((const UInt8 *)[[url path] UTF8String], &ref, NULL);
|
2006-05-12 01:13:00 +00:00
|
|
|
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;
|
|
|
|
}
|
2007-03-04 04:36:10 +00:00
|
|
|
|
2006-05-13 04:50:06 +00:00
|
|
|
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
|
|
|
//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
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
length = ((double)totalFrames*1000.0)/frequency;
|
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
|
|
|
|
2007-03-04 04:36:10 +00:00
|
|
|
[self willChangeValueForKey:@"properties"];
|
|
|
|
[self didChangeValueForKey:@"properties"];
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
+ (NSArray *)fileTypes
|
|
|
|
{
|
|
|
|
OSStatus err;
|
|
|
|
UInt32 size;
|
|
|
|
NSArray *sAudioExtensions;
|
|
|
|
|
|
|
|
size = sizeof(sAudioExtensions);
|
|
|
|
err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllExtensions, 0, NULL, &size, &sAudioExtensions);
|
|
|
|
if(noErr != err) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [sAudioExtensions autorelease];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels],@"channels",
|
|
|
|
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
|
|
|
|
[NSNumber numberWithInt:bitrate],@"bitrate",
|
|
|
|
[NSNumber numberWithFloat:frequency],@"sampleRate",
|
|
|
|
[NSNumber numberWithDouble:length],@"length",
|
2007-05-27 15:11:30 +00:00
|
|
|
[NSNumber numberWithBool:YES], @"seekable",
|
2007-02-24 20:36:27 +00:00
|
|
|
@"big", @"endian",
|
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-07 13:19:23 +00:00
|
|
|
@end
|