(setq standscale 0.5)

(defun exprolloff (stretch n) (exp (/ (- 0 n) stretch)))

(defun dexprolloff (stretch n) (exp (/ (- 1.0 (exp (float (1- n)))) stretch)))

(defun sumharmonics (fundamental rolloff cutoff duration)
  (simrep (n (1- (truncate (/ cutoff fundamental))))
    (scale (apply rolloff (list (1+ n)))
      (sine (hz-to-step (* fundamental (1+ n))) duration)
    )
  )
)

(defun sumenvharmonics (fundamental rolloff cutoff rise duration)
  (simrep (n (1- (truncate (/ cutoff fundamental))))
    (scale (apply rolloff (list (1+ n)))
      (amosc
        (hz-to-step (* fundamental (1+ n)))
        (pwl (apply rise (list n)) 1 duration 1 (+ duration 0.1))
      )
    )
  )
)

(defun powerrise (overall degree n) (* overall (expt (float n) degree)))

(defun exprise (first limit stretch n)
  (+ limit (* (- first limit) (exp (/ (- 1 n) stretch))))
)

(defun standrolloff (n) (exprolloff 1.0 n))

(defun quickex (stretch)
  (scale standscale (sumharmonics 300 (lambda (n) (/ (exprolloff stretch n) stretch)) 7500 0.5))
)

(defun quickrisex (first limit duration)
  (sumenvharmonics 300 (lambda (n) (/ (exprolloff 1.5 n) 1.5)) 7500
    (lambda (n) (exprise first limit 1.0 (1+ n))) duration
  )
)

(setq exppardemo
  (seq
    (seqrep (t 45) (quickex (+ 0.1 (/ t 5.0))))
    (seqrep (t 45) (quickex (- 9.1 (/ t 5.0))))
  )
)

(setq risepardemo
  (seqrep (t 20) (quickrisex (+ 0.0 (/ t 40.0)) (- 0.5 (/ t 40.0)) 2))
)

(defun quickdex (stretch)
  (scale standscale (sumharmonics 300 (lambda (n) (/ (dexprolloff stretch n) stretch)) 7500 0.5))
)

(defun oerolloff (oweight stretch n)
  (* (if (oddp n) oweight (- 2.0 oweight)) (exprolloff stretch n))
)

(defun quickoeex (oweight stretch)
  (scale standscale
    (sumharmonics 300 (lambda (n) (/ (oerolloff oweight stretch n) stretch)) 7500 0.5)
  )
)

(defun oepardemo (stretch)
  (seq
    (seqrep (t 10) (quickoeex (+ 1.0 (/ t 10.0)) stretch))
    (seqrep (t 10) (quickoeex (- 2.0 (/ t 10.0)) stretch))
  )
)

(defun twopardemo (stretch)
  (seq
    (seqrep (t (1- (truncate (* stretch 10)))) (quickex (+ 0.1 (/ t 10.0))))
    (scale standscale (sumharmonics 300 (lambda (n) (/ (exprolloff stretch n) stretch)) 7500 1.5))
    (s-rest 0.1)
    (oepardemo stretch)
  )
)

(defun twthrolloff (stretch third n)
  (if (equal n 3) third (exprolloff stretch n))
)

(defun twfthrolloff (stretch fourth n)
  (if (equal n 4) fourth (exprolloff stretch n))
)

(defun quicktwex (stretch third)
  (scale standscale (sumharmonics 300
    (lambda (n) (/ (twthrolloff stretch third n) stretch)) 7500 0.5)
  )
)

(defun quicktwfex (stretch fourth)
  (scale standscale (sumharmonics 300
    (lambda (n) (/ (twfthrolloff stretch fourth n) stretch)) 7500 0.5)
  )
)

(defun twpardemo (stretch)
  (seq
    (seqrep (t 10) (quicktwex stretch (/ t 20.0)))
    (seqrep (t 10) (quicktwex stretch (- 0.5 (/ t 20.0))))
  )
)

(defun twfthpardemo (stretch)
  (seq
    (seqrep (t 10) (quicktwfex stretch (/ t 20.0)))
    (seqrep (t 10) (quicktwfex stretch (- 0.5 (/ t 20.0))))
  )
)

