Receiving float numbers using OSC

Hi, I am trying to send a float to SP using OSC but I run into a few issues. It works fine with integers. I have tried to round at the senders end but still have issues. Any suggestions please?

Hi @keys.

It’s a bit hard to tell what the exact problem is with the error message half cut off there in the screenshot, but I suspect that you need to place a space between start: and start. At the moment I think Sonic Pi is having trouble telling the difference between the opt start: and the value you are giving it (start) .

What’s likely happening here is that there’s an issue in this line:

start = sync "/osc/test"

The result from sync is always a list. In this case, it’s a list with one element (a float around 0.1-0.4) so the value of start will be something like [0.4000000059604645].

If you want to get at that value you need to look inside the list:

start[0]

Alternatively, you can use the fancy destructuring assignment mechanism to get the value on assignment:

start, _ = sync "/osc/test"

This asks for the first element to be assigned to start and the second to be assigned to _. We use _ as a means of telling readers of the code that we’re not interested in the value. In this case it will be nil as there is no second value.

Sorry about this - this is a confusing edge case which is actually just standard behaviour in Ruby. Perhaps a future version of Sonic Pi which moves away from Ruby might make this slightly less confusing.

2 Likes

Ah thanks! I wrongly assumed that there was something wrong with the float number. Thanks for explaining how the list should be handled - It all makes sense and works perfectly now :grinning::+1:

1 Like

hahaha. I stand corrected :slightly_smiling_face:
That’s what I get for never having used sync in that way :joy:

1 Like