diff options
Diffstat (limited to 'libs/CocosDenshion/CDAudioManager.h')
-rwxr-xr-x | libs/CocosDenshion/CDAudioManager.h | 243 |
1 files changed, 243 insertions, 0 deletions
diff --git a/libs/CocosDenshion/CDAudioManager.h b/libs/CocosDenshion/CDAudioManager.h new file mode 100755 index 0000000..2475929 --- /dev/null +++ b/libs/CocosDenshion/CDAudioManager.h | |||
@@ -0,0 +1,243 @@ | |||
1 | /* | ||
2 | Copyright (c) 2010 Steve Oldmeadow | ||
3 | |||
4 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
5 | of this software and associated documentation files (the "Software"), to deal | ||
6 | in the Software without restriction, including without limitation the rights | ||
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
8 | copies of the Software, and to permit persons to whom the Software is | ||
9 | furnished to do so, subject to the following conditions: | ||
10 | |||
11 | The above copyright notice and this permission notice shall be included in | ||
12 | all copies or substantial portions of the Software. | ||
13 | |||
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
20 | THE SOFTWARE. | ||
21 | |||
22 | $Id$ | ||
23 | */ | ||
24 | |||
25 | #import "CocosDenshion.h" | ||
26 | #if __IPHONE_OS_VERSION_MIN_REQUIRED >= 30000 | ||
27 | #import <AVFoundation/AVFoundation.h> | ||
28 | #else | ||
29 | #import "CDXMacOSXSupport.h" | ||
30 | #endif | ||
31 | |||
32 | /** Different modes of the engine */ | ||
33 | typedef enum { | ||
34 | kAMM_FxOnly, //!Other apps will be able to play audio | ||
35 | kAMM_FxPlusMusic, //!Only this app will play audio | ||
36 | kAMM_FxPlusMusicIfNoOtherAudio, //!If another app is playing audio at start up then allow it to continue and don't play music | ||
37 | kAMM_MediaPlayback, //!This app takes over audio e.g music player app | ||
38 | kAMM_PlayAndRecord //!App takes over audio and has input and output | ||
39 | } tAudioManagerMode; | ||
40 | |||
41 | /** Possible states of the engine */ | ||
42 | typedef enum { | ||
43 | kAMStateUninitialised, //!Audio manager has not been initialised - do not use | ||
44 | kAMStateInitialising, //!Audio manager is in the process of initialising - do not use | ||
45 | kAMStateInitialised //!Audio manager is initialised - safe to use | ||
46 | } tAudioManagerState; | ||
47 | |||
48 | typedef enum { | ||
49 | kAMRBDoNothing, //Audio manager will not do anything on resign or becoming active | ||
50 | kAMRBStopPlay, //Background music is stopped on resign and resumed on become active | ||
51 | kAMRBStop //Background music is stopped on resign but not resumed - maybe because you want to do this from within your game | ||
52 | } tAudioManagerResignBehavior; | ||
53 | |||
54 | /** Notifications */ | ||
55 | extern NSString * const kCDN_AudioManagerInitialised; | ||
56 | |||
57 | @interface CDAsynchInitialiser : NSOperation {} | ||
58 | @end | ||
59 | |||
60 | /** CDAudioManager supports two long audio source channels called left and right*/ | ||
61 | typedef enum { | ||
62 | kASC_Left = 0, | ||
63 | kASC_Right = 1 | ||
64 | } tAudioSourceChannel; | ||
65 | |||
66 | typedef enum { | ||
67 | kLAS_Init, | ||
68 | kLAS_Loaded, | ||
69 | kLAS_Playing, | ||
70 | kLAS_Paused, | ||
71 | kLAS_Stopped, | ||
72 | } tLongAudioSourceState; | ||
73 | |||
74 | @class CDLongAudioSource; | ||
75 | @protocol CDLongAudioSourceDelegate <NSObject> | ||
76 | @optional | ||
77 | /** The audio source completed playing */ | ||
78 | - (void) cdAudioSourceDidFinishPlaying:(CDLongAudioSource *) audioSource; | ||
79 | /** The file used to load the audio source has changed */ | ||
80 | - (void) cdAudioSourceFileDidChange:(CDLongAudioSource *) audioSource; | ||
81 | @end | ||
82 | |||
83 | /** | ||
84 | CDLongAudioSource represents an audio source that has a long duration which makes | ||
85 | it costly to load into memory for playback as an effect using CDSoundEngine. Examples | ||
86 | include background music and narration tracks. The audio file may or may not be compressed. | ||
87 | Bear in mind that current iDevices can only use hardware to decode a single compressed | ||
88 | audio file at a time and playing multiple compressed files will result in a performance drop | ||
89 | as software decompression will take place. | ||
90 | @since v0.99 | ||
91 | */ | ||
92 | @interface CDLongAudioSource : NSObject <AVAudioPlayerDelegate, CDAudioInterruptProtocol>{ | ||
93 | AVAudioPlayer *audioSourcePlayer; | ||
94 | NSString *audioSourceFilePath; | ||
95 | NSInteger numberOfLoops; | ||
96 | float volume; | ||
97 | id<CDLongAudioSourceDelegate> delegate; | ||
98 | BOOL mute; | ||
99 | BOOL enabled_; | ||
100 | BOOL backgroundMusic; | ||
101 | @public | ||
102 | BOOL systemPaused;//Used for auto resign handling | ||
103 | NSTimeInterval systemPauseLocation;//Used for auto resign handling | ||
104 | @protected | ||
105 | tLongAudioSourceState state; | ||
106 | } | ||
107 | @property (readonly) AVAudioPlayer *audioSourcePlayer; | ||
108 | @property (readonly) NSString *audioSourceFilePath; | ||
109 | @property (readwrite, nonatomic) NSInteger numberOfLoops; | ||
110 | @property (readwrite, nonatomic) float volume; | ||
111 | @property (assign) id<CDLongAudioSourceDelegate> delegate; | ||
112 | /* This long audio source functions as background music */ | ||
113 | @property (readwrite, nonatomic) BOOL backgroundMusic; | ||
114 | |||
115 | /** Loads the file into the audio source */ | ||
116 | -(void) load:(NSString*) filePath; | ||
117 | /** Plays the audio source */ | ||
118 | -(void) play; | ||
119 | /** Stops playing the audio soruce */ | ||
120 | -(void) stop; | ||
121 | /** Pauses the audio source */ | ||
122 | -(void) pause; | ||
123 | /** Rewinds the audio source */ | ||
124 | -(void) rewind; | ||
125 | /** Resumes playing the audio source if it was paused */ | ||
126 | -(void) resume; | ||
127 | /** Returns whether or not the audio source is playing */ | ||
128 | -(BOOL) isPlaying; | ||
129 | |||
130 | @end | ||
131 | |||
132 | /** | ||
133 | CDAudioManager manages audio requirements for a game. It provides access to a CDSoundEngine object | ||
134 | for playing sound effects. It provides access to two CDLongAudioSource object (left and right channel) | ||
135 | for playing long duration audio such as background music and narration tracks. Additionally it manages | ||
136 | the audio session to take care of things like audio session interruption and interacting with the audio | ||
137 | of other apps that are running on the device. | ||
138 | |||
139 | Requirements: | ||
140 | - Firmware: OS 2.2 or greater | ||
141 | - Files: CDAudioManager.*, CocosDenshion.* | ||
142 | - Frameworks: OpenAL, AudioToolbox, AVFoundation | ||
143 | @since v0.8 | ||
144 | */ | ||
145 | @interface CDAudioManager : NSObject <CDLongAudioSourceDelegate, CDAudioInterruptProtocol, AVAudioSessionDelegate> { | ||
146 | CDSoundEngine *soundEngine; | ||
147 | CDLongAudioSource *backgroundMusic; | ||
148 | NSMutableArray *audioSourceChannels; | ||
149 | NSString* _audioSessionCategory; | ||
150 | BOOL _audioWasPlayingAtStartup; | ||
151 | tAudioManagerMode _mode; | ||
152 | SEL backgroundMusicCompletionSelector; | ||
153 | id backgroundMusicCompletionListener; | ||
154 | BOOL willPlayBackgroundMusic; | ||
155 | BOOL _mute; | ||
156 | BOOL _resigned; | ||
157 | BOOL _interrupted; | ||
158 | BOOL _audioSessionActive; | ||
159 | BOOL enabled_; | ||
160 | |||
161 | //For handling resign/become active | ||
162 | BOOL _isObservingAppEvents; | ||
163 | tAudioManagerResignBehavior _resignBehavior; | ||
164 | } | ||
165 | |||
166 | @property (readonly) CDSoundEngine *soundEngine; | ||
167 | @property (readonly) CDLongAudioSource *backgroundMusic; | ||
168 | @property (readonly) BOOL willPlayBackgroundMusic; | ||
169 | |||
170 | /** Returns the shared singleton */ | ||
171 | + (CDAudioManager *) sharedManager; | ||
172 | + (tAudioManagerState) sharedManagerState; | ||
173 | /** Configures the shared singleton with a mode*/ | ||
174 | + (void) configure: (tAudioManagerMode) mode; | ||
175 | /** Initializes the engine asynchronously with a mode */ | ||
176 | + (void) initAsynchronously: (tAudioManagerMode) mode; | ||
177 | /** Initializes the engine synchronously with a mode, channel definition and a total number of channels */ | ||
178 | - (id) init: (tAudioManagerMode) mode; | ||
179 | -(void) audioSessionInterrupted; | ||
180 | -(void) audioSessionResumed; | ||
181 | -(void) setResignBehavior:(tAudioManagerResignBehavior) resignBehavior autoHandle:(BOOL) autoHandle; | ||
182 | /** Returns true is audio is muted at a hardware level e.g user has ringer switch set to off */ | ||
183 | -(BOOL) isDeviceMuted; | ||
184 | /** Returns true if another app is playing audio such as the iPod music player */ | ||
185 | -(BOOL) isOtherAudioPlaying; | ||
186 | /** Sets the way the audio manager interacts with the operating system such as whether it shares output with other apps or obeys the mute switch */ | ||
187 | -(void) setMode:(tAudioManagerMode) mode; | ||
188 | /** Shuts down the shared audio manager instance so that it can be reinitialised */ | ||
189 | +(void) end; | ||
190 | |||
191 | /** Call if you want to use built in resign behavior but need to do some additional audio processing on resign active. */ | ||
192 | - (void) applicationWillResignActive; | ||
193 | /** Call if you want to use built in resign behavior but need to do some additional audio processing on become active. */ | ||
194 | - (void) applicationDidBecomeActive; | ||
195 | |||
196 | //New AVAudioPlayer API | ||
197 | /** Loads the data from the specified file path to the channel's audio source */ | ||
198 | -(CDLongAudioSource*) audioSourceLoad:(NSString*) filePath channel:(tAudioSourceChannel) channel; | ||
199 | /** Retrieves the audio source for the specified channel */ | ||
200 | -(CDLongAudioSource*) audioSourceForChannel:(tAudioSourceChannel) channel; | ||
201 | |||
202 | //Legacy AVAudioPlayer API | ||
203 | /** Plays music in background. The music can be looped or not | ||
204 | It is recommended to use .aac files as background music since they are decoded by the device (hardware). | ||
205 | */ | ||
206 | -(void) playBackgroundMusic:(NSString*) filePath loop:(BOOL) loop; | ||
207 | /** Preloads a background music */ | ||
208 | -(void) preloadBackgroundMusic:(NSString*) filePath; | ||
209 | /** Stops playing the background music */ | ||
210 | -(void) stopBackgroundMusic; | ||
211 | /** Pauses the background music */ | ||
212 | -(void) pauseBackgroundMusic; | ||
213 | /** Rewinds the background music */ | ||
214 | -(void) rewindBackgroundMusic; | ||
215 | /** Resumes playing the background music */ | ||
216 | -(void) resumeBackgroundMusic; | ||
217 | /** Returns whether or not the background music is playing */ | ||
218 | -(BOOL) isBackgroundMusicPlaying; | ||
219 | |||
220 | -(void) setBackgroundMusicCompletionListener:(id) listener selector:(SEL) selector; | ||
221 | |||
222 | @end | ||
223 | |||
224 | /** Fader for long audio source objects */ | ||
225 | @interface CDLongAudioSourceFader : CDPropertyModifier{} | ||
226 | @end | ||
227 | |||
228 | static const int kCDNoBuffer = -1; | ||
229 | |||
230 | /** Allows buffers to be associated with file names */ | ||
231 | @interface CDBufferManager:NSObject{ | ||
232 | NSMutableDictionary* loadedBuffers; | ||
233 | NSMutableArray *freedBuffers; | ||
234 | CDSoundEngine *soundEngine; | ||
235 | int nextBufferId; | ||
236 | } | ||
237 | |||
238 | -(id) initWithEngine:(CDSoundEngine *) theSoundEngine; | ||
239 | -(int) bufferForFile:(NSString*) filePath create:(BOOL) create; | ||
240 | -(void) releaseBufferForFile:(NSString *) filePath; | ||
241 | |||
242 | @end | ||
243 | |||