[CM] Example of using CM with Csound

Larry Troxler lt@westnet.com
Fri, 24 Jan 2003 18:39:52 -0500


This is a multi-part message in MIME format.

--Boundary_(ID_HKREUNE4dH23KRo/Bta/Hg)
Content-type: text/plain; charset=us-ascii
Content-transfer-encoding: 7BIT

Here is a Csound orchestra and a lisp file that I hope can serve as an
example of using csound with Common Music. Please let me know if there
are any questions or comments about it.

Larry Troxler

--Boundary_(ID_HKREUNE4dH23KRo/Bta/Hg)
Content-type: text/plain; charset=us-ascii; name=fm-csound.lisp
Content-transfer-encoding: 7BIT
Content-disposition: inline; filename=fm-csound.lisp

(in-package :cm)

;; Set hook to run csound and generate a wav file

(set-sco-output-hook! 
 #'(lambda (fil)
	 (shell "csound -W -d -m0 -o csound.wav fm.orc ~a" fil)))

;; fm instrument class

(defobject fm (i)
  (frequency amplitude amplitude-env 
			 (mratio :initform 1) (index :initform 1) index-env sinetable)
  (:parameters time dur frequency amplitude amplitude-env
			   mratio index index-env sinetable))

;; Linear envelope function table generator class

(defobject gen27 (f)
  ((num :accessor f-num) (size :initform 1025) (gen :initform 27) env)
  (:parameters time size gen &rest env))

;; Harmonic sinusoids function table generator class

(defobject gen10 (f)
  ((num :accessor f-num) (size :initform 8192) (gen :initform 10) strengths)
  (:parameters time size gen &rest strengths))


;; Define a sine wave table and envelope tables

(setf sine (new gen10 time 0 num 1 strengths '(1)))
(setf ampenv (new gen27 time 0 num 2 env '(0 0 250 1 750 1 1024 0)))
(setf idxenv (new gen27 time 0 num 3 env '(0 1 1024 0)))

;; output 20 random Fm notes.

(defun random-fm (n)
  (process repeat n
           output
           (new fm :ins 1 :time (now) :amplitude .15 
				:amplitude-env (f-num ampenv)
                :dur .1
                :frequency (between 220 880)
                :index (interp (now) 0 0 10 10)
				:index-env (f-num idxenv)
				:sinetable (f-num sine))
           wait .25))



;; Write function table generators and our random notes to a csound score file

(events (list sine ampenv idxenv (random-fm 20)) "test.sco")


--Boundary_(ID_HKREUNE4dH23KRo/Bta/Hg)
Content-type: text/plain; charset=us-ascii; name=fm.orc
Content-transfer-encoding: 7BIT
Content-disposition: inline; filename=fm.orc

sr = 44100
kr = 4410
ksmps = 10
nchnls = 2

;  FM instrument 

          instr 1

idur      =   p3
ifreq     =   p4
iamp      =   p5 * 32767
iampenv   =   p6
imratio   =   p7
iidx      =   p8
iidxenv   =   p9
isin      =   p10

kamp      oscil1   0, iamp, idur, iampenv
kidx      oscil1   0, iidx, idur, iidxenv
a1        foscili  kamp, ifreq, 1, imratio, kidx, isin
          outs     a1, a1

          endin

--Boundary_(ID_HKREUNE4dH23KRo/Bta/Hg)--