Capstone
Capstone

Reputation: 2282

Embedding CSound in Common Lisp

I am working on embedding CSound in Lisp. CSound is a music synthesis (and more) open source software.

It has a fairly simple (scripting) language. Quick start (10 min read) is available at the link above. Currently I am working on just the assignment part (which is a large part of the csound language).

Here's my code:

(defparameter *assign-statements* nil)

(defmacro assign (_name value &optional (rate 'i))
  (let* ((name (if (typep _name 'symbol) _name (eval _name)))
         (var (symb (format nil "~(~a~)" rate) name)))
    `(progn 
       (defparameter ,name ',var)
       (defparameter ,var ,value)
       (setf *assign-statements* 
                 (cons (format nil "~A = ~A" ,name ,value) *assign-statements*)))))

(defmacro assign* (&rest args)
  `(progn ,@(mapcar (lambda (arg) (cons 'assign arg)) args)))

(defun opcode-call (opcode &rest args)
  (format nil "~a ~{~a~^, ~}" opcode (mapcar (lambda (arg) 
             (if (stringp arg) 
                 (let ((var (gensym)))
                   (eval (list 'assign (symb (symbol-name var)) arg 'a))
                   (symbol-value (symb (symbol-name var)))) 
                 arg)) 
          args)))

(defmacro op (opcode &rest args)
  `(opcode-call ',opcode ,@args))

To demonstrate what the code does:

(progn  
  (defparameter *assign-statements* nil)
  (assign* 
    (freq 'p4)
    (amp 'p5)
    (att (+ 0.1 0.1))
    (dec 0.4)
    (sus 0.6)
    (rel 0.7)
    (cutoff 5000)
    (res 0.4 k)
    (env (op madsr (op moogladder freq amp) att dec sus rel) k))
  (format t "~{~A~^~%~}~%" 
      (nreverse *assign-statements*)))

outputs:

iFREQ = P4
iAMP = P5
iATT = 0.2
iDEC = 0.4
iSUS = 0.6
iREL = 0.7
iCUTOFF = 5000
kRES = 0.4
aG8707 = MOOGLADDER iFREQ, iAMP
aG8708 = MOOGLADDER iFREQ, iAMP
kENV = MADSR aG8708, iATT, iDEC, iSUS, iREL
NIL  

This is correct in all respects except, "MOOGLADDER iFREQ, iAMP" appears twice.

Why is this? I can't figure out where it's being evaluated twice. How do I eliminate this repetition?


Notes about the code:

Upvotes: 1

Views: 368

Answers (1)

Rainer Joswig
Rainer Joswig

Reputation: 139261

Your macro ASSIGN lets the value be computed twice. See the comment below.

(defmacro assign (_name value &optional (rate 'i))
  (let* ((name (if (typep _name 'symbol) _name (eval _name)))
         (var (symb (format nil "~(~a~)" rate) name)))
    `(progn 
       (defparameter ,name ',var)
       (defparameter ,var ,value)
       (push (format nil "~A = ~A" ,name ,var)    ; <- use the var
             *assign-statements*))))

Try it:

CL-USER 52 > (progn  
               (defparameter *assign-statements* nil)
               (assign* 
                (freq 'p4)
                (amp 'p5)
                (att (+ 0.1 0.1))
                (dec 0.4)
                (sus 0.6)
                (rel 0.7)
                (cutoff 5000)
                (res 0.4 k)
                (env (op madsr (op moogladder freq amp) att dec sus rel) k))
               (format t "~{~A~^~%~}~%" 
                       (nreverse *assign-statements*)))
iFREQ = P4
iAMP = P5
iATT = 0.2
iDEC = 0.4
iSUS = 0.6
iREL = 0.7
iCUTOFF = 5000
kRES = 0.4
aG2719 = MOOGLADDER iFREQ, iAMP
kENV = MADSR aG2719, iATT, iDEC, iSUS, iREL
NIL

Upvotes: 4

Related Questions