defsynth macro help (again)

39 views
Skip to first unread message

Roger Allen

unread,
Jan 10, 2013, 4:47:13 PM1/10/13
to over...@googlegroups.com
Last night, trying to get my single-string synth running with Leipzig, I realized I forgot to :action FREE the synth, so after a while there are too many synths and the server becomes overloaded.

So, I'm trying to conditionally add this free-ing to the macro that creates the synth & running into my own defmacro-fu limitations.  If someone can look this over and help me, I'd really appreciate it...

Instead of trying to show the whole defmacro, I think I've created a smaller snippet that shows the issue.

(defmacro mmx [n t]
  `(defsynth ~n []
     (let [mxxx# (map #(let [t1# (first %)
                             t2# ~(if t '(first ~%) '(second ~%))]
                         (~'* t1# t2#))
                      [ [100 200] [300 400] ])]
       (out 0 (sin-osc mxxx#)))))


this is just a made-up synth, I'm not even sure it will make a sound in the end.  The interesting bit here is the my attempt to set t2# to either "(first %)" or "(second %)" based on the macro input t.  I can't seem to escape things right to get that "%" to make it through the macro expansion process.  Here is the output of (macroexpand-1 '(mmx foo false)):

(defsynth
  foo
  []
  (let [mxxx__18010__auto__ (map
                              #(let
                                [t1__18012__auto__
                                 (first %)
                                 t2__18013__auto__
                                 (second ~p1__18009#)] ;; desire this to be "(second %)]"
                                (*
                                  t1__18012__auto__
                                  t2__18013__auto__))
                              [[100 200] [300 400]])]
    (out 0 (sin-osc mxxx__18010__auto__))))


I've tried a variety of escapes and nothing seems to help...does anyone understand what I'm doing wrong here?

Thanks in advance!

--Roger

p.s. On the off chance someone wants to look at the actual code in progress, here is a gist with the code in question highlighted.
https://gist.github.com/4506057#file-stringed-clj-L78-L83

Joel J.

unread,
Jan 10, 2013, 4:51:14 PM1/10/13
to over...@googlegroups.com
This probably reflects my own naivete, but what happens when you take away the tilde from in front of the % symbol?

--Joel

Roger Allen

unread,
Jan 10, 2013, 5:33:23 PM1/10/13
to over...@googlegroups.com
line in question changes to (second p1__18009#)]

Sam Aaron

unread,
Jan 10, 2013, 5:56:46 PM1/10/13
to over...@googlegroups.com
Hi Roger,

does this do what you wanted:

(defmacro mmx [n t]
`(defsynth ~n []
(let [mxxx# (map (fn [params#] (let [t1# (first params#)
t2# (if t (first params#) (second params#))]
(~'* t1# t2#)))
[ [100 200] [300 400] ])]
(out 0 (sin-osc mxxx#)))))

I essentially moved the anonymous fn to the more explicit variant - which allowed me to give it a gensymed arg param.

Sam

---
http://sam.aaron.name

Roger Allen

unread,
Jan 10, 2013, 6:31:17 PM1/10/13
to over...@googlegroups.com
Yeah, getting rid of the #() reader macro should make this easier...hopefully!

I believe that makes the params# work, but it leaves the if statement wrapper.  I'm trying to make that if statement disappear with the ~escape and only leave either (first param#) or (second param#) assigned to t2# depending on the setting of t.

If I add back the ~ before the if, it fails compilation...trying to add more escapes isn't bearing fruit for me...

--Roger

Roger Allen

unread,
Jan 10, 2013, 10:37:40 PM1/10/13
to over...@googlegroups.com
FWIW, I reduced this issue to a generic Clojure macro question & posted a query to stackoverflow.

http://stackoverflow.com/questions/14271184/clojure-macro-variable-trouble-with-map

--Roger
Reply all
Reply to author
Forward
0 new messages