(defun primeharmonics (stretch duration)
  (sim
    (scale (/ (exprolloff stretch 1) stretch) (sine (hz-to-step 300) duration))
    (scale (/ (exprolloff stretch 3) stretch) (sine (hz-to-step 900) duration))
    (scale (/ (exprolloff stretch 5) stretch) (sine (hz-to-step 1500) duration))
    (scale (/ (exprolloff stretch 7) stretch) (sine (hz-to-step 2100) duration))
    (scale (/ (exprolloff stretch 11) stretch) (sine (hz-to-step 3300) duration))
    (scale (/ (exprolloff stretch 13) stretch) (sine (hz-to-step 3900) duration))
    (scale (/ (exprolloff stretch 17) stretch) (sine (hz-to-step 5100) duration))
    (scale (/ (exprolloff stretch 19) stretch) (sine (hz-to-step 5700) duration))
    (scale (/ (exprolloff stretch 23) stretch) (sine (hz-to-step 6900) duration))
    (scale (/ (exprolloff stretch 29) stretch) (sine (hz-to-step 8700) duration))
    (scale (/ (exprolloff stretch 31) stretch) (sine (hz-to-step 9300) duration))
    (scale (/ (exprolloff stretch 37) stretch) (sine (hz-to-step 11700) duration))
    (scale (/ (exprolloff stretch 41) stretch) (sine (hz-to-step 12300) duration))
    (scale (/ (exprolloff stretch 47) stretch) (sine (hz-to-step 14100) duration))
    (scale (/ (exprolloff stretch 53) stretch) (sine (hz-to-step 15900) duration))
    (scale (/ (exprolloff stretch 59) stretch) (sine (hz-to-step 17700) duration))
    (scale (/ (exprolloff stretch 61) stretch) (sine (hz-to-step 18300) duration))
    (scale (/ (exprolloff stretch 67) stretch) (sine (hz-to-step 20100) duration))
    (scale (/ (exprolloff stretch 71) stretch) (sine (hz-to-step 21300) duration))
    (scale (/ (exprolloff stretch 73) stretch) (sine (hz-to-step 21900) duration))
    (scale (/ (exprolloff stretch 79) stretch) (sine (hz-to-step 23700) duration))
    (scale (/ (exprolloff stretch 83) stretch) (sine (hz-to-step 24900) duration))
    (scale (/ (exprolloff stretch 89) stretch) (sine (hz-to-step 26700) duration))
    (scale (/ (exprolloff stretch 97) stretch) (sine (hz-to-step 29100) duration))
    (scale (/ (exprolloff stretch 101) stretch) (sine (hz-to-step 30300) duration))
    (scale (/ (exprolloff stretch 103) stretch) (sine (hz-to-step 30900) duration))
    (scale (/ (exprolloff stretch 107) stretch) (sine (hz-to-step 32100) duration))
  )
)

(defun sumpartials (partial rolloff numparts duration)
  (simrep (n (1- numparts))
    (scale (apply rolloff (list (1+ n)))
      (sine (hz-to-step (apply partial (list (1+ n)))) duration)
    )
  )
)

(defun quadpartials (fundamental quadcoeff n)
  (* (float fundamental) (+ (float n) (* quadcoeff (float (* (1- n) (1- n))))))
)

(defun quickquadex (stretch quadcoeff)
  (prod (env 0.001 1.0 0.001 standscale standscale standscale 1.006)
    (sumpartials
      (lambda (n) (quadpartials 300 quadcoeff n))
      (lambda (n) (/ (exprolloff stretch n) stretch))
      25 1.006
    )
  )
)

(defun quadpardemo (stretch)
  (seq
    (seqrep (t 40) (quickquadex stretch (/ t 1600.0)))
    (seqrep (t 40) (quickquadex stretch (- 0.025 (/ t 1600.0))))
  )
)
(defun cubpartials (fundamental quadcoeff cubcoeff n)
  (* (float fundamental)
    (+ (float n)
       (* quadcoeff (float (* (1- n) (1- n))))
       (* cubcoeff (float (* (1- n) (* (1- n) (1- n)))))
    )
  )
)

(defun quickcubex (stretch quadcoeff cubcoeff)
  (prod (env 0.001 1.0 0.001 standscale standscale standscale 1.006)
    (sumpartials
      (lambda (n) (cubpartials 300 quadcoeff cubcoeff n))
      (lambda (n) (/ (exprolloff stretch n) stretch))
      25 1.006
    )
  )
)

(defun cubpardemo (stretch quadcoeff)
  (seq
    (seqrep (t 40) (quickcubex stretch  quadcoeff (/ t 1600.0)))
    (seqrep (t 40) (quickcubex stretch quadcoeff (- 0.025 (/ t 1600.0))))
  )
)

(defun sumfuzzharmonics (fundamental rolloff cutoff fuzz)
  (simrep (n (1- (truncate (/ cutoff fundamental))))
    (scale (apply rolloff (list (1+ n)))
      (fmosc (hz-to-step (* fundamental (1+ n))) fuzz)
    )
  )
)

(defun quickfex (stretch fstr)
  (scale standscale
    (sumfuzzharmonics 300
      (lambda (n) (/ (exprolloff stretch n) stretch))
      7500
      (scale fstr (noise 0.5))
    )
  )
)

(defun fuzzpardemo (stretch)
  (seq
    (seqrep (t 10) (quickfex stretch (* (float t) 10.0)))
    (seqrep (t 10) (quickfex stretch (- 100.0 (* (float t) 10.0))))
  )
)
