CDXMacOSXSupport.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <Availability.h>
  21. #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
  22. #elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)
  23. #import "audio/mac/CDXMacOSXSupport.h"
  24. #import "audio/include/SimpleAudioEngine.h"
  25. #import "audio/mac/CocosDenshion.h"
  26. NSString * const AVAudioSessionCategoryAmbient = @"AVAudioSessionCategoryAmbient";
  27. NSString *const AVAudioSessionCategorySoloAmbient = @"AVAudioSessionCategorySoloAmbient";
  28. NSString *const AVAudioSessionCategoryPlayback = @"AVAudioSessionCategoryPlayback";
  29. NSString *const AVAudioSessionCategoryRecord = @"AVAudioSessionCategoryRecord";
  30. NSString *const AVAudioSessionCategoryPlayAndRecord = @"AVAudioSessionCategoryPlayAndRecord";
  31. NSString *const AVAudioSessionCategoryAudioProcessing = @"AVAudioSessionCategoryAudioProcessing";
  32. OSStatus AudioSessionGetProperty(UInt32 inID, UInt32 *ioDataSize, void *outData) {
  33. //TODO: set outData appropriately
  34. return 0;
  35. }
  36. @implementation CCAudioPlayer
  37. @synthesize delegate, numberOfChannels, pan, deviceCurrentTime, url, data;
  38. - (id)initWithContentsOfURL:(NSURL *)theUrl error:(NSError **)outError {
  39. if ((self = [super init])) {
  40. _player = [[NSSound alloc] initWithContentsOfURL:theUrl byReference:YES];
  41. if (_player != nil) {
  42. _player.delegate = self;
  43. CDLOG(@"Denshion::CDXMacOSXSupport - NSSound allocated for %@", theUrl);
  44. }
  45. }
  46. return self;
  47. }
  48. - (id)initWithData:(NSData *)theData error:(NSError **)outError {
  49. if ((self = [super init])) {
  50. _player = [[NSSound alloc] initWithData:theData];
  51. if (_player != nil) {
  52. _player.delegate = self;
  53. CDLOG(@"Denshion::CDXMacOSXSupport - NSSound allocated for %@", theData);
  54. }
  55. }
  56. return self;
  57. }
  58. -(void) dealloc {
  59. [_player release];
  60. [super dealloc];
  61. }
  62. - (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finished {
  63. if (self.delegate && [self.delegate respondsToSelector:@selector(audioPlayerDidFinishPlaying:successfully:)]) {
  64. [self.delegate audioPlayerDidFinishPlaying:self successfully:finished];
  65. }
  66. }
  67. - (BOOL)play {
  68. BOOL result;
  69. result = [_player play];
  70. if (!result) {
  71. //May be paused, try resuming instead
  72. result = [_player resume];
  73. }
  74. return result;
  75. }
  76. - (BOOL) resume{
  77. BOOL result = [_player resume];
  78. return result;
  79. }
  80. -(void) pause {
  81. [_player pause];
  82. }
  83. -(void) stop {
  84. [_player stop];
  85. }
  86. -(BOOL) isPlaying {
  87. return [_player isPlaying];
  88. }
  89. -(void) setVolume:(float) vol {
  90. [_player setVolume:vol];
  91. }
  92. -(float) volume {
  93. return [_player volume];
  94. }
  95. -(void) setNumberOfLoops:(NSInteger) nOfLoops {
  96. if (nOfLoops < 0) {
  97. [_player setLoops:YES];
  98. } else {
  99. [_player setLoops:NO];
  100. }
  101. }
  102. -(NSInteger) numberOfLoops {
  103. if (_player.loops) {
  104. return -1;
  105. } else {
  106. return 0;
  107. }
  108. }
  109. -(void) setCurrentTime:(NSTimeInterval) aCurrentTime {
  110. [_player setCurrentTime:aCurrentTime];
  111. }
  112. -(NSTimeInterval) currentTime {
  113. return [_player currentTime];
  114. }
  115. -(NSTimeInterval) duration {
  116. return [_player duration];
  117. }
  118. #pragma mark unsupported
  119. - (BOOL)prepareToPlay {
  120. return YES;
  121. }
  122. -(BOOL)playAtTime:(NSTimeInterval)time {
  123. return YES;
  124. }
  125. -(void) setMeteringEnabled:(BOOL) enabled {
  126. }
  127. -(BOOL) isMeteringEnabled {
  128. return NO;
  129. }
  130. - (void)updateMeters{}
  131. - (float)peakPowerForChannel:(NSUInteger)channelNumber{return 0.0f;}
  132. - (float)averagePowerForChannel:(NSUInteger)channelNumber{return 0.0f;}
  133. @end
  134. /**
  135. A "do nothing" implementation - AVAudioSession is not really relevant to Mac OS X.
  136. */
  137. @implementation AVAudioSession
  138. @synthesize delegate, category, preferredHardwareSampleRate, preferredIOBufferDuration;
  139. @synthesize inputIsAvailable, currentHardwareSampleRate, currentHardwareInputNumberOfChannels, currentHardwareOutputNumberOfChannels;
  140. + (id)sharedInstance {
  141. return nil;
  142. }
  143. - (BOOL)setActive:(BOOL)beActive error:(NSError**)outError {return YES;}
  144. - (BOOL)setActive:(BOOL)beActive withFlags:(NSInteger)flags error:(NSError**)outError {return YES;}
  145. - (BOOL)setCategory:(NSString*)theCategory error:(NSError**)outError {return YES;}
  146. - (BOOL)setPreferredHardwareSampleRate:(double)sampleRate error:(NSError**)outError {return YES;}
  147. - (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration error:(NSError**)outError {return YES;}
  148. @end
  149. #endif