1
0

SimpleAudioEngine_objc.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. Copyright (c) 2010 Steve Oldmeadow
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. $Id$
  19. */
  20. #import "audio/mac/SimpleAudioEngine_objc.h"
  21. @implementation SimpleAudioEngine
  22. static SimpleAudioEngine *sharedEngine = nil;
  23. static CDSoundEngine* soundEngine = nil;
  24. static CDAudioManager *am = nil;
  25. static CDBufferManager *bufferManager = nil;
  26. // Init
  27. + (SimpleAudioEngine *) sharedEngine
  28. {
  29. @synchronized(self) {
  30. if (!sharedEngine)
  31. sharedEngine = [[SimpleAudioEngine alloc] init];
  32. }
  33. return sharedEngine;
  34. }
  35. + (id) alloc
  36. {
  37. @synchronized(self) {
  38. NSAssert(sharedEngine == nil, @"Attempted to allocate a second instance of a singleton.");
  39. return [super alloc];
  40. }
  41. return nil;
  42. }
  43. -(id) init
  44. {
  45. if((self=[super init])) {
  46. am = [CDAudioManager sharedManager];
  47. soundEngine = am.soundEngine;
  48. bufferManager = [[CDBufferManager alloc] initWithEngine:soundEngine];
  49. mute_ = NO;
  50. enabled_ = YES;
  51. }
  52. return self;
  53. }
  54. // Memory
  55. - (void) dealloc
  56. {
  57. am = nil;
  58. soundEngine = nil;
  59. bufferManager = nil;
  60. [super dealloc];
  61. }
  62. +(void) end
  63. {
  64. am = nil;
  65. [CDAudioManager end];
  66. [bufferManager release];
  67. [sharedEngine release];
  68. sharedEngine = nil;
  69. }
  70. #pragma mark SimpleAudioEngine - background music
  71. -(void) preloadBackgroundMusic:(NSString*) filePath {
  72. [am preloadBackgroundMusic:filePath];
  73. }
  74. -(void) playBackgroundMusic:(NSString*) filePath
  75. {
  76. [am playBackgroundMusic:filePath loop:TRUE];
  77. }
  78. -(void) playBackgroundMusic:(NSString*) filePath loop:(BOOL) loop
  79. {
  80. [am playBackgroundMusic:filePath loop:loop];
  81. }
  82. -(void) stopBackgroundMusic
  83. {
  84. [am stopBackgroundMusic];
  85. }
  86. -(void) pauseBackgroundMusic {
  87. [am pauseBackgroundMusic];
  88. }
  89. -(void) resumeBackgroundMusic {
  90. [am resumeBackgroundMusic];
  91. }
  92. -(void) rewindBackgroundMusic {
  93. [am rewindBackgroundMusic];
  94. }
  95. -(BOOL) isBackgroundMusicPlaying {
  96. return [am isBackgroundMusicPlaying];
  97. }
  98. -(BOOL) willPlayBackgroundMusic {
  99. return [am willPlayBackgroundMusic];
  100. }
  101. #pragma mark SimpleAudioEngine - sound effects
  102. -(ALuint) playEffect:(NSString*) filePath loop:(BOOL) loop
  103. {
  104. return [self playEffect:filePath loop:loop pitch:1.0f pan:0.0f gain:1.0f];
  105. }
  106. -(ALuint) playEffect:(NSString*) filePath loop:(BOOL) loop pitch:(Float32) pitch pan:(Float32) pan gain:(Float32) gain
  107. {
  108. int soundId = [bufferManager bufferForFile:filePath create:YES];
  109. if (soundId != kCDNoBuffer) {
  110. return [soundEngine playSound:soundId sourceGroupId:0 pitch:pitch pan:pan gain:gain loop:loop];
  111. } else {
  112. return CD_MUTE;
  113. }
  114. }
  115. -(void) stopEffect:(ALuint) soundId {
  116. [soundEngine stopSound:soundId];
  117. }
  118. -(void) pauseEffect:(ALuint) soundId {
  119. [soundEngine pauseSound: soundId];
  120. }
  121. -(void) pauseAllEffects {
  122. [soundEngine pauseAllSounds];
  123. }
  124. -(void) resumeEffect:(ALuint) soundId {
  125. [soundEngine resumeSound: soundId];
  126. }
  127. -(void) resumeAllEffects {
  128. [soundEngine resumeAllSounds];
  129. }
  130. -(void) stopAllEffects {
  131. [soundEngine stopAllSounds];
  132. }
  133. -(void) preloadEffect:(NSString*) filePath
  134. {
  135. int soundId = [bufferManager bufferForFile:filePath create:YES];
  136. if (soundId == kCDNoBuffer) {
  137. CDLOG(@"Denshion::SimpleAudioEngine sound failed to preload %@",filePath);
  138. }
  139. }
  140. -(void) unloadEffect:(NSString*) filePath
  141. {
  142. CDLOGINFO(@"Denshion::SimpleAudioEngine unloadedEffect %@",filePath);
  143. [bufferManager releaseBufferForFile:filePath];
  144. }
  145. #pragma mark Audio Interrupt Protocol
  146. -(BOOL) mute
  147. {
  148. return mute_;
  149. }
  150. -(void) setMute:(BOOL) muteValue
  151. {
  152. if (mute_ != muteValue) {
  153. mute_ = muteValue;
  154. am.mute = mute_;
  155. }
  156. }
  157. -(BOOL) enabled
  158. {
  159. return enabled_;
  160. }
  161. -(void) setEnabled:(BOOL) enabledValue
  162. {
  163. if (enabled_ != enabledValue) {
  164. enabled_ = enabledValue;
  165. am.enabled = enabled_;
  166. }
  167. }
  168. #pragma mark SimpleAudioEngine - BackgroundMusicVolume
  169. -(float) backgroundMusicVolume
  170. {
  171. return am.backgroundMusic.volume;
  172. }
  173. -(void) setBackgroundMusicVolume:(float) volume
  174. {
  175. am.backgroundMusic.volume = volume;
  176. }
  177. #pragma mark SimpleAudioEngine - EffectsVolume
  178. -(float) effectsVolume
  179. {
  180. return am.soundEngine.masterGain;
  181. }
  182. -(void) setEffectsVolume:(float) volume
  183. {
  184. am.soundEngine.masterGain = volume;
  185. }
  186. -(CDSoundSource *) soundSourceForFile:(NSString*) filePath {
  187. int soundId = [bufferManager bufferForFile:filePath create:YES];
  188. if (soundId != kCDNoBuffer) {
  189. CDSoundSource *result = [soundEngine soundSourceForSound:soundId sourceGroupId:0];
  190. CDLOGINFO(@"Denshion::SimpleAudioEngine sound source created for %@",filePath);
  191. return result;
  192. } else {
  193. return nil;
  194. }
  195. }
  196. @end