diff options
Diffstat (limited to 'libs/CocosDenshion/SimpleAudioEngine.m')
| -rwxr-xr-x | libs/CocosDenshion/SimpleAudioEngine.m | 220 |
1 files changed, 220 insertions, 0 deletions
| diff --git a/libs/CocosDenshion/SimpleAudioEngine.m b/libs/CocosDenshion/SimpleAudioEngine.m new file mode 100755 index 0000000..cdff26c --- /dev/null +++ b/libs/CocosDenshion/SimpleAudioEngine.m | |||
| @@ -0,0 +1,220 @@ | |||
| 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 "SimpleAudioEngine.h" | ||
| 26 | |||
| 27 | @implementation SimpleAudioEngine | ||
| 28 | |||
| 29 | static SimpleAudioEngine *sharedEngine = nil; | ||
| 30 | static CDSoundEngine* soundEngine = nil; | ||
| 31 | static CDAudioManager *am = nil; | ||
| 32 | static CDBufferManager *bufferManager = nil; | ||
| 33 | |||
| 34 | // Init | ||
| 35 | + (SimpleAudioEngine *) sharedEngine | ||
| 36 | { | ||
| 37 | @synchronized(self) { | ||
| 38 | if (!sharedEngine) | ||
| 39 | sharedEngine = [[SimpleAudioEngine alloc] init]; | ||
| 40 | } | ||
| 41 | return sharedEngine; | ||
| 42 | } | ||
| 43 | |||
| 44 | + (id) alloc | ||
| 45 | { | ||
| 46 | @synchronized(self) { | ||
| 47 | NSAssert(sharedEngine == nil, @"Attempted to allocate a second instance of a singleton."); | ||
| 48 | return [super alloc]; | ||
| 49 | } | ||
| 50 | return nil; | ||
| 51 | } | ||
| 52 | |||
| 53 | -(id) init | ||
| 54 | { | ||
| 55 | if((self=[super init])) { | ||
| 56 | am = [CDAudioManager sharedManager]; | ||
| 57 | soundEngine = am.soundEngine; | ||
| 58 | bufferManager = [[CDBufferManager alloc] initWithEngine:soundEngine]; | ||
| 59 | mute_ = NO; | ||
| 60 | enabled_ = YES; | ||
| 61 | } | ||
| 62 | return self; | ||
| 63 | } | ||
| 64 | |||
| 65 | // Memory | ||
| 66 | - (void) dealloc | ||
| 67 | { | ||
| 68 | am = nil; | ||
| 69 | soundEngine = nil; | ||
| 70 | bufferManager = nil; | ||
| 71 | [super dealloc]; | ||
| 72 | } | ||
| 73 | |||
| 74 | +(void) end | ||
| 75 | { | ||
| 76 | am = nil; | ||
| 77 | [CDAudioManager end]; | ||
| 78 | [bufferManager release]; | ||
| 79 | [sharedEngine release]; | ||
| 80 | sharedEngine = nil; | ||
| 81 | } | ||
| 82 | |||
| 83 | #pragma mark SimpleAudioEngine - background music | ||
| 84 | |||
| 85 | -(void) preloadBackgroundMusic:(NSString*) filePath { | ||
| 86 | [am preloadBackgroundMusic:filePath]; | ||
| 87 | } | ||
| 88 | |||
| 89 | -(void) playBackgroundMusic:(NSString*) filePath | ||
| 90 | { | ||
| 91 | [am playBackgroundMusic:filePath loop:TRUE]; | ||
| 92 | } | ||
| 93 | |||
| 94 | -(void) playBackgroundMusic:(NSString*) filePath loop:(BOOL) loop | ||
| 95 | { | ||
| 96 | [am playBackgroundMusic:filePath loop:loop]; | ||
| 97 | } | ||
| 98 | |||
| 99 | -(void) stopBackgroundMusic | ||
| 100 | { | ||
| 101 | [am stopBackgroundMusic]; | ||
| 102 | } | ||
| 103 | |||
| 104 | -(void) pauseBackgroundMusic { | ||
| 105 | [am pauseBackgroundMusic]; | ||
| 106 | } | ||
| 107 | |||
| 108 | -(void) resumeBackgroundMusic { | ||
| 109 | [am resumeBackgroundMusic]; | ||
| 110 | } | ||
| 111 | |||
| 112 | -(void) rewindBackgroundMusic { | ||
| 113 | [am rewindBackgroundMusic]; | ||
| 114 | } | ||
| 115 | |||
| 116 | -(BOOL) isBackgroundMusicPlaying { | ||
| 117 | return [am isBackgroundMusicPlaying]; | ||
| 118 | } | ||
| 119 | |||
| 120 | -(BOOL) willPlayBackgroundMusic { | ||
| 121 | return [am willPlayBackgroundMusic]; | ||
| 122 | } | ||
| 123 | |||
| 124 | #pragma mark SimpleAudioEngine - sound effects | ||
| 125 | |||
| 126 | -(ALuint) playEffect:(NSString*) filePath | ||
| 127 | { | ||
| 128 | return [self playEffect:filePath pitch:1.0f pan:0.0f gain:1.0f]; | ||
| 129 | } | ||
| 130 | |||
| 131 | -(ALuint) playEffect:(NSString*) filePath pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain | ||
| 132 | { | ||
| 133 | int soundId = [bufferManager bufferForFile:filePath create:YES]; | ||
| 134 | if (soundId != kCDNoBuffer) { | ||
| 135 | return [soundEngine playSound:soundId sourceGroupId:0 pitch:pitch pan:pan gain:gain loop:false]; | ||
| 136 | } else { | ||
| 137 | return CD_MUTE; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | -(void) stopEffect:(ALuint) soundId { | ||
| 142 | [soundEngine stopSound:soundId]; | ||
| 143 | } | ||
| 144 | |||
| 145 | -(void) preloadEffect:(NSString*) filePath | ||
| 146 | { | ||
| 147 | int soundId = [bufferManager bufferForFile:filePath create:YES]; | ||
| 148 | if (soundId == kCDNoBuffer) { | ||
| 149 | CDLOG(@"Denshion::SimpleAudioEngine sound failed to preload %@",filePath); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | -(void) unloadEffect:(NSString*) filePath | ||
| 154 | { | ||
| 155 | CDLOGINFO(@"Denshion::SimpleAudioEngine unloadedEffect %@",filePath); | ||
| 156 | [bufferManager releaseBufferForFile:filePath]; | ||
| 157 | } | ||
| 158 | |||
| 159 | #pragma mark Audio Interrupt Protocol | ||
| 160 | -(BOOL) mute | ||
| 161 | { | ||
| 162 | return mute_; | ||
| 163 | } | ||
| 164 | |||
| 165 | -(void) setMute:(BOOL) muteValue | ||
| 166 | { | ||
| 167 | if (mute_ != muteValue) { | ||
| 168 | mute_ = muteValue; | ||
| 169 | am.mute = mute_; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | -(BOOL) enabled | ||
| 174 | { | ||
| 175 | return enabled_; | ||
| 176 | } | ||
| 177 | |||
| 178 | -(void) setEnabled:(BOOL) enabledValue | ||
| 179 | { | ||
| 180 | if (enabled_ != enabledValue) { | ||
| 181 | enabled_ = enabledValue; | ||
| 182 | am.enabled = enabled_; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | #pragma mark SimpleAudioEngine - BackgroundMusicVolume | ||
| 188 | -(float) backgroundMusicVolume | ||
| 189 | { | ||
| 190 | return am.backgroundMusic.volume; | ||
| 191 | } | ||
| 192 | |||
| 193 | -(void) setBackgroundMusicVolume:(float) volume | ||
| 194 | { | ||
| 195 | am.backgroundMusic.volume = volume; | ||
| 196 | } | ||
| 197 | |||
| 198 | #pragma mark SimpleAudioEngine - EffectsVolume | ||
| 199 | -(float) effectsVolume | ||
| 200 | { | ||
| 201 | return am.soundEngine.masterGain; | ||
| 202 | } | ||
| 203 | |||
| 204 | -(void) setEffectsVolume:(float) volume | ||
| 205 | { | ||
| 206 | am.soundEngine.masterGain = volume; | ||
| 207 | } | ||
| 208 | |||
| 209 | -(CDSoundSource *) soundSourceForFile:(NSString*) filePath { | ||
| 210 | int soundId = [bufferManager bufferForFile:filePath create:YES]; | ||
| 211 | if (soundId != kCDNoBuffer) { | ||
| 212 | CDSoundSource *result = [soundEngine soundSourceForSound:soundId sourceGroupId:0]; | ||
| 213 | CDLOGINFO(@"Denshion::SimpleAudioEngine sound source created for %@",filePath); | ||
| 214 | return result; | ||
| 215 | } else { | ||
| 216 | return nil; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | @end | ||
