(definstrument drum-ks (start-time amplitude
		&key (b 0.5)                 ; blend factor
		     (p 200)                 ; wavetable length
		     (duration 2)            ; sound duration
		     (amp-env '(0 1 100 1))) ; amplitude envelope
  (multiple-value-bind (beg end) (get-beg-end start-time duration)
    (let* ((wave (loop for i from 1 to p collect (- (random 2.0) 1)))
	   (amp (make-env :envelope amp-env :scaler amplitude :duration duration)))
      (loop for i from beg to end do
	(let ((r (- 0.5 (round (+ b -0.5 (random 1.0))))))
	  (setf wave (append wave (list (+ (* 0.5 (first wave)) (* r (second wave))))))
	  (setf wave (rest wave))
	  (outa i (* (env amp) (car (last wave))))
	  )))))

#|
; Snares:
(with-sound () (drum-ks 0 .5 :p 800 :b 0.5))
(with-sound () (drum-ks 0 .5 :p 1000 :b 0.6))

; Cymbals:
(with-sound () (drum-ks 0 .5 :p 4000 :b 1 :amp-env '(0 1 90 1 100 0)))
(with-sound () (drum-ks 0 .5 :p 2000 :b 1 :amp-env '(0 1 80 0 100 0)))

; Metallic plinks:
(with-sound () (drum-ks 0 .5 :p 100 :b .98 :amp-env '(0 1 90 1 100 0)))
(with-sound () (drum-ks 0 .5 :p 20 :b .997))
(with-sound () (drum-ks 0 .5 :p 50 :b .99))
(with-sound () (drum-ks 0 .5 :p 150 :b .99 :amp-env '(0 1 90 1 100 0)))
(with-sound () (drum-ks 0 .5 :p 25 :b 1))

; Plucked strings:
(with-sound () (drum-ks 0 .5 :p 40 :b 0 :amp-env '(0 1 90 1 100 0)))
(with-sound () (drum-ks 0 1 :p 25 :b 0))
(with-sound () (drum-ks 0 .5 :p 200 :b 0 :amp-env '(0 1 50 1 100 0) :duration 2))
(with-sound () (drum-ks 0 .5 :p 400 :b 0 :amp-env '(0 1 50 1 100 0) :duration 2))
|#