# HG changeset patch # User koda # Date 1333934717 -7200 # Node ID a187c280dd3dd5c0a5087a78819c72e1dad41915 # Parent f31b5213b1632c46a489602912f51be0760a9a74 ios: convert audio operation from class to instance, plays better with memory diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/AboutViewController.m --- a/project_files/HedgewarsMobile/Classes/AboutViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/AboutViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -52,12 +52,12 @@ } -(IBAction) buttonPressed:(id) sender { - [AudioManagerController playBackSound]; + [[AudioManagerController mainManager] playBackSound]; [[self parentViewController] dismissModalViewControllerAnimated:YES]; } -(IBAction) segmentedControlChanged:(id) sender { - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; [self.tableView setContentOffset:CGPointMake(0, 0) animated:NO]; [self.tableView reloadData]; } diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/AudioManagerController.h --- a/project_files/HedgewarsMobile/Classes/AudioManagerController.h Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/AudioManagerController.h Mon Apr 09 03:25:17 2012 +0200 @@ -18,23 +18,41 @@ #import +#import +@class AVAudioPlayer; + @interface AudioManagerController : NSObject { + @private + AVAudioPlayer *backgroundMusic; + SystemSoundID clickSound; + SystemSoundID backSound; + SystemSoundID selSound; + NSOperationQueue *audioFaderQueue; } -+(void) playBackgroundMusic; -+(void) pauseBackgroundMusic; -+(void) stopBackgroundMusic; +@property (nonatomic,retain) AVAudioPlayer *backgroundMusic; +@property (assign) SystemSoundID clickSound; +@property (assign) SystemSoundID backSound; +@property (assign) SystemSoundID selSound; + +@property (nonatomic,retain) NSOperationQueue *audioFaderQueue; + ++(id) mainManager; -+(void) fadeInBackgroundMusic; -+(void) fadeOutBackgroundMusic; +-(void) playBackgroundMusic; +-(void) pauseBackgroundMusic; +-(void) stopBackgroundMusic; -+(void) playClickSound; -+(void) playBackSound; -+(void) playSelectSound; +-(void) fadeInBackgroundMusic; +-(void) fadeOutBackgroundMusic; -+(void) releaseCache; +-(void) playClickSound; +-(void) playBackSound; +-(void) playSelectSound; +-(SystemSoundID) loadSound:(NSString *)snd; +-(void) unloadSounds; @end diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/AudioManagerController.m --- a/project_files/HedgewarsMobile/Classes/AudioManagerController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/AudioManagerController.m Mon Apr 09 03:25:17 2012 +0200 @@ -19,137 +19,157 @@ #import "AudioManagerController.h" #import "AVFoundation/AVAudioPlayer.h" -#import #import "MXAudioPlayerFadeOperation.h" -static AVAudioPlayer *backgroundMusic = nil; -static SystemSoundID clickSound = -1; -static SystemSoundID backSound = -1; -static SystemSoundID selSound = -1; +#define DEFAULT_VOLUME 0.45f +#define FADEOUT_DURATION 3.0f +#define FADEIN_DURATION 2.0f -static NSOperationQueue *audioFaderQueue = nil; -static MXAudioPlayerFadeOperation *fadeIn = nil; -static MXAudioPlayerFadeOperation *fadeOut = nil; +static AudioManagerController *mainInstance; @implementation AudioManagerController +@synthesize backgroundMusic, clickSound, backSound, selSound, audioFaderQueue; + ++(id) mainManager { + if (mainInstance == nil) + mainInstance = [[self alloc] init]; + return mainInstance; +} + +-(id) init { + if (self = [super init]) { + self.backgroundMusic = nil; + self.clickSound = -1; + self.backSound = -1; + self.selSound = -1; + + self.audioFaderQueue = nil; + } + return self; +} + +-(void) dealloc { + [self unloadSounds]; + releaseAndNil(backgroundMusic); + releaseAndNil(audioFaderQueue); + mainInstance = nil; + [super dealloc]; +} + +-(void) didReceiveMemoryWarning { + if (self.backgroundMusic.playing == NO) + self.backgroundMusic = nil; + if ([self.audioFaderQueue operationCount] == 0) + self.audioFaderQueue = nil; + + [self unloadSounds]; + MSG_MEMCLEAN(); +} #pragma mark - #pragma mark background music control -+(void) loadBackgroundMusic { - NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"]; - backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil]; - - backgroundMusic.delegate = nil; - backgroundMusic.volume = 0; - backgroundMusic.numberOfLoops = -1; - [backgroundMusic prepareToPlay]; -} - -+(void) playBackgroundMusic { +-(void) playBackgroundMusic { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO) return; - if (backgroundMusic == nil) - [AudioManagerController loadBackgroundMusic]; - - backgroundMusic.volume = 0.45f; - [backgroundMusic play]; + if (self.backgroundMusic == nil) { + NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"]; + self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil]; + self.backgroundMusic.delegate = nil; + self.backgroundMusic.numberOfLoops = -1; + } + + self.backgroundMusic.volume = DEFAULT_VOLUME; + [self.backgroundMusic play]; } -+(void) pauseBackgroundMusic { - [backgroundMusic pause]; +-(void) pauseBackgroundMusic { + [self.backgroundMusic pause]; } -+(void) stopBackgroundMusic { - [backgroundMusic stop]; +-(void) stopBackgroundMusic { + [self.backgroundMusic stop]; } -+(void) fadeOutBackgroundMusic { +-(void) fadeOutBackgroundMusic { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO) return; - if (audioFaderQueue == nil) - audioFaderQueue = [[NSOperationQueue alloc] init]; - if (backgroundMusic == nil) - [AudioManagerController loadBackgroundMusic]; - if (fadeOut == nil) - fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.0 overDuration:3.0]; - - [audioFaderQueue addOperation:fadeOut]; + if (self.audioFaderQueue == nil) + self.audioFaderQueue = [[NSOperationQueue alloc] init]; + + MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic + toVolume:0.0 + overDuration:FADEOUT_DURATION]; + [self.audioFaderQueue addOperation:fadeOut]; + [fadeOut release]; } -+(void) fadeInBackgroundMusic { +-(void) fadeInBackgroundMusic { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO) return; - if (audioFaderQueue == nil) - audioFaderQueue = [[NSOperationQueue alloc] init]; - if (backgroundMusic == nil) - [AudioManagerController loadBackgroundMusic]; - if (fadeIn == nil) - fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.45 overDuration:2.0]; + if (self.audioFaderQueue == nil) + self.audioFaderQueue = [[NSOperationQueue alloc] init]; + [self playBackgroundMusic]; + MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic + toVolume:DEFAULT_VOLUME + overDuration:FADEIN_DURATION]; [audioFaderQueue addOperation:fadeIn]; + [fadeIn release]; } #pragma mark - #pragma mark sound effects control -+(SystemSoundID) loadSound:(NSString *)snd { - // get the filename of the sound file: - NSString *path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd]; +-(SystemSoundID) loadSound:(NSString *)snd { + SystemSoundID soundID; - // declare a system sound id and get a URL for the sound file - SystemSoundID soundID; + // get the filename of the sound file in a NSURL format + NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd]; NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO]; + [path release]; // use audio sevices to create and play the sound AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID); return soundID; } -+(void) playClickSound { - if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) - return; - - if (clickSound == -1) - clickSound = [AudioManagerController loadSound:@"clickSound.wav"]; - - AudioServicesPlaySystemSound(clickSound); +-(void) unloadSounds { + AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1; + AudioServicesDisposeSystemSoundID(backSound), backSound = -1; + AudioServicesDisposeSystemSoundID(selSound), selSound = -1; } -+(void) playBackSound { +-(void) playClickSound { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) return; - if (backSound == -1) - backSound = [AudioManagerController loadSound:@"backSound.wav"]; + if (self.clickSound == -1) + self.clickSound = [self loadSound:@"clickSound.wav"]; - AudioServicesPlaySystemSound(backSound); + AudioServicesPlaySystemSound(self.clickSound); } -+(void) playSelectSound { +-(void) playBackSound { if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) return; - if (selSound == -1) - selSound = [AudioManagerController loadSound:@"selSound.wav"]; + if (self.backSound == -1) + self.backSound = [self loadSound:@"backSound.wav"]; - AudioServicesPlaySystemSound(selSound); + AudioServicesPlaySystemSound(self.backSound); } -#pragma mark - -#pragma mark memory management -+(void) releaseCache { - [backgroundMusic stop]; - [backgroundMusic release], backgroundMusic = nil; - [fadeOut release], fadeOut = nil; - [fadeIn release], fadeIn = nil; - [audioFaderQueue release], audioFaderQueue = nil; - AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1; - AudioServicesDisposeSystemSoundID(backSound), backSound = -1; - AudioServicesDisposeSystemSoundID(selSound), selSound = -1; - MSG_MEMCLEAN(); +-(void) playSelectSound { + if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO) + return; + + if (self.selSound == -1) + self.selSound = [self loadSound:@"selSound.wav"]; + + AudioServicesPlaySystemSound(self.selSound); } @end diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/GameConfigViewController.m --- a/project_files/HedgewarsMobile/Classes/GameConfigViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/GameConfigViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -47,12 +47,12 @@ [alert show]; [alert release]; } else { - [AudioManagerController playBackSound]; + [[AudioManagerController mainManager] playBackSound]; [[self parentViewController] dismissModalViewControllerAnimated:YES]; } break; case 1: - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; if ([self isEverythingSet] == NO) return; theButton.enabled = NO; @@ -66,7 +66,7 @@ break; case 2: - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; if (self.helpPage == nil) self.helpPage = [[HelpPageLobbyViewController alloc] initWithNibName:@"HelpPageLobbyViewController-iPad" bundle:nil]; self.helpPage.view.alpha = 0; @@ -85,7 +85,7 @@ UISegmentedControl *theSegment = (UISegmentedControl *)sender; - [AudioManagerController playSelectSound]; + [[AudioManagerController mainManager] playSelectSound]; switch (theSegment.selectedSegmentIndex) { case 0: // this message is compulsory otherwise the table won't be loaded at all diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m --- a/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/GameInterfaceBridge.m Mon Apr 09 03:25:17 2012 +0200 @@ -32,7 +32,7 @@ // prepares the controllers for hosting a game -(void) earlyEngineLaunch:(NSDictionary *)optionsOrNil { [self retain]; - [AudioManagerController fadeOutBackgroundMusic]; + [[AudioManagerController mainManager] fadeOutBackgroundMusic]; EngineProtocolNetwork *engineProtocol = [[EngineProtocolNetwork alloc] init]; self.proto = engineProtocol; @@ -101,7 +101,7 @@ [[NSFileManager defaultManager] removeItemAtPath:self.savePath error:nil]; // restart music and we're done - [AudioManagerController fadeInBackgroundMusic]; + [[AudioManagerController mainManager] fadeInBackgroundMusic]; [HWUtils setGameStatus:gsNone]; [HWUtils setGameType:gtNone]; [self release]; diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m --- a/project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/GeneralSettingsViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -43,7 +43,7 @@ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults synchronize]; if ([[userDefaults objectForKey:@"music"] boolValue] == NO) - [AudioManagerController stopBackgroundMusic]; + [[AudioManagerController mainManager] stopBackgroundMusic]; [super viewWillDisappear:animated]; } @@ -63,7 +63,7 @@ [theOtherSwitch setOn:NO animated:YES]; // since switching sound on won't turn music on anyways, we can always turn off music - [AudioManagerController pauseBackgroundMusic]; + [[AudioManagerController mainManager]pauseBackgroundMusic]; [settings setObject:[NSNumber numberWithBool:NO] forKey:@"music"]; break; case 20: //musicSwitch @@ -76,9 +76,9 @@ [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"music"]; if (theSwitch.on) - [AudioManagerController playBackgroundMusic]; + [[AudioManagerController mainManager] playBackgroundMusic]; else - [AudioManagerController pauseBackgroundMusic]; + [[AudioManagerController mainManager] pauseBackgroundMusic]; break; case 30: //alternateSwitch [settings setObject:[NSNumber numberWithBool:theSwitch.on] forKey:@"alternate"]; diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m --- a/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/HedgewarsAppDelegate.m Mon Apr 09 03:25:17 2012 +0200 @@ -68,7 +68,7 @@ [HWUtils releaseCache]; // don't stop music if it is playing if ([HWUtils isGameLaunched]) { - [AudioManagerController releaseCache]; + [[AudioManagerController mainManager] didReceiveMemoryWarning]; HW_memoryWarningCallback(); } MSG_MEMCLEAN(); diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/MainMenuViewController.m --- a/project_files/HedgewarsMobile/Classes/MainMenuViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/MainMenuViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -82,7 +82,7 @@ } -(void) viewWillAppear:(BOOL)animated { - [AudioManagerController playBackgroundMusic]; + [[AudioManagerController mainManager] playBackgroundMusic]; [super viewWillAppear:animated]; } @@ -93,7 +93,7 @@ NSString *xib = nil; NSString *debugStr = nil; - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; switch (button.tag) { case 0: if (nil == self.gameConfigViewController) { diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/MapConfigViewController.m --- a/project_files/HedgewarsMobile/Classes/MapConfigViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/MapConfigViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -35,7 +35,7 @@ } -(IBAction) mapButtonPressed:(id) sender { - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; [self updatePreview]; } @@ -290,7 +290,7 @@ [self updatePreview]; oldValue = num; } - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; } // perform actions based on the activated section, then call updatePreview to visually update the selection @@ -299,7 +299,7 @@ NSString *mapgen, *staticmap, *mission; NSInteger newPage = self.segmentedControl.selectedSegmentIndex; - [AudioManagerController playSelectSound]; + [[AudioManagerController mainManager] playSelectSound]; switch (newPage) { case 0: // Random mapgen = @"e$mapgen 0"; diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m --- a/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/MissionTrainingViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -64,7 +64,7 @@ UIButton *button = (UIButton *)sender; if (button.tag == 0) { - [AudioManagerController playBackSound]; + [[AudioManagerController mainManager] playBackSound]; [[self parentViewController] dismissModalViewControllerAnimated:YES]; } else { [GameInterfaceBridge registerCallingController:self]; diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/OverlayViewController.m --- a/project_files/HedgewarsMobile/Classes/OverlayViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/OverlayViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -227,13 +227,13 @@ HW_backjump(); break; case 10: - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; HW_pause(); [self clearOverlay]; [self showPopover]; break; case 11: - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; [self clearOverlay]; HW_ammoMenu(); break; diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/RestoreViewController.m --- a/project_files/HedgewarsMobile/Classes/RestoreViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/RestoreViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -31,11 +31,11 @@ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (theButton.tag != 0) { - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; [GameInterfaceBridge registerCallingController:self.parentViewController]; [GameInterfaceBridge startSaveGame:[[NSUserDefaults standardUserDefaults] objectForKey:@"savedGamePath"]]; } else { - [AudioManagerController playBackSound]; + [[AudioManagerController mainManager] playBackSound]; [defaults setObject:@"" forKey:@"savedGamePath"]; [defaults synchronize]; } diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/SavedGamesViewController.m --- a/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/SavedGamesViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -66,7 +66,7 @@ UIButton *button = (UIButton *)sender; if (button.tag == 0) { - [AudioManagerController playBackSound]; + [[AudioManagerController mainManager] playBackSound]; [self.tableView setEditing:NO animated:YES]; [[self parentViewController] dismissModalViewControllerAnimated:YES]; } else { diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/SettingsBaseViewController.m --- a/project_files/HedgewarsMobile/Classes/SettingsBaseViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/SettingsBaseViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -132,7 +132,7 @@ } -(void) dismissSplitView { - [AudioManagerController playBackSound]; + [[AudioManagerController mainManager] playBackSound]; [[[HedgewarsAppDelegate sharedAppDelegate] mainViewController] dismissModalViewControllerAnimated:YES]; } @@ -240,7 +240,7 @@ nextController.navigationItem.hidesBackButton = YES; [nextController viewWillAppear:NO]; [targetController.navigationController pushViewController:nextController animated:NO]; - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; } } diff -r f31b5213b163 -r a187c280dd3d project_files/HedgewarsMobile/Classes/StatsPageViewController.m --- a/project_files/HedgewarsMobile/Classes/StatsPageViewController.m Mon Apr 09 02:02:14 2012 +0200 +++ b/project_files/HedgewarsMobile/Classes/StatsPageViewController.m Mon Apr 09 03:25:17 2012 +0200 @@ -161,7 +161,7 @@ #pragma mark - #pragma mark button delegate -(void) dismissView { - [AudioManagerController playClickSound]; + [[AudioManagerController mainManager] playClickSound]; [self dismissModalViewControllerAnimated:YES]; }