oscsupercollider

How to use array values as function arguments in sclang (in the context of sending lots of floats via OSC)


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.


Solution

  • 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");