;;; -*- Lisp -*-

;;; from VOIDAT.SAI[220,JDC] and GLSVOI.SAI[220,JDC]

(defvar Fnc nil)
  ;; Fnc[sex,vowel,formant number,formant freq,amp or fm index]
(defvar VibFreqFun nil)
(defvar I3Fun1 nil)
(defvar I3Fun2 nil)

(defun flipxy (env)
  (loop for y in env by #'cddr and 
            x in (cdr env) by #'cddr
    collect x 
    collect y))

(defun addenv (env1 sc1 off1 env2 sc2 off2)
  (env+ (scale-envelope env1 sc1 off1)
	(scale-envelope env2 sc2 off2)))

(defun checkPt (att dur)
  (if (or (zerop att)
	  (minusp att))
      (* 100 (/ .01 dur))
    (if (< att dur)
	(* 100 (/ att dur))
      100)))

(defun fillfnc ()
  (when (null Fnc)

    (setf Fnc (make-array '(3 6 4 4) :initial-element nil))
    (setf VibFreqFun (make-array 3 :initial-element nil))
    (setf I3Fun1 (make-array 3 :initial-element nil))
    (setf I3Fun2 (make-array 3 :initial-element nil))

    (setf (aref Fnc 1 1 1 1) (flipxy '(350  130.8 524  261.6 392  392 523 523.2 784 784 1046 1064 1568 1568)))
    (setf (aref Fnc 1 1 1 2) (flipxy '(.3   130.8   .8 261.6   .9 392  .9 523.2  .7 784   .86 1064 .86  1568)))
    (setf (aref Fnc 1 1 1 3) (flipxy '(1.4  130.8  1.4 261.6  1.0 392  .8 523.2  .5 784   .3 1064 .2 1568)))
    (setf (aref Fnc 1 1 2 1) (flipxy '(1100 130.8 1100 261.6 1100 392 1200 523.2 1500 784 1800 1064 2200 1568)))
    (setf (aref Fnc 1 1 2 2) (flipxy '(.1   130.8   .2 261.6   .3 392   .3 523.2   .1 784  .05 1064 .05  1568)))
    (setf (aref Fnc 1 1 2 3) (flipxy '(1.0  130.8  1.0 261.6   .4 392   .4 523.2   .2 784   .2 1064 .1 1568)))
    (setf (aref Fnc 1 1 3 1) (flipxy '(3450 130.8 3400 261.6 3400 392 3600 523.2 4500 784 5000 1064 5800 1568)))
    (setf (aref Fnc 1 1 3 2) (flipxy '(.04  130.8  .04 261.6  .04 392 .045 523.2  .03 784  .02 1064 .02  1568)))
    (setf (aref Fnc 1 1 3 3) (flipxy '(3.5  130.8  2.0 261.6  1.5 392  1.2 523.2   .8 784   .8 1064 1.0  1568)))
    (setf (aref Fnc 1 2 1 1) (flipxy '(175  130.8 262  261.6 392  392 523 523.2 784 784 1046 1064 1568 1568)))
    (setf (aref Fnc 1 2 1 2) (flipxy '(.25  130.8   .6 261.6   .6 392  .6 523.2  .7 784   .86 1064 .86  1568)))
    (setf (aref Fnc 1 2 1 3) (flipxy '(0.5  130.8  0.3 261.6  0.1 392 .05 523.2 .04 784  .03 1064 .02 1568)))
    (setf (aref Fnc 1 2 2 1) (flipxy '(2900 130.8 2700 261.6 2600 392 2400 523.2 2300 784 2200 1064 2100 1568)))
    (setf (aref Fnc 1 2 2 2) (flipxy '(.01  130.8  .05 261.6  .08 392   .1 523.2   .1 784  .1  1064 .05  1568)))
    (setf (aref Fnc 1 2 2 3) (flipxy '(1.5  130.8  1.0 261.6  1.0 392  1.0 523.2  1.0 784  1.0 1064 .5 1568)))
    (setf (aref Fnc 1 2 3 1) (flipxy '(4200 130.8 3900 261.6 3900 392 3900 523.2 3800 784 3700 1064 3600 1568)))
    (setf (aref Fnc 1 2 3 2) (flipxy '(.01  130.8  .04 261.6  .03 392 .03  523.2  .03 784  .03 1064 .02  1568)))
    (setf (aref Fnc 1 2 3 3) (flipxy '(1.2  130.8   .8 261.6   .8 392   .8 523.2   .8 784   .8 1064 .5  1568)))
    (setf (aref Fnc 1 3 1 1) (flipxy '(175  130.8 262  261.6 392  392 523 523.2 784 784 1046 1064 1568 1568)))
    (setf (aref Fnc 1 3 1 2) (flipxy '(.3   130.8   .7 261.6   .8 392  .6 523.2  .7 784   .86 1064 .86  1568)))
    (setf (aref Fnc 1 3 1 3) (flipxy '(0.4  130.8  0.2 261.6  0.4 392 .4  523.2 .7  784  .5  1064 .2  1568)))
    (setf (aref Fnc 1 3 2 1) (flipxy '(1000 130.8 1000 261.6 1100 392 1200 523.2 1400 784 1800 1064 2200 1568)))
    (setf (aref Fnc 1 3 2 2) (flipxy '(.055 130.8  .1  261.6  .15 392  .13 523.2   .1 784  .1  1064 .05  1568)))
    (setf (aref Fnc 1 3 2 3) (flipxy '(0.3  130.8  0.4 261.6  0.4 392  0.4 523.2  0.3 784  0.2 1064 0.1  1568)))
    (setf (aref Fnc 1 3 3 1) (flipxy '(2600 130.8 2600 261.6 3000 392 3400 523.2 4500 784 5000 1064 5800 1568)))
    (setf (aref Fnc 1 3 3 2) (flipxy '(.005 130.8  .03 261.6  .04 392  .04 523.2  .02 784  .02 1064 .02  1568)))
    (setf (aref Fnc 1 3 3 3) (flipxy '(1.1  130.8  1.0 261.6  1.2 392 1.2  523.2  0.8 784  0.8 1064 1.0  1568)))
    (setf (aref Fnc 1 4 1 1) (flipxy '(353  130.8 530  261.6 530  392 523 523.2 784 784 1046 1064 1568 1568)))
    (setf (aref Fnc 1 4 1 2) (flipxy '(.5   130.8   .8 261.6   .8 392  .6 523.2  .7 784   .86 1064 .86  1568)))
    (setf (aref Fnc 1 4 1 3) (flipxy '(0.6  130.8  0.7 261.6  1.0 392 0.8 523.2  .7 784  .5  1064 .2  1568)))
    (setf (aref Fnc 1 4 2 1) (flipxy '(1040 130.8 1040 261.6 1040 392 1200 523.2 1400 784 1800 1064 2200 1568)))
    (setf (aref Fnc 1 4 2 2) (flipxy '(.050 130.8  .05 261.6  .1  392  .2  523.2   .1 784  .1  1064 .05  1568)))
    (setf (aref Fnc 1 4 2 3) (flipxy '(0.1  130.8  0.1 261.6  0.1 392  0.4 523.2  0.3 784  0.2 1064 0.1  1568)))
    (setf (aref Fnc 1 4 3 1) (flipxy '(2695 130.8 2695 261.6 2695 392 3400 523.2 4500 784 5000 1064 5800 1568)))
    (setf (aref Fnc 1 4 3 2) (flipxy '( .05 130.8  .05 261.6  .04 392  .04 523.2  .02 784  .02 1064 .02  1568)))
    (setf (aref Fnc 1 4 3 3) (flipxy '(1.2  130.8  1.2 261.6  1.2 392 1.2  523.2  0.8 784  0.8 1064 1.0  1568)))
    (setf (aref Fnc 1 5 1 1) (flipxy '(175  130.8 262  261.6 392  392 523 523.2 784 784 1046 1064 1568 1568)))
    (setf (aref Fnc 1 5 1 2) (flipxy '(.4   130.8   .4 261.6   .8 392  .8 523.2  .8 784   .8 1064 .8   1568)))
    (setf (aref Fnc 1 5 1 3) (flipxy '(0.1  130.8  0.1 261.6  0.1 392 0.1 523.2  .0 784  .0  1064 .0  1568)))
    (setf (aref Fnc 1 5 2 1) (flipxy '( 350 130.8  524 261.6  784 392  950 523.2 1568 784 2092 1064 3136 1568)))
    (setf (aref Fnc 1 5 2 2) (flipxy '(.8   130.8  .8  261.6  .4  392  .2  523.2   .1 784  .1  1064 .0   1568)))
    (setf (aref Fnc 1 5 2 3) (flipxy '(0.5  130.8  0.1 261.6  0.1 392  0.1 523.2  0.0 784  0.0 1064 0.0  1568)))
    (setf (aref Fnc 1 5 3 1) (flipxy '(2700 130.8 2700 261.6 2500 392 2450 523.2 2400 784 2350 1064 4500 1568)))
    (setf (aref Fnc 1 5 3 2) (flipxy '( .1  130.8  .15 261.6  .15 392  .15 523.2  .15 784  .1  1064 .1   1568)))
    (setf (aref Fnc 1 5 3 3) (flipxy '(2.0  130.8  1.6 261.6  1.6 392 1.6  523.2  1.6 784  1.6 1064 1.0  1568)))
    (setf (aref Fnc 2 1 1 1) (flipxy '( 33   16.5  33   24.5  33 32.7  49  49.0  65 65.41    98   98 131 130.8)))
    (setf (aref Fnc 2 1 1 2) (flipxy '( .3   16.5  .5   24.5  .6 32.7  .5  49.0  .47 65.41   .135 98 .2  130.8)))
    (setf (aref Fnc 2 1 1 3) (flipxy '(2.4   16.5  2.0  24.5 1.8 32.7 1.6  49.0  1.5 65.41    1.2 98 .8  130.8)))
    (setf (aref Fnc 2 1 2 1) (flipxy '(400   16.5  400  24.5 400 32.7  400 49.0 400 65.41    400  98 400 130.8)))
    (setf (aref Fnc 2 1 2 2) (flipxy '( .2   16.5  .2   24.5 .35 32.7  .37 49.0  .4  65.41   .6   98 .8  130.8)))
    (setf (aref Fnc 2 1 2 3) (flipxy '(6.0   16.5  5.0  24.5 4.0 32.7 3.0  49.0  2.7 65.41    2.2 98 1.8  130.8)))
    (setf (aref Fnc 2 1 3 1) (flipxy '(2142  16.5  2142 24.5 2142 32.7 2142 49.0 2142 65.41 2142 98 2142 130.8)))
    (setf (aref Fnc 2 1 3 2) (flipxy '(.02   16.5  .025 24.5 .05  32.7  .09 49.0  .13 65.41  .29 98 .4  130.8)))
    (setf (aref Fnc 2 1 3 3) (flipxy '(9.0   16.5  8.0  24.5 7.2  32.7 5.5  49.0  3.9 65.41   3.0 98 1.8  130.8)))
    (setf (aref Fnc 2 2 1 1) (flipxy '( 33   16.5  33   24.5  33 32.7  49  49.0  65  65.41    98  98 131 130.8)))
    (setf (aref Fnc 2 2 1 2) (flipxy '( .75  16.5  .83  24.5 .91 32.7 .91  49.0  .91 65.41   .79  98 .67 130.8)))
    (setf (aref Fnc 2 2 1 3) (flipxy '(2.5   16.5  2.5  24.5 2.5 32.7 2.1  49.0  1.8 65.41    1.4 98 1.0  130.8)))
    (setf (aref Fnc 2 2 2 1) (flipxy '(1500  16.5 1500 24.5 1500 32.7 1500 49.0 1500 65.41   1500 98 1500 130.8)))
    (setf (aref Fnc 2 2 2 2) (flipxy '( .01  16.5  .02  24.5 .02 32.7  .02 49.0  .02 65.41   .08  98 .08 130.8)))
    (setf (aref Fnc 2 2 2 3) (flipxy '(1.5   16.5  1.37 24.5 1.25 32.7 1.07 49.0 0.9 65.41    0.7 98 0.5  130.8)))
    (setf (aref Fnc 2 2 3 1) (flipxy '(2300  16.5  2300 24.5 2300 32.7 2325 49.0 2350 65.41 2375 98 2400 130.8)))
    (setf (aref Fnc 2 2 3 2) (flipxy '(.05   16.5  .065 24.5 .70  32.7  .07 49.0  .07 65.41  .16 98 .2  130.8)))
    (setf (aref Fnc 2 2 3 3) (flipxy '(11.0  16.5  10.0 24.5 10.0 32.7 7.7  49.0  5.4 65.41   3.7 98 2.0  130.8)))
    (setf (aref Fnc 2 3 1 1) (flipxy '( 33   16.5  33   24.5  33 32.7  49  49.0  65  65.41    98  98 131 130.8)))
    (setf (aref Fnc 2 3 1 2) (flipxy '( .75  16.5  .83  24.5 .87 32.7 .88  49.0  .90 65.41   .87  98 .85 130.8)))
    (setf (aref Fnc 2 3 1 3) (flipxy '(1.4   16.5  1.4  24.5 1.4 32.7 1.4  49.0  1.4 65.41    1.4 98 1.4  130.8)))
    (setf (aref Fnc 2 3 2 1) (flipxy '( 450  16.5  450 24.5  450 32.7  450 49.0  450 65.41    450 98 450 130.8)))
    (setf (aref Fnc 2 3 2 2) (flipxy '( .01  16.5  .02  24.5 .08 32.7 .065 49.0  .05 65.41   .05  98 .05 130.8)))
    (setf (aref Fnc 2 3 2 3) (flipxy '(3.0   16.5  2.6  24.5 2.1  32.7 1.75 49.0 1.4 65.41   1.05 98 0.7  130.8)))
    (setf (aref Fnc 2 3 3 1) (flipxy '(2100  16.5  2100 24.5 2100 32.7 2125 49.0 2150 65.41 2175 98 2100 130.8)))
    (setf (aref Fnc 2 3 3 2) (flipxy '(.05   16.5  .05  24.5 .05  32.7  .05 49.0  .05 65.41 .075 98 .1  130.8)))
    (setf (aref Fnc 2 3 3 3) (flipxy '( 9.0  16.5   8.0 24.5  7.0 32.7 4.5  49.0  2.1 65.41  1.75 98 1.4  130.8)))
    (setf (aref Fnc 2 4 1 1) (flipxy '( 33   16.5  33   24.5  33 32.7  49  49.0  65  65.41    98  98 131 130.8)))
    (setf (aref Fnc 2 4 1 2) (flipxy '( .35  16.5  .40  24.5 .43 32.7 .47  49.0  .50 65.41   .57  98 .45 130.8)))
    (setf (aref Fnc 2 4 1 3) (flipxy '(1.4   16.5  1.4  24.5 1.0 32.7 1.0  49.0  1.0 65.41    1.1 98 1.0  130.8)))
    (setf (aref Fnc 2 4 2 1) (flipxy '( 300  16.5  300 24.5  300 32.7  300 49.0  300 65.41    300 98 300 130.8)))
    (setf (aref Fnc 2 4 2 2) (flipxy '( .75  16.5  .80  24.5 .85 32.7 .90  49.0  .95 65.41   .99  98 .99 130.8)))
    (setf (aref Fnc 2 4 2 3) (flipxy '(3.0   16.5  2.5  24.5 2.0  32.7 1.9  49.0 1.8 65.41   1.65 98 0.25 130.8)))
    (setf (aref Fnc 2 4 3 1) (flipxy '(2200  16.5  2200 24.5 2200 32.7 2225 49.0 2250 65.41 2275 98 2300 130.8)))
    (setf (aref Fnc 2 4 3 2) (flipxy '(.02   16.5  .02  24.5 .02  32.7 .035 49.0  .05 65.41 .07  98 .05  130.8)))
    (setf (aref Fnc 2 4 3 3) (flipxy '( 5.0  16.5   4.0 24.5  3.0 32.7 2.8  49.0  2.6 65.41  1.9  98 1.2  130.8)))
    (setf (aref Fnc 2 5 1 1) (flipxy '(175   16.5  262  24.5 392 32.7  523 49.0  784 65.41  1046  98 1568 130.8)))
    (setf (aref Fnc 2 5 1 2) (flipxy '( .40  16.5  .40  24.5 .80 32.7 .80  49.0  .80 65.41   .80  98 .80 130.8)))
    (setf (aref Fnc 2 5 1 3) (flipxy '(0.1   16.5  0.1  24.5 0.1 32.7 0.1  49.0  0.0 65.41    0.0 98 0.0  130.8)))
    (setf (aref Fnc 2 5 2 1) (flipxy '( 350  16.5  524 24.5  784 32.7  950 49.0 1568 65.41   2092 98 3136 130.8)))
    (setf (aref Fnc 2 5 2 2) (flipxy '( .80  16.5  .80  24.5 .40 32.7 .20  49.0  .10 65.41   .10  98 .00 130.8)))
    (setf (aref Fnc 2 5 2 3) (flipxy '(0.5   16.5  0.1  24.5 0.1  32.7 0.1  49.0 0.0 65.41   0.0  98 0.0  130.8)))
    (setf (aref Fnc 2 5 3 1) (flipxy '(2700  16.5  2700 24.5 2500 32.7 2450 49.0 2400 65.41 2350 98 4500 130.8)))
    (setf (aref Fnc 2 5 3 2) (flipxy '(.10   16.5  .15  24.5 .15  32.7 .15  49.0  .15 65.41 .10  98 .10  130.8)))
    (setf (aref Fnc 2 5 3 3) (flipxy '( 2.0  16.5   1.6 24.5  1.6 32.7 1.6  49.0  1.6 65.41  1.5  98 1.0  130.8)))

    ;; These are vibrato frequencies functions (pitch dependent);

    (setf (aref VibFreqFun 1) (flipxy '(4.5 138.8 5 1568)))
    (setf (aref VibFreqFun 2) (flipxy '(4.5  16.5 5 130.8)))

    ;; These are Index Functions for Cascade Modulater (pitch dependent);

    (setf (aref I3Fun1 1) (flipxy '(4 138.8 4 784 1 1568)))
    (setf (aref I3Fun1 2) (flipxy '(4  16.5 4 65.41 1 130.8)))

    (setf (aref I3Fun2 1) (flipxy '(.4 138.8 .1 1568)))
    (setf (aref I3Fun2 2) (flipxy '(.4  16.5 .1 130.8)))))

(defun fncval (ptr pitch)
  (list-interp pitch ptr))

(definstrument Fm-Voice (Beg Dur Pitch Amp Vowel-1 Sex-1 AmpFun1 AmpFun2 AmpFun3 IndxFun SkewFun VibFun RanFun
			 Dis PcRev Deg VibScl pcran SkewScl RanPower GlissFun GlissAt GlissDc GlissAmt At1 At2 Dc1 Dc2)
  (fillfnc)
  (let* ((c 261.62)
	 (Vowel (floor Vowel-1))
	 (Sex (floor Sex-1))
	 (AmpRef (expt Amp .8))
	 (Deg (- Deg 45))
	 (VibFreq (FncVal (aref VibFreqFun Sex) Pitch))
	 (VibPc (* .01 (/ (log pitch) (log 2)) (+ .15 (sqrt amp)) vibscl))
	 (RanFreq 20)
	 (RanPc (* .002 (/ (log pitch) (log 2)) (- 2 (expt amp .25)) pcran))
	 (SkewPc (if (= sex 1)
		     (* (sqrt (+ .1 (* .05 Ampref (- 1568 130.8)))) SkewScl)
		   (* (sqrt (+ .1 (* .05 Ampref (- 130.8 16.5)))) SkewScl)))
	 (form1 (/ (FncVal (aref Fnc Sex Vowel 1 1) Pitch) Pitch))
	 (form2 (/ (FncVal (aref Fnc Sex Vowel 2 1) Pitch) Pitch))
	 (form3 (/ (FncVal (aref Fnc Sex Vowel 3 1) Pitch) Pitch))
	 (FmntFreq1 (round form1))
	 (FmntFreq2 (round form2))
	 (FmntFreq3 (round form3))
	 (Fm2 3)
	 (MScale 1)
	 (MConst 0)
	 (Mfq (+ (* Pitch MScale) MConst))
	 (FreqRatios1 Fm2)
	 (FreqRatios2 MScale)
	 (FreqRatios3 MScale)
	 (FreqRatios4 MScale)
	 (FreqRatios5 FmntFreq1)
	 (FreqRatios6 FmntFreq2)
	 (FreqRatios7 FmntFreq3)
	 (AtDrGl GlissAt)
	 (DcDrGl GlissDc)
	 (Amp1 (sqrt Amp))
	 (Amp2 (expt Amp 1.5))
	 (Amp3 (* Amp Amp))
	 (formscl1 (ABS (- form1 FmntFreq1)))
	 (formscl2 (ABS (- form2 FmntFreq2)))
	 (formscl3 (ABS (- form3 FmntFreq3)))
	 (CarAmp1Sc (* (FncVal (aref Fnc Sex Vowel 1 2) Pitch) (- 1 formscl1) Amp1))
	 (CarAmp2Sc (* (FncVal (aref Fnc Sex Vowel 2 2) Pitch) (- 1 formscl2) Amp2))
	 (CarAmp3Sc (* (FncVal (aref Fnc Sex Vowel 3 2) Pitch) (- 1 formscl3) Amp3))
	 (ScDev1 (FncVal (aref Fnc Sex Vowel 1 3) Pitch))
	 (ScDev2 (FncVal (aref Fnc Sex Vowel 2 3) Pitch))
	 (ScDev3 (FncVal (aref Fnc Sex Vowel 3 3) Pitch))
	 (Indx0 (if (or (= Vowel 3) (= Vowel 4)) 0 1.5))
	 (Indx1 1)
	 (I3 (if (< pitch (/ c 2)) (FncVal (aref I3Fun1 Sex) Pitch) (FncVal (aref I3Fun2 Sex) pitch)))
	 (Dev (in-Hz (* I3 Mfq)))
	 (Dev0 (in-Hz (* Indx0 Mfq)))
	 (Dev1 (in-Hz (* (- Indx1 Indx0) Mfq)))
	 (A1DrCr1 At1) (A2DrCr1 At2) (D1DrCr1 Dc1) (D2DrCr1 Dc2)
	 (A1DrCr2 At1) (A2DrCr2 At2) (D1DrCr2 Dc1) (D2DrCr2 Dc2)
	 (A1DrCr3 At1) (A2DrCr3 At2) (D1DrCr3 Dc1) (D2DrCr3 Dc2)
	 (A1DrIdx At1) (A2DrIdx At2) (D1DrIdx Dc1) (D2DrIdx Dc2)
	 (A1DrFrqV At1) (A2DrFrqV At2) (D1DrFrqV Dc1) (D2DrFrqV Dc2)
	 (A1DrFrqR At1) (A2DrFrqR At2) (D1DrFrqR Dc1) (D2DrFrqR Dc2)
	 (A1DrSk (* .35 (/ (log Pitch) (log 16)) (sqrt Amp)))
	 (A2DrSk .01)
	 (D1DrSk .01)
	 (D2DrSk Dc2))
    (if (> (+ A1DrSk A2DrSk D1DrSk D2DrSk .03) Dur)
	(setf A1DrSk A1DrCr1))
    (let* ((TmpAmpFun1 (divenv AmpFun1 Dur
			       20 A1DrCr1 40 A2DrCr1 
			       60 (- Dur (+ A1DrCr1 A2DrCr1 D1DrCr1 D2DrCr1))
			       80 D1DrCr1 100 D2DrCr1))
	   (TmpAmpFun2 (divenv AmpFun2 Dur
			       20 A1DrCr2 40 A2DrCr2 
			       60 (- Dur (+ A1DrCr2 A2DrCr2 D1DrCr2 D2DrCr2))
			       80 D1DrCr2 100 D2DrCr2))
	   (TmpAmpFun3 (divenv AmpFun3 Dur
			       20 A1DrCr3 40 A2DrCr3 
			       60 (- Dur (+ A1DrCr3 A2DrCr3 D1DrCr3 D2DrCr3))
			       80 D1DrCr3 100 D2DrCr3))
	   (TmpIndxFun (divenv IndxFun Dur
			       20 A1DrIdx 40 A2DrIdx
			       60 (- Dur (+ A1DrIdx A2DrIdx D1DrIdx D2DrIdx))
			       80 D1DrIdx 100 D2DrIdx))
	   (TmpSkewFun (divenv SkewFun Dur
			       20 A1DrSk 40 A2DrSk 
			       60 (- Dur (+ A1DrSk A2DrSk D1DrSk D2DrSk))
			       80 D1DrSk 100 D2DrSk))
	   (TmpVibFun (divenv VibFun Dur
			      20 A1DrFrqV 40 A2DrFrqV 
			      60 (- Dur (+ A1DrFrqV A2DrFrqV D1DrFrqV D2DrFrqV))
			      80 D1DrFrqV 100 D2DrFrqV))
	   (TmpRanFun (divenv RanFun Dur
			      20 A1DrFrqR 40 A2DrFrqR
			      60 (- Dur (+ A1DrFrqR A2DrFrqR D1DrFrqR D2DrFrqR))
			      80 D1DrFrqR 100 D2DrFrqR))
	   (TmpGlissFun (divseg (addenv GlissFun (* GlissAmt Pitch) 0 
					TmpSkewFun (* SkewPc Pitch) Pitch)
				25 (checkPt AtDrGl Dur)
				75 (- 100 (checkpt DcDrGl Dur))))
	   (Gens1 (make-oscil :frequency 0))
	   (Gens2 (make-oscil :frequency 0 :initial-phase half-pi))
	   (Gens2AmpEnv (make-env :envelope TmpIndxFun 
				  :scaler (* ScDev1 dev1)
				  :offset (* ScDev1 dev0)
				  :start-time beg :duration dur))
	   (Gens3 (make-oscil :frequency 0 :initial-phase half-pi))
	   (Gens3AmpEnv (make-env :envelope TmpIndxFun 
				  :scaler (* ScDev2 dev1)
				  :offset (* ScDev2 dev0)
				  :start-time beg :duration dur))
	   (Gens4 (make-oscil :frequency 0 :initial-phase half-pi))
	   (Gens4AmpEnv (make-env :envelope TmpIndxFun 
				  :scaler (* ScDev3 dev1)
				  :offset (* ScDev3 dev0)
				  :start-time beg :duration dur))
	   (Gens5 (make-oscil :frequency 0))
	   (Gens5AmpEnv (make-env :envelope TmpAmpFun1
				  :scaler (* Amp CarAmp1Sc .75)
				  :start-time beg :duration dur))
	   (Gens6 (make-oscil :frequency 0))
	   (Gens6AmpEnv (make-env :envelope TmpAmpFun2
				  :scaler (* Amp CarAmp2Sc .75)
				  :start-time beg :duration dur))
	   (Gens7 (make-oscil :frequency 0))
	   (Gens7AmpEnv (make-env :envelope TmpAmpFun3
				  :scaler (* Amp CarAmp3Sc .75)
				  :start-time beg :duration dur))
	   (freqEnv (make-env :envelope TmpGlissFun
			      :scaler frequency-mag
			      :start-time beg :duration dur))
	   (pervEnv (make-env :envelope TmpVibFun
			      :scaler VibPc
			      :start-time beg :duration dur))
	   (ranvEnv (make-env :envelope TmpRanFun
			      :scaler RanPc
			      :start-time beg :duration dur))
	   (per-vib (make-triangle-wave :frequency VibFreq
					:amplitude (in-Hz Pitch)))
	   (ran-vib (make-randh :frequency RanFreq
				:amplitude (in-Hz Pitch)))
	   (loc (make-locsig :degree deg :distance dis :revscale PcRev))
	   (vib 0.0)
	   (CascadeOut 0.0)
	   (start (floor (* sampling-rate beg)))
	   (end (+ start (floor (* sampling-rate dur)))))
      (Run
       (loop for i from start to end do
	 (setf vib (+ (env freqenv) 
		      (* (env pervEnv) 
			 (triangle-wave per-vib)) 
		      (* (env ranvEnv) 
			 (randh ran-vib))))
	 (setf CascadeOut (* dev (oscil Gens1 (* vib FreqRatios1))))
	 (locsig loc i (+ (* (env Gens5AmpEnv) 
			     (oscil Gens5 (+ (* vib FreqRatios5) 
					     (* (env Gens2AmpEnv) 
						(oscil Gens2 (+ CascadeOut (* vib FreqRatios2)))))))
			  (* (env Gens6AmpEnv) 
			     (oscil Gens6 (+ (* vib FreqRatios6)
					     (* (env Gens3AmpEnv) 
						(oscil Gens3 (+ CascadeOut (* vib FreqRatios3)))))))
			  (* (env Gens7AmpEnv) 
			     (oscil Gens7 (+ (* vib FreqRatios7)
					     (* (env Gens4AmpEnv) 
						(oscil Gens4 (+ CascadeOut (* vib FreqRatios4))))))))))))))



;;; (let ((ampf '(0 0 1 1 2 1 3 0))) 
;;;   (with-sound () (fm-voice 0 1 300 .8 3 1 ampf ampf ampf ampf ampf ampf ampf 1 0 0 1 3 .01 0 ampf .1 .1 0 .1 .1 .1 .1)))
#|
(defmacro voi (Beg Dur Pitch Amp Vowel-1 Sex-1 AmpFun1 AmpFun2 AmpFun3 IndxFun SkewFun VibFun RanFun
	       Dis PcRev Deg VibScl SkewScl RanPower At1 At2 Dc1 Dc2)
  `(Fm-Voice ,Beg ,Dur ,Pitch ,Amp ,Vowel-1 ,Sex-1 ,AmpFun1 ,AmpFun2 ,AmpFun3 ,IndxFun ,SkewFun ,VibFun ,RanFun
		  ,Dis ,PcRev ,Deg ,VibScl 0 ,SkewScl ,RanPower '(0 0 100 0) 0 0 0 ,At1 ,At2 ,Dc1 ,Dc2))
|#