project_files/HedgewarsMobile/Classes/MXAudioPlayerFadeOperation.m
author Wuzzy <Wuzzy2@mail.ru>
Sun, 19 Jul 2020 23:28:45 +0200
changeset 15753 26247a74d823
parent 12872 00215a7ec5f5
permissions -rw-r--r--
Fix piano spawning vgtNote if slot key without sound was pressed
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 ()
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    13
@property (nonatomic, strong, 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) {
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    34
    result = _audioPlayer;
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    35
  }
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    36
  return result;
6656
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) {
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    42
      _audioPlayer = anAudioPlayer;
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    43
    }
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
#pragma mark -
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    48
#pragma mark NSOperation
12872
00215a7ec5f5 - BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents: 11137
diff changeset
    49
- (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
    50
  if ((self = [super init])) {
6656
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    51
    self.audioPlayer = player;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    52
    [player prepareToPlay];
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    53
    _fadeDuration = duration;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    54
    _finishVolume = volume;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    55
    _playBeforeFade = YES;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    56
    _stopAfterFade = NO;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    57
    _pauseAfterFade = (volume == 0.0) ? YES : NO;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    58
    _delay = timeDelay;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    59
  }
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    60
  return self;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    61
}
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
- (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
    64
  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
    65
}
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
- (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
    68
  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
    69
}
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
- (id)initFadeWithAudioPlayer:(AVAudioPlayer*)player {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    72
  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
    73
}
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
- (id) init {
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    76
  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
    77
  return nil;
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
    78
}
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
- (void)main {
11137
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 8441
diff changeset
    81
  @autoreleasepool {
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 8441
diff changeset
    82
6656
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
11137
14f50dde3e8c - Using of @autoreleasepool is better
Anton Malmygin <antonc27@mail.ru>
parents: 8441
diff changeset
    91
  }
6656
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
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   128
@end
6aeaba3ee584 ios: added MXAudioPlayerFadeOperation to allow easy fade in and out of background music
koda
parents:
diff changeset
   129