PageRenderTime 83ms CodeModel.GetById 11ms app.highlight 67ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 27#import "OALAudioActions.h"
 28
 29
 30#pragma mark OAL_GainProtocol
 31
 32/** (INTERNAL USE) Protocol to keep the compiler happy. */
 33@protocol OAL_GainProtocol
 34
 35/** The gain (volume), represented as a float from 0.0 to 1.0. */
 36@property(readwrite) float gain;
 37
 38@end
 39
 40
 41#pragma mark -
 42#pragma mark OALGainAction
 43
 44@implementation OALGainAction
 45
 46
 47#pragma mark Utility
 48
 49+ (id<OALFunction,NSObject>) defaultFunction
 50{
 51	return [OALSCurveFunction function];
 52}
 53
 54
 55#pragma mark Functions
 56
 57- (void) prepareWithTarget:(id) targetIn
 58{
 59	NSAssert([targetIn respondsToSelector:@selector(gain)]
 60			 && [targetIn respondsToSelector:@selector(setGain:)],
 61			 @"Target does not respond to selectors [gain] and [setGain:]");
 62	
 63	// NAN is a special marker value instructing us to use the current value from the target.
 64	if(isnan(startValue))
 65	{
 66		startValue = [(id<OAL_GainProtocol>)targetIn gain];
 67	}
 68	
 69	[super prepareWithTarget:targetIn];
 70}
 71
 72- (void) updateCompletion:(float) proportionComplete
 73{
 74	[(id<OAL_GainProtocol>)target setGain:lowValue
 75	 + [realFunction valueForInput:proportionComplete] * delta];
 76}
 77
 78@end
 79
 80
 81#pragma mark -
 82#pragma mark OAL_PitchProtocol
 83
 84/** (INTERNAL USE) Protocol to keep the compiler happy. */
 85@protocol OAL_PitchProtocol
 86
 87/** The pitch, represented as a float with 1.0 representing normal pitch. */
 88@property(readwrite) float pitch;
 89
 90@end
 91
 92
 93#pragma mark -
 94#pragma mark OALPitchAction
 95
 96@implementation OALPitchAction
 97
 98
 99#pragma mark Utility
