def encode_osc_data(type, data): if type == "i": value = ctypes.c_int32(ctypes.c_uint32(int(data)).value).value return osc_types.write_int(value) elif type == "f": return osc_types.write_float(float(data)) elif type == "s": return osc_types.write_string(data) elif type == "b": return osc_types.write_blob(data) else: raise RuntimeError("Unknown OSC type")
def build(self) -> osc_message.OscMessage: """Builds an OscMessage from the current state of this builder. Raises: - BuildError: if the message could not be build or if the address was empty. Returns: - an osc_message.OscMessage instance. """ if not self._address: raise BuildError('OSC addresses cannot be empty') dgram = b'' try: # Write the address. dgram += osc_types.write_string(self._address) if not self._args: dgram += osc_types.write_string(',') return osc_message.OscMessage(dgram) # Write the parameters. arg_types = "".join([arg[0] for arg in self._args]) dgram += osc_types.write_string(',' + arg_types) for arg_type, value in self._args: if arg_type == self.ARG_TYPE_STRING: dgram += osc_types.write_string(value) elif arg_type == self.ARG_TYPE_INT: dgram += osc_types.write_int(value) elif arg_type == self.ARG_TYPE_INT64: dgram += osc_types.write_int64(value) elif arg_type == self.ARG_TYPE_FLOAT: dgram += osc_types.write_float(value) elif arg_type == self.ARG_TYPE_DOUBLE: dgram += osc_types.write_double(value) elif arg_type == self.ARG_TYPE_BLOB: dgram += osc_types.write_blob(value) elif arg_type == self.ARG_TYPE_RGBA: dgram += osc_types.write_rgba(value) elif arg_type == self.ARG_TYPE_MIDI: dgram += osc_types.write_midi(value) elif arg_type in (self.ARG_TYPE_TRUE, self.ARG_TYPE_FALSE, self.ARG_TYPE_ARRAY_START, self.ARG_TYPE_ARRAY_STOP, self.ARG_TYPE_NIL): continue else: raise BuildError('Incorrect parameter type found {}'.format( arg_type)) return osc_message.OscMessage(dgram) except osc_types.BuildError as be: raise BuildError('Could not build the message: {}'.format(be))
def build(self): """Builds an OscMessage from the current state of this builder. Raises: - BuildError: if the message could not be build or if the address was empty. Returns: - an osc_message.OscMessage instance. """ if not self._address: raise BuildError('OSC addresses cannot be empty') dgram = b'' try: # Write the address. dgram += osc_types.write_string(self._address) if not self._args: dgram += osc_types.write_string(',') return osc_message.OscMessage(dgram) # Write the parameters. arg_types = "".join([arg[0] for arg in self._args]) dgram += osc_types.write_string(',' + arg_types) for arg_type, value in self._args: if arg_type == self.ARG_TYPE_STRING: dgram += osc_types.write_string(value) elif arg_type == self.ARG_TYPE_INT: dgram += osc_types.write_int(value) elif arg_type == self.ARG_TYPE_FLOAT: dgram += osc_types.write_float(value) elif arg_type == self.ARG_TYPE_DOUBLE: dgram += osc_types.write_double(value) elif arg_type == self.ARG_TYPE_BLOB: dgram += osc_types.write_blob(value) elif arg_type == self.ARG_TYPE_RGBA: dgram += osc_types.write_rgba(value) elif arg_type == self.ARG_TYPE_MIDI: dgram += osc_types.write_midi(value) elif arg_type in (self.ARG_TYPE_TRUE, self.ARG_TYPE_FALSE, self.ARG_TYPE_ARRAY_START, self.ARG_TYPE_ARRAY_STOP): continue else: raise BuildError('Incorrect parameter type found {}'.format( arg_type)) return osc_message.OscMessage(dgram) except osc_types.BuildError as be: raise BuildError('Could not build the message: {}'.format(be))
def build_dgram(self): """Builds an OscMessage datagram from the current state of this builder. Raises: - BuildError: if the message could not be build or if the address was empty. Returns: - an osc_message.OscMessage instance. """ if not self._address: raise BuildError('OSC addresses cannot be empty') dgram = b'' try: # Write the address. dgram += osc_types.write_string(self._address) if not self._args: dgram += osc_types.write_string(',') return osc_message.OscMessage(dgram) # Write the parameters. arg_types = "".join([arg[0] for arg in self._args]) dgram += osc_types.write_string(',' + arg_types) for arg_type, value in self._args: if arg_type == self.ARG_TYPE_STRING: dgram += osc_types.write_string(value) elif arg_type == self.ARG_TYPE_INT: dgram += osc_types.write_int(value) elif arg_type == self.ARG_TYPE_FLOAT: dgram += osc_types.write_float(value) elif arg_type == self.ARG_TYPE_BLOB: dgram += osc_types.write_blob(value) elif arg_type == self.ARG_TYPE_TRUE or arg_type == self.ARG_TYPE_FALSE: continue else: raise BuildError('Incorrect parameter type found {}'.format( arg_type)) return dgram except osc_types.BuildError as be: raise BuildError('Could not build the message: {}'.format(be))
def build(self): """Builds an OscMessage from the current state of this builder. Raises: - BuildError: if the message could not be build or if the address was empty. Returns: - an osc_message.OscMessage instance. """ if not self._address: raise BuildError("OSC addresses cannot be empty") dgram = b"" try: # Write the address. dgram += osc_types.write_string(self._address) if not self._args: return osc_message.OscMessage(dgram) # Write the parameters. arg_types = "".join([arg[0] for arg in self._args]) dgram += osc_types.write_string("," + arg_types) for arg_type, value in self._args: if arg_type == self.ARG_TYPE_STRING: dgram += osc_types.write_string(value) elif arg_type == self.ARG_TYPE_INT: dgram += osc_types.write_int(value) elif arg_type == self.ARG_TYPE_FLOAT: dgram += osc_types.write_float(value) elif arg_type == self.ARG_TYPE_BLOB: dgram += osc_types.write_blob(value) elif arg_type == self.ARG_TYPE_TRUE or arg_type == self.ARG_TYPE_FALSE: continue else: raise BuildError("Incorrect parameter type found {}".format(arg_type)) return osc_message.OscMessage(dgram) except osc_types.BuildError as be: raise BuildError("Could not build the message: {}".format(be))
def test_float(self): self.assertEqual(b'\x00\x00\x00\x00', osc_types.write_float(0.0)) self.assertEqual(b'?\x00\x00\x00', osc_types.write_float(0.5)) self.assertEqual(b'?\x80\x00\x00', osc_types.write_float(1.0)) self.assertEqual(b'?\x80\x00\x00', osc_types.write_float(1))