/libs/ObjectAL/Actions/OALAudioActions.m

http://github.com/kstenerud/ObjectAL-for-iPhone · Objective C · 395 lines · 238 code · 115 blank · 42 comment · 19 complexity · 3afb50f0bc6cc9a8b007361be941a90c MD5 · raw file

  1. //
  2. // OALAudioActions.m
  3. // ObjectAL
  4. //
  5. // Created by Karl Stenerud on 10-10-10.
  6. //
  7. // Copyright 2010 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 "OALAudioActions.h"
  27. #pragma mark OAL_GainProtocol
  28. /** (INTERNAL USE) Protocol to keep the compiler happy. */
  29. @protocol OAL_GainProtocol
  30. /** The gain (volume), represented as a float from 0.0 to 1.0. */
  31. @property(readwrite) float gain;
  32. @end
  33. #pragma mark -
  34. #pragma mark OALGainAction
  35. @implementation OALGainAction
  36. #pragma mark Utility
  37. + (id<OALFunction,NSObject>) defaultFunction
  38. {
  39. return [OALSCurveFunction function];
  40. }
  41. #pragma mark Functions
  42. - (void) prepareWithTarget:(id) targetIn
  43. {
  44. NSAssert([targetIn respondsToSelector:@selector(gain)]
  45. && [targetIn respondsToSelector:@selector(setGain:)],
  46. @"Target does not respond to selectors [gain] and [setGain:]");
  47. // NAN is a special marker value instructing us to use the current value from the target.
  48. if(isnan(startValue))
  49. {
  50. startValue = [(id<OAL_GainProtocol>)targetIn gain];
  51. }
  52. [super prepareWithTarget:targetIn];
  53. }
  54. - (void) updateCompletion:(float) proportionComplete
  55. {
  56. [(id<OAL_GainProtocol>)target setGain:lowValue
  57. + [realFunction valueForInput:proportionComplete] * delta];
  58. }
  59. @end
  60. #pragma mark -
  61. #pragma mark OAL_PitchProtocol
  62. /** (INTERNAL USE) Protocol to keep the compiler happy. */
  63. @protocol OAL_PitchProtocol
  64. /** The pitch, represented as a float with 1.0 representing normal pitch. */
  65. @property(readwrite) float pitch;
  66. @end
  67. #pragma mark -
  68. #pragma mark OALPitchAction
  69. @implementation OALPitchAction
  70. #pragma mark Utility
  71. + (id<OALFunction,NSObject>) defaultFunction
  72. {
  73. return [OALLinearFunction function];
  74. }
  75. #pragma mark Functions
  76. - (void) prepareWithTarget:(id) targetIn
  77. {
  78. NSAssert([targetIn respondsToSelector:@selector(pitch)]
  79. && [targetIn respondsToSelector:@selector(setPitch:)],
  80. @"Target does not respond to selectors [pitch] and [setPitch:]");
  81. // NAN is a special marker value instructing us to use the current value from the target.
  82. if(isnan(startValue))
  83. {
  84. startValue = [(id<OAL_PitchProtocol>)targetIn pitch];
  85. }
  86. [super prepareWithTarget:targetIn];
  87. }
  88. - (void) updateCompletion:(float) proportionComplete
  89. {
  90. [(id<OAL_PitchProtocol>)target setPitch:startValue
  91. + [realFunction valueForInput:proportionComplete] * delta];
  92. }
  93. @end
  94. #pragma mark -
  95. #pragma mark OAL_PanProtocol
  96. /** (INTERNAL USE) Protocol to keep the compiler happy. */
  97. @protocol OAL_PanProtocol
  98. /** The pan, represented as a float from -1.0 to 1.0. */
  99. @property(readwrite) float pan;
  100. @end
  101. #pragma mark -
  102. #pragma mark OALPanAction
  103. @implementation OALPanAction
  104. #pragma mark Utility
  105. + (id<OALFunction,NSObject>) defaultFunction
  106. {
  107. return [OALLinearFunction function];
  108. }
  109. #pragma mark Functions
  110. - (void) prepareWithTarget:(id) targetIn
  111. {
  112. NSAssert([targetIn respondsToSelector:@selector(pan)]
  113. && [targetIn respondsToSelector:@selector(setPan:)],
  114. @"Target does not respond to selectors [pan] and [setPan:]");
  115. // NAN is a special marker value instructing us to use the current value from the target.
  116. if(isnan(startValue))
  117. {
  118. startValue = [(id<OAL_PanProtocol>)targetIn pan];
  119. }
  120. [super prepareWithTarget:targetIn];
  121. }
  122. - (void) updateCompletion:(float) proportionComplete
  123. {
  124. [(id<OAL_PanProtocol>)target setPan:startValue
  125. + [realFunction valueForInput:proportionComplete] * delta];
  126. }
  127. @end
  128. #pragma mark -
  129. #pragma mark OAL_PositionProtocol
  130. /** (INTERNAL USE) Protocol to keep the compiler happy. */
  131. @protocol OAL_PositionProtocol
  132. /** The position in 3D space. */
  133. @property(readwrite,assign) ALPoint position;
  134. @end
  135. #pragma mark -
  136. #pragma mark OALPlaceAction
  137. @implementation OALPlaceAction
  138. #pragma mark Object Management
  139. + (id) actionWithPosition:(ALPoint) position
  140. {
  141. return [[(OALPlaceAction*)[self alloc] initWithPosition:position] autorelease];
  142. }
  143. - (id) initWithPosition:(ALPoint) positionIn
  144. {
  145. if(nil != (self = [super init]))
  146. {
  147. position = positionIn;
  148. }
  149. return self;
  150. }
  151. #pragma mark Properties
  152. @synthesize position;
  153. #pragma mark Functions
  154. - (void) prepareWithTarget:(id) targetIn
  155. {
  156. NSAssert([targetIn respondsToSelector:@selector(setPosition:)],
  157. @"Target does not respond to selector [setPosition:]");
  158. [super prepareWithTarget:targetIn];
  159. }
  160. - (void) updateCompletion:(float) proportionComplete
  161. {
  162. [super updateCompletion:proportionComplete];
  163. [(id<OAL_PositionProtocol>)target setPosition:position];
  164. }
  165. @end
  166. #pragma mark -
  167. #pragma mark OALMoveToAction
  168. @implementation OALMoveToAction
  169. #pragma mark Object Management
  170. + (id) actionWithDuration:(float) duration position:(ALPoint) position
  171. {
  172. return [[(OALMoveToAction*)[self alloc] initWithDuration:duration position:position] autorelease];
  173. }
  174. + (id) actionWithUnitsPerSecond:(float) unitsPerSecond position:(ALPoint) position
  175. {
  176. return [[[self alloc] initWithUnitsPerSecond:unitsPerSecond position:position] autorelease];
  177. }
  178. - (id) initWithDuration:(float) durationIn position:(ALPoint) positionIn
  179. {
  180. if(nil != (self = [super initWithDuration:durationIn]))
  181. {
  182. position = positionIn;
  183. }
  184. return self;
  185. }
  186. - (id) initWithUnitsPerSecond:(float) unitsPerSecondIn position:(ALPoint) positionIn
  187. {
  188. if(nil != (self = [super init]))
  189. {
  190. position = positionIn;
  191. unitsPerSecond = unitsPerSecondIn;
  192. }
  193. return self;
  194. }
  195. #pragma mark Properties
  196. @synthesize position;
  197. @synthesize unitsPerSecond;
  198. #pragma mark Functions
  199. - (void) prepareWithTarget:(id) targetIn
  200. {
  201. NSAssert([targetIn respondsToSelector:@selector(setPosition:)],
  202. @"Target does not respond to selector [setPosition:]");
  203. [super prepareWithTarget:targetIn];
  204. startPoint = [(id<OAL_PositionProtocol>)targetIn position];
  205. delta = ALPointMake(position.x-startPoint.x, position.y-startPoint.y, position.z - startPoint.z);
  206. // If unitsPerSecond was set, we use that to calculate duration. Otherwise just use the current
  207. // value in duration.
  208. if(unitsPerSecond > 0)
  209. {
  210. duration = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z) / unitsPerSecond;
  211. }
  212. }
  213. - (void) updateCompletion:(float) proportionComplete
  214. {
  215. [(id<OAL_PositionProtocol>)target setPosition:
  216. ALPointMake(startPoint.x + delta.x*proportionComplete,
  217. startPoint.y + delta.y*proportionComplete,
  218. startPoint.z + delta.z*proportionComplete)];
  219. }
  220. @end
  221. #pragma mark -
  222. #pragma mark OALMoveByAction
  223. @implementation OALMoveByAction
  224. #pragma mark Object Management
  225. + (id) actionWithDuration:(float) duration delta:(ALPoint) delta
  226. {
  227. return [[[self alloc] initWithDuration:duration delta:delta] autorelease];
  228. }
  229. + (id) actionWithUnitsPerSecond:(float) unitsPerSecond delta:(ALPoint) delta
  230. {
  231. return [[[self alloc] initWithUnitsPerSecond:unitsPerSecond delta:delta] autorelease];
  232. }
  233. - (id) initWithDuration:(float) durationIn delta:(ALPoint) deltaIn
  234. {
  235. if(nil != (self = [super initWithDuration:durationIn]))
  236. {
  237. delta = deltaIn;
  238. }
  239. return self;
  240. }
  241. - (id) initWithUnitsPerSecond:(float) unitsPerSecondIn delta:(ALPoint) deltaIn
  242. {
  243. if(nil != (self = [super init]))
  244. {
  245. delta = deltaIn;
  246. unitsPerSecond = unitsPerSecondIn;
  247. if(unitsPerSecond > 0)
  248. {
  249. // If unitsPerSecond was set, we use that to calculate duration. Otherwise just use the current
  250. // value in duration.
  251. duration = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z) / unitsPerSecond;
  252. }
  253. }
  254. return self;
  255. }
  256. #pragma mark Properties
  257. @synthesize delta;
  258. @synthesize unitsPerSecond;
  259. #pragma mark Functions
  260. - (void) prepareWithTarget:(id) targetIn
  261. {
  262. NSAssert([targetIn respondsToSelector:@selector(setPosition:)],
  263. @"Target does not respond to selector [setPosition:]");
  264. [super prepareWithTarget:targetIn];
  265. startPoint = [(id<OAL_PositionProtocol>)targetIn position];
  266. if(unitsPerSecond > 0)
  267. {
  268. // If unitsPerSecond was set, we use that to calculate duration. Otherwise just use the current
  269. // value in duration.
  270. duration = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z) / unitsPerSecond;
  271. }
  272. }
  273. - (void) updateCompletion:(float) proportionComplete
  274. {
  275. [(id<OAL_PositionProtocol>)target setPosition:
  276. ALPointMake(startPoint.x + delta.x*proportionComplete,
  277. startPoint.y + delta.y*proportionComplete,
  278. startPoint.z + delta.z*proportionComplete)];
  279. }
  280. @end