Tag Archives: disklavier

Dan’s advice is to keep Disklavier velocities to…

Dan’s advice is to keep Disklavier velocities to within the 30 to 90 range. I’ve just implemented the rescale function to help with this (see below) so if you have events with amplitudes between 0.0 and 1.0 then the following would map those to velocities of 30 to 90:

(midi-play +jitterbug+ :force-velocity
	   #'(lambda (event) 
	       (round (rescale (amplitude event) 0 1 30 90))))

;;; ****f* utilities/rescale
;;; DATE
;;; June 8th 2016, Edinburgh
;;; 
;;; DESCRIPTION
;;; Given a value within an original range, return its value withing a new range
;;; 
;;; ARGUMENTS
;;; - the value we want to rescale
;;; - the original minimum
;;; - the original maximum
;;; - the new minimum
;;; - the new maximum
;;; 
;;; RETURN VALUE
;;; The value within the new range (a number)
;;; 
;;; EXAMPLE
#|
(rescale .5 0 1 0 100)
==> 50.0
|#
;;; SYNOPSIS
(defun rescale (val min max new-min new-max)
;;; ****
  (when (or (>= min max)
            (>= new-min new-max))
    (error "utilities::rescale: argument 2 (~a) must be < argument 3 (~a) ~
            ~%and sim. for argument 4 (~a) and 5 (~a)" min max new-min new-max))
  (unless (and (>= val min)
               (<= val max))
    (error "utilities::rescale: first argument (~a) must be within original ~
            range (~a to ~a)" val min max))
  (let* ((range1 (float (- max min)))
         (range2 (float (- new-max new-min)))
         (prop (float (/ (- val min) range1))))
    (+ new-min (* prop range2))))