Input from TextField to set SynthDef name

I’m trying to work on a GUI where I can type in a synthDef name and then link that to a sequence. I have the input loading from the textField in the post window, but can’t seem to get the named synthDef to play when I press the button. Am I getting something wrong about data types? Synths are definitely on the server.

And follow up question - as part of the sequencer I’m trying to get certain points of the sequences to trigger multiple Synths or other changes. I’m from a Pure Data background, and I’m used to just sending a “bang”. Is there any kind of equivalent that might be useful for this problem? Starting to get a handle on some aspects of Supercollider after a few years, but still miss this quite direct way of connecting events. I’m using language-side routines using s.bind for synths as I prefer the flexibility (somewhat following Nathan Ho’s approach), but open to suggestions.


(
SynthDef(\test1,{|freq=100|
	var sig, env;
	sig = SinOsc.ar(freq,mul: 0.1);
	env = Env.perc(0.001,0.1,curve: -4).ar(Done.freeSelf);
	Out.ar(0, sig*env);
}).add;

SynthDef(\test2,{|freq=200|
	var sig, env;
	sig = SinOsc.ar(freq,mul: 0.1);
	env = Env.perc(0.001,0.1,curve: -4).ar(Done.freeSelf);
	Out.ar(0, sig*env);
}).add;
)

(
var window,textField2,loadSynthDef;

Window.closeAll;

//overall window
window = Window.new("Sequencer", Rect(10, 10, 500, 500),scroll: true)
.front
.alwaysOnTop_(true);


//wide sequencer options panel
e = CompositeView(window, Rect(25, 25, 400, 300)).background_(Color.rand);
e.decorator = FlowLayout(e.bounds, 15@15, 0@0);
e.visible_(1);


loadSynthDef = ({
	
	var synthName, btn;
	
	textField2 = TextField(e,Rect(25, 225, 200, 50))
	.action_({|textContent|
		synthName = textContent.value;
		("synthName name is: "++synthName).postln;
	});

	btn = Button(e, Rect(25, 25, 20, 50))
	.action_({
		{
			Synth(synthName.asSymbol);
		}.defer;
	});

});
loadSynthDef.();
)
//test synths work
Synth(\test1);
Synth(\test2);
1 Like

The code works for me, I can input either ‘test1’ or ‘test2’, followed by return and press the button to hear the sound.

1 Like

Yes, as Thor said. If you want to not have to press enter you could replace the TextView .action with .keyUpAction.

1 Like

Ahh…thanks for checking this! just realised I was entering in a backslash also “\test1”, doubling down on the string → symbol. It works for me too now…thanks again!

1 Like