Skip to content

Instantly share code, notes, and snippets.

@scztt
Last active November 17, 2024 18:00
Show Gist options
  • Save scztt/1acc0160b1d719e61ed0cfba11017c6a to your computer and use it in GitHub Desktop.
Save scztt/1acc0160b1d719e61ed0cfba11017c6a to your computer and use it in GitHub Desktop.
(
Ndef(\synth, {
var lpf, res, distort, rate, decay,
freq, amp, sig, env, trig, detune;
// CONTROLS
lpf = \lpf.ar(540);
res = \res.ar(0.9);
distort = \distort.ar(0.7);
rate = \rate.kr(3);
decay = (1 / rate) * \decay.ar(1.8);
amp = \amp.kr(-24.dbamp);
freq = \freq.kr(60);
[lpf, res, distort, rate, decay].poll(1); // print out controls
trig = Impulse.ar(rate);
env = Env.perc(0, decay).ar(gate:trig);
detune = [0, 1].resamp1(8) * env.linlin(0, 1, 0.001, 0.04);
sig = 0.5 * Saw.ar(freq * detune.midiratio).sum;
sig = env * sig;
sig = RLPF.ar(sig, lpf, res);
sig = (distort.linlin(0, 1, 1, 8) * sig).fold(-1.0, 1.0);
sig = RLPF.ar(sig, lpf * 2, res);
sig = sig + Pluck.ar(
sig,
ClipNoise.ar,
1,
(1 / (freq * LFDNoise3.ar([4, 2]).range(-0.1, 0.1).midiratio)),
4.2,
0.8
);
sig = sig + Pluck.ar(
sig,
ClipNoise.ar,
1,
([3, 2] / 8) * LFDNoise3.ar(1/[4, 4]).range(-1, 1).midiratio,
5,
0.9
);
sig = LeakDC.ar(sig);
sig * [1, 1] * amp;
}).play;
)
(
// 1: Make an empty Ndef, with 5 channels bc we have 5 controls. Just output DC.ar(0), e.g. an audio
// rate signal of 0.
Ndef(\synthControls, { DC.ar(0) ! 5 });
// Fadetime controls how fast you fade between control settings
Ndef(\synthControls).fadeTime = 10;
// Map the outputs of synthControls to your \synth controls.
// The order of the outputs of your control Ndefs should match this, e.g.
// the first output should always be lpf.
Ndef(\synth).set(
\lpf, Ndef(\synthControls).bus.subBus(0).asMap,
\res, Ndef(\synthControls).bus.subBus(1).asMap,
\distort, Ndef(\synthControls).bus.subBus(2).asMap,
\rate, Ndef(\synthControls).bus.subBus(3).asMap,
\decay, Ndef(\synthControls).bus.subBus(4).asMap,
);
// Breaking it down:
// Ndef(\synthControls) ... the Ndef
// .bus ... the Bus that this Ndef is sending to. This is 5 channels, as per above definition
// .subBus ... we want to map just ONE channel of the 5 channel bus, so we use subBus to get 0..5
// .asMap ... We need this in order to MAP the bus to this control, otherwise we just set the control value once
)
(
// 2: Define some preset control behaviors. They should output a list of signals, correponding to the
// controls youre mapping above.
Ndef(\basic, {
[
200, // lpf,
0.7, // res,
0.2, // distort,
1/2, // rate,
1, // decay,
]
});
Ndef(\fast, {
[
300, // lpf,
0.7, // res,
0.2, // distort,
4, // rate,
1, // decay,
]
});
Ndef(\fastChanging, {
[
LFDNoise3.ar(1/8).range(200, 700), // lpf,
0.7, // res,
LFDNoise3.ar(1/8).range(0.2, 0.6), // distort,
4, // rate,
1, // decay,
]
});
Ndef(\wild, {
[
LFDNoise3.ar(9).exprange(320, 4900), // lpf,
0.3, // res,
LFDNoise3.ar(2).range(2, 3.6), // distort,
TChoose.ar(Impulse.ar(2), DC.ar([1, 4, 2])), // rate,
2, // decay,
]
});
Ndef(\fastest, {
[
LFDNoise3.ar(1).exprange(20, 3900), // lpf,
0.9, // res,
LFDNoise3.ar(2).range(0.7, 13.6), // distort,
LFDNoise3.ar(1/2).exprange(28, 295), // rate,
LFDNoise3.ar(1/16).range(0.8, 1.1), // decay,
]
});
)
(
// 3: Set your "preset" Ndefs into synthControls.
// It will slowly fade over to this depending on fadeTime.
Ndef(\synthControls).fadeTime = 10;
Ndef(\synthControls, Ndef(\basic));
// Ndef(\synthControls, Ndef(\fast));
// Ndef(\synthControls, Ndef(\fastChanging));
// Ndef(\synthControls, Ndef(\wild));
// Ndef(\synthControls, Ndef(\fastest));
)
(
// 4: You can independently set other controls as well...
// And, you can set inline defined ndefs directly as .set parameters
Ndef(\synth).set(
\freq, Ndef(\freq, {
TChoose.kr(
Dust.kr(4),
30 + [0, 2, 3, 7, 54]
)
* TChoose.kr(
Dust.kr(2),
[0, 2, 4, 9] - 2
).midiratio
})
);
)
(
// Also, remember that your "preset" Ndefs can have controls as well, which you can use to
// e.g. control with MIDI:
Ndef(\wildMIDI, {
// Make out midi controls take 0..1 numbers, and then remap with linlin linexp
// bc its easier
var lowpass = \lowpass.kr(0)
.linexp(0, 1, 200, 5000)
.lag(0.1);
var filterSpeed = \filterSpeed.kr(0)
.linlin(0, 1, 1, 9)
.lag(0.1);
[
LFDNoise3.ar(4).linexp(
-1, 1,
120, lowpass
), // lpf,
0.3, // res,
LFDNoise3.ar(2).range(2, 3.6), // distort,
TChoose.ar(Impulse.ar(2), DC.ar([1, 4, 2])), // rate,
2, // decay,
]
});
Ndef(\synthControls, Ndef(\wildMIDI));
Ndef(\wildMIDI).set(\lowpass, 0.3, \filterSpeed, 0.4);
MIDIdef.cc(\midiDef_lowpass, {
|value|
Ndef(\wildMIDI).set(\lowpass, value / 127.0)
}, 0);
MIDIdef.cc(\midiDef_filterSpeed, {
|value|
Ndef(\wildMIDI).set(\filterSpeed, value / 127.0)
}, 1)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment