// Written by Alik Rustamoff
// src: https://youtu.be/j0leDcRS21M?si=AuLBTZQixsdxBp5x
// https://github.com/alikthename/Musical-Design-in-Supercollider

(
z = SynthDef("fm_pulsar",{
	var a,e,f;
	var t, overlap, carfreq, modfreq, mInd, vel, vel_;

	f = \freq.kr(100);
	// f = f + SinOsc.ar(LFNoise1.kr(0.3!2).range(1,4));

	vel = \kel.kr(0.5);
	vel_ = vel.clip(0,1.0).linlin( 0, 1, 2, 0.7);

	e = EnvGen.kr(
		Env.adsr(
			\att.kr(0.025) * vel_,
			\dec.kr(0.02) * vel_,
			\susl.kr(0.7),
			\rel.kr(4),
			curve: \curve.kr(-4)
		),
		gate: \gate.kr(1),
		doneAction:2
	) * vel;

	// e = e * PinkNoise.ar(1!2).range( 0.1, 1 ).lag(0.2);

	carfreq = f * \cf.kr(2) + LFNoise1.kr(3).range(0,50);
	modfreq = carfreq * \mf.kr(2) + LFNoise1.kr(3).range(0,100);

	t = Impulse.ar(f);

	mInd = e.pow(3.3);
	overlap = 1.1 - e.pow(0.5);
	d = overlap/f;

	a = Mix([
		SinOsc.ar(f),
		GrainFM.ar( 1, t, d, carfreq, modfreq, mInd ),
	]);

	a = LeakDC.ar(a * e) ;
	Out.ar(\outBus.kr(0), a.dup * \gain.kr(0.1));

}).play;
)
