Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 157 lines (132 sloc) 3.668 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#import "iphone_sound.h"
#include "SDL.h"

#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

static AVAudioPlayer *song = nil;
static const float fade_interval = 0.05;
static float master_volume = 1.0;
static void (*song_finished_callback)() = NULL;

@interface AudioDelegate : NSObject <AVAudioPlayerDelegate>
{
float amount;
BOOL fading;
}

@property (readwrite) float amount;
@property (readonly) BOOL fading;

- (void) fadeInMusic:(id)obj;
- (void) fadeOutMusic:(id)obj;

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag;
@end

AudioDelegate *music_delegate;

void iphone_init_music (void (*callback)())
{
music_delegate = [[AudioDelegate alloc] init];
song_finished_callback = callback;

AudioSessionInitialize (NULL, NULL, NULL, NULL);
AudioSessionSetActive (true);
UInt32 sessionCategory = kAudioSessionCategory_SoloAmbientSound;
AudioSessionSetProperty (kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
}

void iphone_fade_in_music (int duration)
{
if (song == nil || music_delegate == nil || music_delegate.fading) return;
song.volume = 0.0;
music_delegate.amount = master_volume/((duration/1000.0) * (1.0/fade_interval));
//NSLog(@"Starting fade in, volume=%f | amount=%f", song.volume, music_delegate.amount);
[music_delegate fadeInMusic:nil];
}

void iphone_fade_out_music (int duration)
{
if (song == nil || music_delegate == nil || music_delegate.fading) return;
music_delegate.amount = master_volume/((duration/1000.0) * (1.0/fade_interval));
[music_delegate fadeOutMusic:nil];
}

void iphone_play_music (const char *file, int loops)
{
int timer = SDL_GetTicks();
if (song)
{
[song stop];
[song release];
}
song = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[NSString stringWithUTF8String: file]] error:NULL];
//NSLog(@"Initializing the new song took %i ms", SDL_GetTicks()-timer);
song.delegate = music_delegate;
song.numberOfLoops = loops;
song.volume = master_volume;

timer = SDL_GetTicks();
[song play];
//NSLog(@"Playing the new song (%s) took %i ms", file, SDL_GetTicks()-timer);
}

void iphone_pause_music ()
{
[song pause];
}

void iphone_resume_music ()
{
[song play];
}

void iphone_kill_music ()
{
if (song)
{
[song stop];
[song release];
song = nil;
}
[music_delegate release];
music_delegate = nil;
}

float iphone_get_music_volume ()
{
return master_volume;
}

void iphone_set_music_volume (float v)
{
master_volume = v;
if (song != nil)
song.volume = master_volume;
}

@implementation AudioDelegate

@synthesize amount;
@synthesize fading;

- (void) fadeInMusic:(id)obj
{
if (song.volume + amount >= master_volume)
{
//NSLog(@"Done fading in.");
fading = NO;
song.volume = master_volume; //fading is done
} else {
fading = YES;
song.volume += amount;
//NSLog(@"Fading in, volume=%f", song.volume);
[self performSelector:@selector(fadeInMusic:) withObject:nil afterDelay:fade_interval];
}
}

- (void) fadeOutMusic:(id)obj
{
if (song.volume - amount <= 0.0)
{
fading = NO;
song.volume = 0.0; //fading is done, and the volume should be exactly 0.0
[self audioPlayerDidFinishPlaying:song successfully:YES];
} else {
fading = YES;
song.volume -= amount;
[self performSelector:@selector(fadeOutMusic:) withObject:nil afterDelay:fade_interval];
}
}

- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[player stop];
[player release];
song = nil;
(*song_finished_callback)();
NSLog(@"Finished playing music");
}

- (void) audioPlayerEndInterruption:(AVAudioPlayer *)player
{
[player play];
}

@end
Something went wrong with that request. Please try again.