// subharmonic distortion (C) www.zlatko.hu 2010

( var name, func, specs, componentType, componentSubtype, builder;
name = "Subharmonic";

func =  {|modulo=2,offset=0,exponent=1,cutoff=20000,rq=1,wetdry=0.5|
 	var above,below,amul,bmul;
 	a=AudioIn.ar([1,2]);
 	above=a*(a>0);
	below=a*(a<0);
	b=PulseCount.ar(a);
	amul= b % modulo / (modulo.ceil-1) * 2 - 1 ** exponent;
	bmul= b + (modulo*offset).floor % modulo / (modulo.ceil-1) * 2 - 1 * -1 ** exponent;
	z=above*amul+(below*bmul);
	z=XFade2.ar(RLPF.ar(z,cutoff,rq),a,wetdry);
	Out.ar(0,z);
};

specs =  #[
	[2,32,\Linear,0,\Indexed],
	[0,1,\Linear,0,\Generic],
	[0.05, 20, \Logarithmic, 1, \Generic],
	[10, 20000, \Logarithmic, 20000, \Hertz],
	[0,1,\Linear,0.5,\Generic],
	[-1,1,\Linear,0,\Generic]
];	

componentType = \aufx;	
componentSubtype = "SBHM"; 
builder = AudioUnitBuilder.new(name, componentSubtype,func, specs, componentType);
builder.makeInstall;
)