|
cocos2d for iPhone 1.0.0
2D engine for iOS and OS X
|
00001 /* 00002 Copyright (c) 2010 Steve Oldmeadow 00003 00004 Permission is hereby granted, free of charge, to any person obtaining a copy 00005 of this software and associated documentation files (the "Software"), to deal 00006 in the Software without restriction, including without limitation the rights 00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 copies of the Software, and to permit persons to whom the Software is 00009 furnished to do so, subject to the following conditions: 00010 00011 The above copyright notice and this permission notice shall be included in 00012 all copies or substantial portions of the Software. 00013 00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00020 THE SOFTWARE. 00021 00022 $Id$ 00023 */ 00024 00025 00026 00070 #import <OpenAL/al.h> 00071 #import <OpenAL/alc.h> 00072 #import <AudioToolbox/AudioToolbox.h> 00073 #import <Foundation/Foundation.h> 00074 #import "CDConfig.h" 00075 00076 00077 #if !defined(CD_DEBUG) || CD_DEBUG == 0 00078 #define CDLOG(...) do {} while (0) 00079 #define CDLOGINFO(...) do {} while (0) 00080 00081 #elif CD_DEBUG == 1 00082 #define CDLOG(...) NSLog(__VA_ARGS__) 00083 #define CDLOGINFO(...) do {} while (0) 00084 00085 #elif CD_DEBUG > 1 00086 #define CDLOG(...) NSLog(__VA_ARGS__) 00087 #define CDLOGINFO(...) NSLog(__VA_ARGS__) 00088 #endif // CD_DEBUG 00089 00090 00091 #import "CDOpenALSupport.h" 00092 00093 //Tested source limit on 2.2.1 and 3.1.2 with up to 128 sources and appears to work. Older OS versions e.g 2.2 may support only 32 00094 #define CD_SOURCE_LIMIT 32 //Total number of sources we will ever want, may actually get less 00095 #define CD_NO_SOURCE 0xFEEDFAC //Return value indicating playback failed i.e. no source 00096 #define CD_IGNORE_AUDIO_SESSION 0xBEEFBEE //Used internally to indicate audio session will not be handled 00097 #define CD_MUTE 0xFEEDBAB //Return value indicating sound engine is muted or non functioning 00098 #define CD_NO_SOUND = -1; 00099 00100 #define CD_SAMPLE_RATE_HIGH 44100 00101 #define CD_SAMPLE_RATE_MID 22050 00102 #define CD_SAMPLE_RATE_LOW 16000 00103 #define CD_SAMPLE_RATE_BASIC 8000 00104 #define CD_SAMPLE_RATE_DEFAULT 44100 00105 00106 extern NSString * const kCDN_BadAlContext; 00107 extern NSString * const kCDN_AsynchLoadComplete; 00108 00109 extern float const kCD_PitchDefault; 00110 extern float const kCD_PitchLowerOneOctave; 00111 extern float const kCD_PitchHigherOneOctave; 00112 extern float const kCD_PanDefault; 00113 extern float const kCD_PanFullLeft; 00114 extern float const kCD_PanFullRight; 00115 extern float const kCD_GainDefault; 00116 00117 enum bufferState { 00118 CD_BS_EMPTY = 0, 00119 CD_BS_LOADED = 1, 00120 CD_BS_FAILED = 2 00121 }; 00122 00123 typedef struct _sourceGroup { 00124 int startIndex; 00125 int currentIndex; 00126 int totalSources; 00127 bool enabled; 00128 bool nonInterruptible; 00129 int *sourceStatuses;//pointer into array of source status information 00130 } sourceGroup; 00131 00132 typedef struct _bufferInfo { 00133 ALuint bufferId; 00134 int bufferState; 00135 void* bufferData; 00136 ALenum format; 00137 ALsizei sizeInBytes; 00138 ALsizei frequencyInHertz; 00139 } bufferInfo; 00140 00141 typedef struct _sourceInfo { 00142 bool usable; 00143 ALuint sourceId; 00144 ALuint attachedBufferId; 00145 } sourceInfo; 00146 00147 #pragma mark CDAudioTransportProtocol 00148 00149 @protocol CDAudioTransportProtocol <NSObject> 00151 -(BOOL) play; 00153 -(BOOL) pause; 00155 -(BOOL) stop; 00157 -(BOOL) rewind; 00158 @end 00159 00160 #pragma mark CDAudioInterruptProtocol 00161 00162 @protocol CDAudioInterruptProtocol <NSObject> 00164 -(BOOL) mute; 00166 -(void) setMute:(BOOL) muteValue; 00168 -(BOOL) enabled; 00170 -(void) setEnabled:(BOOL) enabledValue; 00171 @end 00172 00173 #pragma mark CDUtilities 00174 00177 @interface CDUtilities : NSObject 00178 { 00179 } 00180 00182 +(NSString*) fullPathFromRelativePath:(NSString*) relPath; 00183 00184 @end 00185 00186 00187 #pragma mark CDSoundEngine 00188 00203 @class CDSoundSource; 00204 @interface CDSoundEngine : NSObject <CDAudioInterruptProtocol> { 00205 00206 bufferInfo *_buffers; 00207 sourceInfo *_sources; 00208 sourceGroup *_sourceGroups; 00209 ALCcontext *context; 00210 NSUInteger _sourceGroupTotal; 00211 UInt32 _audioSessionCategory; 00212 BOOL _handleAudioSession; 00213 ALfloat _preMuteGain; 00214 NSObject *_mutexBufferLoad; 00215 BOOL mute_; 00216 BOOL enabled_; 00217 00218 ALenum lastErrorCode_; 00219 BOOL functioning_; 00220 float asynchLoadProgress_; 00221 BOOL getGainWorks_; 00222 00223 //For managing dynamic allocation of sources and buffers 00224 int sourceTotal_; 00225 int bufferTotal; 00226 00227 } 00228 00229 @property (readwrite, nonatomic) ALfloat masterGain; 00230 @property (readonly) ALenum lastErrorCode;//Last OpenAL error code that was generated 00231 @property (readonly) BOOL functioning;//Is the sound engine functioning 00232 @property (readwrite) float asynchLoadProgress; 00233 @property (readonly) BOOL getGainWorks;//Does getting the gain for a source work 00235 @property (readonly) int sourceTotal; 00237 @property (readonly) NSUInteger sourceGroupTotal; 00238 00240 +(void) setMixerSampleRate:(Float32) sampleRate; 00241 00243 -(id)init; 00244 00246 -(ALuint) playSound:(int) soundId sourceGroupId:(int)sourceGroupId pitch:(float) pitch pan:(float) pan gain:(float) gain loop:(BOOL) loop; 00247 00250 -(CDSoundSource *) soundSourceForSound:(int) soundId sourceGroupId:(int) sourceGroupId; 00251 00253 - (void) stopSound:(ALuint) sourceId; 00255 - (void) stopSourceGroup:(int) sourceGroupId; 00257 -(void) stopAllSounds; 00258 -(void) defineSourceGroups:(NSArray*) sourceGroupDefinitions; 00259 -(void) defineSourceGroups:(int[]) sourceGroupDefinitions total:(NSUInteger) total; 00260 -(void) setSourceGroupNonInterruptible:(int) sourceGroupId isNonInterruptible:(BOOL) isNonInterruptible; 00261 -(void) setSourceGroupEnabled:(int) sourceGroupId enabled:(BOOL) enabled; 00262 -(BOOL) sourceGroupEnabled:(int) sourceGroupId; 00263 -(BOOL) loadBufferFromData:(int) soundId soundData:(ALvoid*) soundData format:(ALenum) format size:(ALsizei) size freq:(ALsizei) freq; 00264 -(BOOL) loadBuffer:(int) soundId filePath:(NSString*) filePath; 00265 -(void) loadBuffersAsynchronously:(NSArray *) loadRequests; 00266 -(BOOL) unloadBuffer:(int) soundId; 00267 -(ALCcontext *) openALContext; 00268 00270 -(float) bufferDurationInSeconds:(int) soundId; 00272 -(ALsizei) bufferSizeInBytes:(int) soundId; 00274 -(ALsizei) bufferFrequencyInHertz:(int) soundId; 00275 00277 -(void) _soundSourcePreRelease:(CDSoundSource *) soundSource; 00278 00279 @end 00280 00281 #pragma mark CDSoundSource 00282 00289 @interface CDSoundSource : NSObject <CDAudioTransportProtocol, CDAudioInterruptProtocol> { 00290 ALenum lastError; 00291 @public 00292 ALuint _sourceId; 00293 ALuint _sourceIndex; 00294 CDSoundEngine* _engine; 00295 int _soundId; 00296 float _preMuteGain; 00297 BOOL enabled_; 00298 BOOL mute_; 00299 } 00300 @property (readwrite, nonatomic) float pitch; 00301 @property (readwrite, nonatomic) float gain; 00302 @property (readwrite, nonatomic) float pan; 00303 @property (readwrite, nonatomic) BOOL looping; 00304 @property (readonly) BOOL isPlaying; 00305 @property (readwrite, nonatomic) int soundId; 00307 @property (readonly) float durationInSeconds; 00308 00310 @property (readonly) ALenum lastError; 00312 -(id)init:(ALuint) theSourceId sourceIndex:(int) index soundEngine:(CDSoundEngine*) engine; 00313 00314 @end 00315 00316 #pragma mark CDAudioInterruptTargetGroup 00317 00322 @interface CDAudioInterruptTargetGroup : NSObject <CDAudioInterruptProtocol> { 00323 BOOL mute_; 00324 BOOL enabled_; 00325 NSMutableArray *children_; 00326 } 00327 -(void) addAudioInterruptTarget:(NSObject<CDAudioInterruptProtocol>*) interruptibleTarget; 00328 @end 00329 00330 #pragma mark CDAsynchBufferLoader 00331 00335 @interface CDAsynchBufferLoader : NSOperation { 00336 NSArray *_loadRequests; 00337 CDSoundEngine *_soundEngine; 00338 } 00339 00340 -(id) init:(NSArray *)loadRequests soundEngine:(CDSoundEngine *) theSoundEngine; 00341 00342 @end 00343 00344 #pragma mark CDBufferLoadRequest 00345 00347 @interface CDBufferLoadRequest: NSObject 00348 { 00349 NSString *filePath; 00350 int soundId; 00351 //id loader; 00352 } 00353 00354 @property (readonly) NSString *filePath; 00355 @property (readonly) int soundId; 00356 00357 - (id)init:(int) theSoundId filePath:(const NSString *) theFilePath; 00358 @end 00359 00361 typedef enum { 00362 kIT_Linear, 00363 kIT_SCurve, 00364 kIT_Exponential 00365 } tCDInterpolationType; 00366 00367 #pragma mark CDFloatInterpolator 00368 @interface CDFloatInterpolator: NSObject 00369 { 00370 float start; 00371 float end; 00372 float lastValue; 00373 tCDInterpolationType interpolationType; 00374 } 00375 @property (readwrite, nonatomic) float start; 00376 @property (readwrite, nonatomic) float end; 00377 @property (readwrite, nonatomic) tCDInterpolationType interpolationType; 00378 00381 -(float) interpolate:(float) t; 00382 -(id) init:(tCDInterpolationType) type startVal:(float) startVal endVal:(float) endVal; 00383 00384 @end 00385 00386 #pragma mark CDPropertyModifier 00387 00389 @interface CDPropertyModifier: NSObject 00390 { 00391 CDFloatInterpolator *interpolator; 00392 float startValue; 00393 float endValue; 00394 id target; 00395 BOOL stopTargetWhenComplete; 00396 00397 } 00398 @property (readwrite, nonatomic) BOOL stopTargetWhenComplete; 00399 @property (readwrite, nonatomic) float startValue; 00400 @property (readwrite, nonatomic) float endValue; 00401 @property (readwrite, nonatomic) tCDInterpolationType interpolationType; 00402 00403 -(id) init:(id) theTarget interpolationType:(tCDInterpolationType) type startVal:(float) startVal endVal:(float) endVal; 00405 -(void) modify:(float) t; 00406 00407 -(void) _setTargetProperty:(float) newVal; 00408 -(float) _getTargetProperty; 00409 -(void) _stopTarget; 00410 -(Class) _allowableType; 00411 00412 @end 00413 00414 #pragma mark CDSoundSourceFader 00415 00417 @interface CDSoundSourceFader : CDPropertyModifier{} 00418 @end 00419 00420 #pragma mark CDSoundSourcePanner 00421 00423 @interface CDSoundSourcePanner : CDPropertyModifier{} 00424 @end 00425 00426 #pragma mark CDSoundSourcePitchBender 00427 00429 @interface CDSoundSourcePitchBender : CDPropertyModifier{} 00430 @end 00431 00432 #pragma mark CDSoundEngineFader 00433 00435 @interface CDSoundEngineFader : CDPropertyModifier{} 00436 @end 00437 00438 00439 00440