How to plot a SynthDef?

BTW the reason why we can have { ... }.plot but not SynthDef(...).plot is:

  • When plotting a function, we can assume that the function’s return value (which is a network of UGen connections) is the signal that you want to plot.

  • If it’s a SynthDef, there is no guaranteed, reliable way to know what signal to plot. SynthDefs use Out.ar to write the signal to a bus. To plot, we would need to know which bus. There is a loose convention that SynthDefs should have an out argument for the output bus number. But if the user said outbus or bus or audioOutputBus, how would SynthDef:plot know this? (Worse, a lot of new users take a shortcut and write Out.ar(0, ...) in which case it would be impossible for a hypothetical SynthDef:plot method to isolate the signal from other things playing at the same time.)

poll is one workaround.

Another is to write a RecordBuf into the SynthDef (temporarily, delete it later), then plot the buffer’s contents.

s.boot;

b = Buffer.alloc(s, 44100, 1);

(
// hack the default synthdef
SynthDef(\default, { arg out = 0, freq = 440, amp = 0.1, pan = 0, gate = 1;
	var z;
	z = LPF.ar(
		Mix.new(VarSaw.ar(freq + [0, Rand(-0.4, 0.0), Rand(0.0, 0.4)], 0, 0.3, 0.3)),
		XLine.kr(Rand(4000, 5000), Rand(2500, 3200), 1)
	) * Linen.kr(gate, 0.01, 0.7, 0.3, 2);
	RecordBuf.ar(z, b, loop: 0, doneAction: 2);  // <<-- inserted
	OffsetOut.ar(out, Pan2.ar(z, pan, amp));
}, [\ir]).add;
)

a = Synth(\default);  // will stop by itself

b.plot

b.getToFloatArray(wait: -1, timeout: 5, action: { |data| d = data; "done".postln });

d[0..500].plot;

hjh

1 Like