2 ======================================================================
5 Envelope functions for an LWO2 reader.
8 ====================================================================== */
10 #include "../picointernal.h"
14 ======================================================================
17 Free the memory used by an lwEnvelope.
18 ====================================================================== */
20 void lwFreeEnvelope( lwEnvelope *env )
23 if ( env->name ) _pico_free( env->name );
24 lwListFree( env->key, _pico_free );
25 lwListFree( env->cfilter, (void *) lwFreePlugin );
31 static int compare_keys( lwKey *k1, lwKey *k2 )
33 return k1->time > k2->time ? 1 : k1->time < k2->time ? -1 : 0;
38 ======================================================================
41 Read an ENVL chunk from an LWO2 file.
42 ====================================================================== */
44 lwEnvelope *lwGetEnvelope( picoMemStream_t *fp, int cksize )
52 int i, nparams, pos, rlen;
55 /* allocate the Envelope structure */
57 env = _pico_calloc( 1, sizeof( lwEnvelope ));
58 if ( !env ) goto Fail;
60 /* remember where we started */
63 pos = _pico_memstream_tell( fp );
67 env->index = getVX( fp );
69 /* first subchunk header */
73 if ( 0 > get_flen() ) goto Fail;
75 /* process subchunks as they're encountered */
83 env->type = getU2( fp );
87 env->name = getS0( fp );
91 env->behavior[ 0 ] = getU2( fp );
95 env->behavior[ 1 ] = getU2( fp );
99 key = _pico_calloc( 1, sizeof( lwKey ));
100 if ( !key ) goto Fail;
101 key->time = getF4( fp );
102 key->value = getF4( fp );
103 lwListInsert( (void **) &env->key, key, (void *) compare_keys );
108 if ( !key ) goto Fail;
109 key->shape = getU4( fp );
111 nparams = ( sz - 4 ) / 4;
112 if ( nparams > 4 ) nparams = 4;
113 for ( i = 0; i < nparams; i++ )
114 f[ i ] = getF4( fp );
116 switch ( key->shape ) {
118 key->tension = f[ 0 ];
119 key->continuity = f[ 1 ];
126 for ( i = 0; i < nparams; i++ )
127 key->param[ i ] = f[ i ];
133 plug = _pico_calloc( 1, sizeof( lwPlugin ));
134 if ( !plug ) goto Fail;
136 plug->name = getS0( fp );
137 plug->flags = getU2( fp );
138 plug->data = getbytes( fp, sz - get_flen() );
140 lwListAdd( (void *) &env->cfilter, plug );
148 /* error while reading current subchunk? */
151 if ( rlen < 0 || rlen > sz ) goto Fail;
153 /* skip unread parts of the current subchunk */
156 _pico_memstream_seek( fp, sz - rlen, PICO_SEEK_CUR );
158 /* end of the ENVL chunk? */
160 rlen = _pico_memstream_tell( fp ) - pos;
161 if ( cksize < rlen ) goto Fail;
162 if ( cksize == rlen ) break;
164 /* get the next subchunk header */
169 if ( 6 != get_flen() ) goto Fail;
175 lwFreeEnvelope( env );
181 ======================================================================
184 Returns an lwEnvelope pointer, given an envelope index.
185 ====================================================================== */
187 lwEnvelope *lwFindEnvelope( lwEnvelope *list, int index )
193 if ( env->index == index ) break;
201 ======================================================================
204 Given the value v of a periodic function, returns the equivalent value
205 v2 in the principal interval [lo, hi]. If i isn't NULL, it receives
206 the number of wavelengths between v and v2.
208 v2 = v - i * (hi - lo)
210 For example, range( 3 pi, 0, 2 pi, i ) returns pi, with i = 1.
211 ====================================================================== */
213 static float range( float v, float lo, float hi, int *i )
215 float v2, r = hi - lo;
222 v2 = lo + v - r * ( float ) floor(( double ) v / r );
223 if ( i ) *i = -( int )(( v2 - v ) / r + ( v2 > v ? 0.5 : -0.5 ));
230 ======================================================================
233 Calculate the Hermite coefficients.
234 ====================================================================== */
236 static void hermite( float t, float *h1, float *h2, float *h3, float *h4 )
243 *h2 = 3.0f * t2 - t3 - t3;
251 ======================================================================
254 Interpolate the value of a 1D Bezier curve.
255 ====================================================================== */
257 static float bezier( float x0, float x1, float x2, float x3, float t )
259 float a, b, c, t2, t3;
264 c = 3.0f * ( x1 - x0 );
265 b = 3.0f * ( x2 - x1 ) - c;
268 return a * t3 + b * t2 + c * t + x0;
273 ======================================================================
276 Find the t for which bezier() returns the input time. The handle
277 endpoints of a BEZ2 curve represent the control points, and these have
278 (time, value) coordinates, so time is used as both a coordinate and a
279 parameter for this curve type.
280 ====================================================================== */
282 static float bez2_time( float x0, float x1, float x2, float x3, float time,
283 float *t0, float *t1 )
287 t = *t0 + ( *t1 - *t0 ) * 0.5f;
288 v = bezier( x0, x1, x2, x3, t );
289 if ( fabs( time - v ) > .0001f ) {
294 return bez2_time( x0, x1, x2, x3, time, t0, t1 );
302 ======================================================================
305 Interpolate the value of a BEZ2 curve.
306 ====================================================================== */
308 static float bez2( lwKey *key0, lwKey *key1, float time )
310 float x, y, t, t0 = 0.0f, t1 = 1.0f;
312 if ( key0->shape == ID_BEZ2 )
313 x = key0->time + key0->param[ 2 ];
315 x = key0->time + ( key1->time - key0->time ) / 3.0f;
317 t = bez2_time( key0->time, x, key1->time + key1->param[ 0 ], key1->time,
320 if ( key0->shape == ID_BEZ2 )
321 y = key0->value + key0->param[ 3 ];
323 y = key0->value + key0->param[ 1 ] / 3.0f;
325 return bezier( key0->value, y, key1->param[ 1 ] + key1->value, key1->value, t );
330 ======================================================================
333 Return the outgoing tangent to the curve at key0. The value returned
334 for the BEZ2 case is used when extrapolating a linear pre behavior and
335 when interpolating a non-BEZ2 span.
336 ====================================================================== */
338 static float outgoing( lwKey *key0, lwKey *key1 )
340 float a, b, d, t, out;
342 switch ( key0->shape )
345 a = ( 1.0f - key0->tension )
346 * ( 1.0f + key0->continuity )
347 * ( 1.0f + key0->bias );
348 b = ( 1.0f - key0->tension )
349 * ( 1.0f - key0->continuity )
350 * ( 1.0f - key0->bias );
351 d = key1->value - key0->value;
354 t = ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
355 out = t * ( a * ( key0->value - key0->prev->value ) + b * d );
362 d = key1->value - key0->value;
364 t = ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
365 out = t * ( key0->value - key0->prev->value + d );
373 out = key0->param[ 1 ];
375 out *= ( key1->time - key0->time ) / ( key1->time - key0->prev->time );
379 out = key0->param[ 3 ] * ( key1->time - key0->time );
380 if ( fabs( key0->param[ 2 ] ) > 1e-5f )
381 out /= key0->param[ 2 ];
397 ======================================================================
400 Return the incoming tangent to the curve at key1. The value returned
401 for the BEZ2 case is used when extrapolating a linear post behavior.
402 ====================================================================== */
404 static float incoming( lwKey *key0, lwKey *key1 )
406 float a, b, d, t, in;
408 switch ( key1->shape )
411 d = key1->value - key0->value;
413 t = ( key1->time - key0->time ) / ( key1->next->time - key0->time );
414 in = t * ( key1->next->value - key1->value + d );
421 a = ( 1.0f - key1->tension )
422 * ( 1.0f - key1->continuity )
423 * ( 1.0f + key1->bias );
424 b = ( 1.0f - key1->tension )
425 * ( 1.0f + key1->continuity )
426 * ( 1.0f - key1->bias );
427 d = key1->value - key0->value;
430 t = ( key1->time - key0->time ) / ( key1->next->time - key0->time );
431 in = t * ( b * ( key1->next->value - key1->value ) + a * d );
439 in = key1->param[ 0 ];
441 in *= ( key1->time - key0->time ) / ( key1->next->time - key0->time );
446 in = key1->param[ 1 ] * ( key1->time - key0->time );
447 if ( fabs( key1->param[ 0 ] ) > 1e-5f )
448 in /= key1->param[ 0 ];
464 ======================================================================
467 Given a list of keys and a time, returns the interpolated value of the
468 envelope at that time.
469 ====================================================================== */
471 float evalEnvelope( lwEnvelope *env, float time )
473 lwKey *key0, *key1, *skey, *ekey;
474 float t, h1, h2, h3, h4, in, out, offset = 0.0f;
478 /* if there's no key, the value is 0 */
480 if ( env->nkeys == 0 ) return 0.0f;
482 /* if there's only one key, the value is constant */
484 if ( env->nkeys == 1 )
485 return env->key->value;
487 /* find the first and last keys */
489 skey = ekey = env->key;
490 while ( ekey->next ) ekey = ekey->next;
492 /* use pre-behavior if time is before first key time */
494 if ( time < skey->time ) {
495 switch ( env->behavior[ 0 ] )
504 time = range( time, skey->time, ekey->time, NULL );
508 time = range( time, skey->time, ekey->time, &noff );
510 time = ekey->time - skey->time - time;
514 time = range( time, skey->time, ekey->time, &noff );
515 offset = noff * ( ekey->value - skey->value );
519 out = outgoing( skey, skey->next )
520 / ( skey->next->time - skey->time );
521 return out * ( time - skey->time ) + skey->value;
525 /* use post-behavior if time is after last key time */
527 else if ( time > ekey->time ) {
528 switch ( env->behavior[ 1 ] )
537 time = range( time, skey->time, ekey->time, NULL );
541 time = range( time, skey->time, ekey->time, &noff );
543 time = ekey->time - skey->time - time;
547 time = range( time, skey->time, ekey->time, &noff );
548 offset = noff * ( ekey->value - skey->value );
552 in = incoming( ekey->prev, ekey )
553 / ( ekey->time - ekey->prev->time );
554 return in * ( time - ekey->time ) + ekey->value;
558 /* get the endpoints of the interval being evaluated */
561 while ( time > key0->next->time )
565 /* check for singularities first */
567 if ( time == key0->time )
568 return key0->value + offset;
569 else if ( time == key1->time )
570 return key1->value + offset;
572 /* get interval length, time in [0, 1] */
574 t = ( time - key0->time ) / ( key1->time - key0->time );
578 switch ( key1->shape )
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;
589 return bez2( key0, key1, time ) + offset;
592 return key0->value + t * ( key1->value - key0->value ) + offset;
595 return key0->value + offset;