Derivation of incoming OSC Data

Dear SC Community,

I want to calculate the derivation of incoming OSC Data to use them as a modulator for my synth. I tried to afford that using an array. Unfortunately, the code is not repeated, as I intented to and I dont know how to implement it correctly. Does anyone have a quick solution or a hint for me?

OSCdef.new(‘OSClistenerNEW’, {
arg msg;
var z, d;

z = Array.with(0,0); //Derivation of OSC_data

z.put(0, msg[1]);
z.put(1, z[0]);


d = z[1] - z[0];


msg[1].postln;
d.postln;

}, ‘/OSC_data’);
)

Thank you in advance!

Kind regards,
Ronald

I think the problem is that you reallocate a new array z every time you receive the OSC_data msg.
You should probably move the z = Array.with line outside the OSCdef.

Thank you for your reply!
Unfortunately it does not work. The array always gives out the exact same values in both positions.

This makes sense because your code is setting z[0] = msg, and z[1] = z[0]. So both entries in the array are identical. And later when you subtract one from the other you end up with d == 0.

It would be helpful to know what the OSC data stream looks to help find a way to proceed.

  1. Put the new value into z[0].
  2. Put z[0] into z[1] – now, what is z[0] at this point? Is it the old value (what you expected) or the new value (which you just put in).

You should copy the old value first, before overwriting it.

Next you’ll find that you’re getting the inverse derivative, because you’re doing old - new, where the derivative would be new - old.

Last, a note for forum usage: it helps people to read your code more accurately if you use markdown style for formatting: Code markup: Correct style

hjh

I understand it now, thank you very much for your help and reply.

Thank you!

I got it working now.

(
//an OSC listener that sends set messages
var z;
z = Array.with(0,0);
OSCdef.new('OSClistenerNEW', {
	arg msg;
	var d;


	z.put(1, z[0]);
	z.put(0, msg[1]);


	d = z[0] - z[1];
	
	~synth.set(\der, d);


}, '/OSC');

)

Thank you very much!