View Source OSC.Types.Float (ex_osc v0.1.1)

Encoding and decoding of the OSC float type.

OSC floats are encoded in 32-bit IEEE 754 binary format (a.k.a single, or more recently, binary32). This makes converting them very easy in Elixir, since we can just use <<x::float-size(32)>> binary notation for both encoding and decoding.

precision-and-rounding

Precision and rounding

As is always the case when working with floating point numbers, you should expect precision errors to appear rather quickly when using this datatype — especially since 32-bit floats are not especially precise, only reliable to about 7 digits worth. For example, you will almost always get different results if you encode and then immediately decode a float:

iex> 0.333 |> OSC.Types.Float.encode() |> OSC.Types.Float.decode()
{0.3330000042915344, ""}

This is also before you get into any precision errors introduced by the OSC-controlled hardware itself. For example, your mixing hardware might store a fader volume as an integer, performing float-to-int conversions on "set" operations and int-to-float conversions on "get" operations.

As such, you should always expect a lot of rounding when using the OSC float type, as well as a lot of extraneous digits (that you might want to consider discarding before displaying to the end user).

Link to this section Summary

Types

t()

An OSC float represented by an Elixir float

Functions

Decodes an OSC float to an Elixir float.

Encodes an Elixir float to an OSC float type.

Returns ?f, the type tag for the OSC float type

Link to this section Types

@type t() :: float()

An OSC float represented by an Elixir float

Link to this section Functions

Decodes an OSC float to an Elixir float.

Returns {float, rest} where float is a float decoded from the first four bytes of the input, and rest is a binary containing the remaining data.

examples

Examples

iex> <<63, 128, 0, 0>> |> OSC.Types.Float.decode()
{1.0, ""}

iex> <<68, 249, 184, 0, 234>> |> OSC.Types.Float.decode()
{1997.75, <<234>>}

Encodes an Elixir float to an OSC float type.

Returns a 32-bit binary-encoded float.

examples

Examples

iex> OSC.Types.Float.encode(0.3333)
<<62, 170, 166, 76>>

Returns ?f, the type tag for the OSC float type

iex> <<OSC.Types.Float.type_tag()>>
"f"