Monday, June 29, 2009

Using SoundEngine from apple

Sound Engine 2.0.3 from
http://stormyprods.com/SoundEngine/

Barebones Usage Example

#import 

UInt32     bangSound;

...

// Setup sound engine. Run  it at 44Khz to match the sound files
SoundEngine_Initialize(44100);

// Assume the listener is in the center.
SoundEngine_SetListenerPosition(0.0, 0.0, 1.0);

SoundEngine_LoadEffect([[bundle pathForResource:@"bang" ofType:@"caf"] UTF8String], &bangSound);

...
ALuint sourceID1, sourceID2;
SoundEngine_PrimeEffect( bangSound, &sourceID1);
SoundEngine_StartEffect(sourceID1);
...
// Play a second instance of the same bang sound
SoundEngine_PrimeEffect( bangSound, &sourceID2);
SoundEngine_StartEffect(sourceID2);
...

// Call at program exit
SoundEngine_Teardown();  



I use it in a sound manager class.
It works. but kind of slow when the application start and loading the sounds.
I don't know why. But the AVAudioPlayer seems much faster.

Maybe it's because the application is tested on simulator, a real device might have different results.

the dictionary contains "object". we have to put integers into object eg. NSNumber

#import "SoundFacade.h"
#import "SoundEngine.h"
#define kNumberOfSounds 10

@implementation SoundFacade

static  NSMutableDictionary *soundDictionary;

+(void)open{
SoundEngine_Initialize(44100);
SoundEngine_SetListenerPosition(0.0, 0.0, 1.0);
soundDictionary = [[NSMutableDictionary alloc] initWithCapacity:kNumberOfSounds];

// add and prepare to play sounds
[SoundFacade addSound:@"GameName"];
// .....

}

+(void)addSound:(NSString*)name{
UInt32 sound;
SoundEngine_LoadEffect([[[NSBundle mainBundle] pathForResource:name ofType:@"wav"] UTF8String], &sound);
NSNumber *soundnum = [NSNumber numberWithUnsignedInt:sound];
[soundDictionary setObject:soundnum forKey:name];
NSLog(@"added %@.wav",name);
}

+(void)close{
for( NSNumber *soundnum in soundDictionary){
[soundnum release];
NSLog(@"Released one sound");
}
[soundDictionary release];
NSLog(@"sound-Dictionary released");

SoundEngine_Teardown();  
}

+(void)playMusic{
// use AVAudioPlayer to play background music
}

+(void)stopMusic{
// stop background music
};

+(void)playSound:(NSString*)name{
ALuint sourceID;
NSNumber *soundnum = [soundDictionary objectForKey:name];
SoundEngine_PrimeEffect( [soundnum unsignedIntValue], &sourceID);
SoundEngine_StartEffect(sourceID);
}

@end

No comments:

Post a Comment