Changing MouseX to an OSC control

Hi everyone,

I’m trying to control a SelectX output with an OSC slider. I’ve adapted the code from the help file to my set up as per below:

(
{
    var a;
    a = [
		SoundIn.ar([12, 13]),
		SoundIn.ar([14, 15]),
		SoundIn.ar([16, 17]),
		SoundIn.ar([18, 19]),
        ];
    SelectX.ar(MouseX.kr(0, 1) * a.size, a) * 0.5!2
}.play;
)

Everything works as it should however I cannot figure out how to change the MouseX control to an OSCdef control. I have an OSC slider (/fader10) that I would like to control the X axis movement of SelectX.

I feel like the answer is probably quite obvious but I’m quite new to this and can’t quite figure it out myself yet.

Thanks!

1 Like

Hi!

The thing is, MouseX is a UGen that runs on the server. An OSCdef (or an OSCFunc) runs in the language. So, you’ll first have to adapt your function (which constitutes a Synth running on the server). Then you can set up an OSCdef that set’s a control within you’re running Synth:

(
x = { |ctrl=0| // this control, defined as an argument to your function replaces MouseX 
                    // by default a[0] should be played
	var a;
	a = [
		SoundIn.ar([12, 13]),
		SoundIn.ar([14, 15]),
		SoundIn.ar([16, 17]),
		SoundIn.ar([18, 19]),
	];
	SelectX.ar(ctrl.poll, a) * 0.5!2 // poll posts the currently set value to the post window
}.play;
)

(
OSCdef(\setIndex, { |msg, time, addr, recvPort| 
	x.set(\ctrl, msg[1]) // the value you're interested in is held in msg[1]
}, '/fader10') // your OSC command name
)

// send to the language, not to the server directly
// when working with an external app you will have to set up this in your app, not sclang
n = NetAddr.localAddr

// for testing simply send a message from within sclang
// send command, look at what's being posted in the post window
// (or just listen)
n.sendMsg('/fader10', 2)  

Hope that helps and makes the underlying concept clear.

3 Likes

Thank you so much! Exactly what I was looking for. Thanks for explaining it so well

2 Likes

Just to add a bit of explanation to what’s going on behind the scenes: The way I’ve added a control to the SynthDef (the function that you’ve given in your example really is a SynthDef function) looks just the same as adding an argument to an arbitrary function in sclang. However there’s happening something fundamentally different between an argument to a SynthDef function and an argument in a mere language function. A SynthDef gets compiled to scaffold of UGens that serves as a blueprint for any Synth you create from it (executing x = {...}.play is so to say a shortcut that combines defining a SynthDef and instantiating a Synth).
The point is, the server doesn’t understand sclang at all - it only understands OSC. When adding a control to the SynthDef it gets converted to a UGen during compilation, namely a NamedControl. So, when I added a control to your function I really just replaced your MouseX.kr(...) with a different UGen, a NamedControl.kr(...) which takes values sent over OSC.

1 Like