project_files/HedgewarsMobile/Classes/AudioManagerController.m
changeset 6869 a187c280dd3d
parent 6832 fae8fd118da9
child 6871 5aadbfe02613
equal deleted inserted replaced
6868:f31b5213b163 6869:a187c280dd3d
    17  */
    17  */
    18 
    18 
    19 
    19 
    20 #import "AudioManagerController.h"
    20 #import "AudioManagerController.h"
    21 #import "AVFoundation/AVAudioPlayer.h"
    21 #import "AVFoundation/AVAudioPlayer.h"
    22 #import <AudioToolbox/AudioToolbox.h>
       
    23 #import "MXAudioPlayerFadeOperation.h"
    22 #import "MXAudioPlayerFadeOperation.h"
    24 
    23 
    25 
    24 
    26 static AVAudioPlayer *backgroundMusic = nil;
    25 #define DEFAULT_VOLUME    0.45f
    27 static SystemSoundID clickSound = -1;
    26 #define FADEOUT_DURATION  3.0f
    28 static SystemSoundID backSound = -1;
    27 #define FADEIN_DURATION   2.0f
    29 static SystemSoundID selSound = -1;
       
    30 
    28 
    31 static NSOperationQueue *audioFaderQueue = nil;
    29 static AudioManagerController *mainInstance;
    32 static MXAudioPlayerFadeOperation *fadeIn = nil;
       
    33 static MXAudioPlayerFadeOperation *fadeOut = nil;
       
    34 
    30 
    35 @implementation AudioManagerController
    31 @implementation AudioManagerController
       
    32 @synthesize backgroundMusic, clickSound, backSound, selSound, audioFaderQueue;
       
    33 
       
    34 +(id) mainManager {
       
    35     if (mainInstance == nil)
       
    36         mainInstance = [[self alloc] init];
       
    37     return mainInstance;
       
    38 }
       
    39 
       
    40 -(id) init {
       
    41     if (self = [super init]) {
       
    42         self.backgroundMusic = nil;
       
    43         self.clickSound = -1;
       
    44         self.backSound = -1;
       
    45         self.selSound = -1;
       
    46 
       
    47         self.audioFaderQueue = nil;
       
    48     }
       
    49     return self;
       
    50 }
       
    51 
       
    52 -(void) dealloc {
       
    53     [self unloadSounds];
       
    54     releaseAndNil(backgroundMusic);
       
    55     releaseAndNil(audioFaderQueue);
       
    56     mainInstance = nil;
       
    57     [super dealloc];
       
    58 }
       
    59 
       
    60 -(void) didReceiveMemoryWarning {
       
    61     if (self.backgroundMusic.playing == NO)
       
    62         self.backgroundMusic = nil;
       
    63     if ([self.audioFaderQueue operationCount] == 0)
       
    64         self.audioFaderQueue = nil;
       
    65 
       
    66     [self unloadSounds];
       
    67     MSG_MEMCLEAN();
       
    68 }
    36 
    69 
    37 #pragma mark -
    70 #pragma mark -
    38 #pragma mark background music control
    71 #pragma mark background music control
    39 +(void) loadBackgroundMusic {
    72 -(void) playBackgroundMusic {
    40     NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
       
    41     backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil];
       
    42 
       
    43     backgroundMusic.delegate = nil;
       
    44     backgroundMusic.volume = 0;
       
    45     backgroundMusic.numberOfLoops = -1;
       
    46     [backgroundMusic prepareToPlay];
       
    47 }
       
    48 
       
    49 +(void) playBackgroundMusic {
       
    50     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    73     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    51         return;
    74         return;
    52 
    75 
    53     if (backgroundMusic == nil)
    76     if (self.backgroundMusic == nil) {
    54         [AudioManagerController loadBackgroundMusic];
    77         NSString *musicString = [[NSBundle mainBundle] pathForResource:@"hwclassic" ofType:@"mp3"];
    55 
    78         self.backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:musicString] error:nil];
    56     backgroundMusic.volume = 0.45f;
    79         self.backgroundMusic.delegate = nil;
    57     [backgroundMusic play];
    80         self.backgroundMusic.numberOfLoops = -1;
       
    81     }
       
    82     
       
    83     self.backgroundMusic.volume = DEFAULT_VOLUME;
       
    84     [self.backgroundMusic play];
    58 }
    85 }
    59 
    86 
    60 +(void) pauseBackgroundMusic {
    87 -(void) pauseBackgroundMusic {
    61     [backgroundMusic pause];
    88     [self.backgroundMusic pause];
    62 }
    89 }
    63 
    90 
    64 +(void) stopBackgroundMusic {
    91 -(void) stopBackgroundMusic {
    65     [backgroundMusic stop];
    92     [self.backgroundMusic stop];
    66 }
    93 }
    67 
    94 
    68 +(void) fadeOutBackgroundMusic {
    95 -(void) fadeOutBackgroundMusic {
    69     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    96     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    70         return;
    97         return;
    71 
    98 
    72     if (audioFaderQueue == nil)
    99     if (self.audioFaderQueue == nil)
    73         audioFaderQueue = [[NSOperationQueue alloc] init];
   100         self.audioFaderQueue = [[NSOperationQueue alloc] init];
    74     if (backgroundMusic == nil)
   101     
    75         [AudioManagerController loadBackgroundMusic];
   102     MXAudioPlayerFadeOperation *fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
    76     if (fadeOut == nil)
   103                                                                                              toVolume:0.0
    77         fadeOut = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.0 overDuration:3.0];
   104                                                                                          overDuration:FADEOUT_DURATION];
    78 
   105     [self.audioFaderQueue addOperation:fadeOut];
    79     [audioFaderQueue addOperation:fadeOut];
   106     [fadeOut release];
    80 }
   107 }
    81 
   108 
    82 +(void) fadeInBackgroundMusic {
   109 -(void) fadeInBackgroundMusic {
    83     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
   110     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"music"] boolValue] == NO)
    84         return;
   111         return;
    85 
   112 
    86     if (audioFaderQueue == nil)
   113     if (self.audioFaderQueue == nil)
    87         audioFaderQueue = [[NSOperationQueue alloc] init];
   114         self.audioFaderQueue = [[NSOperationQueue alloc] init];
    88     if (backgroundMusic == nil)
       
    89         [AudioManagerController loadBackgroundMusic];
       
    90     if (fadeIn == nil)
       
    91         fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:backgroundMusic toVolume:0.45 overDuration:2.0];
       
    92 
   115 
       
   116     [self playBackgroundMusic];
       
   117     MXAudioPlayerFadeOperation *fadeIn = [[MXAudioPlayerFadeOperation alloc] initFadeWithAudioPlayer:self.backgroundMusic
       
   118                                                                                             toVolume:DEFAULT_VOLUME
       
   119                                                                                         overDuration:FADEIN_DURATION];
    93     [audioFaderQueue addOperation:fadeIn];
   120     [audioFaderQueue addOperation:fadeIn];
       
   121     [fadeIn release];
    94 }
   122 }
    95 
   123 
    96 #pragma mark -
   124 #pragma mark -
    97 #pragma mark sound effects control
   125 #pragma mark sound effects control
    98 +(SystemSoundID) loadSound:(NSString *)snd {
   126 -(SystemSoundID) loadSound:(NSString *)snd {
    99     // get the filename of the sound file:
   127     SystemSoundID soundID;
   100     NSString *path = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd];
       
   101 
   128 
   102     // declare a system sound id and get a URL for the sound file
   129     // get the filename of the sound file in a NSURL format
   103     SystemSoundID soundID;
   130     NSString *path = [[NSString alloc] initWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],snd];
   104     NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
   131     NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
       
   132     [path release];
   105 
   133 
   106     // use audio sevices to create and play the sound
   134     // use audio sevices to create and play the sound
   107     AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
   135     AudioServicesCreateSystemSoundID((CFURLRef)filePath, &soundID);
   108     return soundID;
   136     return soundID;
   109 }
   137 }
   110 
   138 
   111 +(void) playClickSound {
   139 -(void) unloadSounds {
       
   140     AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
       
   141     AudioServicesDisposeSystemSoundID(backSound), backSound = -1;
       
   142     AudioServicesDisposeSystemSoundID(selSound), selSound = -1;
       
   143 }
       
   144 
       
   145 -(void) playClickSound {
   112     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   146     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   113         return;
   147         return;
   114     
   148     
   115     if (clickSound == -1)
   149     if (self.clickSound == -1)
   116         clickSound = [AudioManagerController loadSound:@"clickSound.wav"];
   150         self.clickSound = [self loadSound:@"clickSound.wav"];
   117     
   151     
   118     AudioServicesPlaySystemSound(clickSound);
   152     AudioServicesPlaySystemSound(self.clickSound);
   119 }
   153 }
   120 
   154 
   121 +(void) playBackSound {
   155 -(void) playBackSound {
   122     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   156     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   123         return;
   157         return;
   124     
   158     
   125     if (backSound == -1)
   159     if (self.backSound == -1)
   126         backSound = [AudioManagerController loadSound:@"backSound.wav"];
   160         self.backSound = [self loadSound:@"backSound.wav"];
   127     
   161     
   128     AudioServicesPlaySystemSound(backSound);
   162     AudioServicesPlaySystemSound(self.backSound);
   129 }
   163 }
   130 
   164 
   131 +(void) playSelectSound {
   165 -(void) playSelectSound {
   132     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   166     if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"] boolValue] == NO)
   133         return;
   167         return;
   134     
   168     
   135     if (selSound == -1)
   169     if (self.selSound == -1)
   136         selSound = [AudioManagerController loadSound:@"selSound.wav"];
   170         self.selSound = [self loadSound:@"selSound.wav"];
   137     
   171     
   138     AudioServicesPlaySystemSound(selSound);
   172     AudioServicesPlaySystemSound(self.selSound);
   139 }
       
   140 
       
   141 #pragma mark -
       
   142 #pragma mark memory management
       
   143 +(void) releaseCache {
       
   144     [backgroundMusic stop];
       
   145     [backgroundMusic release], backgroundMusic = nil;
       
   146     [fadeOut release], fadeOut = nil;
       
   147     [fadeIn release], fadeIn = nil;
       
   148     [audioFaderQueue release], audioFaderQueue = nil;
       
   149     AudioServicesDisposeSystemSoundID(clickSound), clickSound = -1;
       
   150     AudioServicesDisposeSystemSoundID(backSound), backSound = -1;
       
   151     AudioServicesDisposeSystemSoundID(selSound), selSound = -1;
       
   152     MSG_MEMCLEAN();
       
   153 }
   173 }
   154 
   174 
   155 @end
   175 @end