I know this is similar to this topic but the answer there didn’t quite work for me.
I am trying to hack together a kind of livecoding interface using events. I would like to define functions on my event that return a modified copy of the event but I can’t figure out how to access the equivalent of the this
keyword from other languages.
When its run, aside from the error. The console should show the value of currentEnvironment
to be the outer environment and not the event. How do I access this
for an event?
Here is my example:
(~x ={|name, synth|
var api = (
synth: synth,
name: name,
controlPats: (),
play: {
|server|
var s = (type: \set, id: ~synth.id);
var id = ~synth.id;
var ps = ~controlPats.collect({
|p,key|
Pbind(*[type: \set, id: id ]++[key, p.pat, \dur, p.dur])
});
Pdef(name, Ppar(
ps
)).play
},
c: {
|key, pat, dur(1)|
var e = ().putAll(currentEnvironment);
"e is... ".postln;
e.postln;
e.controlPats[key] = (pat: pat, dur: dur);
e
}
);
api
};
~synth !? {~synth.free; ~synth=nil};
~synth = ~synth??{{
var env = EnvGen.ar(Env.perc, \gate.kr(1));
var osc = SinOsc.ar(\freq.kr(440), mul: env*\amp.kr(1));
Pan2.ar(osc, \pan.kr(0))
}.play};
~x.(\fred, ~synth).c.(\degree, Pseq([1,2,3],inf)).play
)