(defun make-cells (size
           &key
        (base 440)
        (limit 4))
  (make-array size
    :initial-contents
    (loop repeat size
      collect (let* ((num (+ 1 (random limit)))
             (den (if (= num 1) 1 (- num 1))))
            (if (< (random 1.0) 0.5)
            (* base (/ num den))
            (* base (/ den num)))))))

(defun step-cells (cells limit)
  (let* ((size (array-dimension cells 0))
         (next (make-array size :initial-element 0)))
    (loop for i from 0 below size do
      (let* ((current (aref cells i)))
    (setf (aref next i)
          (let* ((num (+ 1 (random limit)))
             (den (if (= num 1) 1 (- num 1))))
        (if (< (random 1.0) 0.5)
            (* current (/ num den))
          (* current (/ den num)))
        )
          )))
    next))
         
(defun pan-cells (size
          &key
          (left 0)
          (right 90))
  (make-array size
          :initial-contents
          (loop repeat size
        collect (+ left (random (- right left))))))
 
(definstrument sine-cells (start-time duration freq pan &key (gain 1.))
  (let* ((beg (floor (* start-time *srate*)))
     (end (+ beg (floor (* duration *srate*))))
     (size (array-dimension freq 0))
     (oscillators (make-array size :initial-element 0))
     (locs (make-array size :initial-element 0))
     (result 0))
    (loop for i from 0 below size do
      (setf (aref oscillators i) (make-oscil :frequency (aref freq i))))
    (loop for i from 0 below size do
      (setf (aref locs i) (make-locsig :degree (aref pan i) :distance 1 :reverb 0.1)))
    (run
     (loop for i from beg below end do
      (loop for j from 0 below size do
    (locsig (aref locs j) i (/ (oscil (aref oscillators j)) size)))))))