public
Created

Gist for a macro expansion question on the Overtone email list. The interesting problem is in the area that does (if free-on-silence ...). I want that "%" to make it through the macro. Alternatively, if I could figure out how to put the if only around :action FREE, that is another way to do things, but I get a nil from the if statement in the false case & that causes issues.

  • Download Gist
stringed.clj
Clojure
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
;; A Stringed Synth Generator Macro & Guitar Example Instrument
;;
;; See overtone/examples/instruments/guitar_synth.clj for example usage.
;;
;; Other instruments (like bass-guitar, ukelele, mandolin, etc.) may
;; use the same basic instrument. Watch this space...
(ns overtone.synth.stringed
^{:doc "A Stringed Synth Generator Macro & Guitar Instrument"
:author "Roger Allen"}
(:use [overtone.music pitch time]
[overtone.sc envelope node server synth ugens]
[overtone.sc.cgens mix]))
 
;; ======================================================================
(defmacro gen-stringed-synth
"Macro to generate a stringed defsynth with distortion, reverb and
a low-pass filter. Use the pluck-strings and strum-strings helper
functions to play the instrument.
 
Note: the strings need to be silenced with a gate -> 0 transition
before a gate -> 1 transition activates it. Testing
showed it needed > 25 ms between these transitions to be effective."
[name num-strings free-on-silence]
(let [note-ins (if (= num-strings 1)
[(symbol "note")]
(apply vector
(map #(symbol (format "note-%d" %)) (range num-strings))))
note-default-ins (apply vector
(flatten (map vector
note-ins
(repeat num-strings {:default 60 :min 0 :max 127}))))
gate-ins (if (= num-strings 1)
[(symbol "gate")]
(apply vector
(map #(symbol (format "gate-%d" %)) (range num-strings))))
gate-default-ins (apply vector (flatten (map vector
gate-ins
(repeat num-strings {:default 0}))))
both-default-ins (into note-default-ins gate-default-ins)
note-gate-pairs (apply vector (map vector note-ins gate-ins))
]
`(defsynth ~name
~(str "a stringed instrument synth with " num-strings
" strings mixed and sent thru
distortion and reverb effects followed by a low-pass filter. Use
the pluck-strings and strum-strings helper functions to play the
instrument. Note: the strings need to be silenced with a gate -> 0
transition before a gate -> 1 transition activates it."
(if free-on-silence
" This instrument is transient. When a string becomes silent, it will be freed."
" This instrument is persistent. It will not be freed when the strings go silent."))
 
[~@both-default-ins
~'dur {:default 10.0 :min 1.0 :max 100.0}
~'decay {:default 30 :min 1 :max 100} ;; pluck decay
~'coef {:default 0.3 :min -1 :max 1} ;; pluck coef
~'noise-amp {:default 0.8 :min 0.0 :max 1.0}
~'pre-amp {:default 6.0 :min 0.0 :max 10.0}
~'amp {:default 1.0 :min 0.0 :max 10.0}
;; by default, no distortion, no reverb, no low-pass
~'distort {:default 0.0 :min 0.0 :max 0.9999999999}
~'rvb-mix {:default 0.0 :min 0.0 :max 1.0}
~'rvb-room {:default 0.0 :min 0.0 :max 1.0}
~'rvb-damp {:default 0.0 :min 0.0 :max 1.0}
~'lp-freq {:default 20000 :min 100 :max 20000}
~'lp-rq {:default 1.0 :min 0.1 :max 10.0}
~'pan {:default 0.0 :min -1 :max 1}
~'out-bus {:default 0 :min 0 :max 100}]
(let [strings# (map #(let [frq# (midicps (first %))
nze# (~'* ~'noise-amp (pink-noise))
plk# (pluck nze#
(second %)
(/ 1.0 8.0)
(~'/ 1.0 frq#)
~'decay
~'coef)]
(leak-dc (~'* plk#
~(if free-on-silence
'(env-gen (asr 0.0001 1 0.1)
:gate (second %)
:action FREE)
'(env-gen (asr 0.0001 1 0.1)
:gate (second %))))
0.995))
~note-gate-pairs)
src# (~'* ~'pre-amp (mix strings#))
;; distortion from fx-distortion2
k# (~'/ (~'* 2 ~'distort) (~'- 1 ~'distort))
dis# (~'/ (~'* src# (~'+ 1 k#))
(~'+ 1 (~'* k# (~'abs src#))))
vrb# (free-verb dis# ~'rvb-mix ~'rvb-room ~'rvb-damp)
fil# (rlpf vrb# ~'lp-freq ~'lp-rq)]
(out ~'out-bus (pan2 (~'* ~'amp fil#) ~'pan))))))
;;(macroexpand-1 '(gen-stringed-synth ektara 1 true))
 
;; ======================================================================
;; common routines for stringed instruments
 
(defn- fret-to-note
"given a fret-offset, add to the base note index with special
handling for -1"
[base-note offset]
(if (>= offset 0)
(+ base-note offset)
offset))
 
(defn- mkarg
"useful for making arguments for the instruments strings"
[s i]
(keyword (format "%s-%d" s i)))
 
(defn- now+
"add an epsilon of time to (now) to avoid lots of 'late' error messages"
[]
(+ (now) 21)) ;; 21ms seems to get rid of most for me.
 
;; ======================================================================
;; Main helper functions used to play the instrument: pick or strum
(defn pick-string
"pick the instrument's string depending on the fret selected. A
fret value less than -1 will cause no event; -1 or greater causes
the previous note to be silenced; 0 or greater will also cause a
new note event."
([the-strings the-inst string-index fret t]
(let [the-note (fret-to-note (nth the-strings string-index) fret)]
;; turn off the previous note
(if (>= the-note -1)
(at t (ctl the-inst (mkarg "gate" string-index) 0)))
;; NOTE: there needs to be some time between these
;; FIXME: +50 seems conservative. Find minimum.
(if (>= the-note 0)
(at (+ t 50) (ctl the-inst
(mkarg "note" string-index) the-note
(mkarg "gate" string-index) 1)))))
([the-chord-frets the-inst string-index fret]
(pick-string the-chord-frets the-inst string-index fret (now+))))
 
;; ======================================================================
(defn strum-strings
"strum a chord on the instrument in a direction (:up or :down) with
a strum duration of strum-time at t. If the-chord is a vector, use
it directly for fret indexes."
([chord-fret-map the-strings the-inst the-chord direction strum-time t]
(let [num-strings (count (chord-fret-map :A))
;; ex: [-1 3 2 0 1 0]
chord-frets (if (vector? the-chord)
;; FIXME -- assert len(the-chord) is right?
the-chord ; treat the chord as a series of frets
(chord-fret-map the-chord))
;; account for unplayed strings for delta time calc. Code
;; gets a bit complicated to deal with the case where
;; strings are muted and don't count towards the
;; strum-time.
;; ex: (0 0 1 2 3 4)
fret-times (map first
(rest (reductions
#(vector (if (>= (second %1) 0)
(inc (first %1))
(first %1))
%2)
[0 -1]
chord-frets)))]
(dotimes [i num-strings]
(let [j (if (= direction :up) (- num-strings 1 i) i)
max-t (apply max fret-times)
dt (if (> max-t 0)
(* 1000 (/ strum-time max-t))
0)
fret-delta (if (= direction :up)
(- max-t (nth fret-times i))
(nth fret-times i))]
(pick-string the-strings the-inst j
(nth chord-frets j)
(+ t (* fret-delta dt)))))))
([chord-fret-map the-strings the-inst the-chord direction strum-time]
(strum-strings chord-fret-map the-strings the-inst the-chord
direction strum-time (now+)))
([chord-fret-map the-strings the-inst the-chord direction]
(strum-strings chord-fret-map the-strings the-inst the-chord
direction 0.05 (now+)))
([chord-fret-map the-strings the-inst the-chord]
(strum-strings chord-fret-map the-strings the-inst the-chord
:down 0.05 (now+))))
 
;; ======================================================================
;; The Guitar Instrument Code
;; ======================================================================
;; A map of chords to frets held for that chord. This is not all
;; possible guitar chords, just some of them as there are many
;; alternatives to choose from. Add more as you find/need them.
;;
;; You can pass in your own arrays to strum, too. The values are the
;; fret number of the string to press. This selects the note to play.
;; -1 indicates you mute that string
;; -2 indicates you leave that string alone & keep the current state
;; of either playing or not
;;
(def guitar-chord-frets
{:A [ -1 0 2 2 2 0 ]
:A7 [ -1 0 2 0 2 0 ]
:A9 [ 0 0 2 4 2 3 ]
:Am [ 0 0 2 2 1 0 ]
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 3 5 4 ]
:Gbm [ 2 4 4 2 2 2 ]
:Gbm7 [ 2 -1 2 2 2 -1 ]
 
:G [ 3 2 0 0 0 3 ]
:G7 [ 3 2 0 0 0 1 ]
:G9 [ -1 -1 0 2 0 1 ]
:Gm [ -1 -1 5 3 3 3 ]
:Gm7 [ -1 1 3 0 3 -1 ]
 
:Ab [ -1 -1 6 5 4 4 ]
:Ab7 [ -1 -1 1 1 1 2 ]
:Ab9 [ -1 -1 1 3 1 2 ]
:Abm [ -1 -1 6 4 4 4 ]
:Abm7 [ -1 -1 4 4 7 4 ]
 
:Gadd5 [ 3 2 0 0 3 3 ]
:Cadd9 [ -1 3 2 0 3 3 ]
:Dsus4 [ -1 -1 0 2 3 3 ]
 
})
 
;; ======================================================================
;; an array of 6 guitar strings: EADGBE
(def guitar-string-notes (map note [:e2 :a2 :d3 :g3 :b3 :e4]))
 
;; ======================================================================
;; Main helper functions. Use pick or strum to play the instrument.
(def guitar-pick (partial pick-string guitar-string-notes))
(def guitar-strum (partial strum-strings guitar-chord-frets guitar-string-notes))
 
;; ======================================================================
;; Create the guitar defsynth. Note that it is persistent and will
;; not be freed when any string goes silent.
(gen-stringed-synth guitar 6 false)
 
;; ======================================================================
;; Ektara - a single-string synth. Mainly for use with the midi-poly-player.
;;
;; Since "string" was too generic a name, I asked the google for some
;; help. Wikipedia tells me that there is a single-stringed
;; instrument called the "Ektara", so that is where the name comes
;; from.
;;
;; For use with midi-poly-player, we need to make the default gate 1.
;; Example:
;; (def mpp (midi-poly-player (partial ektara :gate 1)))
;;
;; Note that it is transient and will be freed when the string goes
;; silent.
;;
(gen-stringed-synth ektara 1 true)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.