[CM] is this the best way to do this?

cristopher pierson ewing cewing@u.washington.edu
Thu, 30 Jan 2003 17:04:22 -0800 (PST)


I'm trying to implement a method on clm envelopes that will automatically
rescale a clm-env to write the same output bu over a different duration.
That is to say, given an env that starts at val x and ends at val y
lasting over dur z, I want to be able to return an env that starts at x
and ends at y lasting over dur a (which may be longer or shorter than the
original.)  I also want to preserve all of the original characteristics of
the first env: base, offset, scalar, etc.

Here is my first good stab at the problem (there were several worse ones):

(defmethod change-env-dur ((env clm::seg) dur)
  (restart-env env)
  (let* ((new-env (copy-object env))
         (old-dur (/ (+ (clm::seg-end env) 1) *srate*))
         (dur-ratio (float (/ dur old-dur)))
         (new-data (loop for val in (clm::seg-data env)
                         for i upfrom 0
                         collect (if (evenp i)
                                   val
                                   (/ val dur-ratio))))
         (new-end (floor (- (* (+ (clm::seg-end env) 1) dur-ratio) 1))))
    (setf (clm::seg-data new-env) new-data
          (clm::seg-restart-data new-env) new-data
          (clm::seg-end new-env) new-end)
    new-env))

I know it's a bit sloppy and uses way to many intermediate variables, but
hey, it's a first try.  My question is this, am I missing anything vital
here?  I have tested the method pretty extensively and the only big
problem I see so far is that rounding error in my stupid division and
multiplication steps definitely accumulates, that is to say, if the
duration of the env is changed more that a few times, the distance that
the last value out falls from the desired last value grows larger and
larger.  By the time I've altered the env 6 or 8 times, it has become very
significant.

Now this shouldn't be too big a problem so long as I use the method
non-destructively.  But who knows, I might forget that some day.

Any suggestions as to how to improve this toy?

********************************
Cris Ewing
CARTAH Assistant
University of Washington
Home Phone: (206) 365-3413
E-mail: cewing@u.washington.edu
*******************************