Getting values from a blob

I recieve the following message, it’s floating point meter values delivered as a binary blob. How do I translate f.exs. item 5 and 6 into a float? I can’t wrap my head around this in javascript :thinking:

OSC received: { address: ‘/meters/0’,
args: Uint8Array [ 8, 0, 0, 0, 8, 160, 8, 160, 0, 0, 0, 0, 0, 128, 0, 128, 8, 160, 8, 160 ] } From: 192.168.1.1:10024

You could convert Uint8Arrays to floats in a custom module:

module.exports = {

    oscInFilter:function(data){
        // Filter incomming osc messages

        var {address, args, host, port} = data

        if (args.length && args[0].value instanceof Uint8Array) {

            var view = new DataView(args[0].value.buffer),
                fval = view.getFloat32()
                    
            args[0] = {type: 'f', value: fval}

            
        }
    

        // return data if you want the message to be processed
        return {address, args, host, port}

    }

}

This will probably need some adjustments to work. I’m assuming two things here:

  • your app sends Uint8Arrays to describe floats only
  • the whole array describes a single precision float number

Thank you very much for you reply (I’m very impressed with your massive activity on this forum).

I already tried something along the lines of what you suggested, but I recieve an error, reporting that DataView needs a buffer. Your example simple doesn’t run, so I guess it’s not a Uint8Array, even though the response I mentioned first seems to imply it is…I’m confused :slight_smile:

Sorry, I just realized something was wrong in my code, I just updated it. I replaced args.buffer with args[0].value.buffer (args is always an array containing {type, value} objects that represent the osc arguments).
Edit: edited again

NP, I was on my way to discovering that, but your input helped immensely :slight_smile:

What is the best ways to pass several values for meters, parent.values or recieve?

I assume by parent.values you mean “sending all values to a parent container and let each children retrieve its value from it”. I haven’t done any performance test but I’d go for receive because it’s more straightforward I guess.