Debian Codegraph @

radiantcore/model/picomodel/lib/lwo/envelope.c

raw
1 /*
2 ======================================================================
3 envelope.c
4
5 Envelope functions for an LWO2 reader.
6
7 Ernie Wright 16 Nov 00
8 ====================================================================== */
9
10 #include "../picointernal.h"
11 #include "lwo2.h"
12
13 /*
14 ======================================================================
15 lwFreeEnvelope()
16
17 Free the memory used by an lwEnvelope.
18 ====================================================================== */
19
20 void lwFreeEnvelope( lwEnvelope *env )
21 {
22 if ( env ) {
23 if ( env->name ) _pico_free( env->name );
24 lwListFree( env->key, _pico_free );
25 lwListFree( env->cfilter, (void (*)(void *))lwFreePlugin );
26 _pico_free( env );
27 }
28 }
29
30
31 static int compare_keys( lwKey *k1, lwKey *k2 )
32 {
33 return k1->time > k2->time ? 1 : k1->time < k2->time ? -1 : 0;
34 }
35
36
37 /*
38 ======================================================================
39 lwGetEnvelope()
40
41 Read an ENVL chunk from an LWO2 file.
42 ====================================================================== */
43
44 lwEnvelope *lwGetEnvelope( picoMemStream_t *fp, int cksize )
45 {
46 lwEnvelope *env;
47 lwKey *key;
48 lwPlugin *plug;
49 unsigned int id;
50 unsigned short sz;
51 float f[ 4 ];
52 int i, nparams, pos, rlen;
53
54 key = NULL;
55
56 /* allocate the Envelope structure */
57
58 env = _pico_calloc( 1, sizeof( lwEnvelope ));
59 if ( !env ) goto Fail;
60
61 /* remember where we started */
62
63 set_flen( 0 );
64 pos = (int)_pico_memstream_tell( fp );
65
66 /* index */
67
68 env->index = getVX( fp );
69
70 /* first subchunk header */
71
72 id = getU4( fp );
73 sz = getU2( fp );
74 if ( 0 > get_flen() ) goto Fail;
75
76 /* process subchunks as they're encountered */
77
78 while ( 1 ) {
79 sz += sz & 1;
80 set_flen( 0 );
81
82 switch ( id ) {
83 case ID_TYPE:
84 env->type = getU2( fp );
85 break;
86
87 case ID_NAME:
88 env->name = getS0( fp );
89 break;
90
91 case ID_PRE:
92 env->behavior[ 0 ] = getU2( fp );
93 break;
94
95 case ID_POST:
96 env->behavior[ 1 ] = getU2( fp );
97 break;
98
99 case ID_KEY:
100 key = _pico_calloc( 1, sizeof( lwKey ));
101 if ( !key ) goto Fail;
102 key->time = getF4( fp );
103 key->value = getF4( fp );
104 lwListInsert( (void **) &env->key, key, (int (*)(void *, void *))compare_keys );
105 env->nkeys++;
106 break;
107
108 case ID_SPAN:
109 if ( !key ) goto Fail;
110 key->shape = getU4( fp );
111
112 nparams = ( sz - 4 ) / 4;
113 if ( nparams > 4 ) nparams = 4;
114 for ( i = 0; i < nparams; i++ )
115 f[ i ] = getF4( fp );
116
117 switch ( key->shape ) {
118 case ID_TCB:
119 key->tension = f[ 0 ];
120 key->continuity = f[ 1 ];
121 key->bias = f[ 2 ];
122 break;
123
124 case ID_BEZI:
125 case ID_HERM:
126 case ID_BEZ2:
127 for ( i = 0; i < nparams; i++ )
128 key->param[ i ] = f[ i ];
129 break;
130 }
131 break;
132
133 case ID_CHAN:
134 plug = _pico_calloc( 1, sizeof( lwPlugin ));
135 if ( !plug ) goto Fail;
136
137 plug->name = getS0( fp );
138 plug->flags = getU2( fp );
139 plug->data = getbytes( fp, sz - get_flen() );
140
141 lwListAdd( (void *) &env->cfilter, plug );
142 env->ncfilters++;
143 break;
144
145 default:
146 break;
147 }
148
149 /* error while reading current subchunk? */
150
151 rlen = get_flen();
152 if ( rlen < 0 || rlen > sz ) goto Fail;
153
154 /* skip unread parts of the current subchunk */
155
156 if ( rlen < sz )
157 _pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR );
158
159 /* end of the ENVL chunk? */
160
161 rlen = (int)_pico_memstream_tell( fp ) - pos;
162 if ( cksize < rlen ) goto Fail;
163 if ( cksize == rlen ) break;
164
165 /* get the next subchunk header */
166
167 set_flen( 0 );
168 id = getU4( fp );
169 sz = getU2( fp );
170 if ( 6 != get_flen() ) goto Fail;
171 }
172
173 return env;
174
175 Fail:
176 lwFreeEnvelope( env );
177 return NULL;
178 }
179
180
181 /*
182 ======================================================================
183 lwFindEnvelope()
184
185 Returns an lwEnvelope pointer, given an envelope index.
186 ====================================================================== */
187
188 lwEnvelope *lwFindEnvelope( lwEnvelope *list, int index )
189 {
190 lwEnvelope *env;
191
192 env = list;
193 while ( env ) {
194 if ( env->index == index ) break;
195 env = env->next;
196 }
197 return env;
198 }
199
200
201 /*
202 ======================================================================
203 range()
204
205 Given the value v of a periodic function, returns the equivalent value
206 v2 in the principal interval [lo, hi]. If i isn't NULL, it receives
207 the number of wavelengths between v and v2.
208
209 v2 = v - i * (hi - lo)
210
211 For example, range( 3 pi, 0, 2 pi, i ) returns pi, with i = 1.
212 ====================================================================== */
213
214 static float range( float v, float lo, float hi, int *i )
215 {
216 float v2, r = hi - lo;
217
218 if ( r == 0.0 ) {
219 if ( i ) *i = 0;
220 return lo;
221 }
222
223 v2 = lo + v - r * ( float ) floor(( double ) v / r );
224 if ( i ) *i = -( int )(( v2 - v ) / r + ( v2 > v ? 0.5 : -0.5 ));
225
226 return v2;
227 }
228
229
230 /*
231 ======================================================================
232 hermite()
233
234 Calculate the Hermite coefficients.
235 ====================================================================== */
236
237 static void hermite( float t, float *h1, float *h2, float *h3, float *h4 )
238 {
239 float t2, t3;
240
241 t2 = t * t;
242 t3 = t * t2;
243
244 *h2 = 3.0f * t2 - t3 - t3;
245 *h1 = 1.0f - *h2;
246 *h4 = t3 - t2;
247 *h3 = *h4 - t2 + t;
248 }
249
250
251 /*
252 ======================================================================
253 bezier()
254
255 Interpolate the value of a 1D Bezier curve.
256 ====================================================================== */
257
258 static float bezier( float x0, float x1, float x2, float x3, float t )
259 {
260 float a, b, c, t2, t3;
261
262 t2 = t * t;
263 t3 = t2 * t;
264
265 c = 3.0f * ( x1 - x0 );
266 b = 3.0f * ( x2 - x1 ) - c;
267 a = x3 - x0 - c - b;
268
269 return a * t3 + b * t2 + c * t + x0;
270 }
271
272
273 /*
274 ======================================================================
275 bez2_time()
276
277 Find the t for which bezier() returns the input time. The handle
278 endpoints of a BEZ2 curve represent the control points, and these have
279 (time, value) coordinates, so time is used as both a coordinate and a
280 parameter for this curve type.
281 ====================================================================== */
282
283 static float bez2_time( float x0, float x1, float x2, float x3, float time,
284 float *t0, float *t1 )
285 {
286 float v, t;
287
288 t = *t0 + ( *t1 - *t0 ) * 0.5f;
289 v = bezier( x0, x1, x2, x3, t );
290 if ( fabs( time - v ) > .0001f ) {
291 if ( v > time )
292 *t1 = t;
293 else
294 *t0 = t;
295 return bez2_time( x0, x1, x2, x3, time, t0, t1 );
296 }
297 else
298 return t;
299 }
300
301
302 /*
303 ======================================================================
304 bez2()
305
306 Interpolate the value of a BEZ2 curve.
307 ====================================================================== */
308
309 static float bez2( lwKey *key0, lwKey *key1, float time )
310 {
311 float x, y, t, t0 = 0.0f, t1 = 1.0f;
312
313 if ( key0->shape == ID_BEZ2 )
314 x = key0->time + key0->param[ 2 ];
315 else
316 x = key0->time + ( key1->time - key0->time ) / 3.0f;
317
318 t = bez2_time( key0->time, x, key1->time + key1->param[ 0 ], key1->time,
319 time, &t0, &t1 );
320
321 if ( key0->shape == ID_BEZ2 )
322 y = key0->value + key0->param[ 3 ];
323 else
324 y = key0->value + key0->param[ 1 ] / 3.0f;
325
326 return bezier( key0->value, y, key1->param[ 1 ] + key1->value, key1->value, t );
327 }
328
329
330 /*
331 ======================================================================
332 outgoing()
333
334 Return the outgoing tangent to the curve at key0. The value returned
335 for the BEZ2 case is used when extrapolating a linear pre behavior and
336 when interpolating a non-BEZ2 span.
337 ====================================================================== */
338
339 static float outgoing( lwKey *key0, lwKey *key1 )
340 {
341 float a, b, d, t, out;
342
343 switch ( key0->shape )
344 {
345 case ID_TCB:
346 a = ( 1.0f - key0->tension )
347 * ( 1.0f + key0->continuity )
348 * ( 1.0f + key0->bias );
349 b = ( 1.0f - key0->tension )
350 * ( 1.0f - key0->continuity )
351 * ( 1.0f - key0->bias );
352 d = key1->value - key0->value;
353
354 if ( key0->prev ) {
355 t = ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
356 out = t * ( a * ( key0->value - key0->prev->value ) + b * d );
357 }
358 else
359 out = b * d;
360 break;
361
362 case ID_LINE:
363 d = key1->value - key0->value;
364 if ( key0->prev ) {
365 t = ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
366 out = t * ( key0->value - key0->prev->value + d );
367 }
368 else
369 out = d;
370 break;
371
372 case ID_BEZI:
373 case ID_HERM:
374 out = key0->param[ 1 ];
375 if ( key0->prev )
376 out *= ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
377 break;
378
379 case ID_BEZ2:
380 out = key0->param[ 3 ] * ( key1->time - key0->time );
381 if ( fabs( key0->param[ 2 ] ) > 1e-5f )
382 out /= key0->param[ 2 ];
383 else
384 out *= 1e5f;
385 break;
386
387 case ID_STEP:
388 default:
389 out = 0.0f;
390 break;
391 }
392
393 return out;
394 }
395
396
397 /*
398 ======================================================================
399 incoming()
400
401 Return the incoming tangent to the curve at key1. The value returned
402 for the BEZ2 case is used when extrapolating a linear post behavior.
403 ====================================================================== */
404
405 static float incoming( lwKey *key0, lwKey *key1 )
406 {
407 float a, b, d, t, in;
408
409 switch ( key1->shape )
410 {
411 case ID_LINE:
412 d = key1->value - key0->value;
413 if ( key1->next ) {
414 t = ( key1->time - key0->time ) / ( key1->next->time - key0->time );
415 in = t * ( key1->next->value - key1->value + d );
416 }
417 else
418 in = d;
419 break;
420
421 case ID_TCB:
422 a = ( 1.0f - key1->tension )
423 * ( 1.0f - key1->continuity )
424 * ( 1.0f + key1->bias );
425 b = ( 1.0f - key1->tension )
426 * ( 1.0f + key1->continuity )
427 * ( 1.0f - key1->bias );
428 d = key1->value - key0->value;
429
430 if ( key1->next ) {
431 t = ( key1->time - key0->time ) / ( key1->next->time - key0->time );
432 in = t * ( b * ( key1->next->value - key1->value ) + a * d );
433 }
434 else
435 in = a * d;
436 break;
437
438 case ID_BEZI:
439 case ID_HERM:
440 in = key1->param[ 0 ];
441 if ( key1->next )
442 in *= ( key1->time - key0->time ) / ( key1->next->time - key0->time );
443 break;
444
445 case ID_BEZ2:
446 in = key1->param[ 1 ] * ( key1->time - key0->time );
447 if ( fabs( key1->param[ 0 ] ) > 1e-5f )
448 in /= key1->param[ 0 ];
449 else
450 in *= 1e5f;
451 break;
452
453 case ID_STEP:
454 default:
455 in = 0.0f;
456 break;
457 }
458
459 return in;
460 }
461
462
463 /*
464 ======================================================================
465 evalEnvelope()
466
467 Given a list of keys and a time, returns the interpolated value of the
468 envelope at that time.
469 ====================================================================== */
470
471 float evalEnvelope( lwEnvelope *env, float time )
472 {
473 lwKey *key0, *key1, *skey, *ekey;
474 float t, h1, h2, h3, h4, in, out, offset = 0.0f;
475 int noff;
476
477
478 /* if there's no key, the value is 0 */
479
480 if ( env->nkeys == 0 ) return 0.0f;
481
482 /* if there's only one key, the value is constant */
483
484 if ( env->nkeys == 1 )
485 return env->key->value;
486
487 /* find the first and last keys */
488
489 skey = ekey = env->key;
490 while ( ekey->next ) ekey = ekey->next;
491
492 /* use pre-behavior if time is before first key time */
493
494 if ( time < skey->time ) {
495 switch ( env->behavior[ 0 ] )
496 {
497 case BEH_RESET:
498 return 0.0f;
499
500 case BEH_CONSTANT:
501 return skey->value;
502
503 case BEH_REPEAT:
504 time = range( time, skey->time, ekey->time, NULL );
505 break;
506
507 case BEH_OSCILLATE:
508 time = range( time, skey->time, ekey->time, &noff );
509 if ( noff % 2 )
510 time = ekey->time - skey->time - time;
511 break;
512
513 case BEH_OFFSET:
514 time = range( time, skey->time, ekey->time, &noff );
515 offset = noff * ( ekey->value - skey->value );
516 break;
517
518 case BEH_LINEAR:
519 out = outgoing( skey, skey->next )
520 / ( skey->next->time - skey->time );
521 return out * ( time - skey->time ) + skey->value;
522 }
523 }
524
525 /* use post-behavior if time is after last key time */
526
527 else if ( time > ekey->time ) {
528 switch ( env->behavior[ 1 ] )
529 {
530 case BEH_RESET:
531 return 0.0f;
532
533 case BEH_CONSTANT:
534 return ekey->value;
535
536 case BEH_REPEAT:
537 time = range( time, skey->time, ekey->time, NULL );
538 break;
539
540 case BEH_OSCILLATE:
541 time = range( time, skey->time, ekey->time, &noff );
542 if ( noff % 2 )
543 time = ekey->time - skey->time - time;
544 break;
545
546 case BEH_OFFSET:
547 time = range( time, skey->time, ekey->time, &noff );
548 offset = noff * ( ekey->value - skey->value );
549 break;
550
551 case BEH_LINEAR:
552 in = incoming( ekey->prev, ekey )
553 / ( ekey->time - ekey->prev->time );
554 return in * ( time - ekey->time ) + ekey->value;
555 }
556 }
557
558 /* get the endpoints of the interval being evaluated */
559
560 key0 = env->key;
561 while ( time > key0->next->time )
562 key0 = key0->next;
563 key1 = key0->next;
564
565 /* check for singularities first */
566
567 if ( time == key0->time )
568 return key0->value + offset;
569 else if ( time == key1->time )
570 return key1->value + offset;
571
572 /* get interval length, time in [0, 1] */
573
574 t = ( time - key0->time ) / ( key1->time - key0->time );
575
576 /* interpolate */
577
578 switch ( key1->shape )
579 {
580 case ID_TCB:
581 case ID_BEZI:
582 case ID_HERM:
583 out = outgoing( key0, key1 );
584 in = incoming( key0, key1 );
585 hermite( t, &h1, &h2, &h3, &h4 );
586 return h1 * key0->value + h2 * key1->value + h3 * out + h4 * in + offset;
587
588 case ID_BEZ2:
589 return bez2( key0, key1, time ) + offset;
590
591 case ID_LINE:
592 return key0->value + t * ( key1->value - key0->value ) + offset;
593
594 case ID_STEP:
595 return key0->value + offset;
596
597 default:
598 return offset;
599 }
600 }