Variables as "macros"?


#1

I’m trying to create a couple of “macros” for envelopes. i.e. build a couple of generic envelope shapes and have them set as variables so that i don’t have to type envelope manually or even copy paste them. Instead i just type “fast_envelope” or “slow_envelope” and i’m done. However so far had no luck i guess i’m doing something wrong.

Code example:

fast_envelope =  attack: 0, attack_level: 1, decay: 0, sustain_level: 1, sustain: 1, release: 0

play 60, fast_envelope

And it outputs an error.

Any help appreciated!


#2

I would guess most of us would point you at use_synth_defaults:

play 50

use_synth_defaults amp: 0.5, cutoff: 70

play 50

use_synth_defaults cutoff: 90

play 50

But there should be plenty of other ways such as: define a block of code
to do what you want, then call it with a parameter. I’ve included a
simple example:

Someone else might give you a more specific example…

use_bpm 90
use_debug false
set_volume! 0
sleep 1

notes = (ring :c4, :c4, :d4)
durations = (ring 0.25, 1.25, 0.25)
sleeps = (ring 1.5, 1.5, 2.5)

set_volume! 3

define :sustain do|x|
  
  use_synth_defaults attack: 0, attack_level: 1, decay: 0, sustain_level: 1, sustain: x
  3.times do
    tick
    play (chord notes.look, :minor7), release: durations.look
    play notes.look, release: durations.look
    sleep sleeps.look
  end
end


sustain 1
sleep 5
sustain 4

Eli…


#3

If you wrap the first line in curly braces to make it a hash, it will work:

fast_envelope = {attack: 0, attack_level: 1, decay: 0, sustain_level: 1, sustain: 1, release: 0}
play 60, fast_envelope

#4

Solved!!!

Thank you!