Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 223 lines (188 sloc) 5.105 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
#import "ALBuffer.h"
#if defined(TARGET_IPHONE_SIMULATOR) || defined(TARGET_OS_IPHONE)
#import <AVFoundation/AVFoundation.h>
#endif
#import <AudioToolbox/AudioToolbox.h>
#import <AudioToolbox/ExtendedAudioFile.h>

@interface ALBuffer (Private)
- (BOOL)open;
- (void)close;
@end

@implementation ALBuffer

@synthesize data;
@synthesize alcBuffer;

static NSMutableDictionary *bufferCache = 0x0;

+ (NSMutableDictionary *)bufferCache
{
if (!bufferCache) bufferCache = [[NSMutableDictionary dictionary] retain];
return bufferCache;
}

+ (ALBuffer *)bufferFromCacheForPath:(NSString *)path
{
NSMutableDictionary *cache = [self bufferCache];
id b = [cache objectForKey:path];
if (b) return b;
b = [[[ALBuffer alloc] init] autorelease];
[b loadPath:path];
[cache setObject:b forKey:path];
return b;
}

- (id)init
{
if (self = [super init])
{
[self open];
}

return self;
}

- (void)dealloc
{
[self close];
[self setData:nil];
[super dealloc];
}

- (ALuint)alcBuffer
{
return alcBuffer;
}

- (BOOL)open
{
if (!alcBuffer) alGenBuffers(1, &alcBuffer);
if ([self checkError]) return NO;
return YES;
}

- (void)close
{
    if (alcBuffer) alDeleteBuffers(1, &alcBuffer);
alcBuffer = 0x0;
}

- (BOOL)bindToData
{
alBufferData(alcBuffer, alFormat, [data bytes], [data length], sampleRate);
if ([self checkError]) return NO;
return YES;
}

- (ALenum)alFormat
{
return alFormat;
}

- (BOOL)loadPath:(NSString *)path
{
return [self loadURL:[NSURL fileURLWithPath:path]];
}

- (BOOL)loadURL:(NSURL *)url
{
AudioStreamBasicDescription outFormat;
AudioStreamBasicDescription inFormat;

OSStatus err = noErr;
SInt64 theFileLengthInFrames = 0;
UInt32 thePropertySize = sizeof(inFormat);
ExtAudioFileRef extRef = NULL;
void *theData = NULL;

err = ExtAudioFileOpenURL((CFURLRef)url, &extRef);

if (err)
{
[self setError:[NSString stringWithFormat:@"ExtAudioFileOpenURL FAILED, Error = %ld\n", err]];
goto Exit;
}

// Get the audio data format
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileDataFormat, &thePropertySize, &inFormat);
if (err)
{
printf("ExtAudioFileGetProperty(kExtAudioFileProperty_FileDataFormat) FAILED, Error = %ld\n", (long)err);
goto Exit;
}

if (inFormat.mChannelsPerFrame > 2)
{
[self setError:@"Unsupported Format, channel count is greater than stereo\n"];
goto Exit;
}

// Set the client format to 16 bit signed integer (native-endian) data
// Maintain the channel count and sample rate of the original source format
outFormat.mSampleRate = inFormat.mSampleRate;
outFormat.mChannelsPerFrame = inFormat.mChannelsPerFrame;
outFormat.mFormatID = kAudioFormatLinearPCM;
outFormat.mBytesPerPacket = 2 * outFormat.mChannelsPerFrame;
outFormat.mFramesPerPacket = 1;
outFormat.mBytesPerFrame = 2 * outFormat.mChannelsPerFrame;
outFormat.mBitsPerChannel = 16;
outFormat.mFormatFlags = kAudioFormatFlagsNativeEndian | kAudioFormatFlagIsPacked | kAudioFormatFlagIsSignedInteger;

// Set the desired client (output) data format
err = ExtAudioFileSetProperty(extRef, kExtAudioFileProperty_ClientDataFormat, sizeof(outFormat), &outFormat);
if (err)
{
[self setError:[NSString stringWithFormat:@"ExtAudioFileSetProperty %ld\n", err]];
goto Exit;
}

// Get the total frame count
thePropertySize = sizeof(theFileLengthInFrames);
err = ExtAudioFileGetProperty(extRef, kExtAudioFileProperty_FileLengthFrames, &thePropertySize, &theFileLengthInFrames);
if (err)
{
[self setError:[NSString stringWithFormat:@"ExtAudioFileGetProperty %ld\n", err]];
goto Exit;
}

// Read all the data into memory
UInt32 dataSize = theFileLengthInFrames * outFormat.mBytesPerFrame;
theData = malloc(dataSize);

if (theData)
{
AudioBufferList theDataBuffer;
theDataBuffer.mNumberBuffers = 1;
theDataBuffer.mBuffers[0].mDataByteSize = dataSize;
theDataBuffer.mBuffers[0].mNumberChannels = outFormat.mChannelsPerFrame;
theDataBuffer.mBuffers[0].mData = theData;

// Read the data into an AudioBufferList
err = ExtAudioFileRead(extRef, (UInt32*)&theFileLengthInFrames, &theDataBuffer);
if (err == noErr)
{
// success
alFormat = (outFormat.mChannelsPerFrame > 1) ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
}
else
{
// failure
free(theData);
theData = NULL; // make sure to return NULL
printf("ExtAudioFileRead FAILED, Error = %ld\n", (long)err);
goto Exit;
}
}

[self setData:[NSMutableData dataWithBytes:theData length:dataSize]];
sampleRate = inFormat.mSampleRate;

[self bindToData];
return YES;

Exit:
// Dispose the ExtAudioFileRef, it is no longer needed
if (extRef) ExtAudioFileDispose(extRef);
return NO;
}

- (int)sampleRate
{
ALint i;
alGetBufferi(alcBuffer, AL_FREQUENCY, &i);
[self checkError];
return i;
}

- (int)bitsPerSample
{
ALint i;
alGetBufferi(alcBuffer, AL_FREQUENCY, &i);
[self checkError];
return i;
}

- (int)channels
{
ALint i;
alGetBufferi(alcBuffer, AL_CHANNELS, &i);
[self checkError];
return i;
}

- (int)size
{
ALint i;
alGetBufferi(alcBuffer, AL_SIZE, &i);
[self checkError];
return i;
}

@end
Something went wrong with that request. Please try again.