100
101+ (id<OALFunction,NSObject>) defaultFunction
102{
103	return [OALLinearFunction function];
104}
105
106
107#pragma mark Functions
108
109- (void) prepareWithTarget:(id) targetIn
110{	
111	NSAssert([targetIn respondsToSelector:@selector(pitch)]
112			 && [targetIn respondsToSelector:@selector(setPitch:)],
113			 @"Target does not respond to selectors [pitch] and [setPitch:]");
114	
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_PitchProtocol>)targetIn pitch];
119	}
120	
121	[super prepareWithTarget:targetIn];
122}
123
124- (void) updateCompletion:(float) proportionComplete
125{
126	[(id<OAL_PitchProtocol>)target setPitch:startValue
127	 + [realFunction valueForInput:proportionComplete] * delta];
128}
129
130@end
131
132
133#pragma mark -
134#pragma mark OAL_PanProtocol
135
136/** (INTERNAL USE) Protocol to keep the compiler happy. */
137@protocol OAL_PanProtocol
138
139/** The pan, represented as a float from -1.0 to 1.0. */
140@property(readwrite) float pan;
141
142@end
143
144
145#pragma mark -
146#pragma mark OALPanAction
147
148@implementation OALPanAction
149
150
151#pragma mark Utility
152
153+ (id<OALFunction,NSObject>) defaultFunction
154{
155	return [OALLinearFunction function];
156}
157
158
159#pragma mark Functions
160
161- (void) prepareWithTarget:(id) targetIn
162{	
163	NSAssert([targetIn respondsToSelector:@selector(pan)]
164			 && [targetIn respondsToSelector:@selector(setPan:)],
165			 @"Target does not respond to selectors [pan] and [setPan:]");
166	
167	// NAN is a special marker value instructing us to use the current value from the target.
168	if(isnan(startValue))
169	{
170		startValue = [(id<OAL_PanProtocol>)targetIn pan];
171	}
172	
173	[super prepareWithTarget:targetIn];
174}
175
176- (void) updateCompletion:(float) proportionComplete
177{
178	[(id<OAL_PanProtocol>)target setPan:startValue
179	 + [realFunction valueForInput:proportionComplete] * delta];
180}
181
182@end
183
184
185#pragma mark -
186#pragma mark OAL_PositionProtocol
187
188/** (INTERNAL USE) Protocol to keep the compiler happy. */
189@protocol OAL_PositionProtocol
190
191/** The position in 3D space. */
192@property(readwrite,assign) ALPoint position;
193
194@end
195
196
197#pragma mark -
198#pragma mark OALPlaceAction
199
200@implementation OALPlaceAction
201
202
203#pragma mark Object Management
204
205+ (id) actionWithPosition:(ALPoint) position
206{
207	return [[(OALPlaceAction*)[self alloc] initWithPosition:position] autorelease];
208}
209
210- (id) initWithPosition:(ALPoint) positionIn
211{
212	if(nil != (self = [super init]))
213	{
214		position = positionIn;
215	}
216	return self;
217}
218
219
220#pragma mark Properties
221
222@synthesize position;
223
224
225#pragma mark Functions
226
227- (void) prepareWithTarget:(id) targetIn
228{	
229	NSAssert([targetIn respondsToSelector:@selector(setPosition:)],
230			 @"Target does not respond to selector [setPosition:]");
231	
232	[super prepareWithTarget:targetIn];
233}
234
235- (void) updateCompletion:(float) proportionComplete
236{
237	[super updateCompletion:proportionComplete];
238	[(id<OAL_PositionProtocol>)target setPosition:position];
239}
240
241@end
242
243
244#pragma mark -
245#pragma mark OALMoveToAction
246
247@implementation OALMoveToAction
248
249
250#pragma mark Object Management
251
252+ (id) actionWithDuration:(float) duration position:(ALPoint) position
253{
254	return [[(OALMoveToAction*)[self alloc] initWithDuration:duration position:position] autorelease];
255}
256
257+ (id) actionWithUnitsPerSecond:(float) unitsPerSecond position:(ALPoint) position
258{
259	return [[[self alloc] initWithUnitsPerSecond:unitsPerSecond position:position] autorelease];
260}
261
262- (id) initWithDuration:(float) durationIn position:(ALPoint) positionIn
263{
264	if(nil != (self = [super initWithDuration:durationIn]))
265	{
266		position = positionIn;
267	}
268	return self;
269}
270
271- (id) initWithUnitsPerSecond:(float) unitsPerSecondIn position:(ALPoint) positionIn
272{
273	if(nil != (self = [super init]))
274	{
275		position = positionIn;
276		unitsPerSecond = unitsPerSecondIn;
277	}
278	return self;
279}
280
281
282#pragma mark Properties
283
284@synthesize position;
285@synthesize unitsPerSecond;
286
287
288#pragma mark Functions
289
290- (void) prepareWithTarget:(id) targetIn
291{
292	NSAssert([targetIn respondsToSelector:@selector(setPosition:)],
293			 @"Target does not respond to selector [setPosition:]");
294	
295	[super prepareWithTarget:targetIn];
296
297	startPoint = [(id<OAL_PositionProtocol>)targetIn position];
298	delta = ALPointMake(position.x-startPoint.x, position.y-startPoint.y, position.z - startPoint.z);
299
300	// If unitsPerSecond was set, we use that to calculate duration.  Otherwise just use the current
301	// value in duration.
302	if(unitsPerSecond > 0)
303	{
304		duration = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z) / unitsPerSecond;
305	}
306}
307
308- (void) updateCompletion:(float) proportionComplete
309{
310	[(id<OAL_PositionProtocol>)target setPosition:
311	 ALPointMake(startPoint.x + delta.x*proportionComplete,
312				 startPoint.y + delta.y*proportionComplete,
313				 startPoint.z + delta.z*proportionComplete)];
314}
315
316@end
317
318
319#pragma mark -
320#pragma mark OALMoveByAction
321
322@implementation OALMoveByAction
323
324
325#pragma mark Object Management
326
327+ (id) actionWithDuration:(float) duration delta:(ALPoint) delta
328{
329	return [[[self alloc] initWithDuration:duration delta:delta] autorelease];
330}
331
332+ (id) actionWithUnitsPerSecond:(float) unitsPerSecond delta:(ALPoint) delta
333{
334	return [[[self alloc] initWithUnitsPerSecond:unitsPerSecond delta:delta] autorelease];
335}
336
337- (id) initWithDuration:(float) durationIn delta:(ALPoint) deltaIn
338{
339	if(nil != (self = [super initWithDuration:durationIn]))
340	{
341		delta = deltaIn;
342	}
343	return self;
344}
345
346- (id) initWithUnitsPerSecond:(float) unitsPerSecondIn delta:(ALPoint) deltaIn
347{
348	if(nil != (self = [super init]))
349	{
350		delta = deltaIn;
351		unitsPerSecond = unitsPerSecondIn;
352		if(unitsPerSecond > 0)
353		{
354			// If unitsPerSecond was set, we use that to calculate duration.  Otherwise just use the current
355			// value in duration.
356			duration = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z) / unitsPerSecond;
357		}
358	}
359	return self;
360}
361
362
363#pragma mark Properties
364
365@synthesize delta;
366@synthesize unitsPerSecond;
367
368
369#pragma mark Functions
370
371- (void) prepareWithTarget:(id) targetIn
372{
373	NSAssert([targetIn respondsToSelector:@selector(setPosition:)],
374			 @"Target does not respond to selector [setPosition:]");
375	
376	[super prepareWithTarget:targetIn];
377
378	startPoint = [(id<OAL_PositionProtocol>)targetIn position];
379	if(unitsPerSecond > 0)
380	{
381		// If unitsPerSecond was set, we use that to calculate duration.  Otherwise just use the current
382		// value in duration.
383		duration = sqrtf(delta.x * delta.x + delta.y * delta.y + delta.z * delta.z) / unitsPerSecond;
384	}
385}
386
387- (void) updateCompletion:(float) proportionComplete
388{
389	[(id<OAL_PositionProtocol>)target setPosition:
390	 ALPointMake(startPoint.x + delta.x*proportionComplete,
391				 startPoint.y + delta.y*proportionComplete,
392				 startPoint.z + delta.z*proportionComplete)];
393}
394
395@end