[CM] Syntactic changes to snd-rt
Kjetil S. Matheussen
k.s.matheussen at notam02.no
Wed Aug 12 06:29:22 PDT 2009
I've added a few improvements to the syntax of snd-rt.
What I like most is the use of the keywords ":where" and
":when" which can be placed anywhere in code. (inspired
by various functional languages)
":where" is a postfix operator to add local variables,
and ":when" is a postfix operator for doing conditional tests.
Personally, I think code can becomes more gentle when using
these operators.
For the use of ":where", it is nice to know what you want to do with a
variable, before it is created.
And similarly, for the use of ":when', it is nice to know the action you
may want to do, before checking whether you want to do it.
Example below is the (usual) midi synthesizer using this new
syntax: (It looks much better in emacs with the "rt-DotEmacs"
file loaded!)
(<rt-stalin>
(define (midi-synth)
(while #t
(wait-midi :command note-on
(sound
(out (* (-> adsr next) (midi-vol) (oscil :freq (midi-to-freq (midi-note)))))
(stop :when (-> adsr is-finished)))
(spawn
(wait-midi :command note-off :note (midi-note)
(-> adsr stop)))
:where adsr (make-adsr :a 20:-ms :d 30:-ms :s 0.2 :r 70:-ms))))
(define (reverb input)
(+ (* 0.5 dry)
(* 0.1 wet)
:where dry input
:where wet (+ (comb :scaler 0.742 :size 9601 allpass-sum)
(comb :scaler 0.733 :size 10007 allpass-sum)
(comb :scaler 0.715 :size 10799 allpass-sum)
(comb :scaler 0.697 :size 11597 allpass-sum)
:where allpass-sum (send input :through
(all-pass :feedback -0.7 :feedforward 0.7)
(all-pass :feedback -0.7 :feedforward 0.7)
(all-pass :feedback -0.7 :feedforward 0.7)
(all-pass :feedback -0.7 :feedforward 0.7)))))
(sound
(out (reverb (in (midi-synth))))))
The same code, but without using :when and :where:
(<rt-stalin>
(define (midi-synth)
(while #t
(wait-midi :command note-on
(define adsr (make-adsr :a 20:-ms :d 30:-ms :s 0.2 :r 70:-ms))
(sound
(out (* (-> adsr next) (midi-vol) (oscil :freq (midi-to-freq (midi-note)))))
(if (-> adsr is-finished)
(stop)))
(spawn
(wait-midi :command note-off :note (midi-note)
(-> adsr stop))))))
(define (reverb input)
(define dry input)
(define wet (let ((allpass-sum (send input :through
(all-pass :feedback -0.7 :feedforward 0.7)
(all-pass :feedback -0.7 :feedforward 0.7)
(all-pass :feedback -0.7 :feedforward 0.7)
(all-pass :feedback -0.7 :feedforward 0.7))))
(+ (comb :scaler 0.742 :size 9601 allpass-sum)
(comb :scaler 0.733 :size 10007 allpass-sum)
(comb :scaler 0.715 :size 10799 allpass-sum)
(comb :scaler 0.697 :size 11597 allpass-sum))))
(+ (* 0.5 dry)
(* 0.1 wet)))
(sound
(out (reverb (in (midi-synth))))))
More information about the Cmdist
mailing list