anchor.diff:     dur
      (tolerance :default 0)
      (current-dur :state t)
  -   (current-elapsed :state t :initform 0))
  +   (elapsed-dur :state t :initform 0))
     :documentation "pfindur yields events from PATTERN until their total duration is within TOLERANCE of DUR, or greater than DUR. Any events that would end beyond DUR are cut short. If PATTERN outputs numbers, their total sum is limited instead.
   
   Example:
  
positive.code:                    :tolerance (next tolerance))))
  
  (defmethod next ((psync psync-pstream)) ;; FIX: implement tolerance
    (with-slots (pattern sync-quant maxdur tolerance history) psync
      (let* ((n-event (next pattern))
             (elapsed-dur (reduce #'+ (mapcar #'event-value (remove-if #'null history) (circular-list :delta))))
             (delta (- (round-by-direction elapsed-dur sync-quant) elapsed-dur)))
        (if (null n-event)
            (when (plusp delta)
              (event :type :rest :dur delta))
            (when (or (null maxdur)
                      (not (>= elapsed-dur maxdur)))
              (if (and (not (null maxdur))
                       (> (+ elapsed-dur (event-value n-event :dur)) maxdur))
                  (combine-events n-event (event :dur (- maxdur elapsed-dur)))
                  n-event))))))
  
  ;;; pdurstutter
  ;; FIX: make a version where events skipped with 0 are turned to rests instead (to keep the correct dur)
  
negative.code:            (if (event-p ev)
                 (event-value ev :delta)
                 ev)))
      (with-slots (pattern dur tolerance current-dur elapsed-dur) pfindur
        (when-let ((n-event (next pattern)))
          (unless (slot-boundp pfindur 'current-dur)
            (setf current-dur (next dur)))
          (when current-dur
            (if (eql :inf current-dur)
                n-event
                (let ((new-elapsed (+ (get-delta n-event) elapsed-dur)))
                  (prog1
                      (if (> (if (= 0 tolerance)
                                 new-elapsed
                                 (round-by-direction new-elapsed tolerance))
                             current-dur)
                          (let ((tdur (- current-dur elapsed-dur)))
                            (when (plusp tdur)
                              (if (event-p n-event)
                                  (combine-events n-event (event :dur tdur))
                                  tdur)))
                          n-event)
                    (incf elapsed-dur (get-delta n-event))))))))))
  
  ;;; psync
  
  