LISP code for the filter instrument

from/for Christopher Bailey's Dissertation






;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;
;     THE FILTER INSTRUMENT
;
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************
;*************************************************************************************

;
;  FOR FILTER INSTRUMENT:
;
;
;    0            1   2   3          4   5   6      7     8    9                 10       11      12         13
;   Inst  start: msr beat div  dur: msr beat div  Octve  PC   registral-lyne#   Dyn-lvl  Spatlztn stereo    Artic 
;   name           0   0    0         1   0    0    5     17   0-5               0-5      0-1      0-5     word     
;  
;
;-----------------------------------------------------------------------------------------------------
;
;          14               15          |||          16             17            18           
;                          filter       |||
;       is-it-drone?      noisiness     |||   (soundfile-name) (final duration)  (rewrite-flag)
;        0-1               0-5          |||                      in seconds      0-1           
;                           must be a
;                          list
;
;
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
;
;
;
;
;
;
;Articulation/agitation.  might be a list:
;------------------------------------------
; (staccatto: short duration)
; accent/ten
; Fp
; TRemolo  --- internal rate changes determined by random row form
;"Bartok Pizz"
; vibrato  (on wave & filt)  --- internal rate changes determined by random row form
; random Gliss  (on wave & filt)  (you could include end pitch)
; cresc.    (list includes start and end dynamic)
; decresc.  (list includes start and end dynamic)

;-------------------------------------------
;
;
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;                FILT FILT FILT FILT FILT FILT FILT
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;**************************************************************************************
;;**************************************************************************************
;
;


