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
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
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
|
|
|
|
|
2020-03-09 03:05:03 +00:00
|
|
|
static OSStatus readProc(void* clientData,
|
|
|
|
SInt64 position,
|
|
|
|
UInt32 requestCount,
|
|
|
|
void* buffer,
|
|
|
|
UInt32* actualCount)
|
|
|
|
{
|
|
|
|
NSObject* _handle = (__bridge NSObject *)(clientData);
|
|
|
|
CoreAudioDecoder * __unsafe_unretained pSelf = (id) _handle;
|
|
|
|
|
|
|
|
id<CogSource> source = pSelf->_audioSource;
|
|
|
|
|
|
|
|
if (position != pSelf->_lastPosition) {
|
2020-04-11 07:31:22 +00:00
|
|
|
[source seek:position whence:SEEK_SET];
|
2020-03-09 03:05:03 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 07:23:58 +00:00
|
|
|
size_t copyMax = 0;
|
|
|
|
size_t bytesRead = 0;
|
|
|
|
|
|
|
|
if ( requestCount )
|
|
|
|
bytesRead = [source read:(((uint8_t *)buffer) + copyMax) amount:requestCount];
|
|
|
|
|
2020-03-09 03:05:03 +00:00
|
|
|
pSelf->_lastPosition = position + bytesRead;
|
|
|
|
|
|
|
|
if(actualCount)
|
2020-03-09 07:23:58 +00:00
|
|
|
*actualCount = (UInt32) (copyMax + bytesRead);
|
2020-03-09 03:05:03 +00:00
|
|
|
|
|
|
|
return noErr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SInt64 getSizeProc(void* clientData) {
|
|
|
|
NSObject* _handle = (__bridge NSObject *)(clientData);
|
|
|
|
CoreAudioDecoder * __unsafe_unretained pSelf = (id) _handle;
|
|
|
|
|
|
|
|
id<CogSource> source = pSelf->_audioSource;
|
|
|
|
|
|
|
|
SInt64 size;
|
|
|
|
|
2020-04-11 07:31:22 +00:00
|
|
|
[source seek:0 whence:SEEK_END];
|
|
|
|
size = [source tell];
|
|
|
|
[source seek:pSelf->_lastPosition whence:SEEK_SET];
|
2020-03-09 03:05:03 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
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) {
|
2013-10-11 12:03:55 +00:00
|
|
|
DLog(@"Error closing ExtAudioFile");
|
2006-05-12 01:13:00 +00:00
|
|
|
}
|
2020-03-09 03:05:03 +00:00
|
|
|
|
|
|
|
err = AudioFileClose(_audioFile);
|
|
|
|
if(noErr != err) {
|
|
|
|
DLog(@"Error closing AudioFile");
|
|
|
|
}
|
|
|
|
|
|
|
|
_audioSource = nil;
|
2006-05-07 13:19:23 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 19:57:18 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
[self close];
|
|
|
|
}
|
|
|
|
|
2007-03-04 04:36:10 +00:00
|
|
|
- (BOOL)open:(id<CogSource>)source;
|
2006-05-07 13:19:23 +00:00
|
|
|
{
|
|
|
|
OSStatus err;
|
2020-03-09 03:05:03 +00:00
|
|
|
|
2020-04-11 07:31:22 +00:00
|
|
|
if (![source seekable])
|
|
|
|
return NO;
|
|
|
|
|
2020-03-09 03:05:03 +00:00
|
|
|
_audioSource = source;
|
|
|
|
_lastPosition = [source tell];
|
2020-03-09 07:23:58 +00:00
|
|
|
|
2020-03-09 03:05:03 +00:00
|
|
|
err = AudioFileOpenWithCallbacks((__bridge void *)self, readProc, 0, getSizeProc, 0, 0, &_audioFile);
|
|
|
|
if(noErr != err) {
|
|
|
|
ALog(@"Error opening callback interface to file: %d", err);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ExtAudioFileWrapAudioFileID(_audioFile, false, &_in);
|
2006-05-12 01:13:00 +00:00
|
|
|
if(noErr != err) {
|
2013-10-11 12:03:55 +00:00
|
|
|
ALog(@"Error opening file: %d", 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;
|
|
|
|
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
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
SInt64 total;
|
|
|
|
size = sizeof(total);
|
|
|
|
err = ExtAudioFileGetProperty(_in, kExtAudioFileProperty_FileLengthFrames, &size, &total);
|
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;
|
|
|
|
}
|
2007-11-24 20:16:27 +00:00
|
|
|
totalFrames = total;
|
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;
|
2013-10-05 21:15:09 +00:00
|
|
|
floatingPoint = NO;
|
|
|
|
|
2006-05-07 13:19:23 +00:00
|
|
|
// mBitsPerChannel will only be set for lpcm formats
|
|
|
|
if(0 == bitsPerSample) {
|
2013-10-05 21:15:09 +00:00
|
|
|
bitsPerSample = 32;
|
|
|
|
floatingPoint = YES;
|
2006-05-07 13:19:23 +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;
|
2013-10-05 21:15:09 +00:00
|
|
|
if (floatingPoint) {
|
|
|
|
result.mFormatFlags = kAudioFormatFlagIsFloat | kAudioFormatFlagIsPacked;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result.mFormatFlags = kAudioFormatFlagIsSignedInteger | kLinearPCMFormatFlagIsBigEndian;
|
|
|
|
}
|
2006-05-07 13:19:23 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
- (int) readAudio:(void *)buf frames:(UInt32)frames
|
2006-05-07 13:19:23 +00:00
|
|
|
{
|
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;
|
2007-11-24 20:16:27 +00:00
|
|
|
bufferList.mBuffers[0].mDataByteSize = frames * channels * (bitsPerSample/8);
|
2006-05-07 13:19:23 +00:00
|
|
|
|
|
|
|
// Read a chunk of PCM input (converted from whatever format)
|
2007-11-24 20:16:27 +00:00
|
|
|
frameCount = frames;
|
2006-05-13 04:50:06 +00:00
|
|
|
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
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
return frameCount;
|
2006-05-07 13:19:23 +00:00
|
|
|
}
|
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
- (long) seek:(long)frame
|
2006-05-07 13:19:23 +00:00
|
|
|
{
|
2006-05-13 04:50:06 +00:00
|
|
|
OSStatus err;
|
2006-05-29 22:02:59 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
err = ExtAudioFileSeek(_in, frame);
|
2006-05-13 04:50:06 +00:00
|
|
|
if(noErr != err) {
|
2007-11-24 20:16:27 +00:00
|
|
|
return -1;
|
2006-05-13 04:50:06 +00:00
|
|
|
}
|
2006-05-07 13:19:23 +00:00
|
|
|
|
2007-11-24 20:16:27 +00:00
|
|
|
return frame;
|
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;
|
|
|
|
}
|
|
|
|
|
2016-05-05 20:05:39 +00:00
|
|
|
return sAudioExtensions;
|
2007-02-24 20:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-10-14 19:18:20 +00:00
|
|
|
+ (NSArray *)mimeTypes
|
|
|
|
{
|
2020-03-09 07:23:58 +00:00
|
|
|
OSStatus err;
|
|
|
|
UInt32 size;
|
|
|
|
NSArray *sAudioMIMETypes;
|
|
|
|
|
|
|
|
size = sizeof(sAudioMIMETypes);
|
|
|
|
err = AudioFileGetGlobalInfo(kAudioFileGlobalInfo_AllMIMETypes, 0, NULL, &size, &sAudioMIMETypes);
|
|
|
|
if(noErr != err) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sAudioMIMETypes;
|
2007-10-14 19:18:20 +00:00
|
|
|
}
|
|
|
|
|
Implemented support for multiple decoders per file name extension, with a floating point priority control per interface. In the event that more than one input is registered to a given extension, and we match that extension, it will be passed off to an instance of the multi-decoder wrapper, which will try opening the file with all of the decoders in order of priority, until either one of them accepts it, or all of them have failed. This paves the way for adding a VGMSTREAM input, so I can give it a very low priority, since it has several formats that are verified by file name extension only. All current inputs have been given a priority of 1.0, except for CoreAudio, which was given a priority of 0.5, because it contains an MP3 and AC3 decoders that I'd rather not use if I don't have to.
2013-10-21 17:54:11 +00:00
|
|
|
+ (float)priority
|
|
|
|
{
|
2020-04-11 07:31:22 +00:00
|
|
|
return 0.5;
|
Implemented support for multiple decoders per file name extension, with a floating point priority control per interface. In the event that more than one input is registered to a given extension, and we match that extension, it will be passed off to an instance of the multi-decoder wrapper, which will try opening the file with all of the decoders in order of priority, until either one of them accepts it, or all of them have failed. This paves the way for adding a VGMSTREAM input, so I can give it a very low priority, since it has several formats that are verified by file name extension only. All current inputs have been given a priority of 1.0, except for CoreAudio, which was given a priority of 0.5, because it contains an MP3 and AC3 decoders that I'd rather not use if I don't have to.
2013-10-21 17:54:11 +00:00
|
|
|
}
|
|
|
|
|
2007-02-24 20:36:27 +00:00
|
|
|
- (NSDictionary *)properties
|
|
|
|
{
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[NSNumber numberWithInt:channels],@"channels",
|
|
|
|
[NSNumber numberWithInt:bitsPerSample],@"bitsPerSample",
|
2013-10-05 21:15:09 +00:00
|
|
|
[NSNumber numberWithBool:floatingPoint],@"floatingPoint",
|
2007-02-24 20:36:27 +00:00
|
|
|
[NSNumber numberWithInt:bitrate],@"bitrate",
|
|
|
|
[NSNumber numberWithFloat:frequency],@"sampleRate",
|
2007-11-24 20:16:27 +00:00
|
|
|
[NSNumber numberWithLong:totalFrames],@"totalFrames",
|
2020-04-11 07:31:22 +00:00
|
|
|
[NSNumber numberWithBool:YES], @"seekable",
|
2013-10-05 21:15:09 +00:00
|
|
|
floatingPoint ? @"host" : @"big", @"endian",
|
2007-02-24 20:36:27 +00:00
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-07 13:19:23 +00:00
|
|
|
@end
|