project_files/HedgewarsMobile/Classes/MXAudioPlayerFadeOperation.m
author Periklis Ntanasis <pntanasis@gmail.com>
Sat, 14 Sep 2013 02:13:20 +0300
branchspacecampaign
changeset 9624 51e22761679d
parent 8441 a00b0fa0dbd7
child 11137 14f50dde3e8c
permissions -rw-r--r--
fixed bug making the game not end when thanta was dead,reported by Wuzzy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     1
//  MXAudioPlayerFadeOperation.m
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     2
//
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     3
//  Created by Andrew Mackenzie-Ross on 30/11/10.
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     4
//  mackross.net.
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     5
//
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     6
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     7
#import "MXAudioPlayerFadeOperation.h"
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     8
#import <AVFoundation/AVFoundation.h>
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
     9
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    10
#define SKVolumeChangesPerSecond 15
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    11
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    12
@interface MXAudioPlayerFadeOperation ()
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
    13
@property (nonatomic, retain, readwrite) AVAudioPlayer *audioPlayer;
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    14
- (void)beginFadeOperation;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    15
- (void)finishFadeOperation;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    16
@end
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    17
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    18
@implementation MXAudioPlayerFadeOperation
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    19
#pragma mark -
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    20
#pragma mark Properties
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    21
@synthesize audioPlayer = _audioPlayer;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    22
@synthesize fadeDuration = _fadeDuration;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    23
@synthesize finishVolume = _finishVolume;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    24
@synthesize playBeforeFade = _playBeforeFade;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    25
@synthesize pauseAfterFade = _pauseAfterFade;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    26
@synthesize stopAfterFade = _stopAfterFade;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    27
@synthesize delay = _delay;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    28
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    29
#pragma mark -
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    30
#pragma mark Accessors
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    31
- (AVAudioPlayer *)audioPlayer {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    32
  AVAudioPlayer *result;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    33
  @synchronized(self) {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    34
    result = [_audioPlayer retain];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    35
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    36
  return [result autorelease];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    37
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    38
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    39
- (void)setAudioPlayer:(AVAudioPlayer *)anAudioPlayer {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    40
  @synchronized(self) {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    41
    if (_audioPlayer != anAudioPlayer) {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    42
      [_audioPlayer release];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    43
      _audioPlayer = [anAudioPlayer retain];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    44
    }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    45
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    46
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    47
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    48
#pragma mark -
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    49
#pragma mark NSOperation
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    50
-(id) initFadeWithAudioPlayer:(AVAudioPlayer*)player toVolume:(float)volume overDuration:(NSTimeInterval)duration withDelay:(NSTimeInterval)timeDelay {
6908
896ed2afcfb8 ios: turn on more warning messages and start correcting them
koda
parents: 6659
diff changeset
    51
  if ((self = [super init])) {
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    52
    self.audioPlayer = player;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    53
    [player prepareToPlay];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    54
    _fadeDuration = duration;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    55
    _finishVolume = volume;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    56
    _playBeforeFade = YES;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    57
    _stopAfterFade = NO;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    58
    _pauseAfterFade = (volume == 0.0) ? YES : NO;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    59
    _delay = timeDelay;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    60
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    61
  return self;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    62
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    63
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    64
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player toVolume:(float)volume overDuration:(NSTimeInterval)duration {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    65
  return [self initFadeWithAudioPlayer:player toVolume:volume overDuration:duration withDelay:0.0];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    66
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    67
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    68
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player toVolume:(float)volume {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    69
  return [self initFadeWithAudioPlayer:player toVolume:volume overDuration:1.0];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    70
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    71
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    72
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    73
  return [self initFadeWithAudioPlayer:player toVolume:0.0];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    74
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    75
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    76
- (id) init {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    77
  ALog(@"Failed to init class (%@) with AVAudioPlayer instance, use initFadeWithAudioPlayer:",[self class]);
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    78
  return nil;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    79
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    80
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    81
- (void)main {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    82
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    83
  [NSThread sleepForTimeInterval:_delay];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    84
  if ([self.audioPlayer isKindOfClass:[AVAudioPlayer class]]) {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    85
    [self beginFadeOperation];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    86
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    87
  else {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    88
    ALog(@"AudioPlayerFadeOperation began with invalid AVAudioPlayer");
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    89
  }
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
    90
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    91
  [pool release];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    92
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    93
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    94
- (void)beginFadeOperation {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    95
  if (![self.audioPlayer isPlaying] && _playBeforeFade) [self.audioPlayer play];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
    96
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    97
  if (_fadeDuration != 0.0) {
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
    98
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    99
    NSTimeInterval sleepInterval = (1.0 / SKVolumeChangesPerSecond);
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   100
    NSTimeInterval startTime = [[NSDate date] timeIntervalSinceReferenceDate];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   101
    NSTimeInterval now = startTime;
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
   102
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   103
    float startVolume = [self.audioPlayer volume];
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
   104
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   105
    while (now < (startTime + _fadeDuration)) {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   106
      float ratioOfFadeCompleted = (now - startTime)/_fadeDuration;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   107
      float volume = (_finishVolume * ratioOfFadeCompleted) + (startVolume * (1-ratioOfFadeCompleted));
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   108
      [self.audioPlayer setVolume:volume];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   109
      [NSThread sleepForTimeInterval:sleepInterval];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   110
      now = [[NSDate date] timeIntervalSinceReferenceDate];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   111
    }
8441
a00b0fa0dbd7 some whitespaces from ios files
koda
parents: 6908
diff changeset
   112
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   113
    [self.audioPlayer setVolume:_finishVolume];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   114
    [self finishFadeOperation];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   115
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   116
  else {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   117
    [self.audioPlayer setVolume:_finishVolume];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   118
    [self finishFadeOperation];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   119
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   120
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   121
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   122
- (void)finishFadeOperation {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   123
  if ([self.audioPlayer isPlaying] && _pauseAfterFade) [self.audioPlayer pause];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   124
  if ([self.audioPlayer isPlaying] && _stopAfterFade) [self.audioPlayer stop];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   125
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   126
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   127
- (void)dealloc {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   128
  releaseAndNil(_audioPlayer);
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   129
  [super dealloc];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   130
}
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   131
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   132
@end
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   133