24 Beyond MIDI

Example 24-1. Contents of the Csound orchestra file "blur.orc".

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

;;;
;;; simple oscillator with envelope
;;;
;;; f1: 0  <size>  10  1 0 1 0 1 0 1 0 1 0 1

instr 1
idur = p3
iamp = p4*32767       ; scale logical amplitude
ifreq = p5
iattackt = p6
irelt	= p7

k1	linen	iamp, iattackt, idur, irelt
a1	oscili	k1, ifreq, 1
out	a1
endin

Example 24-2. Defining a new class of object.

(defobject i1 (i)
  (amp
   freq
   (attack :initform .05)
   (release :initform .25))
  (:parameters time dur amp freq attack release))

Interaction 24-1. Creating i1 events.

cm> (new i1)
#i(i1 ins 1 attack .05 release 0.25)
cm> (new i1 :time 0 :freq (random 1000) :amp .5)
#i(i1 time 0 ins 1 amp 0.5 freq 693 attack 0.05 release 0.25)
cm>

Example 24-3. Generate i1 events.

(define (blur )
  (process repeat 100
           output (new i1 :time (now) 
                       :freq (between 20 2000) ; Hertz
                       :dur .4 :amp .1)
	   wait .015))

Interaction 24-2. Listening to blur.

cm> (set-sco-output-hook! #'play-sco-file)

cm> (define sco-header  ; define a score file header string
      "f1 0 8192 10 0.0  0.41 0.06 0.04 0.00 0.11 0.02 0.00 0.00")

cm> (events (blur) "blur.sco" 0
    	:header sco-header
    	:options "-d -m0 -A"
    	:orchestra "blur.orc" 
    	:output "blur.aiff")
"blur.sco"
cm>

Example 24-4. Contents of blur.sco

; Common Music 2.4.0 output on 24 Mar 2003, 09:49:38
f1 0 8192 10 0.0  0.41 0.06 0.04 0.00 0.11 0.02 0.00 0.00
i1 0 0.4 0.1 300 0.05 0.25
i1 0.015 0.4 0.1 406 0.05 0.25
i1 0.03 0.4 0.1 975 0.05 0.25
...

Example 24-5. The contents of the CLM instrument file "beep.ins".

(definstrument beep (start dur freq amp amp-env)
  (let* ((beg (seconds->samples start))
         (end (seconds->samples (+ start dur)))
         (osc (make-oscil :frequency freq))
         (env (make-env :envelope amp-env :scaler amp 
                        :duration dur)))
    (run 
     (loop for i from beg to end 
           do
           (outa i (* (env env) (oscil osc)))))))

Interaction 24-3. Compiling and loading "v.ins".

cm> (cd "/Users/hkt/NM/24/")
"/Users/hkt/NM/24/"
cm> (compile-file "v.ins")
"/Users/hkt/NM/24/v.pfasl"
cm> (load "v")
"/Users/hkt/NM/24/v.pfasl"
cm> (compile-file "nrev.ins")
"/Users/hkt/NM/24/nrev.pfasl"
cm> (load "v")
"/Users/hkt/NM/24/nrev.pfasl"
cm>

Interaction 24-4. Listing an object's parameters.

cm> (object-parameters (new midi))
(time duration keynum amplitude channel)
cm> (object-parameters (new fm-violin))
(startime dur frequency amplitude fm-index amp-env 
 periodic-vibrato-rate random-vibrato-rate 
 periodic-vibrato-amplitude random-vibrato-amplitude
 noise-amount noise-freq ind-noise-freq ind-noise-amount
 amp-noise-freq amp-noise-amount gliss-env glissando-amount
 fm1-env fm2-env fm3-env fm1-rat fm2-rat fm3-rat fm1-index
 fm2-index fm3-index base frobber reverb-amount index-type
 degree distance degrees no-waveshaping denoise denoise-dur
 denoise-amp)
cm>

Example 24-6. Envelopes for the fm-violin example.

(define amp-env '(0 0 2 .5 3 .9 5 .6 75 .2 100 0))

(define morph-weight
  '(0 1 .2 10 .3 20 .35 50 .45 100 .5 100
    .55 100 .65 25 .97 20 .98 10 1 1))

(define spike-env
  '(0 .01 .2 .02 .3 .05 .35 .09 
    .4 .1 .425 .15 .45 .2 .5 2.5
    .55 .2 .575 .15 .6 .1 .65 .09
    .7 .05 .8 .02 1. .01))

(define trough-env
  '(0 9 .2 8 .3 5 .35 4 .45 0 .5 0
    .65 4 .96 8 1 9))

Example 24-7. The fm-morph process.

(define (fm-morph len rhy amp)
  (let* ((w1 1)
         (pat (new random 
                :of `((c1 :weight .3)
                      (c4 :weight,(pval w1))
                      cs4 d4 ds4 e4 f4 fs4
                      (g4 :weight ,(pval w1))
                      gs4 a4 as4 b4
                      (c5 :weight ,(pval w1)))
                :for 1)))
    (process for i below len
             for pct = (/ i len)
             for frq = (next pat)
             for lo? = (scale= frq 'c1)
             do
             (set! w1 (interpl pct morph-weight))
             output
             (new fm-violin :startime (now) 
                  :dur (if lo? 8
                           (interpl pct spike-env))
                  :amplitude amp
                  :frequency (hertz frq)
                  :amp-env amp-env 
                  :reverb-amount .1
                  :fm-index (if lo? 2.5
                                (interpl pct trough-env)))
             wait rhy)))

Interaction 24-5. Generating audio output from fm-morph.

cm> (events (fm-morph 400 .1 .2) "morph.aiff" 0
            :srate 44100 :channels 2 :reverb 'nrev)
; File: "morph.aiff"
; Channels: 2
; Srate: 44100
; Reverb: NREV
"morph.aiff"
cm>

Example 24-8. Example staff definitions.

(define trio-staves
  '((0 :name "Cello" :clef :bass :meter (4 4))
    (1 :name "Oboe" :clef :treble :meter (4 4))
    (2 :name "Flute" :clef :treble :meter (4 4))))

Example 24-9. defining a process to output CMN files.

(define duet-staves
  '((0 :name "Viola" :clef :alto :meter (4 4))
    (2 :name "Flute" :clef :treble :meter (4 4))))

(define (duet-cmn stf len nts)
  (let ((nts (new heap :notes nts))
        ;; choose quarter or two eighths
        (rhy (new random
               :of `(1 ,(new cycle :of '(1/2 1/2))))))
    (process while (< (now) len)
             for n = (next nts)
             for r = (next rhy)
             output (new midi :time (now)
                         :duration r
                         :keynum n
                         :channel stf) ; link to cmn staff
             wait r)))

Interaction 24-6. Outputting a CMN file.

cm> (events (list (duet-cmn 0 12 '(c3 d ef f g))
                  (duet-cmn 1 12 '(c5 d ef f g)))
            "duet.eps" 
            :staffing duet-staves
            :size 24
            :title "Hiho!")
Manuscripting duet.eps...
"duet.eps"
cm> (events (list (duet-cmn 0 12 '(c3 d ef f g))
                  (duet-cmn 1 12 '(c5 d ef f g)))
            "duet.cmn" 
            :staffing duet-staves
            :size 24
            :title "Hiho!")
"duet.cmn"
cm>

Chapter Source Code

The source code to all of the examples and interactions in this chapter can be found in the file beyond.cm located in the same directory as the HTML file for this chapter. The source file can be edited in a text editor or evaluated inside the Common Music application.