Skip to content

Instantly share code, notes, and snippets.

@andr-ew

andr-ew/failure.scd

Last active Feb 10, 2021
Embed
What would you like to do?
FAILURE IN SERVER :(
// to recreate issue:
// 1. connect a midi controller
// 2. open supercollider, boot, execute
// 3. play a midi note, get the error, hear no sounds
// 4. execute the code again, all is well.
(
MIDIClient.init;
MIDIIn.connectAll;
~ops = 3;
//todo: add fixed lags
~synth = SynthDef.new(\fm, {
var peak = \peak.kr(0), pan = \pan.kr(0), hz = \hz.kr([440, 440, 0]),
amp = \amp.kr(Array.fill(~ops, { arg i; 1/(2.pow(i)) })),
ratio = \ratio.kr(Array.fill(~ops, { arg i; (2.pow(i)) })),
mod = Array.fill(~ops, { arg i; NamedControl.kr(\mod ++ i, 0!~ops) }),
a = \attack.kr(0.001!~ops), d = \decay.kr(0!~ops), s = \sustain.kr(1!~ops),
r = \release.kr(0.2!~ops), curve = \curve.kr(-4), done = \done.kr(1!~ops);
var frq = XLine.kr(hz[0], hz[1], hz[2], doneAction:0);
var env = EnvGen.kr(Env.adsr(a, d, s, r, 1, curve), peak > 0, doneAction: (done * 2));
var osc = SinOsc.ar(frq * ratio, Mix.ar(LocalIn.ar(~ops) * mod)) * env * peak;
LocalOut.ar(osc);
Out.ar(\outbuf.kr(0), Pan2.ar(Mix.ar((osc / ~ops) * (amp) * 0.1), pan));
}).add;
~tf = Env([-0.7, 0, 0.7], [1,1], [8,-8]).asSignal(1025);
~tf = ~tf + (
Signal.sineFill(
1025,
(0!3) ++ [0,0,1,1,0,1].scramble,
{rrand(0,2pi)}!9
)/10;
);
~tf = ~tf.normalize;
~tfBuf = Buffer.loadCollection(s, ~tf.asWavetableNoWrap);
~fx = SynthDef.new(\ulaw, {
var in = In.ar(\inbuf.kr(), 2), u = 2.pow(\bits.kr(11)), r = 700, lim = 1,
samps = \samples.kr(26460), dust = \dustiness.kr(1.95);
var sig = in;
sig = Mix.ar([
sig,
EnvFollow.ar(in, 0.99) * Mix.ar([
GrayNoise.ar(1) * Dust.ar(dust) * \crackle.kr(1), //add crackle
//GrayNoise.ar() * PinkNoise.ar() * \noise.kr(0.1) //add noise
Crackle.ar(dust) * \noise.kr(0.1) //add noise
])
]);
sig = LPF.ar(sig, samps/2); //anti-aliasing filter
sig = Compander.ar(sig, sig, //limiter
thresh: 1,
slopeBelow: 1,
slopeAbove: 0.5,
clampTime: 0.01,
relaxTime: 0.01
);
sig = Decimator.ar(sig, samps, 31); //sample rate reduction
sig = (sig / (lim*2)) + (lim/2); //range 0 - 1
sig = ( //u-law encoding (bitcrush pt 1)
1 + ((u * sig) - ((u * sig) % (1 + (u - u.floor))))
).log/(1 + u).log;
sig = (1/u) * ((1+u).pow(sig) - 1); //u-law decoding (bitcrush pt 2)
sig = (sig - (lim/2)) * (lim*2); //revert range
sig = Slew.ar(sig, r, r); //filter out some rough edges
sig = XFade2.ar(sig, Shaper.ar(~tfBuf, sig), (\drive.kr(0.05)*2) - 1); //waveshaper drive
Out.ar(\outbuf.kr(0), XFade2.ar(in, sig, (\drywet.kr(1)*2) - 1)); //dry/wet out
}).add;
~fxbus = Bus.audio(s, 2);
~fx = Synth.new(\ulaw, [\inBuf, ~fxbus]);
~defaults = { arg localvals = false;
var d = Dictionary.new;
~synth.allControlNames.do({ arg c;
var v = c.defaultValue, n = c.name;
if(if(localvals, { (n==\peak) || (n==\hz) }, { (n!=\peak) && (n!=\hz) }), {
d.put(c.name,
if(v.size == 0, { Bus.control(s, 1).set(v); },
{ Bus.control(s, v.size).setn(v); })
)
});
});
d;
};
~voice = Dictionary.new;
~all = ~defaults.value();
~local = Dictionary.new;
~setAt = { arg id, name, offset ...vals;
if(id == \all, {
~all[name].setnAt(offset, vals);
}, {
if(~local[id].isNil, { ~local.put(id, ~defaults.value(true)) });
if(~local[id][name].isNil, { ~local[id].put(name, ~defaults.value) });
~local[id][name].setnAt(offset, vals);
});
};
~set = { arg id, name ...vals;
~setAt.value(id, name, 0, *vals);
if((name == \peak) && (vals[0] > 0), {
var x = Synth.before(~fx, \fm, [\outbus, ~fxbus]);
(~all.keys ++ [\hz, \peak]).do({ arg name;
var bus = ~all[name];
if(~local[id].notNil, {
if(~local[id][name].notNil, { bus = ~local[id][name]; });
});
x.map(name, bus);
});
~voice.put(id, x);
});
};
MIDIdef.noteOn(\keybOn, {
arg vel, nn, chan, src;
~set.value(nn, \hz, nn.midicps, nn.midicps, 0);
~set.value(nn, \peak, vel.linexp(1, 127, 0.9, 1));
});
MIDIdef.noteOff(\keybOff, {
arg vel, nn;
~set.value(nn, \peak, 0);
});
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment