Permalink
Browse files

classes

  • Loading branch information...
Steve Dekorte Steve Dekorte
Steve Dekorte authored and Steve Dekorte committed Aug 21, 2009
1 parent 672d36f commit 1e1fd339669ebdd0c7f1e009b76a7e7f2ef13eaa
Showing with 972 additions and 0 deletions.
  1. +27 −0 ALBuffer.h
  2. +226 −0 ALBuffer.m
  3. +25 −0 ALContext.h
  4. +79 −0 ALContext.m
  5. +19 −0 ALDevice.h
  6. +61 −0 ALDevice.m
  7. +22 −0 ALListener.h
  8. +57 −0 ALListener.m
  9. +29 −0 ALObject.h
  10. +52 −0 ALObject.m
  11. +48 −0 ALSource.h
  12. +222 −0 ALSource.m
  13. +24 −0 ALState.h
  14. +72 −0 ALState.m
  15. +9 −0 README
View
@@ -0,0 +1,27 @@
+#import "ALObject.h"
+
+@interface ALBuffer : ALObject
+{
+ NSData *data;
+ ALenum alFormat;
+ int sampleRate;
+ ALuint alcBuffer;
+}
+
++ bufferFromCacheForPath:(NSString *)path;
+
+@property (nonatomic, assign) NSData *data;
+@property (nonatomic, assign) ALuint alcBuffer;
+
+- (ALuint)alcBuffer;
+- (ALenum)alFormat;
+- (int)sampleRate;
+- (int)bitsPerSample;
+- (int)channels;
+- (int)size;
+
+// ugly - move to a sound file object
+- (BOOL)loadURL:(NSURL *)url;
+- (BOOL)loadPath:(NSString *)path;
+
+@end
View
@@ -0,0 +1,226 @@
+#import "ALBuffer.h"
+#import <AVFoundation/AVFoundation.h>
+#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;
+}
+
++ 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
+{
+ //NSLog(@"loadPath: %@\n", 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", 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", 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;
+}
+
+//alBufferi(alcBuffer, AL_FREQUENCY, iFreq);
+
+
+- (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
View
@@ -0,0 +1,25 @@
+//metadoc ALContext copyright Steve Dekorte 2009
+//metadoc ALContext license BSD revised
+
+#import "ALObject.h"
+#import "ALDevice.h"
+#import "ALListener.h"
+#import "ALSource.h"
+
+@interface ALContext : ALObject
+{
+ ALDevice *device;
+ ALListener *listener;
+ ALCcontext *alcContext;
+}
+
++ defaultContext;
+
+@property (nonatomic, assign) ALDevice *device;
+@property (nonatomic, assign) ALListener *listener;
+
+- (void)open;
+- (void)makeCurrent;
+- (void)close;
+
+@end
View
@@ -0,0 +1,79 @@
+#import "ALContext.h"
+
+@implementation ALContext
+
+@synthesize device;
+@synthesize listener;
+//@synthesize sources;
+
++ defaultContext
+{
+ ALContext *c = [[[ALContext alloc] init] autorelease];
+ [c open];
+ return c;
+}
+
+- (id)init
+{
+ if (self = [super init])
+ {
+ self.listener = [[[ALListener alloc] init] autorelease];
+ [listener setContext:self];
+ }
+
+ return self;
+}
+
+- (void)dealloc
+{
+ [self close];
+ [self setDevice:nil];
+ [self setListener:nil];
+ [super dealloc];
+}
+
+- (void)process
+{
+ alcProcessContext(alcContext);
+ [self checkError];
+}
+
+- (void)suspend
+{
+ alcSuspendContext(alcContext);
+ [self checkError];
+}
+
+- (void)open
+{
+ if (!device)
+ {
+ id d = [ALDevice defaultDevice];
+ [d retain];
+ [self setDevice:d];
+ }
+
+ alcContext = alcCreateContext([device alcDevice], 0);
+ [self checkError];
+ if (alcContext) [self makeCurrent];
+ [self process];
+}
+
+- (void)makeCurrent
+{
+ if (alcContext) alcMakeContextCurrent(alcContext);
+}
+
+- (void)close
+{
+ if (alcContext) alcDestroyContext(alcContext);
+ alcContext = NULL;
+ [device close];
+}
+
+- (ALCdevice *)alcDevice
+{
+ return alcGetContextsDevice(alcContext);
+}
+
+@end
View
@@ -0,0 +1,19 @@
+//metadoc ALDevice copyright Steve Dekorte 2009
+//metadoc ALDevice license BSD revised
+
+#import "ALObject.h"
+
+@interface ALDevice : ALObject
+{
+ ALCdevice *alcDevice;
+}
+
++ defaultDevice;
+
+- (void)openDefault;
+- (void)close;
+
+- (ALCdevice *)alcDevice;
+- newContext;
+
+@end
Oops, something went wrong.

0 comments on commit 1e1fd33

Please sign in to comment.