Gui Window with updating text

I have never used gui in SC till now, when I want a simple window with texts on it which show the current values of parameters of a Synth, whenever they change.
Naming the right class for this, or even better a short example would be very helpful and appreciated!

Here’s a little code to get started:

(
var win = Window();
var text, text2;

text = StaticText()
.string_("Hello")
.align_(\center);

text2 = StaticText()
.string_("world")
.align_(\center);

win.layout_(
	VLayout()
	.add(text)
	.add(text2)
);

CmdPeriod.doOnce({ win.close; });

win.front;
)

Now to update parameters automatically, this will depend on what you mean by that.
If your code does something like that

mySynth.set(\param, 100);

Solution is quite simple:

mySynth.set(\param, 100);
text.string_(100);

If it does this but within a Task or a Routine, you need to { }.defer the text update.

If synth parameters are modified within the SynthDef itself, it is way more complex, and I can’t find the documentation about it :confused: .

1 Like

I am setting the synth parameters as you said, by using a midi-controller (Korg nano). I thought changing your example to:

(
var win = Window();

~text = StaticText()
.string_("Hello")
.align_(\center);

~text2 = StaticText()
.string_("world")
.align_(\center);

win.layout_(
	VLayout()
	.add(~text)
	.add(~text2)
);

CmdPeriod.doOnce({ win.close; });

win.front;
)

and make texts global, and then in my midi-code something like

MIDIFunc.cc(
		{
			arg ...args; 
			var val = args.at(0).linlin(0,127,0,6); 
			~instSynth.set(\rateDrandArrIdxKnob2, val);
			~text2.string_(val)
	}, 17).permanent_(true)

But this gives the error:
ERROR: Qt: You can not use this Qt functionality in the current thread. Try scheduling on AppClock instead.
ERROR: Primitive ‘_QObject_SetProperty’ failed.

Where should I use an AppClock?

I missed that when giving the advice, this is equivalent to the Routine/Task thing I mentioned.

Instead of:

MIDIFunc.cc({
	[...]
	~text2.string_(val)
}, 17).permanent_(true)

Do:

MIDIFunc.cc({
	[...]
	{ ~text2.string_(val); }.defer;
}, 17).permanent_(true)

GUI is a bit special in SuperCollider. There are contexts where you can’t update GUI, so you need to defer the update (until the current context allows GUI updates).

EDIT: You might want to convert your value as string (and eventually round it):

{ ~text2.string_(val.trunc(0.01).asString); }.defer;