Using a Coordinate as an Argument for a SynthDef

Hi there -

I’d like to pick stored frequency values from an array and use them in a SynthDef.
My understanding is that arguments to a SynthDef need to be signals, so I can’t do something like this:

a = { { 200.0.rand }.dup(100) }.dup(100);
b = Array2D.fromArray(100,100, a.flat);
SynthDef(\test,
	{|x, y|
	var sig = SinOsc.ar(b[x, y])*Line.ar(1, 0, 1, doneAction:2);
	Out.ar(0, sig);
}).add;
		

What I thought, though, was that something like what follows would work…but I’m having no luck there. Can someone here correct me? Thank you.

a = { { 200.0.rand }.dup(100) }.dup(100);
b = Array2D.fromArray(100,100, a.flat);

SynthDef(\test,
	{
	var x, y, sig;
		x = \x.kr(1);
		y = \y.kr(1);
		sig = SinOsc.ar(b[x, y])*Line.ar(1, 0, 1, doneAction:2);
	Out.ar(0, sig);
}).add;
		

There are ways to do what you want, but buffer is probably easiest.

s.waitForBoot {
	~col = { { 200.0.rand }.dup(100) }.dup(100).flat;
	~b = Buffer.loadCollection(s, ~col);
	s.sync;
	
	~x = {
		var x = \x.kr(0).round(1);
		var y = \y.kr(0).round(1);
		
		var real_index = x + (y * 100);
		
		var freq = Index.kr(~b, real_index);
		freq.poll(1)
		
	}.play
}

There are ways without a buffer? I’d be interested, since there would be some other impacts to using a buffer.

Probably the easiest approach is to just pass the coordinates from the language as two synth arguments x and y, so to do the lookup in the language side.