(defun make-filt (note-number)
 (let ((file-str ())
       (dynamic '0)
       (frequency '0)
       (desired-dur '0)       
       (this-source ())
       (note-spec ())
       (this-inst ())
       (actual-dur '0.0)
       (stereo-pos '0)
       (spat-command ())
       (roomsize '0.0)
       (wet '0.0)
       (extra-time '0.0)
       )
     ;
     ;   LOCAL FUNCTION to   figure out filename (or use old one) fill in dur and flag, 
     ;                  so note-list is length 18 AND get base file-name string
     ;
  (labels
   ((initialize-note-in-note-list (note-number)
     (let ((nll ())
           (file-str ()))  
      (setf nll (length (nth note-number note-list)))
      (case nll
         ; append filename, duration, re-write flag
        ('16
          (setf file-str (princ-to-string (+ note-number (const-frandom 0.0 1.0))))
          (setf (nth note-number note-list) 
                       (append (nth note-number note-list) 
                               (list (concatenate 'string file-str ".aiff"))))
          (setf (nth note-number note-list) 
                       (append (nth note-number note-list) 
                               (list '0.0)))
          (setf (nth note-number note-list)
                       (append  (nth note-number note-list) 
                               (list '0))))
;
;              change re-write flag
;
        ('19
          (setf (nth filt-rewrite-par (nth note-number note-list)) '0)
          (shell (concatenate 'string "rm " (nth filt-filename-par (nth note-number note-list))))
          (setf file-str (subseq 
                          (nth filt-filename-par (nth note-number note-list))
                          0 
                          (- (length (nth filt-filename-par (nth note-number note-list)))
                                     5)))))
        file-str)))
;
;      MAIN BODY MAIN BODY MAIN BODY MAIN BODY  MAIN BODY  MAIN BODY 
;
    (setf file-str (initialize-note-in-note-list note-number))
    (setf dynamic (nth pitch-dyn-par (nth note-number note-list)))
    (setf frequency
          (pitch-to-freq
                   (nth pitch-octave-par (nth note-number note-list))
                   (nth pitch-pc-par (nth note-number note-list))))
    (setf desired-dur  (convert-dur-from-mbd-sec
                          (subseq (nth note-number note-list) pitch-dur-msr-par  
                                        (+ 1 pitch-dur-div-par))))
;
;   open the main filter CMIX score file
;
    (setf score-filename (concatenate 'string file-str ".sco"))
    (with-open-file (score-file score-filename :direction :output)
;
;   make an object instance out of the note
;
      (setf note-spec
            (if (listp (nth pitch-artic-par (nth note-number note-list)))
;
;          we're going to use extra-params 
;       if the articulation is a list with extra-parameters like cresc or gliss
;                       (for example:  (cresc 2 5)   )
                (make-instance
                   (first (nth pitch-artic-par (nth note-number note-list)))
                         :desired-dur  desired-dur
                         :frequency    frequency
                         :dynamic      dynamic
                         :extra-params (rest (nth pitch-artic-par (nth note-number note-list)))
                         :note-number  note-number)
;
;    if the articulation is just a word like accent-tenuto or bartok-pizz
;
                (make-instance 
                   (nth pitch-artic-par (nth note-number note-list))
                         :desired-dur  desired-dur
                         :frequency    frequency
                         :dynamic      dynamic
                         :note-number  note-number)))
;
      (setf this-inst (make-instance 'filt)) 
      (setf actual-dur (write-cmix-score note-spec this-inst score-file)))
;            run CMIX
      (shell (concatenate 'string "CMIX < " score-filename))
;            remove score file
      (shell (concatenate 'string "rm " file-str ".sco"))
;            remove noise aiff and score not really necessary.
;
;       spatialize. . .
;
     (setf stereo-pos (/  (nth pitch-stereo-par (nth note-number note-list)) '5.0))
     (setf spat-score-name (concatenate 'string file-str ".spat.sco"))
     (case (nth pitch-spatq-par (nth note-number note-list))
;                no spatialization:
           ('0  
;               write cmix STEREO score
             (with-open-file (spat-score spat-score-name :direction :output)
               (format spat-score 
                    "~% rtsetparams(44100, 2) ~%
                        load(\"STEREO\") ~%
                        rtinput(\"~a\")  ~%
                        rtoutput(\"~a.spat.aiff\") ~%
                        STEREO( 0.0, 0.0, ~a, ~a, ~a)~%~%"
;                              output file, 
                   (concatenate 'string file-str ".aiff")
                   file-str actual-dur
;                      amp factor corrects for stereo loss
                         (- 1.8 (* 2.0 (abs (- stereo-pos 0.5))))
                             stereo-pos))
;                              run it
              (shell (concatenate 'string "CMIX < " spat-score-name ))
;                              remove score-file
              (shell (concatenate 'string "rm " spat-score-name ))
;                              mv file to .aiff   
              (shell (concatenate 'string "mv " file-str ".spat.aiff" "  " file-str ".aiff" ))
           )
;
           ('1 
;               yes spatialization:
;     if yes,----
;           originals are mono, higherish gain facotr with freeverb
;           remember spat involves a change of duration: note that.
; 
             (setf spat-command (concatenate 'string "freeverbit "
                        file-str ".aiff "
;                                     roomsize:
                        (princ-to-string (setf roomsize (const-frandom .4 .7))) " "
;                                              damp:
                        "0.3 "
;                                       wet:
                        (princ-to-string (setf wet (const-frandom .3 .5))) " "
;                                                dry:
                        (princ-to-string (const-frandom .6 .7)) " "
;                                            width:
                        "3 "
;                                      gain:
                        ".65 "
;                              extra-time:
                        (princ-to-string (setf extra-time '1.2))
                        " dn "                
;                        stereo position:
                        (princ-to-string (- 1.0 stereo-pos))))
             (print spat-command)
             (shell spat-command)
;               move the rev'd file to the .aiff file
             (shell (concatenate 'string "mv "
                               file-str "_reverbed.aiff " file-str ".aiff"))
           )
;
;
           (otherwise (format t "~% you imbecile.  Illegal spatialzation request in
                               note number ~a ~%~%" file-str) (bye)))

;       write exact duration in seconds to list (if needed)
     (setf (nth filt-dur-par (nth note-number note-list)) (+ extra-time actual-dur)))))
;
; 

;****************************************************************************
;****************************************************************************
;****************************************************************************
;****************************************************************************
;****************************************************************************
;           DONE MAIN INSTRUMENT PART
;****************************************************************************
;****************************************************************************
;****************************************************************************
;****************************************************************************
;****************************************************************************
;****************************************************************************
;****************************************************************************














;***************************************
;*****************************************
;   INSTRUMENT CLASS  
;*************************************
;************************************


(defclass filt (inst) ())


;*************************************
;**************************************
;    ARTICULATION CLASSES
;**************************************
;***************************************


(defclass accent-tenuto (articulation) ())
(defclass tenuto-ellfilt (articulation) ())
(defclass accent-ten-fswp (articulation) ())
(defclass fp              (articulation) ())
(defclass tremolo         (articulation) ())
(defclass vibrato         (articulation) ())
;  (gliss w d)  wildness--number of glisses, d---average distance
(defclass gliss           (articulation) ())
(defclass cresc           (articulation) ())
(defclass bartok-pizz     (articulation) ())
;  (cresc-decresc m e)  m--middle e-end times, specifying proportion
(defclass cresc-decresc   (articulation) ())

;**************************************************************
;**************************************************************
;**************************************************************
;**************************************************************
;**************************************************************
;   AND NOW FOR THE METHODS FOR DIFFERENT ARTICULATIONS
;**************************************************************
;**************************************************************
;**************************************************************
;**************************************************************
;**************************************************************
;













;
;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   ACCENT-TENUTO
;            using IIR
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note accent-tenuto)
                             (instr filt)
                             (score-file t))

 (let ((dur-in-ms '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)
       (values ())
       (amp-makegen ())
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (this-note (nth (note-number note) note-list))
       )
;
;  write setup stuff:
;
      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"IIR\") ~%
                 srand(~a) ~%
                 reset(44100)
                 rtoutput(\"~a\") ~%"
              (random 100000)
              (nth filt-filename-par this-note))
;
; write makegen 1 -- amp env.
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 50) 60)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
      (setf values '(0.0))
      (dotimes (ct (- (length times) 2))
          (setf values 
           (append values (list
               (const-frandom .9 1.0)))))
      (setf values (append values (list '0.0)))
;
;
      (setf amp-makegen ())
      (dotimes (ct (length times) amp-makegen)
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;       set bandwidth
;
      (setf noisiness (first (nth filt-noisiness-par this-note))) 
      (setf base-bandwidth (nth noisiness
            '(-.002 -.01 -.03 -.08 -.15 -.20)))
;
;     ACTUAL FILT STATEMENT
;      
      (setf amp (case (dynamic note)
                ('0 1000) ('1 10000) ('2 15500) ('3 11000) ('4 21000) ('5 26500)))
;     adjust for register, not too steeply
      (setf amp (* amp
                (case (nth pitch-octave-par this-note)
                (0 0.65) (1 0.65) (2 0.65) (3 0.65) (4 0.65) (5 0.65) (6 0.65) (7 .52)
                (8 0.4) (9 0.35) (10 0.3) (11 0.3) (12 0.3) (13 0.3) (14 0.3) (15 0.3))))
;
;       to prevent clipping
      (setf amp (/ amp 3.1))
;
      (setf amp (* amp (case noisiness (0 0.8) (1 0.7) (2 0.7) (3 0.7) (4 0.6) (5 0.6)))) 
;
      (format score-file "setup(~a, ~a, ~a)" 
;       center frequency
                 (frequency note)
;       bandwidth
                 base-bandwidth
;       relative amp
                 5.0)
;    noise statement
      (format score-file "NOISE(0.0, ~a, ~a, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp)
;      
;
;        duplicate FILT statement if loud  at 2nd harmonic********************
;
      (if (> (dynamic note) 2)
          (prog ()
            (setf amp (case (dynamic note)
                ('3 1000) ('4 8000) ('5 13000)))
;     adjust for register, not too steeply       2nd harmonic****************
            (setf amp (* amp
                (case (+ 1 (nth pitch-octave-par this-note))
                (0 0.7) (1 0.7) (2 0.7) (3 0.7) (4 0.7) (5 0.7) (6 0.65) (7 .5) 
                (8 0.4) (9 0.35) (10 0.3) (11 0.2) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;       to prevent clipping         2nd harmonic ***********************
            (setf amp (/ amp 3.3))
;
      (setf amp (* amp (case noisiness (0 0.8) (1 0.7) (2 0.7) (3 0.7) (4 0.6) (5 0.6)))) 
 ;
            (format score-file "setup(~a, ~a, ~a)" 
;       center frequency           2nd harmonic********************
                 (* 2.0 (frequency note))
;       bandwidth                   2nd harmonic*******************
                 base-bandwidth
;       relative amp              2nd harmonic********************
                 5.0)
;    noise statement               2nd harmonic********************
            (format score-file "NOISE(0.0, ~a, ~a, 0)~%"                           
;       duration                2nd harmonic********************
               (desired-dur note)
;       amp                    2nd harmonic*********************
               amp)))
;   RETURN DURATION!!!!!  Then deal with it back in the main proc.
      (desired-dur note)))
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************




























;
;***********************************************
;***********************************************
;***********************************************
;***********************************************
;  FILT:  TENUTO-ELLFILT  (i.e.--soft-attack)
;***********************************************
;***********************************************
;***********************************************
;***********************************************
;


(defmethod write-cmix-score ((note tenuto-ellfilt)
                             (instr filt)
                             (score-file t))

 (let ((dur-in-ms '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)      
       (values ())
       (amp-makegen ())
       (noisiness '0)
       (lower-passband '0.0)
       (higher-passband '0.0)
       (stopband-cutoff '0.0)
       (attenuation '0)
       (amp '0)
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )
;

;   make a noise file
; 
    (with-open-file (noise-score-file "noise.source.sco" :direction :output)
      (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
      (random 100000) (desired-dur note)))
;   write sound to noise file
    (shell "sfc1 noise.source.aiff")
    (shell "CMIX < noise.source.sco")
;


;  write setup stuff:
;
      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"ELL\")
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%"
              (nth filt-filename-par this-note))
;
; write makegen 1 -- amp env.
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 20))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
      (setf values '(0.0))
      (dotimes (ct (- (length times) 2))
          (setf values 
           (append values (list
               (const-frandom .9 1.0)))))
      (setf values (append values (list '0.0)))
;
;
      (setf amp-makegen ())
      (dotimes (ct (length times))
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
      (setf noisiness (first (nth filt-noisiness-par this-note)))
;
;     ACTUAL ELL STATEMENTS
;      
      (format score-file "ellset(~a, ~a, ~a, 0.2, ~a)~%"
;  lower passband cutoff
        (setf lower-passband
            (* (frequency note) (case noisiness (0 .99)
                   (1 .963) (2 .935) (3 .91) (4 .85) (5 .80))))
;  higher passband cutoff
        (setf higher-passband
            (* (frequency note) (case noisiness (0 1.01)
                   (1 1.025) (2 1.043) (3 1.06) (4 1.105) (5 1.15))))
;  stopband cutoff, higher
        (setf stopband-cutoff
            (* (frequency note) (case noisiness (0 1.1)
                   (1 1.12)  (2 1.14)  (3 1.16)  (4 1.2) (5 1.25))))
; set up  attenuation argument for noisiness
        (setf attenuation
            (case noisiness (0 80) (1 70) (2 65) (3 55) (4 50) (5 45))))
;
;   first set amp by octave
;   
      (setf amp
          (case (nth pitch-octave-par this-note)
                (0 25.0) (1 25.0) (2 25.0) (3 25.0) (4 25.0) (5 25.0) (6 20.0) (7 13.0) 
                (8 9.0) (9 6.0) (10 3.0)  (11 2.0) (12 1.0) (13 1.0) (14 1.0) (15 1.0)))
;   adjust for noisiness
      (setf amp
          (* amp
                (case noisiness
                   (0 3.0) (1 1.5) (2 1.2) (3 .9) (4 .8) (5 .5))))
;    adjust for loudness
      (setf amp
          (* amp
	        (case (dynamic note)
		   (0 .23) (1 .475) (2 .75) (3 .45) (4 .77) (5 .98))))
;
;
      (format score-file "ELL(0.0, 0.0, ~a, ~a, ~a, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
           (*
             (case (nth pitch-octave-par this-note) 
                (0 7.0) (1 7.0) (2 7.0) (3 7.0) (4 6.0) (5 6.0) (6 4.0) (7 2.0) 
                (8 1.0) (9 .6) (10 .4)  (11 .3) (12 .1) (13 .1) (14 .1) (15 .1))
             (case noisiness (0 1.0) (1 .9) (2 .8) (3 .7) (4 .6) (5 .5)))))
;
;doubling for loudness, at 2nd-harmonic***********
;
      (if (> (dynamic note) 2)
        (prog ()
          (format score-file "ellset(~a, ~a, ~a, 0.2, ~a)~%"
             (setf lower-passband
                (* 2.0 (frequency note) (case noisiness (0 .99)
                     (1 .963) (2 .935) (3 .91) (4 .85) (5 .80))))
             (setf higher-passband
                (* 2.0 (frequency note) (case noisiness (0 1.01)
                     (1 1.025) (2 1.043) (3 1.06) (4 1.105) (5 1.15))))
             (setf stopband-cutoff
                (* 2.0 (frequency note) (case noisiness (0 1.1)
                     (1 1.12)  (2 1.14)  (3 1.16)  (4 1.2) (5 1.25))))
             attenuation)
;
;   first set amp by octave     2nd harmonic***********
;   
          (setf amp
             (case (+ 1 (nth pitch-octave-par this-note)) 
                  (0 25.0) (1 25.0) (2 25.0) (3 25.0) (4 25.0) (5 22.0) (6 17.0) (7 13.0) 
                  (8 8.0) (9 4.5) (10 1.0)  (11 .3) (12 .3) (13 .1) (14 .1) (15 .1)))
;   adjust for noisiness         2nd harmonic**********
          (setf amp
             (* amp
                  (case noisiness
                   (0 2.5) (1 1.5) (2 1.2) (3 .9) (4 .8) (5 .5))))
;    adjust for loudness         2nd harmonic**********
          (setf amp
             (* amp
	          (case (dynamic note)
		     (3 .1) (4 .45) (5 .6))))
;
;
          (format score-file "ELL(0.0, 0.0, ~a, ~a, ~a, 0, 0)~%"
              (desired-dur note) amp ringdown-dur)))
;
;   RETURN DURATION!!!!!  Then deal with it back in the main proc.
       (+ ringdown-dur (desired-dur note))))

;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************






























;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   FP
;            using IIR
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note fp)
                             (instr filt)
                             (score-file t))

 (let ((dur-in-ms '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)
       (values ())
       (amp-makegen ())
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (this-note (nth (note-number note) note-list))
       (at-score-filename ()) 
       (at-aiff-filename ())
       (ae-score-filename ())
       (ae-aiff-filename ())
       (file-str (subseq
                  (nth filt-filename-par
                   (nth (note-number note) note-list))
                  0
                  (- (length (nth filt-filename-par
                   (nth (note-number note) note-list))) 5)))
       )
;
;
;
;             fill a dummy score for when you get back to main proc
;
      (format score-file "~%        ~%")
; 
;  files:   score for accent-tenuto
;           aiff output file
;          
;           score for STEREO for amp env.
;           aiff output file---should be the filename
;                      that will get spatialized
;     
      (setf at-score-filename 
           (concatenate 'string file-str ".at.sco"))
      (setf at-aiff-filename 
           (concatenate 'string file-str ".at.aiff"))
      (setf ae-score-filename 
           (concatenate 'string file-str ".env.sco"))
      (setf ae-aiff-filename 
           (concatenate 'string file-str ".aiff"))
;
; open the at score file:
      (with-open-file (at-score at-score-filename :direction :output)
;
;  write setup stuff:
;
        (format at-score 
             "~% rtsetparams(44100, 1) ~%
                 load(\"IIR\") ~%
                 srand(~a) ~%
                 reset(44100)
                 rtoutput(\"~a\") ~%"
              (random 100000)
              at-aiff-filename)
;
; write makegen 1 -- amp env.
;
        (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
        (setf times (append (list '0) (list (+ (random 20) 40))))
        (setf last-usable-time-point (- dur-in-ms (+ (random 50) 60)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
        (setf cur-time  (second times))
        (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
        (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
        (setf values '(0.0))
        (dotimes (ct (- (length times) 2))
          (setf values 
           (append values (list
               (const-frandom .9 1.0)))))
        (setf values (append values (list '0.0)))
;
;
        (setf amp-makegen ())
        (dotimes (ct (length times) amp-makegen)
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
        (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
        (format at-score "~% makegen(1")
        (dolist (elle amp-makegen)
              (format at-score ", ~a" elle))
        (format at-score ")~% ~% ~%")
;
;       set bandwidth
;
        (setf noisiness (first (nth filt-noisiness-par this-note))) 
        (setf base-bandwidth (nth noisiness
            '(-.002 -.01 -.03 -.08 -.15 -.20)))
;
;     ACTUAL FILT STATEMENT
;      
        (setf amp (case (dynamic note)
                ('0 1000) ('1 10000) ('2 15500) ('3 11000) ('4 21000) ('5 26500)))
;     adjust for register, not too steeply
        (setf amp (* amp
                (case (nth pitch-octave-par this-note)
                (0 0.6) (1 0.6) (2 0.6) (3 0.6) (4 0.6) (5 0.6) (6 0.6) (7 .5)
                (8 0.4) (9 0.4) (10 0.3) (11 0.3) (12 0.3) (13 0.3) (14 0.3) (15 0.3))))
;
;       to prevent clipping
        (setf amp (/ amp 3.1))
;
        (setf amp (* amp (case noisiness (0 0.8) (1 0.7) (2 0.7) (3 0.7) (4 0.6) (5 0.6)))) 
;
        (format at-score "setup(~a, ~a, ~a)~%~%" 
;       center frequency
                 (frequency note)
;       bandwidth
                 base-bandwidth
;       relative amp
                 5.0)
;    noise statement
        (format at-score "NOISE(0.0, ~a, ~a, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp)
;      
;
;        duplicate FILT statement if loud
;
;            BUT FOR FP,  have it only last .14-.16 ms long!!!
;
        (if (> (dynamic note) 2)
          (prog ()
            (setf amp (case (dynamic note)
                ('3 1000) ('4 8000) ('5 13000)))
;     adjust for register, not too steeply          2nd harmonic********************
            (setf amp (* amp
                (case (+ 1 (nth pitch-octave-par this-note))
                (0 0.7) (1 0.7) (2 0.7) (3 0.7) (4 0.7) (5 0.7) (6 0.6) (7 .5) 
                (8 0.4) (9 0.4) (10 0.3) (11 0.2) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;       to prevent clipping               2nd harmonic*******************
            (setf amp (/ amp 3.3))
;
        (setf amp (* amp (case noisiness (0 0.8) (1 0.7) (2 0.7) (3 0.7) (4 0.6) (5 0.6)))) 
 ;
            (format at-score "setup(~a, ~a, ~a)~%~%" 
;       center frequency         2nd harmonic*********************
                 (* 2.0 (frequency note))
;       bandwidth              2nd harmonic*****************
                 base-bandwidth
;       relative amp           2nd harmonic*****************
                 5.0)
;    noise statement       2nd harmonic********************
            (format at-score "NOISE(0.0, ~a, ~a, 0)~%"                           
;       duration               2nd harmonic****************
               (if (> (desired-dur note) .14)
                   (const-frandom .10 .16) 
                   (desired-dur note))
;       amp               2nd harmonic******************
               amp))))
;
;        run the accent-tenuto score
      (shell (concatenate 'string "CMIX < " at-score-filename))
;
;     now,  add an envelope externally.
;  actually we might need the duration value
;
      (with-open-file (ae-score ae-score-filename :direction :output)
;
;
        (setf dur-in-ms (* 1000 (desired-dur note)))
        (format ae-score 
             "~% rtsetparams(44100, 1) ~%
                 reset(44100) ~%
                 rtinput(\"~a\") ~%
                 rtoutput(\"~a\") ~%
                 makegen(1, 24, 10000, 0, 0.0,  ~a, 1.0,  ~a, 1.0,  ~a, ~a,  ~a, ~a,  ~a, 0.0) ~%
                 MIX(0.0, 0.0, ~a, 2.0, 0) ~%"
              at-aiff-filename
              ae-aiff-filename
;        up to full   
              (+ 10 (random 15))
;           turn-down-point
              (+ 270 (random 30))
;           hit-low-time-point
              (+ 300 (random 30))
;           low-value
              (const-frandom 0.07 0.17)
;           almost-end-time
              (if (< (desired-dur note) .16)
                  (+ 330 (random 30))
                  (- dur-in-ms (+ 20 (random 30))))
;           lowvalue
              (const-frandom 0.07 0.17)
;           endtime
              (if (< (desired-dur note) .16)
                  (+ 360 (random 30))
                  dur-in-ms)
;           duration
              (desired-dur note)))
;
;  run ae-score.
        (shell (concatenate 'string "CMIX < " ae-score-filename))
        (shell (concatenate 'string "rm " at-aiff-filename))
        (shell (concatenate 'string "rm " at-score-filename))
        (shell (concatenate 'string "rm " ae-score-filename))
;   return duration.
        (desired-dur note)))

;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************


























;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   TREMOLO
;            using ell and AMINST
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note tremolo)
                             (instr filt)
                             (score-file t))

 (let ((dur-in-ms '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)
       (values ())
       (amp-makegen ())
       (noisiness '0)
       (amp '0)
       (start '0)
       (lower-passband '0)
       (higher-passband '0)
       (stopband-cutoff '0)
       (attenuation '0)
       (points '(0)) 
       (new-series ())
       (series-countr '0)      
       (cur-point '0)
       (total-duration 0.0)
       (this-bit-dur (+ .06 (random .08)))
       (modufreq '0)
       (bitcounter '0)
       (modu-vals ())
       (series '(6 1 4 3 8 5 18 2 16 0 14 17 13 9 12 7 10 15 11))
       (this-note (nth (note-number note) note-list))
       (at-score-filename ()) 
       (at-aiff-filename ())
       (am-score-filename ())
       (am-aiff-filename ())
       (file-str (subseq
                  (nth filt-filename-par
                   (nth (note-number note) note-list))
                  0
                  (- (length (nth filt-filename-par
                   (nth (note-number note) note-list))) 5)))
       )
;
;
;
;             fill a dummy score for when you get back to main proc
;
      (format score-file "~%        ~%")
; 
;  files:   score for accent-tenuto
;           aiff output file
;          
;           score for AM
;           aiff output file---should be the filename
;                      that will get spatialized
;     
      (setf at-score-filename 
           (concatenate 'string file-str ".at.sco"))
      (setf at-aiff-filename 
           (concatenate 'string file-str ".at.aiff"))
      (setf am-score-filename 
           (concatenate 'string file-str ".am.sco"))
      (setf am-aiff-filename 
           (concatenate 'string file-str ".aiff"))
;
;
;    make the noise file:
;   make a noise file
; 
      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
        (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
              (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;
; open the at score file:
      (with-open-file (at-score at-score-filename :direction :output)
        (format at-score
             "~% rtsetparams(44100, 1) ~%
                 load(\"ELL\") ~%
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%"
                 at-aiff-filename)
;
; write makegen 1 -- amp env.
;
        (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
        (setf times  (append (list '0) (list (+ (random 20) 20))))
        (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
        (setf cur-time  (second times))
        (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
        (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
        (setf values '(0.0))
        (dotimes (ct (- (length times) 2))
          (setf values 
            (append values (list
               (const-frandom .9 1.0)))))
        (setf values (append values (list '0.0)))
;
;
        (setf amp-makegen ())
        (dotimes (ct (length times) amp-makegen)
           (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
        (setf amp-makegen
           (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
        (format at-score "~% makegen(1")
        (dolist (elle amp-makegen)
              (format at-score ", ~a" elle))
        (format at-score ")~% ~% ~%")
;
;
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
        (setf noisiness (first (nth filt-noisiness-par this-note)))
;
;     ACTUAL ELL STATEMENTS
;      
        (format at-score "ellset(~a, ~a, ~a, 0.2, ~a)~%"
;  lower passband cutoff    main note
        (setf lower-passband
            (* (frequency note) (case noisiness (0 .99)
                   (1 .963) (2 .935) (3 .91) (4 .85) (5 .80))))
;  higher passband cutoff   main note
        (setf higher-passband
            (* (frequency note) (case noisiness (0 1.01)
                   (1 1.025) (2 1.043) (3 1.06) (4 1.105) (5 1.15))))
;  stopband cutoff, higher  main note
        (setf stopband-cutoff
            (* (frequency note) (case noisiness (0 1.1)
                   (1 1.12)  (2 1.14)  (3 1.16)  (4 1.2) (5 1.25))))
; set up  attenuation argument for noisiness
        (setf attenuation
            (case noisiness (0 80) (1 70) (2 65) (3 55) (4 50) (5 45))))
;
;   first set amp by octave  main note
;   
        (setf amp
           (case (nth pitch-octave-par this-note)
                (0 25.0) (1 25.0) (2 25.0) (3 25.0) (4 25.0) (5 25.0) (6 20.0) (7 13.0) 
                (8 9.0) (9 6.0) (10 3.0)  (11 2.0) (12 1.0) (13 1.0) (14 1.0) (15 1.0)))
;   adjust for noisiness     main note
        (setf amp
           (* amp
                (case noisiness
                   (0 3.0) (1 1.5) (2 1.2) (3 .9) (4 .8) (5 .5))))
;    adjust for loudness     main note
        (setf amp
           (* amp
	        (case (dynamic note)
		   (0 .23) (1 .475) (2 .75) (3 .45) (4 .77) (5 .96))))
;
;
        (format at-score "ELL(0.0, 0.0, ~a, ~a, ~a, 0, 0)~%"                           
;              duration            main note
              (desired-dur note)
;              amp mult             main note
              amp
;              ring-down duration      main note
            (setf ringdown-dur
              (*
                (case (nth pitch-octave-par this-note) 
                  (0 7.0) (1 7.0) (2 7.0) (3 7.0) (4 6.0) (5 6.0) (6 4.0) (7 2.0) 
                (8 1.0) (9 .6) (10 .4)  (11 .3) (12 .1) (13 .1) (14 .1) (15 .1))
                (case noisiness (0 1.0) (1 .9) (2 .8) (3 .7) (4 .6) (5 .5)))))
;
;doubling for loudness, AT 2nd harmonic:
;         ********    **********   ************
;
        (if (> (dynamic note) 2)
          (prog ()
            (format at-score "ellset(~a, ~a, ~a, 0.2, ~a)~%"
               (setf lower-passband
                  (* 2.0 (frequency note) (case noisiness (0 .99)
                     (1 .963) (2 .935) (3 .91) (4 .85) (5 .80))))
               (setf higher-passband
                  (* 2.0 (frequency note) (case noisiness (0 1.01)
                     (1 1.025) (2 1.043) (3 1.06) (4 1.105) (5 1.15))))
               (setf stopband-cutoff
                  (* 2.0 (frequency note) (case noisiness (0 1.1)
                     (1 1.12)  (2 1.14)  (3 1.16)  (4 1.2) (5 1.25))))
               attenuation)
;
;   first set amp by octave   2nd harmonic*******
;   
            (setf amp
               (case (+ 1 (nth pitch-octave-par this-note)) 
                  (0 25.0) (1 25.0) (2 25.0) (3 25.0) (4 25.0) (5 22.0) (6 17.0) (7 13.0) 
                  (8 8.0) (9 4.5) (10 1.0)  (11 .3) (12 .3) (13 .1) (14 .1) (15 .1)))
;   adjust for noisiness      2nd harmonic******
            (setf amp
               (* amp
                  (case noisiness
                   (0 2.5) (1 1.5) (2 1.2) (3 .9) (4 .8) (5 .5))))
;    adjust for loudness      2nd harmonic******
            (setf amp
               (* amp
	          (case (dynamic note)
		     (3 .1) (4 .45) (5 .6))))
;
;
            (format at-score "ELL(0.0, 0.0, ~a, ~a, ~a, 0, 0)~%"
               (desired-dur note) amp ringdown-dur))))
;
;
      (setf total-duration (+ ringdown-dur (desired-dur note)))
;
;        run the accent-tenuto score
;
      (shell (concatenate 'string "CMIX < " at-score-filename))
;
;     now,  do AM on the note, externally.
;  actually we might need the duration value
;
      (with-open-file (am-score am-score-filename :direction :output)
;
;
        (setf dur-in-ms (* 1000 total-duration))
        (format am-score 
             "~% rtsetparams(44100, 1) ~%
                 reset(44100) ~%
                 load(\"AM\") ~%
                 rtinput(\"~a\") ~%
                 rtoutput(\"~a\") ~% "
                 at-aiff-filename
                 am-aiff-filename)
;
;   start at 0
;       LOOP:
;         (if we are within 125 ms of the end of the note, 
;                           dur = end - start, this is the last.)
;       write an around 100 ms block at a certain am freq
;     next block, start time is + 76
;              read times = write times 
;      check if this was the last block? if not, go on.         
;      update start
;
        (setf trans-fact (random 19))
        (setf series 
           (dolist (elle series new-series)
                 (setf new-series
                   (append new-series (list 
                      (mod (+ elle trans-fact) 19))))))
        (setf new-series ())
;
;      then use it to make a modulation-freq value list:
        (dotimes (countr (* 100 total-duration))
           (setf modu-vals
             (append modu-vals
                (list
                   (+ 2.0 (* .473684211
;  used ta be 1.473684211
                      (nth series-countr series))))))
           (setf series-countr (+ series-countr 1))
           (if (> series-countr 18) (setf series-countr '0)))
        (setf series-countr '0)

        (loop 
;      write makegens            
;         makegen 1 
            (format am-score "~% makegen(2, 10, 5000, 1) ~% ")
            (format am-score "makegen(1, 24, 2000, 0,0, ~a,1,~a,1,~a,0)~%"
                 (+ 1 (random 3))
                 (+ 9 (random 4))
                 (+ 19 (random 4)))
            (setf modufreq (nth bitcounter modu-vals))
;      write AM statement 
;
            (format am-score
               "AM(~a, ~a, ~a, 0.8, ~a, 0, 0) ~%~%"
               start start this-bit-dur modufreq)
;
;
;      set stuff for the next go-around 
            (setf start (+ start .06 (random .037)))
            (setf bitcounter (+ bitcounter 1))
            (if (>= start total-duration) (return))
            (if (< (- total-duration start) .125)
                (setf this-bit-dur (- total-duration start))
                (setf this-bit-dur (+ .07 (random .06)))))) 
;
;  finished writing am score
;
;* am -- amplitude modulate an input signal
;*  p0 = output skip
;*  p1 = input skip
;*  p2 = duration
;*  p3 = amplitude
;*  p4 = AM modulator frequency (hz)
;*  p5 = input channal [optional]
;*  p6 = stereo spread <0-1> [optional]
;*  assumes function table 1 is the amplitude envelope
;*  function table 2 is the AM modulator waveform
;
;  run am-score.
        (shell (concatenate 'string "CMIX < " am-score-filename))
        (shell (concatenate 'string "rm " at-aiff-filename))
        (shell (concatenate 'string "rm " at-score-filename))
        (shell (concatenate 'string "rm " am-score-filename))
;   return duration.
        total-duration))

;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************














































;
;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   VIBRATO
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note vibrato)
                             (instr filt)
                             (score-file t))     

 (let ((dur-in-ms '0.0)
       (series '(6 1 4 3 8 5 18 2 16 0 14 17 13 9 12 7 10 15 11))
       (new-series ())
       (series-countr '0)
       (duration '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)      
       (values ())
       (base '0.0)
       (increment '0.0)
       (amp-makegen ())
       (gliss-makegen ())
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (bandwidth-makegen ())
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )

;
;   make a noise file
; 

      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
         (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
         (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;
;  write setup stuff:
;

      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"FILTSWEEP\") ~%
                 reset(44100)  ~%
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%
                 bus_config(\"FILTSWEEP\", \"in 0\", \"aux 0 out\") ~%~%
                                  ~%~%"

                       (nth filt-filename-par this-note))
;
;
; write makegen 1 -- amp env.
;               2 -- gliss curve (for center freq, etc.)
;               3 -- bandwidth curve
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
      (setf noisiness (first (nth filt-noisiness-par this-note)))

;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 20))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
      (setf values '(0.0))
      (dotimes (ct (- (length times) 2))
          (setf values 
           (append values (list
               (const-frandom .9 1.0)))))
      (setf values (append values (list '0.0)))
;
;
      (setf amp-makegen ())
      (dotimes (ct (length times) amp-makegen)
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;     GLISS CURVE -- slight vibrato, wavering
;
;  list of times in ms of gliss changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 30))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
      (setf values ())
;  this is a loop that makes random deviations of amp. every 70 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 50.0 190.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;      calculate curve of values
;       first transpose the series randomly:
;
      (setf trans-fact (random 19))
      (setf series 
           (dolist (elle series new-series)
                 (setf new-series
                   (append new-series (list 
                      (mod (+ elle trans-fact) 19))))))
      (setf new-series ())
;
; set width of vibrato according to noisiness
;          0.93675 0.006798245
;
      (setf base (case noisiness  (0 0.93675) (1 0.88888) (2 0.83333)
                                  (3 0.57143) (4 .57143) (5 0.5)))
      (setf increment (case noisiness (0 0.006798245) (1 0.0124269) (2 0.019298)
                                  (3 0.05614) (4 .06203) (5 0.078947368)))
;
;      then use it to make values curve:
      (dotimes (countr (length times))
         (setf values
            (append values
;     calculates a frequency:
               (list (* (frequency note)
                        (+ base
                          (* (nth series-countr series) increment))))))
         (setf series-countr (+ series-countr 1))
         (if (> series-countr 18) (setf series-countr '0)))
      (setf series-countr '0)
;
;
;
      (dotimes (ct (length times))
          (setf gliss-makegen (append gliss-makegen
              (list (nth ct times)) (list (nth ct values)))))
;                 finally, write in the 24, xxxxxx values
      (setf gliss-makegen
          (append '(18)
                  (list dur-in-ms)
                  gliss-makegen))
;
;          write it to the file
      (format score-file "~% makegen(-2")
      (dolist (elle gliss-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;    BANDWIDTH CURVE
;             (more or less constant value, but depends on 
;                noisiness parameter)
;
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
;
      (setf base-bandwidth (nth noisiness
            '(-.01 -.1 -.25 -.4 -.6 -.9)))
;
;  list of times in ms of bandwidth changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 30) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the bandwidth makegen, make a list of 
;   of length
      (setf values (list (* base-bandwidth (const-frandom 3.0 5.0))))
      (dotimes (ct (length times))
          (setf values 
           (append values (list
               (* (const-frandom .9995 1.0005) base-bandwidth)))))
;
;
      (setf bandwidth-makegen ())
      (dotimes (ct (length times) bandwidth-makegen)
          (setf bandwidth-makegen (append bandwidth-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf bandwidth-makegen
          (append '(18)
                  (list dur-in-ms)
                  bandwidth-makegen))
;          write it to the file
      (format score-file "~% makegen(3")
      (dolist (elle bandwidth-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;     ACTUAL FILT STATEMENT
;      
      (setf amp (case (dynamic note)
                ('0 .03) ('1 .09) ('2 .14) ('3 .20) ('4 .26) ('5 .34)))
;     adjust for register, not too steeply
      (setf amp (* amp 1.84
                (case (nth pitch-octave-par this-note)
                (0 0.9) (1 0.9) (2 0.9) (3 0.9) (4 0.9) (5 0.9) (6 0.8) (7 .6) 
                (8 0.6) (9 0.5) (10 0.465) (11 0.3) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;
      (format score-file "FILTSWEEP(0.0, 0.0, ~a, ~a, ~a, ~a, 1, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
             (if (< 1.0 (desired-dur note))
                 (* .5 (desired-dur note))
                 (const-frandom .1 .3)))
;       sharpness                           
          (nth noisiness '(5 5 5 4 4 3)))
;
;  MIX to get rid of opening click:
;
      (setf duration (+ ringdown-dur (desired-dur note)))
      (format score-file
             "bus_config(\"MIX\", \"aux 0 in\", \"out 0\") ~%~%
              makegen(1, 24, 10000, 0,0,~a,0,~a,1,~a,1)~%~%
              MIX(0, 0, ~a, 1.0, 0)~%~%"
                  9.0 12.0 dur-in-ms duration                
                  )
;  return duration
      (+ ringdown-dur (desired-dur note))))



;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************


















































;
;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   ACCENT-TEN-FSWP
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note accent-ten-fswp)
                             (instr filt)
                             (score-file t))     

 (let ((dur-in-ms '0.0)
       (duration '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)      
       (values ())
       (amp-makegen ())
       (gliss-makegen ())
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (bandwidth-makegen ())
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )

;
;   make a noise file
; 
      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
         (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
         (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;
;  write setup stuff:
;

      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"FILTSWEEP\") ~%
                 reset(44100)  ~%
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%
                 bus_config(\"FILTSWEEP\", \"in 0\", \"aux 0 out\") ~%~%
                                  ~%~%"

                       (nth filt-filename-par this-note))
;
;
; write makegen 1 -- amp env.
;               2 -- gliss curve (for center freq, etc.)
;               3 -- bandwidth curve
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 20))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
      (setf values '(0.0))
      (dotimes (ct (- (length times) 2))
          (setf values 
           (append values (list
               (const-frandom .9 1.0)))))
      (setf values (append values (list '0.0)))
;
;
      (setf amp-makegen ())
      (dotimes (ct (length times) amp-makegen)
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;     GLISS CURVE -- slight vibrato, wavering
;
;
;  list of times in ms of gliss changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 40) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the gliss makegen, first make a
;     a pitch distant fromt he actual, for a "jump"
;       then, a list of *almost* pitches, to length
;
      (setf values (list
         (* (frequency note) (const-frandom 1.3 4.0))))
      (dotimes (ct (- (length times) 1))
         (setf values 
             (append values (list
                (* (const-frandom .9995 1.0005) (frequency note))))))
;
;
      (setf gliss-makegen ())
      (dotimes (ct (length times) gliss-makegen)
         (setf gliss-makegen (append gliss-makegen
             (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf gliss-makegen
          (append '(18)
                  (list dur-in-ms)
                  gliss-makegen))
;
;          write it to the file
      (format score-file "~% makegen(2")
      (dolist (elle gliss-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;    BANDWIDTH CURVE
;             (more or less constant value, but depends on 
;                noisiness parameter)
;
      (setf noisiness (first (nth filt-noisiness-par this-note)))
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
;
      (setf base-bandwidth (nth noisiness
            '(-.01 -.1 -.25 -.4 -.6 -.9)))
;
;  list of times in ms of bandwidth changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 30) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the bandwidth makegen, make a list of 
;   of length
      (setf values (list (* base-bandwidth (const-frandom 3.0 5.0))))
      (dotimes (ct (length times))
          (setf values 
           (append values (list
               (* (const-frandom .9995 1.0005) base-bandwidth)))))
;
;
      (setf bandwidth-makegen ())
      (dotimes (ct (length times) bandwidth-makegen)
          (setf bandwidth-makegen (append bandwidth-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf bandwidth-makegen
          (append '(18)
                  (list dur-in-ms)
                  bandwidth-makegen))
;          write it to the file
      (format score-file "~% makegen(3")
      (dolist (elle bandwidth-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;     ACTUAL FILT STATEMENT
;      
      (setf amp (case (dynamic note)
                ('0 .03) ('1 .09) ('2 .14) ('3 .20) ('4 .26) ('5 .34)))
;     adjust for register, not too steeply
      (setf amp (* amp 1.84
                (case (nth pitch-octave-par this-note)
                (0 0.9) (1 0.9) (2 0.9) (3 0.9) (4 0.9) (5 0.9) (6 0.8) (7 .6) 
                (8 0.6) (9 0.5) (10 0.465) (11 0.3) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;
      (format score-file "FILTSWEEP(0.0, 0.0, ~a, ~a, ~a, ~a, 1, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
             (if (< 1.0 (desired-dur note))
                 (* .5 (desired-dur note))
                 (const-frandom .1 .3)))
;       sharpness                           
          (nth noisiness '(5 5 5 4 4 3)))
;
;
;  MIX to get rid of opening click:
;
      (setf duration (+ ringdown-dur (desired-dur note)))
      (format score-file
             "bus_config(\"MIX\", \"aux 0 in\", \"out 0\") ~%~%
              makegen(1, 24, 10000, 0,0,~a,0,~a,1,~a,1)~%~%
              MIX(0, 0, ~a, 1.0, 0)~%~%"
                  9.0 12.0 dur-in-ms duration                
                  )
;  return duration
      (+ ringdown-dur (desired-dur note))))



;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************



















































;
;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   CRESC
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note cresc)
                             (instr filt)
                             (score-file t))     

 (let ((dur-in-ms '0.0)
       (duration '0.0)
       (times ())
       (end '100.0)
       (last-usable-time-point ())
       (cur-time '0)      
       (values ())
       (amp-makegen ())
       (gliss-makegen ())
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (bandwidth-makegen ())
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )

;
;   make a noise file
; 
      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
         (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
         (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;
;  write setup stuff:
;

      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"FILTSWEEP\") ~%
                 reset(44100)  ~%
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%
                 bus_config(\"FILTSWEEP\", \"in 0\", \"aux 0 out\") ~%~%
                                  ~%~%"
                       (nth filt-filename-par this-note))
;
;
; write makegen 1 -- amp env.
;               2 -- gliss curve (for center freq, etc.)
;               3 -- bandwidth curve
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;
;     AMPLITUDE MAKEGEN
;
;
      (setf times 
        (append
         '(0.0)
          (list (/ (* (const-frandom .02 .03)
                      end)
                   (desired-dur note)))
          (list (* (const-frandom .23 .35) end))
          (list (* (const-frandom .55 .75) end)) 
          (list (* (const-frandom .85 .9) end)) 
          (list (* (const-frandom .93 .97) end))
          (list  (- end (/ (* (const-frandom .02 .03)
                              end)
                           (desired-dur note))))
          (list end)))
;
;
      (setf values
        (append
         '(0.0)
          (list (const-frandom 0.03 0.07))
          (list (const-frandom 0.1 0.3))
          (list (const-frandom 0.4 0.5))
          (list (const-frandom 0.7 0.8))
          (list (const-frandom 0.85 0.95))
          (list (const-frandom 0.95 1.0))
          '(0.0)))
;
;         
      (dotimes (ct (length times))
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;                 finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(-1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;     GLISS CURVE -- slight vibrato, wavering
;
;
;  list of times in ms of gliss changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 40) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;  for the values in the gliss makegen, first make a
;     a pitch distant fromt he actual, for a "jump"
;       then, a list of *almost* pitches, to length
;
      (setf values (list
         (* (frequency note) (const-frandom 1.3 4.0))))
      (dotimes (ct (- (length times) 1))
         (setf values 
             (append values (list
                (* (const-frandom .9995 1.0005) (frequency note))))))
;
;
      (setf gliss-makegen ())
      (dotimes (ct (length times) gliss-makegen)
         (setf gliss-makegen (append gliss-makegen
             (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf gliss-makegen
          (append '(18)
                  (list dur-in-ms)
                  gliss-makegen))
;
;          write it to the file
      (format score-file "~% makegen(2")
      (dolist (elle gliss-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;    BANDWIDTH CURVE
;             (more or less constant value, but depends on 
;                noisiness parameter)
;
      (setf noisiness (first (nth filt-noisiness-par this-note)))
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
;
      (setf base-bandwidth (nth noisiness
            '(-.01 -.1 -.25 -.4 -.6 -.9)))
;
;  list of times in ms of bandwidth changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 30) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the bandwidth makegen, make a list of 
;   of length
      (setf values (list (* base-bandwidth (const-frandom 3.0 5.0))))
      (dotimes (ct (length times))
          (setf values 
           (append values (list
               (* (const-frandom .9995 1.0005) base-bandwidth)))))
;
;
      (setf bandwidth-makegen ())
      (dotimes (ct (length times) bandwidth-makegen)
          (setf bandwidth-makegen (append bandwidth-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf bandwidth-makegen
          (append '(18)
                  (list dur-in-ms)
                  bandwidth-makegen))
;          write it to the file
      (format score-file "~% makegen(3")
      (dolist (elle bandwidth-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;     ACTUAL FILT STATEMENT
;      
      (setf amp (case (dynamic note)
                ('0 .03) ('1 .09) ('2 .14) ('3 .20) ('4 .26) ('5 .34)))
;     adjust for register, not too steeply
      (setf amp (* amp 1.84
                (case (nth pitch-octave-par this-note)
                (0 0.9) (1 0.9) (2 0.9) (3 0.9) (4 0.9) (5 0.9) (6 0.8) (7 .6) 
                (8 0.6) (9 0.5) (10 0.465) (11 0.3) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;
      (format score-file "FILTSWEEP(0.0, 0.0, ~a, ~a, ~a, ~a, 1, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
             (if (< 1.0 (desired-dur note))
                 (* .5 (desired-dur note))
                 (const-frandom .1 .3)))
;       sharpness                           
          (nth noisiness '(5 5 5 4 4 3)))
;
;
;  MIX to get rid of opening click:
;
      (setf duration (+ ringdown-dur (desired-dur note)))
      (format score-file
             "bus_config(\"MIX\", \"aux 0 in\", \"out 0\") ~%~%
              makegen(1, 24, 10000, 0,0,~a,0,~a,1,~a,1)~%~%
              MIX(0, 0, ~a, 1.0, 0)~%~%"
                  9.0 12.0 dur-in-ms duration                
                  )
;  return duration
      (+ ringdown-dur (desired-dur note))))



;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
































;
;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   (GLISS W D)
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note gliss)
                             (instr filt)
                             (score-file t))     

 (let ((dur-in-ms '0.0)
       (duration '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)      
       (values ())
       (amp-makegen ())
       (gliss-makegen ())
       (wildness (first (extra-params note)))
       (distance (second (extra-params note)))
       (this-distance '0.0)
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (bandwidth-makegen ())
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )

;
;   make a noise file
; 
      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
         (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
         (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;
;  write setup stuff:
;

      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"FILTSWEEP\") ~%
                 reset(44100)  ~%
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%
                 bus_config(\"FILTSWEEP\", \"in 0\", \"aux 0 out\") ~%~%
                                  ~%~%"

                       (nth filt-filename-par this-note))
;
;
; write makegen 1 -- amp env.
;               2 -- gliss curve (for center freq, etc.)
;               3 -- bandwidth curve
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;  list of times in ms of amp changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 20))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the amp makegen, first make a list of 
;              almost 1's, of length -2 (for outer 0's)
;
      (setf values '(0.0))
      (dotimes (ct (- (length times) 2))
          (setf values 
           (append values (list
               (const-frandom .9 1.0)))))
      (setf values (append values (list '0.0)))
;
;
      (setf amp-makegen ())
      (dotimes (ct (length times) amp-makegen)
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;     GLISS CURVE -- slight vibrato, wavering
;
;     number of values is wildness, then + 4 for original pitch 
;          stability periods
;
      (setf values ())
      (setf this-distance (const-frandom
                             (* 0.7 distance) (* 1.4 distance)))
      (dotimes (cto wildness)
         (setf values
            (append values
               (list 
                 (const-frandom
                    (* (frequency note) (expt 1.0594631 (* -1.0 this-distance)))
                    (* (frequency note) (expt 1.0594631 this-distance))))))
         (setf this-distance (const-frandom
            (* 0.7 distance) (* 1.4 distance))))
      (setf values (append (list (frequency note)(frequency note)) values
                           (list (frequency note)(frequency note))))
;
;       times, find average distance between
;        always use less
;
      (setf ave-time-dist 
          (/ dur-in-ms (length values)))
      (setf times '(0))
      (setf cur-time '0.0)
      (dotimes (ctq (- (length values) 1))
        (setf times
           (append times
              (list
                 (setf cur-time
                    (+ cur-time (const-frandom
                                (* .8 ave-time-dist)
                                (* 2.1 ave-time-dist))))))))
;
;
      (setf gliss-makegen ())
      (dotimes (ct (length times) gliss-makegen)
         (setf gliss-makegen (append gliss-makegen
             (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf gliss-makegen
          (append '(18)
                  (list dur-in-ms)
                  gliss-makegen))
;
;          write it to the file
      (format score-file "~% makegen(2")
      (dolist (elle gliss-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;    BANDWIDTH CURVE
;             (more or less constant value, but depends on 
;                noisiness parameter)
;
      (setf noisiness (first (nth filt-noisiness-par this-note)))
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
;
      (setf base-bandwidth (nth noisiness
            '(-.01 -.1 -.25 -.4 -.6 -.9)))
;
;  list of times in ms of bandwidth changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 30) 40))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
;  this is a loop that makes random deviations of amp. every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
;  for the values in the bandwidth makegen, make a list of 
;   of length
      (setf values (list (* base-bandwidth (const-frandom 3.0 5.0))))
      (dotimes (ct (length times))
          (setf values 
           (append values (list
               (* (const-frandom .9995 1.0005) base-bandwidth)))))
;
;
      (setf bandwidth-makegen ())
      (dotimes (ct (length times) bandwidth-makegen)
          (setf bandwidth-makegen (append bandwidth-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf bandwidth-makegen
          (append '(18)
                  (list dur-in-ms)
                  bandwidth-makegen))
;          write it to the file
      (format score-file "~% makegen(3")
      (dolist (elle bandwidth-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;     ACTUAL FILT STATEMENT
;      
      (setf amp (case (dynamic note)
                ('0 .03) ('1 .09) ('2 .14) ('3 .20) ('4 .26) ('5 .34)))
;     adjust for register, not too steeply
      (setf amp (* amp 1.84
                (case (nth pitch-octave-par this-note)
                (0 0.9) (1 0.9) (2 0.9) (3 0.9) (4 0.9) (5 0.9) (6 0.8) (7 .6) 
                (8 0.6) (9 0.5) (10 0.465) (11 0.3) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;
      (format score-file "FILTSWEEP(0.0, 0.0, ~a, ~a, ~a, ~a, 1, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
             (if (< 1.0 (desired-dur note))
                 (* .5 (desired-dur note))
                 (const-frandom .1 .3)))
;       sharpness                           
          (nth noisiness '(5 5 5 4 4 3)))
;
;
;  MIX to get rid of opening click:
;
      (setf duration (+ ringdown-dur (desired-dur note)))
      (format score-file
             "bus_config(\"MIX\", \"aux 0 in\", \"out 0\") ~%~%
              makegen(1, 24, 10000, 0,0,~a,0,~a,1,~a,1)~%~%
              MIX(0, 0, ~a, 1.0, 0)~%~%"
                  9.0 12.0 dur-in-ms duration                
                  )
;  return duration
      (+ ringdown-dur (desired-dur note))))



;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************


































;
;
;             for bartok pizz, move inward quickly from high wide bandwidth
;





;
;******************************************
;******************************************
;******************************************
;******************************************
;   FILT:   BARTOK-PIZZ
;******************************************
;******************************************
;******************************************
;******************************************


(defmethod write-cmix-score ((note bartok-pizz)
                             (instr filt)
                             (score-file t))     

 (let ((dur-in-ms '0.0)
       (duration '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)
       (cter '0)      
       (values ())
       (amp-makegen ())
       (float-hi 1.0)
       (float-low (const-frandom .1 .2))
       (first-part-length '0)
       (gliss-makegen ())
       (pizztimes ())
       (pizzvalues ())  
       (lvtimes ())
       (lvvalues ())
       (noisiness '0)
       (amp '0)
       (base-bandwidth '0)
       (bandwidth-makegen ())
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )

;
;   make a noise file
; 
      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
         (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
         (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;
;  write setup stuff:
;

      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"FILTSWEEP\") ~%
                 reset(44100)  ~%
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%
                 bus_config(\"FILTSWEEP\", \"in 0\", \"aux 0 out\") ~%~%
                                  ~%~%"

                       (nth filt-filename-par this-note))
;
;
; write makegen 1 -- amp env.
;               2 -- gliss curve (for center freq, etc.)
;               3 -- bandwidth curve
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
;     first 100 ms at high amp, rest at low amp
;
;          list of times in ms of amp changes, for makegen curve
      (setf times  (append (list '0) (list (+ (random 20) 10))))
      (setf last-usable-time-point (- dur-in-ms (+ (random 150) 170)))
;             this is a loop that makes a list of times every 100 ms or so, 
      (setf cur-time  (second times))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf times (append times (list cur-time))))
      (setf times (append times (list last-usable-time-point) (list dur-in-ms)))
;
;
      (setf first-part-length 
            (loop 
                (if (> (nth cter times) 100) (return cter))
                (setf cter (+ cter 1))))

      (dotimes (counter first-part-length)
               (setf values (append values (list float-hi))))
      (dotimes (counter (- (- (length times) first-part-length) 1))
               (setf values (append values (list float-low))))
      (setf values (piddle values '0.01))
;            set first and last to 0.0
      (setf (first values) 0.0)
      (setf values (append values '(0.0)))
;
      (setf amp-makegen ())
      (dotimes (ct (length times) amp-makegen)
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
      (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
      (format score-file "~% makegen(1")
      (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;     GLISS CURVE 
;

;    pizztimes is (0)
;         add around 3-6 until greater than (0. . . .  50)
;
      (setf cur-time '0) 
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 6.0 13.0))))
           (when (> cur-time 70.0) (return))
           (setf pizztimes (append pizztimes (list cur-time))))
      (dolist (elle pizztimes)
           (setf pizzvalues
               (append pizzvalues
                 (list
                   (const-frandom
                      (* .5 (frequency note))
                      (* 2.0 (frequency note)))))))
;
;
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
      (setf cur-time (+ 40 (first (last pizztimes))))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf lvtimes (append lvtimes (list cur-time))))
      (setf lvtimes (append lvtimes (list last-usable-time-point) (list dur-in-ms)))
;
      (setf lvvalues '(0.0))
      (dotimes (ct (- (length lvtimes) 2))
         (setf lvvalues 
           (append lvvalues 
              (list
                 (const-frandom 
                    (* 0.99 (frequency note))
                    (* 1.01 (frequency note)))))))
      (setf lvvalues (append lvvalues (list '0.0)))
;
;
      (dotimes (ct (length pizztimes))
          (setf gliss-makegen (append gliss-makegen
              (list (nth ct pizztimes)) (list (nth ct pizzvalues)))))
      (dotimes (ct (length lvtimes))
          (setf gliss-makegen (append gliss-makegen
              (list (nth ct lvtimes)) (list (nth ct lvvalues)))))
;
;        finally, write in the 24, xxxxxx values
      (setf gliss-makegen
          (append '(18)
                  (list dur-in-ms)
                  gliss-makegen))
;
;          write it to the file
      (format score-file "~% makegen(2")
      (dolist (elle gliss-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;    BANDWIDTH CURVE
;             (more or less constant value, but depends on 
;                noisiness parameter)
;
      (setf noisiness (first (nth filt-noisiness-par this-note)))
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
;
      (setf base-bandwidth (nth noisiness
            '(-.01 -.1 -.25 -.4 -.6 -.9)))
;
      (setf pizzvalues ())
      (setf pizztimes ())
      (setf lvtimes ())
      (setf lvvalues ())
      (setf cur-time '0) 
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 6.0 13.0))))
           (when (> cur-time 70.0) (return))
           (setf pizztimes (append pizztimes (list cur-time))))
      (dolist (elle pizztimes)
           (setf pizzvalues
               (append pizzvalues
                 (list
                   (const-frandom
                      (* 2.0 base-bandwidth)
                      (* 1.0 base-bandwidth))))))
;
;
      (setf last-usable-time-point (- dur-in-ms (+ (random 20) 30)))
      (setf cur-time (+ 40 (first (last pizztimes))))
      (loop
           (setf cur-time (+ cur-time (truncate (const-frandom 60.0 130.0))))
           (when (> cur-time last-usable-time-point) (return))
           (setf lvtimes (append lvtimes (list cur-time))))
      (setf lvtimes (append lvtimes (list last-usable-time-point) (list dur-in-ms)))
;
      (setf lvvalues '(0.0))
      (dotimes (ct (- (length lvtimes) 2))
         (setf lvvalues 
           (append lvvalues 
              (list
                 (const-frandom 
                    (* 1.07 base-bandwidth)
                    (* 0.92 base-bandwidth))))))
      (setf lvvalues (append lvvalues (list '0.0)))
;
;
      (dotimes (ct (length pizztimes))
          (setf bandwidth-makegen (append bandwidth-makegen
              (list (nth ct pizztimes)) (list (nth ct pizzvalues)))))
      (dotimes (ct (length lvtimes))
          (setf bandwidth-makegen (append bandwidth-makegen
              (list (nth ct lvtimes)) (list (nth ct lvvalues)))))
;
;        finally, write in the 24, xxxxxx values
      (setf bandwidth-makegen
          (append '(18)
                  (list dur-in-ms)
                  bandwidth-makegen))
;          write it to the file
      (format score-file "~% makegen(3")
      (dolist (elle bandwidth-makegen)
              (format score-file ", ~a" elle))
      (format score-file ")~% ~% ~%")
;
;
;
;
;     ACTUAL FILT STATEMENT
;      
      (setf amp (case (dynamic note)
                ('0 .03) ('1 .09) ('2 .14) ('3 .20) ('4 .26) ('5 .34)))
;     adjust for register, not too steeply
      (setf amp (* amp 1.84
                (case (nth pitch-octave-par this-note)
                (0 0.9) (1 0.9) (2 0.9) (3 0.9) (4 0.9) (5 0.9) (6 0.8) (7 .6) 
                (8 0.6) (9 0.5) (10 0.465) (11 0.3) (12 0.2) (13 0.1) (14 0.1) (15 0.1))))
;
      (format score-file "FILTSWEEP(0.0, 0.0, ~a, ~a, ~a, ~a, 1, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
             (if (< 1.0 (desired-dur note))
                 (* .5 (desired-dur note))
                 (const-frandom .1 .3)))
;       sharpness                           
          (nth noisiness '(5 5 5 4 4 3)))
;
;
;  MIX to get rid of opening click:
;
      (setf duration (+ ringdown-dur (desired-dur note)))
      (format score-file
             "bus_config(\"MIX\", \"aux 0 in\", \"out 0\") ~%~%
              makegen(1, 24, 10000, 0,0,~a,0,~a,1,~a,1)~%~%
              MIX(0, 0, ~a, 1.0, 0)~%~%"
                  9.0 12.0 dur-in-ms duration                
                  )
;  return duration
      (+ ringdown-dur (desired-dur note))))



;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************








































;
;***********************************************
;***********************************************
;***********************************************
;***********************************************
;  FILT:  (CRESC-DECRESC M E)
;***********************************************
;***********************************************
;***********************************************
;***********************************************
;


(defmethod write-cmix-score ((note cresc-decresc)
                             (instr filt)
                             (score-file t))

 (let ((dur-in-ms '0.0)
       (times ())
       (last-usable-time-point ())
       (cur-time '0)      
       (values ())
       (mid (first (extra-params note)))
       (end (second (extra-params note)))
       (amp-makegen ())
       (noisiness '0)
       (lower-passband '0.0)
       (higher-passband '0.0)
       (stopband-cutoff '0.0)
       (attenuation '0)
       (amp '0)
       (ringdown-dur '0.0)
       (this-note (nth (note-number note) note-list))
       )
;
;   make a noise file
; 
      (with-open-file (noise-score-file "noise.source.sco" :direction :output)
        (format noise-score-file
              "load(\"noise\")~%
               srand(~a)~%
               output(\"noise.source.aiff\")~%
               noise(0, ~a, 31000)~%"
        (random 100000) (desired-dur note)))
;   write sound to noise file
      (shell "sfc1 noise.source.aiff")
      (shell "CMIX < noise.source.sco")
;


;  write setup stuff:
;
      (format score-file 
             "~% rtsetparams(44100, 1) ~%
                 load(\"ELL\")
                 rtinput(\"noise.source.aiff\")  ~%
                 rtoutput(\"~a\") ~%"
              (nth filt-filename-par this-note))
;
; write makegen 1 -- amp env.
;
      (setf dur-in-ms (truncate (* 1000.0 (desired-dur note))))
;
;     AMPLITUDE MAKEGEN
;
      (setf times 
        (append
         '(0.0)
          (list (/ (* (const-frandom .02 .03)
                      end)
                   (desired-dur note)))
          (list (* (const-frandom .23 .35) mid))
          (list (* (const-frandom .55 .75) mid)) 
          (list (* (const-frandom .85 .9) mid)) 
          (list (* (const-frandom .93 .97) mid)) 
          (list mid) 
          (list (+ mid (* (const-frandom .03 .05) (- end mid)))) 
          (list (+ mid (* (const-frandom .1 .3) (- end mid)))) 
          (list (+ mid (* (const-frandom .35 .45) (- end mid)))) 
          (list (+ mid (* (const-frandom .55 .65) (- end mid)))) 
          (list  (- end (/ (* (const-frandom .02 .03)
                              end)
                           (desired-dur note))))
          (list end)))
;
;
      (setf values
        (append
         '(0.0)
          (list (const-frandom 0.03 0.07))
          (list (const-frandom 0.1 0.3))
          (list (const-frandom 0.4 0.5))
          (list (const-frandom 0.6 0.8))
          (list (const-frandom 0.85 0.95))
          '(1.0)
          (list (const-frandom 0.85 0.95))
          (list (const-frandom 0.6 0.8))
          (list (const-frandom 0.3 0.4))
          (list (const-frandom 0.1 0.3))
          (list (const-frandom 0.03 0.07))
          '(0.0)))
;
;
       (dotimes (ct (length times))
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;                 finally, write in the 24, xxxxxx values
;
       (setf amp-makegen ())
       (dotimes (ct (length times))
          (setf amp-makegen (append amp-makegen
              (list (nth ct times)) (list (nth ct values)))))
;        finally, write in the 24, xxxxxx values
       (setf amp-makegen
          (append '(24)
                  (list dur-in-ms)
                  amp-makegen))
;          write it to the file
       (format score-file "~% makegen(1")
       (dolist (elle amp-makegen)
              (format score-file ", ~a" elle))
       (format score-file ")~% ~% ~%")
;
;  note:  noisiness is contained in a list because 
;  it's the last input parameter, and
;  the engine might confuse it for a re-write flag.
       (setf noisiness (first (nth filt-noisiness-par this-note)))
;
;     ACTUAL ELL STATEMENTS
;      
       (format score-file "ellset(~a, ~a, ~a, 0.2, ~a)~%"
;  lower passband cutoff
         (setf lower-passband
            (* (frequency note) (case noisiness (0 .99)
                   (1 .963) (2 .935) (3 .91) (4 .85) (5 .80))))
;  higher passband cutoff
         (setf higher-passband
            (* (frequency note) (case noisiness (0 1.01)
                   (1 1.025) (2 1.043) (3 1.06) (4 1.105) (5 1.15))))
;  stopband cutoff, higher
         (setf stopband-cutoff
            (* (frequency note) (case noisiness (0 1.1)
                   (1 1.12)  (2 1.14)  (3 1.16)  (4 1.2) (5 1.25))))
; set up  attenuation argument for noisiness
         (setf attenuation
            (case noisiness (0 80) (1 70) (2 65) (3 55) (4 50) (5 45))))
;
;   first set amp by octave
;   
      (setf amp
          (case (nth pitch-octave-par this-note)
                (0 25.0) (1 25.0) (2 25.0) (3 25.0) (4 25.0) (5 25.0) (6 20.0) (7 13.0) 
                (8 9.0) (9 6.0) (10 3.0)  (11 2.0) (12 1.0) (13 1.0) (14 1.0) (15 1.0)))
;   adjust for noisiness
      (setf amp
          (* amp
                (case noisiness
                   (0 3.0) (1 1.5) (2 1.2) (3 .9) (4 .8) (5 .5))))
;    adjust for loudness
      (setf amp
          (* amp
	        (case (dynamic note)
		   (0 .23) (1 .475) (2 .75) (3 .45) (4 .77) (5 .98))))
;
;
      (format score-file "ELL(0.0, 0.0, ~a, ~a, ~a, 0, 0)~%"                           
;       duration
          (desired-dur note)
;       amp mult
          amp
;       ring-down duration
          (setf ringdown-dur
           (*
             (case (nth pitch-octave-par this-note) 
                (0 7.0) (1 7.0) (2 7.0) (3 7.0) (4 6.0) (5 6.0) (6 4.0) (7 2.0) 
                (8 1.0) (9 .6) (10 .4)  (11 .3) (12 .1) (13 .1) (14 .1) (15 .1))
             (case noisiness (0 1.0) (1 .9) (2 .8) (3 .7) (4 .6) (5 .5)))))
;
;doubling for loudness, at 2nd-harmonic***********
;
      (if (> (dynamic note) 2)
        (prog ()
          (format score-file "ellset(~a, ~a, ~a, 0.2, ~a)~%"
             (setf lower-passband
                (* 2.0 (frequency note) (case noisiness (0 .99)
                     (1 .963) (2 .935) (3 .91) (4 .85) (5 .80))))
             (setf higher-passband
                (* 2.0 (frequency note) (case noisiness (0 1.01)
                     (1 1.025) (2 1.043) (3 1.06) (4 1.105) (5 1.15))))
             (setf stopband-cutoff
                (* 2.0 (frequency note) (case noisiness (0 1.1)
                     (1 1.12)  (2 1.14)  (3 1.16)  (4 1.2) (5 1.25))))
             attenuation)
;
;   first set amp by octave     2nd harmonic***********
;   
          (setf amp
             (case (+ 1 (nth pitch-octave-par this-note)) 
                  (0 25.0) (1 25.0) (2 25.0) (3 25.0) (4 25.0) (5 22.0) (6 17.0) (7 13.0) 
                  (8 8.0) (9 4.5) (10 1.0)  (11 .3) (12 .3) (13 .1) (14 .1) (15 .1)))
;   adjust for noisiness         2nd harmonic**********
          (setf amp
             (* amp
                  (case noisiness
                   (0 2.5) (1 1.5) (2 1.2) (3 .9) (4 .8) (5 .5))))
;    adjust for loudness         2nd harmonic**********
          (setf amp
             (* amp
	          (case (dynamic note)
		     (3 .1) (4 .45) (5 .6))))
;
;
          (format score-file "ELL(0.0, 0.0, ~a, ~a, ~a, 0, 0)~%"
              (desired-dur note) amp ringdown-dur)))
;
;   RETURN DURATION!!!!!  Then deal with it back in the main proc.
       (+ ringdown-dur (desired-dur note))))

;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************
;*************************************************************************