-
Notifications
You must be signed in to change notification settings - Fork 3
/
SoundManager.h
167 lines (128 loc) · 7.42 KB
/
SoundManager.h
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
158
159
160
161
162
163
164
165
166
167
//
// SoundManager.h
// SLQTSOR
//
// Created by Michael Daley on 22/05/2009.
// Copyright 2009 Michael Daley. All rights reserved.
//
#import <OpenAL/al.h>
#import <OpenAL/alc.h>
#import <AudioToolbox/AudioToolbox.h>
#import <AVFoundation/AVFoundation.h>
#define kFadeInterval (1.0f/60) // Causes the music volume to be updated 60 times per second when music is fading
// SoundManager provides a basic wrapper for OpenAL and AVAudioPlayer. It is a singleton
// class that allows sound clips to be loaded and cached with a key and then played back
// using that key. It also allows for music tracks to be played, stopped and paused
//
@interface SoundManager : NSObject <AVAudioPlayerDelegate, AVAudioSessionDelegate> {
//////////////////// Sound Setup
ALCcontext *context; // Context in which all sounds will be played
ALCdevice *device; // Reference to the device to use when playing sounds
ALenum alError; // Any OpenAL errors that are rasied
CGPoint listenerPosition; // Location of the OpenAL Listener
NSString *soundCategory; // The sound category to be used by the sound manager
NSMutableArray *soundSources; // Mutable array of all sound sources
AVAudioSession *audioSession; // Reference to an audio session
NSError *audioSessionError; // Audiosession errors are placed in this ivar
//////////////////// Sound dicionaries
NSMutableDictionary *soundLibrary; // Dictionary of all sounds loaded and their keys
NSMutableDictionary *musicLibrary; // Dictionary of all music/ambient sounds loaded and their keys
NSMutableDictionary *musicPlaylists; // Dictionary of playlists
NSMutableArray *currentPlaylistTracks; // Array of tracks for the current play list
//////////////////// Music
AVAudioPlayer *musicPlayer; // AVAudioPlayer instance for the music
//////////////////// Volume
float currentMusicVolume; // Volume of music/ambient sounds played through AVAudioPlayer
float fxVolume; // Volume of OpenAL sound effects
float musicVolume; // The master music volume. This value is not affected by fading music
//////////////////// Fading sound
NSTimer *timer; // Timer used fade the music volume up or down
float fadeAmount; // Amount the volume should be faded each timer call
float fadeDuration; // The amount of time the fade has been running
float targetFadeDuration; // The duration the current fade should run for
//////////////////// Flags
BOOL isExternalAudioPlaying; // YES if music was playing before the sound engine was initialized i.e.
BOOL isFading; // YES if the sound manager is currently fading music
BOOL isMusicPlaying; // YES if music is currently playing
BOOL stopMusicAfterFade; // YES if music is to be stopped once fading has finished
BOOL usePlaylist; // YES if tracks in the playlist should be played one after the other
BOOL loopPlaylist; // YES if the playlist should loop when it reaches the end
BOOL loopLastPlaylistTrack; // YES if you want the last track of the playlist to be looped forever
//////////////////// Playlist tracking
int playlistIndex; // Current index being played in the playlist
NSString *currentPlaylistName; // Holds the name of the currently playing play list
}
@property (nonatomic, assign) float currentMusicVolume;
@property (nonatomic, assign) float fxVolume;
@property (nonatomic, assign) BOOL isExternalAudioPlaying;
@property (nonatomic, assign) BOOL isMusicPlaying;
@property (nonatomic, assign) BOOL usePlaylist;
@property (nonatomic, assign) BOOL loopLastPlaylistTrack;
@property (nonatomic, assign) float musicVolume;
// Returns as instance of the SoundManager class. If an instance has already been created
// then this instance is returned, otherwise a new instance is created and returned.
+ (SoundManager *)sharedSoundManager;
// Designated initializer.
- (id)init;
// Plays the sound which is found with |aSoundKey| using the provided |aGain| and |aPitch|.
// |aLocation| is used to set the location of the sound source in relation to the listener
// and |aLoop| specifies if the sound should be continuously looped or not.
- (NSUInteger)playSoundWithKey:(NSString*)aSoundKey gain:(float)aGain pitch:(float)aPitch
location:(CGPoint)aLocation shouldLoop:(BOOL)aLoop sourceID:(NSUInteger)aSourceID;
// Stops all sounds playing with the supplied sound key
- (void)stopSoundWithKey:(NSString*)aSoundKey;
// Loads a sound with the supplied key, filename, file extension and frequency. Frequency
// could be worked out from the file but this implementation takes it as an argument. If no
// sound is found with a matching key then nothing happens.
- (void)loadSoundWithKey:(NSString*)aSoundKey musicFile:(NSString*)aMusicFile;
// Removes the sound with the supplied key from the sound library. This deletes the buffer that was created
// to hold the sound
- (void)removeSoundWithKey:(NSString*)aSoundKey;
// Plays the music with the supplied key. If no music is found then nothing happens.
// |aRepeatCount| specifies the number of times the music should loop.
- (void)playMusicWithKey:(NSString*)aMusicKey timesToRepeat:(NSUInteger)aRepeatCount;
// Plays the next track in the play list if there is one
- (void)playNextTrack;
// Loads the path of a music files into a dictionary with the a key of |aMusicKey|
- (void)loadBackgroundMusicWithKey:(NSString*)aMusicKey musicFile:(NSString*)aMusicFile;
// Removes the path to the music file with the matching key from the music library array.
- (void)removeBackgroundMusicWithKey:(NSString*)aMusicKey;
// Adds a track to the named play list
- (void)addToPlaylistNamed:(NSString*)aPlaylistName track:(NSString*)aTrackName;
// Plays the contents of the named play list
- (void)startPlaylistNamed:(NSString*)aPlaylistName;
// Removes the named track from the named playlist
- (void)removeFromPlaylistNamed:(NSString*)aPlaylistName track:(NSString*)aTrackName;
// Removes the named playlist
- (void)removePlaylistNamed:(NSString*)aPlaylistName;
// Clears the named playlist
- (void)clearPlaylistNamed:(NSString*)aPlaylistName;
// Stops any currently playing music.
- (void)stopMusic;
// Pauses any currently playing music.
- (void)pauseMusic;
// Resumes music that has been paused
- (void)resumeMusic;
// Fades the music volume down to the specified value over the specified period of time. This method
// does not change the musicVolume for the sound manager which allows you to always get the music volume
// allowing you to fade music down and then back up to the defined value set inside the settings screen for
// example
- (void)fadeMusicVolumeFrom:(float)aFromVolume toVolume:(float)aToVolume duration:(float)aSeconds stop:(BOOL)aStop;
// Cross fades between the currently playing track and the track specified over the duration
// specified
- (void)crossFadeTo:(NSString*)aTrack duration:(float)aDuration;
// Shutsdown the SoundManager class and deallocates resources which have been assigned.
- (void)shutdownSoundManager;
#pragma mark -
#pragma mark SoundManager settings
// Set the volume for music which is played.
- (void)setMusicVolume:(float)aVolume;
// Sets the location of the OpenAL listener.
- (void)setListenerPosition:(CGPoint)aPosition;
// Sets the orientation of the listener. This is used to make sure that sound
// is played correctly based on the direction the player is moving in
- (void)setOrientation:(CGPoint)aPosition;
// Sets the volume for all sounds which are played. This acts as a global FX volume for
// all sounds.
- (void)setFxVolume:(float)aVolume;
@end