2

I would like to use an array of floats as function arguments for OSC.sendMessage(). For example in PHP I'm aware of call_user_func_array(). Is there something similar availabe in sclang?

The context is that I would like to send lots of float values via OSC from sclang to Unity. AFAIK it's not possible to directly embed an array of values in an OSC message.

I'm also open for suggestions if there is better way to achieve this as my understanding is that there could be constraints regarding the amount of values I can pack into 1 OSC message and maybe I have to handle fragmentation / spanning over multiuple messages myself.

For a fixed array size / argument count I figured out this:

(
var floats = [13.37, 31337.1337, 1.0];
{ | a, b, c | o.sendMsg("/LoadAudioData", a, b, c); }.valueArray(floats);
)

But I need a more generic solution for different array sizes.

8yteCrunch
  • 23
  • 2

1 Answers1

1

There's syntactic sugar in SuperCollider to unpack arrays -

  1. For function calls:
(
var list = [1, 2, 3];
func(*list);  // equivalent to func(list[0], list[1], list[2])
)
  1. For assignment:
var a, b, c;
#a, b, c = [1, 2, 3]; // equivalent to a=1; b=2; c=3;

And, even for your example code, SC supports a parameter pack style syntax to deal with arrays of variables:

(
var formatWarning = {
   |string, ...args| // where args becomes an array of all remaining variables
   string.format(*args).warn;
};
formatWarning.value("Too much % %!", "syntactic sugar", "for me");
scztt
  • 1,053
  • 8
  • 10
  • Also, be aware that if you're passing arrayed values to a synth argument, you need to do a few special things - see the "Literal Array Arguments" section of the `SynthDef` help file, or the `NamedControl` help. – scztt May 08 '19 at 20:36
  • Thanks for this intriguing explanation ! I just figured out, that this unpacking doesn't work for FloatArrays although it works for Lists. So I ended up with something like this: ``` ( var arr = FloatArray[13.37, 3.3333333, 123.456]; var list = List.new(arr.size); for (0, arr.size-1, { arg i; list.add(arr[i]) }); list.postln; o = NetAddr.new("127.0.0.1", 6969); o.sendMsg("/LoadAudioData", *list); ) ``` Also, there seems to be a max limit of values I can put into one OSC message. But thats not in the original scope of the question... Thank you very much sir ! – 8yteCrunch May 09 '19 at 13:43