/libs/ObjectAL/OpenAL/ALListener.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 224 lines · 164 code · 34 blank · 26 comment · 8 complexity · a51199fa28739f0fb6966d48e3afd45d MD5 · raw file

  1. //
  2. // ALListener.m
  3. // ObjectAL
  4. //
  5. // Created by Karl Stenerud on 10-01-07.
  6. //
  7. // Copyright 2009 Karl Stenerud
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. // Note: You are NOT required to make the license available from within your
  22. // iOS application. Including it in your project is sufficient.
  23. //
  24. // Attribution is not required, but appreciated :)
  25. //
  26. #import "ALListener.h"
  27. #import "ObjectALMacros.h"
  28. #import "ALWrapper.h"
  29. #import "ALContext.h"
  30. @implementation ALListener
  31. #pragma mark Object Management
  32. + (id) listenerForContext:(ALContext*) context
  33. {
  34. return [[[self alloc] initWithContext:context] autorelease];
  35. }
  36. - (id) initWithContext:(ALContext*) contextIn
  37. {
  38. if(nil != (self = [super init]))
  39. {
  40. suspendHandler = [[OALSuspendHandler alloc] initWithTarget:nil selector:nil];
  41. context = contextIn;
  42. gain = 1.0f;
  43. }
  44. return self;
  45. }
  46. - (void) dealloc
  47. {
  48. [suspendHandler release];
  49. [super dealloc];
  50. }
  51. #pragma mark Properties
  52. @synthesize context;
  53. - (bool) muted
  54. {
  55. OPTIONALLY_SYNCHRONIZED(self)
  56. {
  57. return muted;
  58. }
  59. }
  60. - (void) setMuted:(bool) value
  61. {
  62. OPTIONALLY_SYNCHRONIZED(self)
  63. {
  64. if(self.suspended)
  65. {
  66. OAL_LOG_DEBUG(@"%@: Called mutator on suspended object", self);
  67. return;
  68. }
  69. muted = value;
  70. // Force a re-evaluation of gain.
  71. [self setGain:gain];
  72. }
  73. }
  74. - (float) gain
  75. {
  76. OPTIONALLY_SYNCHRONIZED(self)
  77. {
  78. return gain;
  79. }
  80. }
  81. - (void) setGain:(float) value
  82. {
  83. OPTIONALLY_SYNCHRONIZED(self)
  84. {
  85. if(self.suspended)
  86. {
  87. OAL_LOG_DEBUG(@"%@: Called mutator on suspended object", self);
  88. return;
  89. }
  90. gain = value;
  91. if(muted)
  92. {
  93. value = 0;
  94. }
  95. [ALWrapper listenerf:AL_GAIN value:value];
  96. }
  97. }
  98. - (ALOrientation) orientation
  99. {
  100. ALOrientation result;
  101. OPTIONALLY_SYNCHRONIZED(self)
  102. {
  103. [ALWrapper getListenerfv:AL_ORIENTATION values:(float*)&result];
  104. }
  105. return result;
  106. }
  107. - (void) setOrientation:(ALOrientation) value
  108. {
  109. OPTIONALLY_SYNCHRONIZED(self)
  110. {
  111. if(self.suspended)
  112. {
  113. OAL_LOG_DEBUG(@"%@: Called mutator on suspended object", self);
  114. return;
  115. }
  116. [ALWrapper listenerfv:AL_ORIENTATION values:(float*)&value];
  117. }
  118. }
  119. - (ALPoint) position
  120. {
  121. ALPoint result;
  122. OPTIONALLY_SYNCHRONIZED(self)
  123. {
  124. [ALWrapper getListener3f:AL_POSITION v1:&result.x v2:&result.y v3:&result.z];
  125. }
  126. return result;
  127. }
  128. - (void) setPosition:(ALPoint) value
  129. {
  130. OPTIONALLY_SYNCHRONIZED(self)
  131. {
  132. if(self.suspended)
  133. {
  134. OAL_LOG_DEBUG(@"%@: Called mutator on suspended object", self);
  135. return;
  136. }
  137. [ALWrapper listener3f:AL_POSITION v1:value.x v2:value.y v3:value.z];
  138. }
  139. }
  140. - (ALVector) velocity
  141. {
  142. ALVector result;
  143. OPTIONALLY_SYNCHRONIZED(self)
  144. {
  145. [ALWrapper getListener3f:AL_VELOCITY v1:&result.x v2:&result.y v3:&result.z];
  146. }
  147. return result;
  148. }
  149. - (void) setVelocity:(ALVector) value
  150. {
  151. OPTIONALLY_SYNCHRONIZED(self)
  152. {
  153. if(self.suspended)
  154. {
  155. OAL_LOG_DEBUG(@"%@: Called mutator on suspended object", self);
  156. return;
  157. }
  158. [ALWrapper listener3f:AL_VELOCITY v1:value.x v2:value.y v3:value.z];
  159. }
  160. }
  161. #pragma mark Suspend Handler
  162. - (void) addSuspendListener:(id<OALSuspendListener>) listener
  163. {
  164. [suspendHandler addSuspendListener:listener];
  165. }
  166. - (void) removeSuspendListener:(id<OALSuspendListener>) listener
  167. {
  168. [suspendHandler removeSuspendListener:listener];
  169. }
  170. - (bool) manuallySuspended
  171. {
  172. return suspendHandler.manuallySuspended;
  173. }
  174. - (void) setManuallySuspended:(bool) value
  175. {
  176. suspendHandler.manuallySuspended = value;
  177. }
  178. - (bool) interrupted
  179. {
  180. return suspendHandler.interrupted;
  181. }
  182. - (void) setInterrupted:(bool) value
  183. {
  184. suspendHandler.interrupted = value;
  185. }
  186. - (bool) suspended
  187. {
  188. return suspendHandler.suspended;
  189. }
  190. @end