示例#1
0
    def write_file(
        cls,
        messages: Dict[float, List[OSCMessage]],
        path: Union[str, bytes, os.PathLike],
        tempo: float = 1,
    ):
        """Write this score as binary OSC file for NRT synthesis.

        Parameters
        ----------
        messages : Dict[float, List[OSCMessage]]
            Dict with times as key and lists of OSC messages as values
        path : Union[str, bytes, os.PathLike]
            output path for the binary OSC file
        tempo : float
            Times will be multiplied by 1/tempo
        """
        tempo_factor = tempo / 1
        with open(path, "wb") as file:
            msg = None
            for timetag, msgs in messages.items():
                builder = OscBundleBuilder((timetag * tempo_factor) -
                                           2208988800.0)
                for msg in msgs:
                    builder.add_content(msg.to_pythonosc())
                dgram = builder.build().dgram
                file.write(osc_types.write_int(len(dgram)))
                file.write(dgram)
            if msg and (msg.address != "/c_set" or msg.parameters != [0, 0]):
                warnings.warn(
                    "Missing /c_set [0, 0] at the end of the messages. "
                    "Recording will stop with last timetag")
示例#2
0
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))
示例#4
0
  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))
示例#5
0
  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):
		"""Build an OscBundle with the current state of this builder.

		Raises:
			- BuildError: if we could not build the bundle.
		"""
		dgram = b'#bundle\x00'
		try:
			dgram += osc_types.write_date(self._timestamp)
			for content in self._contents:
				if (type(content) == osc_message.OscMessage
						or type(content) == osc_bundle.OscBundle):
					size = content.size
					dgram += osc_types.write_int(size)
					dgram += content.dgram
				else:
					raise BuildError(
							"Content must be either OscBundle or OscMessage"
							"found {}".format(type(content)))
			return osc_bundle.OscBundle(dgram)
		except osc_types.BuildError as be:
			raise BuildError('Could not build the bundle {}'.format(be))
  def build(self):
    """Build an OscBundle with the current state of this builder.

    Raises:
      - BuildError: if we could not build the bundle.
    """
    dgram = b'#bundle\x00'
    try:
      dgram += osc_types.write_date(self._timestamp)
      for content in self._contents:
        if (type(content) == osc_message.OscMessage
            or type(content) == osc_bundle.OscBundle):
          size = content.size
          dgram += osc_types.write_int(size)
          dgram += content.dgram
        else:
          raise BuildError(
              "Content must be either OscBundle or OscMessage"
              "found {}".format(type(content)))
      return osc_bundle.OscBundle(dgram)
    except osc_types.BuildError as be:
      raise BuildError('Could not build the bundle {}'.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))
示例#9
0
 def test_int(self):
   self.assertEqual(b'\x00\x00\x00\x00', osc_types.write_int(0))
   self.assertEqual(b'\x00\x00\x00\x01', osc_types.write_int(1))
示例#10
0
 def test_int(self):
     self.assertEqual(b'\x00\x00\x00\x00', osc_types.write_int(0))
     self.assertEqual(b'\x00\x00\x00\x01', osc_types.write_int(1))