(define *srate* 44100)

(definstrument (soundfile-ampmod start-time duration soundfile frequency-envelope amplitude-envelope)
  (let* ((start-sample (* start-time *srate*))
         (end-sample (+ start-sample (* duration *srate*)))
         (readin (make-readin soundfile))
         (wavetable (make-src readin))
         (am-oscillator (make-oscil :frequency 0))
         (freq-env (make-env :duration duration 
                             :envelope frequency-envelope 
                             :scaler (hz->radians 1)))
         (amp-env (make-env :duration duration
                            :envelope amplitude-envelope)))
     (do ((sample-number start-sample (+ sample-number 1)))
         ((> sample-number end-sample))
       (outa sample-number (* (env amp-env)
                              (* (+ (* (oscil am-oscillator (env freq-env)) 
                                       0.5) 
                                    0.5)
                                 (src wavetable)))))))

;; Example usage
(with-sound (:srate *srate* :statistics #t) 
  (soundfile-ampmod 0 60 "/Users/danielhensel/1-4.wav" 
                    '(0 220 0.5 440 1 110) 
                    '(0 0 0.01 1 0.99 1 1 0)))
