123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899 |
- #import "audio/mac/CDAudioManager.h"
- NSString * const kCDN_AudioManagerInitialised = @"kCDN_AudioManagerInitialised";
- @implementation CDAsynchInitialiser
- -(void) main {
- [super main];
- [CDAudioManager sharedManager];
- }
- @end
- @implementation CDLongAudioSource
- @synthesize audioSourcePlayer, audioSourceFilePath, delegate, backgroundMusic, paused;
- -(id) init {
- if ((self = [super init])) {
- state = kLAS_Init;
- volume = 1.0f;
- mute = NO;
- enabled_ = YES;
- paused = NO;
- }
- return self;
- }
- -(void) dealloc {
- CDLOGINFO(@"Denshion::CDLongAudioSource - deallocating %@", self);
- [audioSourcePlayer release];
- [audioSourceFilePath release];
- [super dealloc];
- }
- -(void) load:(NSString*) filePath {
-
- if (state == kLAS_Init || ![filePath isEqualToString:audioSourceFilePath]) {
- CDLOGINFO(@"Denshion::CDLongAudioSource - Loading new audio source %@",filePath);
-
- if (state != kLAS_Init) {
- [audioSourceFilePath release];
- [audioSourcePlayer release];
- }
- audioSourceFilePath = [filePath copy];
- NSError *error = nil;
- NSString *path = [CDUtilities fullPathFromRelativePath:audioSourceFilePath];
- audioSourcePlayer = [(CCAudioPlayer*)[CCAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];
- if (error == nil) {
- [audioSourcePlayer prepareToPlay];
- audioSourcePlayer.delegate = self;
- if (delegate && [delegate respondsToSelector:@selector(cdAudioSourceFileDidChange:)]) {
-
- [delegate cdAudioSourceFileDidChange:self];
- }
- } else {
- CDLOG(@"Denshion::CDLongAudioSource - Error initialising audio player: %@",error);
- }
- } else {
-
- [self pause];
- [self rewind];
- }
- audioSourcePlayer.volume = volume;
- audioSourcePlayer.numberOfLoops = numberOfLoops;
- state = kLAS_Loaded;
- }
- -(void) play {
- if (enabled_) {
- systemPaused = NO;
- paused = NO;
- [audioSourcePlayer play];
- } else {
- CDLOGINFO(@"Denshion::CDLongAudioSource long audio source didn't play because it is disabled");
- }
- }
- -(void) stop {
- paused = NO;
- [audioSourcePlayer stop];
- }
- -(void) pause {
- paused = YES;
- [audioSourcePlayer pause];
- }
- -(void) rewind {
- paused = NO;
- [audioSourcePlayer setCurrentTime:0];
- [audioSourcePlayer play];
- }
- -(void) resume {
- paused = NO;
- [audioSourcePlayer resume];
- }
- -(BOOL) isPlaying {
- if (state != kLAS_Init) {
- return [audioSourcePlayer isPlaying];
- } else {
- return NO;
- }
- }
- -(void) setVolume:(float) newVolume
- {
- volume = newVolume;
- if (state != kLAS_Init && !mute) {
- audioSourcePlayer.volume = newVolume;
- }
- }
- -(float) volume
- {
- return volume;
- }
- #pragma mark Audio Interrupt Protocol
- -(BOOL) mute
- {
- return mute;
- }
- -(void) setMute:(BOOL) muteValue
- {
- if (mute != muteValue) {
- if (mute) {
-
- audioSourcePlayer.volume = volume;
- } else {
- audioSourcePlayer.volume = 0.0f;
- }
- mute = muteValue;
- }
- }
- -(BOOL) enabled
- {
- return enabled_;
- }
- -(void) setEnabled:(BOOL)enabledValue
- {
- if (enabledValue != enabled_) {
- enabled_ = enabledValue;
- if (!enabled_) {
-
- [self pause];
- [self rewind];
- }
- }
- }
- -(NSInteger) numberOfLoops {
- return numberOfLoops;
- }
- -(void) setNumberOfLoops:(NSInteger) loopCount
- {
- audioSourcePlayer.numberOfLoops = loopCount;
- numberOfLoops = loopCount;
- }
- - (void)audioPlayerDidFinishPlaying:(CCAudioPlayer *)player successfully:(BOOL)flag {
- CDLOGINFO(@"Denshion::CDLongAudioSource - audio player finished");
- #if TARGET_IPHONE_SIMULATOR
- CDLOGINFO(@"Denshion::CDLongAudioSource - workaround for OpenAL clobbered audio issue");
-
-
-
-
-
- player.numberOfLoops = -1;
- player.volume = 0;
- [player play];
- #endif
- if (delegate && [delegate respondsToSelector:@selector(cdAudioSourceDidFinishPlaying:)]) {
- [delegate cdAudioSourceDidFinishPlaying:self];
- }
- }
- -(void)audioPlayerBeginInterruption:(CCAudioPlayer *)player {
- CDLOGINFO(@"Denshion::CDLongAudioSource - audio player interrupted");
- }
- -(void)audioPlayerEndInterruption:(CCAudioPlayer *)player {
- CDLOGINFO(@"Denshion::CDLongAudioSource - audio player resumed");
- if (self.backgroundMusic) {
-
-
-
- if([CDAudioManager sharedManager].willPlayBackgroundMusic) {
- [player play];
- }
- } else {
- [player play];
- }
- }
- @end
- @interface CDAudioManager (PrivateMethods)
- -(BOOL) audioSessionSetActive:(BOOL) active;
- -(BOOL) audioSessionSetCategory:(NSString*) category;
- -(void) badAlContextHandler;
- @end
- @implementation CDAudioManager
- #define BACKGROUND_MUSIC_CHANNEL kASC_Left
- @synthesize soundEngine, willPlayBackgroundMusic;
- static CDAudioManager *sharedManager;
- static tAudioManagerState _sharedManagerState = kAMStateUninitialised;
- static tAudioManagerMode configuredMode;
- static BOOL configured = FALSE;
- -(BOOL) audioSessionSetActive:(BOOL) active {
- NSError *activationError = nil;
- if ([[AVAudioSession sharedInstance] setActive:active error:&activationError]) {
- _audioSessionActive = active;
- CDLOGINFO(@"Denshion::CDAudioManager - Audio session set active %i succeeded", active);
- return YES;
- } else {
-
- CDLOG(@"Denshion::CDAudioManager - Audio session set active %i failed with error %@", active, activationError);
- return NO;
- }
- }
- -(BOOL) audioSessionSetCategory:(NSString*) category {
- NSError *categoryError = nil;
- if ([[AVAudioSession sharedInstance] setCategory:category error:&categoryError]) {
- CDLOGINFO(@"Denshion::CDAudioManager - Audio session set category %@ succeeded", category);
- return YES;
- } else {
-
- CDLOG(@"Denshion::CDAudioManager - Audio session set category %@ failed with error %@", category, categoryError);
- return NO;
- }
- }
- + (CDAudioManager *) sharedManager
- {
- @synchronized(self) {
- if (!sharedManager) {
- if (!configured) {
-
- configuredMode = kAMM_FxPlusMusicIfNoOtherAudio;
- }
- sharedManager = [[CDAudioManager alloc] init:configuredMode];
- _sharedManagerState = kAMStateInitialised;
- [[NSNotificationCenter defaultCenter] postNotificationName:kCDN_AudioManagerInitialised object:nil];
- }
- }
- return sharedManager;
- }
- + (tAudioManagerState) sharedManagerState {
- return _sharedManagerState;
- }
- + (void) initAsynchronously: (tAudioManagerMode) mode {
- @synchronized(self) {
- if (_sharedManagerState == kAMStateUninitialised) {
- _sharedManagerState = kAMStateInitialising;
- [CDAudioManager configure:mode];
- CDAsynchInitialiser *initOp = [[[CDAsynchInitialiser alloc] init] autorelease];
- NSOperationQueue *opQ = [[[NSOperationQueue alloc] init] autorelease];
- [opQ addOperation:initOp];
- }
- }
- }
- + (id) alloc
- {
- @synchronized(self) {
- NSAssert(sharedManager == nil, @"Attempted to allocate a second instance of a singleton.");
- return [super alloc];
- }
- return nil;
- }
- + (void) configure: (tAudioManagerMode) mode {
- configuredMode = mode;
- configured = TRUE;
- }
- -(BOOL) isOtherAudioPlaying {
- UInt32 isPlaying = 0;
- UInt32 varSize = sizeof(isPlaying);
- AudioSessionGetProperty (kAudioSessionProperty_OtherAudioIsPlaying, &varSize, &isPlaying);
- return (isPlaying != 0);
- }
- -(void) setMode:(tAudioManagerMode) mode {
- _mode = mode;
- switch (_mode) {
-
- case kAMM_FxOnly:
-
- CDLOGINFO(@"Denshion::CDAudioManager - Audio will be shared");
-
- _audioSessionCategory = AVAudioSessionCategoryAmbient;
- willPlayBackgroundMusic = NO;
- break;
-
- case kAMM_FxPlusMusic:
-
- CDLOGINFO(@"Denshion::CDAudioManager - Audio will be exclusive");
-
- _audioSessionCategory = AVAudioSessionCategorySoloAmbient;
- willPlayBackgroundMusic = YES;
- break;
-
- case kAMM_MediaPlayback:
-
- CDLOGINFO(@"Denshion::CDAudioManager - Media playback mode, audio will be exclusive");
-
- _audioSessionCategory = AVAudioSessionCategoryPlayback;
- willPlayBackgroundMusic = YES;
- break;
-
- case kAMM_PlayAndRecord:
-
- CDLOGINFO(@"Denshion::CDAudioManager - Play and record mode, audio will be exclusive");
-
- _audioSessionCategory = AVAudioSessionCategoryPlayAndRecord;
- willPlayBackgroundMusic = YES;
- break;
-
- default:
-
- if ([self isOtherAudioPlaying]) {
- CDLOGINFO(@"Denshion::CDAudioManager - Other audio is playing audio will be shared");
-
- _audioSessionCategory = AVAudioSessionCategoryAmbient;
- willPlayBackgroundMusic = NO;
- } else {
- CDLOGINFO(@"Denshion::CDAudioManager - Other audio is not playing audio will be exclusive");
-
- _audioSessionCategory = AVAudioSessionCategorySoloAmbient;
- willPlayBackgroundMusic = YES;
- }
-
- break;
- }
-
- [self audioSessionSetCategory:_audioSessionCategory];
-
- }
- - (void) badAlContextHandler {
- if (_interrupted && alcGetCurrentContext() == NULL) {
- CDLOG(@"Denshion::CDAudioManager - bad OpenAL context detected, attempting to resume audio session");
- [self audioSessionResumed];
- }
- }
- - (id) init: (tAudioManagerMode) mode {
- if ((self = [super init])) {
-
-
- AVAudioSession* session = [AVAudioSession sharedInstance];
- session.delegate = self;
-
- _mode = mode;
- backgroundMusicCompletionSelector = nil;
- _isObservingAppEvents = FALSE;
- _mute = NO;
- _resigned = NO;
- _interrupted = NO;
- enabled_ = YES;
- _audioSessionActive = NO;
- [self setMode:mode];
- soundEngine = [[CDSoundEngine alloc] init];
-
-
- audioSourceChannels = [[NSMutableArray alloc] init];
- CDLongAudioSource *leftChannel = [[CDLongAudioSource alloc] init];
- leftChannel.backgroundMusic = YES;
- CDLongAudioSource *rightChannel = [[CDLongAudioSource alloc] init];
- rightChannel.backgroundMusic = NO;
- [audioSourceChannels insertObject:leftChannel atIndex:kASC_Left];
- [audioSourceChannels insertObject:rightChannel atIndex:kASC_Right];
- [leftChannel release];
- [rightChannel release];
-
- backgroundMusic = [self audioSourceForChannel:BACKGROUND_MUSIC_CHANNEL];
- backgroundMusic.delegate = self;
-
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(badAlContextHandler) name:kCDN_BadAlContext object:nil];
- }
- return self;
- }
- -(void) dealloc {
- CDLOGINFO(@"Denshion::CDAudioManager - deallocating");
- [self stopBackgroundMusic];
- [soundEngine release];
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- [self audioSessionSetActive:NO];
- [audioSourceChannels release];
- [super dealloc];
- }
- -(CDLongAudioSource*) audioSourceForChannel:(tAudioSourceChannel) channel
- {
- return (CDLongAudioSource*)[audioSourceChannels objectAtIndex:channel];
- }
- -(CDLongAudioSource*) audioSourceLoad:(NSString*) filePath channel:(tAudioSourceChannel) channel
- {
- CDLongAudioSource *audioSource = [self audioSourceForChannel:channel];
- if (audioSource) {
- [audioSource load:filePath];
- }
- return audioSource;
- }
- -(BOOL) isBackgroundMusicPlaying {
- return [self.backgroundMusic isPlaying];
- }
- -(BOOL) isDeviceMuted {
- #if TARGET_IPHONE_SIMULATOR
-
- return NO;
- #else
- CFStringRef newAudioRoute;
- UInt32 propertySize = sizeof (CFStringRef);
-
- AudioSessionGetProperty (
- kAudioSessionProperty_AudioRoute,
- &propertySize,
- &newAudioRoute
- );
-
- if (newAudioRoute == NULL) {
-
- return YES;
- } else {
- CFComparisonResult newDeviceIsMuted = CFStringCompare (
- newAudioRoute,
- (CFStringRef) @"",
- 0
- );
-
- return (newDeviceIsMuted == kCFCompareEqualTo);
- }
- #endif
- }
- #pragma mark Audio Interrupt Protocol
- -(BOOL) mute {
- return _mute;
- }
- -(void) setMute:(BOOL) muteValue {
- if (muteValue != _mute) {
- _mute = muteValue;
- [soundEngine setMute:muteValue];
- for( CDLongAudioSource *audioSource in audioSourceChannels) {
- audioSource.mute = muteValue;
- }
- }
- }
- -(BOOL) enabled {
- return enabled_;
- }
- -(void) setEnabled:(BOOL) enabledValue {
- if (enabledValue != enabled_) {
- enabled_ = enabledValue;
- [soundEngine setEnabled:enabled_];
- for( CDLongAudioSource *audioSource in audioSourceChannels) {
- audioSource.enabled = enabled_;
- }
- }
- }
- -(CDLongAudioSource*) backgroundMusic
- {
- return backgroundMusic;
- }
- -(void) preloadBackgroundMusic:(NSString*) filePath
- {
- [self.backgroundMusic load:filePath];
- }
- -(void) playBackgroundMusic:(NSString*) filePath loop:(BOOL) loop
- {
- [self.backgroundMusic load:filePath];
- if (loop) {
- [self.backgroundMusic setNumberOfLoops:-1];
- } else {
- [self.backgroundMusic setNumberOfLoops:0];
- }
- if (!willPlayBackgroundMusic || _mute) {
- CDLOGINFO(@"Denshion::CDAudioManager - play bgm aborted because audio is not exclusive or sound is muted");
- return;
- }
- [self.backgroundMusic play];
- }
- -(void) stopBackgroundMusic
- {
- [self.backgroundMusic stop];
- }
- -(void) pauseBackgroundMusic
- {
- [self.backgroundMusic pause];
- }
- -(void) resumeBackgroundMusic
- {
- if (!willPlayBackgroundMusic || _mute) {
- CDLOGINFO(@"Denshion::CDAudioManager - resume bgm aborted because audio is not exclusive or sound is muted");
- return;
- }
-
- if (![self.backgroundMusic paused]) {
- return;
- }
-
- [self.backgroundMusic resume];
- }
- -(void) rewindBackgroundMusic
- {
- [self.backgroundMusic rewind];
- }
- -(void) setBackgroundMusicCompletionListener:(id) listener selector:(SEL) selector {
- backgroundMusicCompletionListener = listener;
- backgroundMusicCompletionSelector = selector;
- }
- -(void) setResignBehavior:(tAudioManagerResignBehavior) resignBehavior autoHandle:(BOOL) autoHandle {
- if (!_isObservingAppEvents && autoHandle) {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:@"UIApplicationWillResignActiveNotification" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidBecomeActive:) name:@"UIApplicationDidBecomeActiveNotification" object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillTerminate:) name:@"UIApplicationWillTerminateNotification" object:nil];
- _isObservingAppEvents = TRUE;
- }
- _resignBehavior = resignBehavior;
- }
- - (void) applicationWillResignActive {
- _resigned = YES;
-
-
- [self audioSessionSetCategory:AVAudioSessionCategoryAmbient];
-
- switch (_resignBehavior) {
-
- case kAMRBStopPlay:
-
- for( CDLongAudioSource *audioSource in audioSourceChannels) {
- if (audioSource.isPlaying) {
- audioSource->systemPaused = YES;
- audioSource->systemPauseLocation = audioSource.audioSourcePlayer.currentTime;
- [audioSource stop];
- } else {
-
-
- audioSource->systemPaused = NO;
- [audioSource stop];
- }
- }
- break;
-
- case kAMRBStop:
-
-
- for( CDLongAudioSource *audioSource in audioSourceChannels) {
- [audioSource stop];
- }
-
- default:
- break;
-
- }
- CDLOGINFO(@"Denshion::CDAudioManager - handled resign active");
- }
- - (void) applicationWillResignActive:(NSNotification *) notification
- {
- [self applicationWillResignActive];
- }
- - (void) applicationDidBecomeActive {
-
- if (_resigned) {
- _resigned = NO;
-
- [self setMode:_mode];
- switch (_resignBehavior) {
-
- case kAMRBStopPlay:
-
-
-
-
-
- if (self.willPlayBackgroundMusic) {
- for( CDLongAudioSource *audioSource in audioSourceChannels) {
- if (audioSource->systemPaused) {
- [audioSource resume];
- audioSource->systemPaused = NO;
- }
- }
- }
- break;
-
- default:
- break;
-
- }
- CDLOGINFO(@"Denshion::CDAudioManager - audio manager handled become active");
- }
- }
- - (void) applicationDidBecomeActive:(NSNotification *) notification
- {
- [self applicationDidBecomeActive];
- }
- - (void) applicationWillTerminate:(NSNotification *) notification
- {
- CDLOGINFO(@"Denshion::CDAudioManager - audio manager handling terminate");
- [self stopBackgroundMusic];
- }
- - (void) cdAudioSourceDidFinishPlaying:(CDLongAudioSource *) audioSource {
- CDLOGINFO(@"Denshion::CDAudioManager - audio manager got told background music finished");
- if (backgroundMusicCompletionSelector != nil) {
- [backgroundMusicCompletionListener performSelector:backgroundMusicCompletionSelector];
- }
- }
- -(void) beginInterruption {
- CDLOGINFO(@"Denshion::CDAudioManager - begin interruption");
- [self audioSessionInterrupted];
- }
- -(void) endInterruption {
- CDLOGINFO(@"Denshion::CDAudioManager - end interruption");
- [self audioSessionResumed];
- }
- #if __CC_PLATFORM_IOS >= 40000
- -(void) endInterruptionWithFlags:(NSUInteger)flags {
- CDLOGINFO(@"Denshion::CDAudioManager - interruption ended with flags %i",flags);
- if (flags == AVAudioSessionInterruptionFlags_ShouldResume) {
- [self audioSessionResumed];
- }
- }
- #endif
- -(void)audioSessionInterrupted
- {
- if (!_interrupted) {
- CDLOGINFO(@"Denshion::CDAudioManager - Audio session interrupted");
- _interrupted = YES;
-
- [self audioSessionSetActive:NO];
-
- if (alcGetCurrentContext() != NULL) {
- CDLOGINFO(@"Denshion::CDAudioManager - Setting OpenAL context to NULL");
- ALenum error = AL_NO_ERROR;
-
- alcMakeContextCurrent(NULL);
-
- if((error = alGetError()) != AL_NO_ERROR) {
- CDLOG(@"Denshion::CDAudioManager - Error making context current %x\n", error);
- }
- #pragma unused(error)
- }
- }
- }
- -(void)audioSessionResumed
- {
- if (_interrupted) {
- CDLOGINFO(@"Denshion::CDAudioManager - Audio session resumed");
- _interrupted = NO;
-
- BOOL activationResult = NO;
-
- activationResult = [self audioSessionSetActive:YES];
-
-
-
-
-
-
- if (!activationResult) {
- CDLOG(@"Denshion::CDAudioManager - Failure reactivating audio session, will try wait-try cycle");
- int activateCount = 0;
- while (!activationResult && activateCount < 10) {
- [NSThread sleepForTimeInterval:0.5];
- activationResult = [self audioSessionSetActive:YES];
- activateCount++;
- CDLOGINFO(@"Denshion::CDAudioManager - Reactivation attempt %i status = %i",activateCount,activationResult);
- }
- }
-
- if (alcGetCurrentContext() == NULL) {
- CDLOGINFO(@"Denshion::CDAudioManager - Restoring OpenAL context");
- ALenum error = AL_NO_ERROR;
-
- alcMakeContextCurrent([soundEngine openALContext]);
- if((error = alGetError()) != AL_NO_ERROR) {
- CDLOG(@"Denshion::CDAudioManager - Error making context current%x\n", error);
- }
- #pragma unused(error)
- }
- }
- }
- +(void) end {
- [sharedManager release];
- sharedManager = nil;
- }
- @end
- @implementation CDLongAudioSourceFader
- -(void) _setTargetProperty:(float) newVal {
- ((CDLongAudioSource*)target).volume = newVal;
- }
- -(float) _getTargetProperty {
- return ((CDLongAudioSource*)target).volume;
- }
- -(void) _stopTarget {
-
- [((CDLongAudioSource*)target) pause];
- }
- -(Class) _allowableType {
- return [CDLongAudioSource class];
- }
- @end
- @implementation CDBufferManager
- -(id) initWithEngine:(CDSoundEngine *) theSoundEngine {
- if ((self = [super init])) {
- soundEngine = theSoundEngine;
- loadedBuffers = [[NSMutableDictionary alloc] initWithCapacity:CD_BUFFERS_START];
- freedBuffers = [[NSMutableArray alloc] init];
- nextBufferId = 0;
- }
- return self;
- }
- -(void) dealloc {
- [loadedBuffers release];
- [freedBuffers release];
- [super dealloc];
- }
- -(int) bufferForFile:(NSString*) filePath create:(BOOL) create {
-
- NSNumber* soundId = (NSNumber*)[loadedBuffers objectForKey:filePath];
- if(soundId == nil)
- {
- if (create) {
- NSNumber* bufferId = nil;
-
- if ([freedBuffers count] > 0) {
- bufferId = [[[freedBuffers lastObject] retain] autorelease];
- [freedBuffers removeLastObject];
- CDLOGINFO(@"Denshion::CDBufferManager reusing buffer id %i",[bufferId intValue]);
- } else {
- bufferId = [[NSNumber alloc] initWithInt:nextBufferId];
- [bufferId autorelease];
- CDLOGINFO(@"Denshion::CDBufferManager generating new buffer id %i",[bufferId intValue]);
- nextBufferId++;
- }
-
- if ([soundEngine loadBuffer:[bufferId intValue] filePath:filePath]) {
-
- CDLOGINFO(@"Denshion::CDBufferManager buffer loaded %@ %@",bufferId,filePath);
- [loadedBuffers setObject:bufferId forKey:filePath];
- return [bufferId intValue];
- } else {
-
- [freedBuffers addObject:bufferId];
- return kCDNoBuffer;
- }
- } else {
-
- return kCDNoBuffer;
- }
- } else {
- return [soundId intValue];
- }
- }
- -(void) releaseBufferForFile:(NSString *) filePath {
- int bufferId = [self bufferForFile:filePath create:NO];
- if (bufferId != kCDNoBuffer) {
- [soundEngine unloadBuffer:bufferId];
- [loadedBuffers removeObjectForKey:filePath];
- NSNumber *freedBufferId = [[NSNumber alloc] initWithInt:bufferId];
- [freedBufferId autorelease];
- [freedBuffers addObject:freedBufferId];
- }
- }
- @end
|