def test_string(self): self.assertEqual(b'\x00\x00\x00\x00', osc_types.write_string('')) self.assertEqual(b'A\x00\x00\x00', osc_types.write_string('A')) self.assertEqual(b'AB\x00\x00', osc_types.write_string('AB')) self.assertEqual(b'ABC\x00', osc_types.write_string('ABC')) self.assertEqual(b'ABCD\x00\x00\x00\x00', osc_types.write_string('ABCD'))
def build_msg(self): msg_buffer = bytearray(self.__size) header_bytes = osc_types.write_string(self.__address) + \ osc_types.write_string(self.__type_tag) msg_buffer[0:len(header_bytes)] = header_bytes for key, elem in self.__data.items(): self.write_elem_in_buffer(elem, msg_buffer) return msg_buffer
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 set_elem(self, data_elem): is_new = data_elem.key not in self.__data if is_new: new_type_tag = self.__type_tag + data_elem.type_tag offset_diff = len(osc_types.write_string(new_type_tag)) - \ len(osc_types.write_string(self.__type_tag)) data_elem.offset = self.__size self.__data[data_elem.key] = data_elem self.__type_tag = new_type_tag self.__size += offset_diff + len(data_elem.encode()) for key, elem in self.__data.items(): elem.offset += offset_diff else: self.__data[data_elem.key].update(data_elem) return is_new
def osc_msg_from_np_arr(address, np_arr, id): msg_length = 3 + (np_arr.shape[0] * np_arr.shape[1]) dgram = b'' if len(address[0:1]) == 1: dgram += osc_types.write_string(address) arg_types = 'i' + ''.join(['f' for i in range(msg_length)]) dgram += unsafe_write_string(',' + arg_types) dgram += unsafe_write_int(id) dgram += unsafe_write_float(2) dgram += unsafe_write_float(np_arr.shape[0]) dgram += unsafe_write_float(np_arr.shape[1]) dgram += unsafe_write_floats(np_arr.flat, (np_arr.shape[0] * np_arr.shape[1])) return UnsafeOscMessage(dgram)
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_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 __init__(self, address): self.__address = address self.__type_tag = "," self.__size = len(osc_types.write_string(self.__address)) + \ len(osc_types.write_string(self.__type_tag)) self.__data = {}