Ejemplo n.º 1
0
class MessageSender:
    def __init__(self, port):
        self.client = SimpleUDPClient("127.0.0.1", port)

    def sendMessage(self, address, messages):
        print(f'sending message {address}: {messages}')
        self.client.send_message(address, messages)
Ejemplo n.º 2
0
def send_to_vuo(hand,
                host=settings.ONEHAND_VUO_HOST,
                port=settings.ONEHAND_VUO_PORT):
    osc_client = SimpleUDPClient(address=host, port=port)
    for finger, i, data in hand.finger_to_finger():
        osc_client.send_message(
            settings.ONEHAND_OSC_FINGER_ADDRESS.format(finger, i), data)
 def on_press(self, key):
     client = SimpleUDPClient(self.client_ip, self.client_port)
     if key == (KeyCode.from_char(chr(list(self.pitch.keys())[0]))):
         out = self.updatePitch(key_status='on')
         client.send_message("/outputs/" + out[0], out[1])
     elif key == (KeyCode.from_char(chr(list(self.modulation.keys())[0]))):
         out = self.updateModulation(key_status='on')
         client.send_message("/outputs/" + out[0], out[1])
Ejemplo n.º 4
0
class Board(object):
    def __init__(self, ip: str, port: int) -> None:
        self._ip = ip
        self._port = port
        self._client = SimpleUDPClient(self._ip, self._port)

    def _send_message(self, event, message: str, param: Any = None) -> None:
        if event.event_type == "up":
            self._client.send_message(message, param)
Ejemplo n.º 5
0
def abort():
    """
    Sends an abort message (make the arm stop running program and return to its init position) to the UDPClient.
    :return: JSON Indicating success (200)
    """
    address = "/Instructions"
    c = SimpleUDPClient('127.0.0.1', 5001)
    c.send_message(address, "___kill___")
    return json.dumps({'success': True}), 200, {
        'ContentType': 'application/json'
    }
Ejemplo n.º 6
0
class Osc:
    """Control switches via OSC."""
    def __init__(self, machine):
        """Initialise switch player."""
        self.log = logging.getLogger('OSC Plugin')
        self.machine = machine  # type: MachineController

        if 'osc_plugin' not in machine.config:
            machine.log.debug('"osc_plugin:" section not found in '
                              'machine configuration, so the OSC'
                              'plugin will not be used.')
            return

        if not Dispatcher:
            raise AssertionError(
                "To use the OSC plugin you need to install the pythonosc extension."
            )

        self.config = self.machine.config['osc_plugin']
        self.machine.config_validator.validate_config("osc_plugin",
                                                      self.config)
        if not self.config['enabled']:
            return

        self.dispatcher = Dispatcher()
        self.dispatcher.map("/sw/*", self.handle_switch)
        self.server = AsyncIOOSCUDPServer(
            (self.config['server_ip'], self.config['server_port']),
            self.dispatcher, self.machine.clock.loop)

        self.machine.events.add_async_handler("init_phase_5", self._start)

        self.client = SimpleUDPClient(self.config['client_ip'],
                                      self.config['client_port'])

    @asyncio.coroutine
    def _start(self):
        yield from self.server.create_serve_endpoint()
        self.machine.switch_controller.add_monitor(self._notify_switch_changes)

    def __repr__(self):
        """Return string representation."""
        return '<Osc>'

    def handle_switch(self, switch_name, state):
        """Handle Switch change from OSC."""
        self.machine.switch_controller.process_switch(switch_name,
                                                      bool(state),
                                                      logical=True)

    def _notify_switch_changes(self, change: MonitoredSwitchChange):
        """Send switch change to OSC client."""
        self.client.send_message("/sw/{}".format(change.name), change.state)
Ejemplo n.º 7
0
class Client(Node):
    """A simple OSC client."""
    def __init__(self, address="", ip="127.0.0.1", port=5005):
        if not address or not isinstance(address, str):
            raise ValueError("You must provide an address.")
        self._address = address
        self._client = SimpleUDPClient(ip, port)

    def update(self):
        if self.i.data is not None:
            for row in self.i.data.itertuples(index=False):
                self._client.send_message(self._address, row)
Ejemplo n.º 8
0
class OSCOutputDevice(OutputDevice):
    """
    OSCOutputDevice: Wraps MIDI messages in OSC.
    /note [ note, velocity, channel ]
    /control [ control, value, channel ]
    """
    def __init__(self, host, port):
        """
        Args:
            host: Hostname to send OSC messages to
            port: Port number to send OSC messages to
        """
        try:
            self.osc = SimpleUDPClient(host, port)

        except NameError:
            raise Exception("python-osc must be installed")

    def note_on(self, note=60, velocity=64, channel=0):
        self.osc.send_message("/note", velocity, channel)

    def note_off(self, note=60, channel=0):
        self.osc.send_message("/note", 0, channel)

    def control(self, control, value, channel=0):
        self.osc.send_message("/control", value, channel)

    def send(self, address, params=None):
        if params is not None:
            params = [Pattern.value(param) for param in params]
        self.osc.send_message(address, params)
Ejemplo n.º 9
0
class OSCOut:
    """ OSCOut: Wraps MIDI messages in OSC.
    /note [ note, velocity, channel ]
    /control [ control, value, channel ] """

    def __init__(self, host = "localhost", port = 7000):
        self.osc = SimpleUDPClient(host, port)

    def tick(self, tick_length):
        pass

    def note_on(self, note = 60, velocity = 64, channel = 0):
        msg = OSCMessage("/note")
        self.osc.send_message("/note", velocity, channel)

    def note_off(self, note = 60, channel = 0):
        self.osc.send_message("/note", 0, channel)

    def all_notes_off(self, channel = 0):
        for n in range(128):
            self.note_off(n, channel)

    def control(self, control, value, channel = 0):
        self.osc.send_message("/control", value, channel)

    def __destroy__(self):
        pass

    def send(self, address, params = None):
        self.osc.send_message(address, *params)
Ejemplo n.º 10
0
def osc_loop(arduino, lockduino):
    IP = "127.0.0.1"
    PORT = 9001

    # Representation of the last received data
    status = {
                1 : False,
                2 : False,
                "text" : None 
            }

    to_client = SimpleUDPClient(IP, PORT)

    while True:
        # Acquire lock on the Arduino Object
        with lockduino:
            # fetch data from the Arduino Object
            if arduino.is_pressed.get(1, False):
                status1 = arduino.is_pressed[1]
            if arduino.is_pressed.get(2, False):
                status2 = arduino.is_pressed[2]
            texture = arduino.texture

        # OSC: /ino/texture, textureID
        # if texture is different
        if texture != status["text"]:
            to_client.send_message("/ino/texture", texture)
            
            status["text"] = texture

        # OSC: /ino/step, [actionID, footID]
        # if 1 steps in
        if arduino.is_pressed.get(1, False):
            if status1 == True and status[1] == False:
                to_client.send_message("/ino/step", [1, 1])
            # if 1 steps out
            elif status1 == False and status[1] == True:
                to_client.send_message("/ino/step", [2, 1])

            status[1] = status1

        # if 2 steps in
        if arduino.is_pressed.get(2, False):
            if status2 == True and status[2] == False:
                to_client.send_message("/ino/step", [1, 2])
            # if 2 steps out
            elif status2 == False and status[2] == True:
                to_client.send_message("/ino/step", [2, 2])

            status[2] = status2
Ejemplo n.º 11
0
class OSCOut:
    """ OSCOut: Wraps MIDI messages in OSC.
    /note [ note, velocity, channel ]
    /control [ control, value, channel ] """
    def __init__(self, host="localhost", port=7000):
        self.osc = SimpleUDPClient(host, port)

    def tick(self, tick_length):
        pass

    def note_on(self, note=60, velocity=64, channel=0):
        msg = OSCMessage("/note")
        self.osc.send_message("/note", velocity, channel)

    def note_off(self, note=60, channel=0):
        self.osc.send_message("/note", 0, channel)

    def all_notes_off(self, channel=0):
        for n in range(128):
            self.note_off(n, channel)

    def control(self, control, value, channel=0):
        self.osc.send_message("/control", value, channel)

    def __destroy__(self):
        pass

    def send(self, address, params=None):
        self.osc.send_message(address, *params)
Ejemplo n.º 12
0
class OscSender():
    def __init__(self, sender_configs):
        """
        Constructor for OSC_SENDER CLASS

        :param ip: ip address of client ==> 127.0.0.1 (for local host/ inter app communication on same machine)
        :param sending_to_port: the port on which pure data listens for incoming data
        """

        self.sender_configs = sender_configs

        self.ip = self.sender_configs["ip"]
        self.sending_to_port = self.sender_configs["port"]

        self.playback_sequence_queue = self.sender_configs[
            "playback_sequence_queue"]

        self.client = SimpleUDPClient(self.ip, self.sending_to_port)

    def send_to_pd(self, message_parameters, message_values):
        """
        sends a list of messages to pd
        Note 1: Messages are sent in the same order as presented in the lists
        Note 2: ALWAYS USE LISTS EVEN IF SENDING A SINGLE PARAM/VALUE

        :param message_parameters: list of str: example ["/note/pitch", /note/duration"]
        :param message_values: list of ints, floats: example [53, 1000]
        """

        if len(message_parameters) != len(message_values):
            raise ValueError(
                "The number of message_types do not match the values")

        else:
            for ix, param in enumerate(message_parameters):
                self.client.send_message(param, message_values[ix])

    def get_ip(self):
        return self.ip

    def get_sending_to_port(self):
        return self.sending_to_port

    def get_client(self):
        return self.client

    def change_ip_port(self, ip, port):
        self.ip = ip
        self.sending_to_port = port
        self.client = SimpleUDPClient(self.ip, self.sending_to_port)
Ejemplo n.º 13
0
class OSCClient:
    def __init__(self, ip='127.0.0.1', port='1337'):
        '''
        Parameters:
        ------------
            :ip: ip adress, default = 127.0.0.1
            :port: port, default = 1337
        '''

        #initialization of values
        self.ip = ip
        self.port = int(port)

        try:
            #initialization of client
            self.client = SimpleUDPClient(self.ip, self.port)
        except Exception as e:
            print('Client could not be created \n {}'.format(e))

    #function to send values to the pure data server
    def sendValues(self, distance):
        '''
        Parameters:
        ------------
            :distance: distance between words to be sent to the osc server (Pure Data)
        '''

        # Send distances OSC messages
        try:
            self.client.send_message("/distance", distance)
        except Exception as e:
            print('Distances could not be sent: \n {}'.format(e))

        return

    #function to send trigger to the pure data server
    def sendTriggerValue(self, trigger):
        '''
        Parameters:
        ------------
            :trigger: trigger to be sent to server. 1 (start conversation, the OSC messaages will start to be sent), 0 (stop conversation, all the OSC messages have been sent) 
        '''
        #send trigger message
        try:
            self.client.send_message("/trigger", trigger)
        except Exception as e:
            print('Trigger message could not be sent: \n {}'.format(e))

        return
Ejemplo n.º 14
0
class OSCConsole(Console):
    def __init__(self, host, port, address):
        self.client = SimpleUDPClient(address=host, port=port)
        self.address = address
        self.line = ""

    def write(self, text):
        self.line += text
        if "\r\n" in self.line:
            self.line = self.line.replace("\r\n", "")
            print(self.line)
            self.osc_send(self.line)
            self.line = ""

    def osc_send(self, text):
        data = list(map(float, text.split("\t")))
        self.client.send_message(self.address, data)
Ejemplo n.º 15
0
def start_mixer():
    # Check if pe.audio.sys loudspeaker is a fullrange kind of
    if not check_fullrange():
        print(__doc__)
        sys.exit()
    # Stop Brutefir
    stop_brutefir()
    # Start JackMinix with 3 input channels
    cmd = f'jackminimix -p 9985 -c 3 -v'
    Popen(cmd.split())
    sleep(.5)
    # Attenuate all channel gains
    client = SimpleUDPClient(gethostname(), 9985)
    client.send_message('/mixer/channel/set_gain', [1, -40.0])
    client.send_message('/mixer/channel/set_gain', [2, -40.0])
    client.send_message('/mixer/channel/set_gain', [3, -40.0])
    # Connect the mixer output to the sound card
    jack_connect_bypattern('pre_in_loop', 'minimixer')
    jack_connect_bypattern('minimixer', 'system')
Ejemplo n.º 16
0
class ArduinoOscInput:
    def __init__(
        self,
        ip="127.0.0.1",
        osc_address="/arduino/sensor1",
        osc_port=51593,
        serial_device="/dev/tty.usbmodem14201",
        baud_rate=19200,
        datalog_file=None,
    ):
        self.osc_client_ip = ip
        self.osc_address = osc_address
        self.osc_port = osc_port
        self.serial_device = serial_device
        self.baud_rate = baud_rate

    def start(self):
        print(
            f"OSC UDP client sending to {self.osc_client_ip} on port {self.osc_port}, address: {self.osc_address}"
        )
        print(
            f"Connecting to serial at {self.serial_device} with baud {self.baud_rate}"
        )
        self.osc_client = SimpleUDPClient(self.osc_client_ip, self.osc_port)
        self.ser = serial.Serial(
            self.serial_device, baudrate=self.baud_rate, timeout=None
        )
        self.ser.flushInput()

        while True:
            try:
                ser_bytes = self.ser.readline()
                # print(ser_bytes)
                decoded_bytes = float(ser_bytes[0 : len(ser_bytes) - 2].decode("utf-8"))
                # Send a float through OSC
                self.osc_client.send_message(self.osc_address, decoded_bytes)
                print(decoded_bytes)
                # with open("test_data.csv","a") as f:
                #     writer = csv.writer(f,delimiter=",")
                #     writer.writerow([time.time(),decoded_bytes])
            except Exception as e:
                print(f"Keyboard interrupt!: {e}" )
                break
Ejemplo n.º 17
0
def oscSend(host, port, addr, args):
    print("out---- %s:%i" % (host, port))
    print("        %s %s" % (addr, "" if len(args) == 0 else " ".join(args))
          )  # not showing the list explicitly makes it easier to cut and paste
    try:
        k = ":".join([host, str(port)])
        if not k in clients:
            client = SimpleUDPClient(host, port)
            clients[k] = client
        else:
            client = clients[k]
        if not globalArgs.test:
            client.send_message(addr, args)
    except socket.gaierror as e:
        print("UDP socket error", e)
        if e.args[0] == socket.EAI_NONAME:
            print("Bad host: ", host)
    except Exception:
        print("UDP client general error")
        traceback.print_exc(file=sys.stdout)
Ejemplo n.º 18
0
def run_code():
    """
    receives python code Strings and sends them as message to the UDPClient.
    :return: JSON Indicating success (200)
    """
    code = request.json['code']

    # for debugging purpose
    print(request.data)
    print(request.json)
    print(code)

    ADDRESS = "/Instructions"
    c = SimpleUDPClient('127.0.0.1', 5001)

    # Can only pass up to 9000 characters.
    c.send_message(ADDRESS, code)
    c.send_message(ADDRESS, "xxx")

    return json.dumps({'success': True}), 200, {'ContentType': 'application/json'}
Ejemplo n.º 19
0
class OSCConnector(Connector):
    def __init__(self, backend, host, port):
        super().__init__(backend)

        self.client = SimpleUDPClient(host, port)

        self._osc_server_lock = threading.Lock()
        dispatcher = Dispatcher()
        dispatcher.map("/track/*/volume/str", self.osc_volume_handler)
        dispatcher.map("/track/*/mute/toggle", self.osc_mute_handler)
        #dispatcher.set_default_handler(self.osc_default_handler)
        self._osc_server = BlockingOSCUDPServer(("127.0.0.1", 8001),
                                                dispatcher)
        self._osc_server_thread = threading.Thread(
            target=self._osc_server.serve_forever)
        self._osc_server_thread.start()

    def recv(self, msg):
        if not isinstance(msg, AudioMessage):
            return

        if isinstance(msg, VolumeMessage):
            self.set_volume(msg.track, msg.volume)

        if isinstance(msg, MuteMessage):
            self.set_muted(msg.track, msg.muted)

    def stop(self):
        self._osc_server.shutdown()

    def send_refresh_request(self):
        print("Sending refresh request to REAPER...")
        self.client.send_message("/action", 41743)

    def osc_default_handler(self, address, *args):
        print("OSC default handler:", address, args)

    def osc_volume_handler(self, address, *args):
        #print(address, args)
        with self._osc_server_lock:
            try:
                track_number = int(address.split("/")[2])
                self.send_to_frontend(
                    VolumeMessage(track_number, float(args[0][:-2])))
            except:
                pass

    def osc_mute_handler(self, address, *args):
        with self._osc_server_lock:
            try:
                track_number = int(address.split("/")[2])
                self.send_to_frontend(MuteMessage(track_number, bool(args[0])))
            except:
                pass

    def set_volume(self, track_number, volume):
        self.client.send_message(f"/track/{track_number}/volume/db", volume)

    def set_muted(self, track_number, muted):
        self.client.send_message(f"/track/{track_number}/mute", int(muted))
Ejemplo n.º 20
0
def sendValues(distance):
    '''
	Parameters:
	------------
		:distance: distance between words to send to the osc server (Pure Data)
	'''

    #setting netwrok address IP and port
    ip = "127.0.0.1"
    port = 1337

    # Create client
    try:
        client = SimpleUDPClient(ip, port)
    except Exception as e:
        print('Client could not be created \n {}'.format(e))
        return
    print('Values that will be sent: {}\n'.format(distance))

    # Send array message
    client.send_message("/values", distance)

    return
Ejemplo n.º 21
0
def send_cave_to_vuo(hands,
                     duration,
                     host=settings.ONEHAND_VUO_HOST,
                     port=settings.ONEHAND_VUO_PORT):
    osc_client = SimpleUDPClient(address=host, port=port)

    osc_client.send_message(settings.ONEHAND_OSC_CAVE_HANDS_ADDRESS, [
        ";".join(
            map(str, [
                hand.uid, hand.position.x, hand.position.y, hand.size.w,
                hand.size.h, hand.rotation
            ])) for hand in hands
    ])

    osc_client.send_message(settings.ONEHAND_OSC_CAVE_START_ADDRESS, 1)
    osc_client.send_message(settings.ONEHAND_OSC_CAVE_FADE_ADDRESS, 1)
    time.sleep(duration)
    osc_client.send_message(settings.ONEHAND_OSC_CAVE_FADE_ADDRESS, 2)
    def send_genMIDI(self, ip, port):

        self.gen_MIDI_flag = True
        client = SimpleUDPClient(ip, port)

        sample_no = random.randint(21, 30)
        mid = mido.MidiFile('gen_midi_samples/sample_' + str(sample_no) +
                            '.mid')
        for msg in mid.play():
            if self.gen_MIDI_flag:
                if msg.type == 'note_on':
                    client.send_message("/outputs/gen_MIDI",
                                        [msg.note, msg.velocity])
                elif msg.type == 'note_off':
                    client.send_message("/outputs/gen_MIDI", [msg.note, 0])
        for i in range(128):
            client.send_message("/outputs/gen_MIDI", [i, 0])
        return None
Ejemplo n.º 23
0
class Query(LoggingObject):
    """ Object responsible for passing OSC queries to the LiveOSC server,
    parsing and proxying responses.

    This object is a singleton, under the assumption that only one Live instance
    can be running, so only one global Live Query object should be needed.

    Following this assumption, static helper functions also exist:

        live.query(path, *args)
        live.cmd(path, *args)
    """
    def __init__(self, address=("127.0.0.1", 9900), listen_port=9002):
        self.beat_callback = None
        self.startup_callback = None
        self.listen_port = listen_port

        #------------------------------------------------------------------------
        # Handler callbacks for particular messages from Live.
        # Used so that other processes can register callbacks when states change.
        #------------------------------------------------------------------------
        self.handlers = {}

        self.osc_address = address
        if OSC_BACKEND == 'liblo':
            self.osc_target = liblo.Address(address[0], address[1])
            self.osc_server = liblo.Server(listen_port)
            self.osc_server.add_method(None, None, self.handler)
            self.osc_server.add_bundle_handlers(self.start_bundle_handler,
                                                self.end_bundle_handler)

        elif OSC_BACKEND == 'pythonosc':
            # TODO how to deal w/ bundles? even necessary?
            # (the handlers seem to be just logging...)
            # (i think only some of the clip code refers to bundles at all)

            ip = address[0]
            self.osc_client = SimpleUDPClient(ip, address[1])

            self.dispatcher = Dispatcher()
            self.dispatcher.set_default_handler(self.pythonosc_handler_wrapper)

            # TODO TODO may need to take more care that this, or the other
            # pythonosc objects, actually close all of their connections before
            # exit / atexit
            # for some reason, maybe most likely something else, there seem to
            # be less frequent apparent "connection" issues with liblo than with
            # pythonosc...
            self.osc_server = ThreadingOSCUDPServer((ip, listen_port),
                                                    self.dispatcher)

        self.osc_server_thread = None

        self.osc_read_event = None
        self.osc_timeout = 3.0

        self.osc_server_events = {}

        self.query_address = None
        self.query_rv = []

        self.listen()

    def osc_server_read(self):
        assert OSC_BACKEND == 'liblo'
        while True:
            self.osc_server.recv(10)

    def listen(self):
        if OSC_BACKEND == 'liblo':
            target = self.osc_server_read
        elif OSC_BACKEND == 'pythonosc':
            target = self.osc_server.serve_forever

        self.osc_server_thread = threading.Thread(target=target)
        self.osc_server_thread.setDaemon(True)
        self.osc_server_thread.start()

    def stop(self):
        """ Terminate this query object and unbind from OSC listening. """
        pass

    def cmd(self, msg, *args):
        """ Send a Live command without expecting a response back:

            live.cmd("/live/tempo", 110.0) """

        self.log_debug("OSC output: %s %s", msg, args)
        try:
            if OSC_BACKEND == 'liblo':
                liblo.send(self.osc_target, msg, *args)

            elif OSC_BACKEND == 'pythonosc':
                # not clear on whether this unpacking in len(1) case in
                # necessary, just trying to make it look like examples in docs
                if len(args) == 1:
                    args = args[0]

                self.osc_client.send_message(msg, args)

        # TODO TODO need to modify pythonosc client call / handling so it will
        # also raise an error in this case? (probably)
        except Exception as e:
            self.log_debug(f"During cmd({msg}, {args})")
            raise LiveConnectionError(
                "Couldn't send message to Live (is LiveOSC present and activated?)"
            )

    # TODO maybe compute something like the average latency for a response to
    # arrive for a query (maybe weighted by recency) for debugging whether the
    # timeout is reasonable?
    # TODO + number of commands already processed / sent maybe + maybe log
    # whether particular commands always fail?

    def query(self, msg, *args, **kwargs):
        """ Send a Live command and synchronously wait for its response:

            return live.query("/live/tempo")

        Returns a list of values. """

        #------------------------------------------------------------------------
        # Use **kwargs because we want to be able to specify an optional kw
        # arg after variable-length args --
        # eg live.query("/set/freq", 440, 1.0, response_address = "/verify/freq")
        #
        # http://stackoverflow.com/questions/5940180/python-default-keyword-arguments-after-variable-length-positional-arguments
        #------------------------------------------------------------------------

        #------------------------------------------------------------------------
        # Some calls produce responses at different addresses
        # (eg /live/device -> /live/deviceall). Specify a response_address to
        # take account of this.
        #------------------------------------------------------------------------
        response_address = kwargs.get("response_address", None)
        if response_address:
            response_address = response_address
        else:
            response_address = msg

        #------------------------------------------------------------------------
        # Create an Event to block the thread until this response has been
        # triggered.
        #------------------------------------------------------------------------
        self.osc_server_events[response_address] = threading.Event()

        #------------------------------------------------------------------------
        # query_rv will be populated by the callback, storing the return value
        # of the OSC query.
        #------------------------------------------------------------------------
        self.query_address = response_address
        self.query_rv = []
        self.cmd(msg, *args)

        #------------------------------------------------------------------------
        # Wait for a response.
        #------------------------------------------------------------------------
        timeout = kwargs.get("timeout", self.osc_timeout)
        rv = self.osc_server_events[response_address].wait(timeout)

        if not rv:
            self.log_debug(f"Timeout during query({msg}, {args}, {kwargs})")
            # TODO could change error message to not question whether LiveOSC
            # is setup correctly if there has been any successful communication
            # so far...
            raise LiveConnectionError(
                "Timed out waiting for response from LiveOSC. Is Live running and LiveOSC installed?"
            )

        return self.query_rv

    # TODO maybe pythonosc.osc_bundle_builder / osc_message_builder could
    # replace some of what these are doing (in OSC_BACKEND == 'pythonosc' case)?
    # (though not clear these are critical...)
    def start_bundle_handler(self, *args):
        assert OSC_BACKEND == 'liblo'
        self.log_debug("OSC: start bundle")

    def end_bundle_handler(self, *args):
        assert OSC_BACKEND == 'liblo'
        self.log_debug("OSC: end bundle")

    def pythonosc_handler_wrapper(self, address, *args):
        assert OSC_BACKEND == 'pythonosc'
        # TODO may need to unwrap len(args) == 0 case or something like that
        self.handler(address, args, None)

    def handler(self, address, data, types):
        self.log_debug("OSC input: %s %s" % (address, data))

        #------------------------------------------------------------------------
        # Execute any callbacks that have been registered for this message
        #------------------------------------------------------------------------
        if address in self.handlers:
            for handler in self.handlers[address]:
                handler(*data)

        #------------------------------------------------------------------------
        # If this message is awaiting a synchronous return, trigger the
        # thread event and update our return value.
        #------------------------------------------------------------------------
        if address == self.query_address:
            self.query_rv += data
            self.osc_server_events[address].set()
            return

        if address == "/live/beat":
            if self.beat_callback is not None:
                #------------------------------------------------------------------------
                # Beat callbacks are used if we want to trigger an event on each beat,
                # to synchronise with the timing of the Live set.
                #
                # Callbacks may take one argument: the current beat count.
                # If not specified, call with 0 arguments.
                #------------------------------------------------------------------------
                has_arg = False
                try:
                    signature = inspect.signature(self.beat_callback)
                    has_arg = len(signature.parameters) > 0
                except:
                    # Python 2
                    argspec = inspect.getargspec(self.beat_callback)
                    has_arg = len(
                        argspec.args) > 0 and argspec.args[-1] != "self"

                if has_arg:
                    self.beat_callback(data[0])
                else:
                    self.beat_callback()

        elif address == "/remix/oscserver/startup":
            if self.startup_callback is not None:
                self.startup_callback()

    def add_handler(self, address, handler):
        if not address in self.handlers:
            self.handlers[address] = []
        self.handlers[address].append(handler)
Ejemplo n.º 24
0
from pythonosc.udp_client import SimpleUDPClient
from time import sleep

ip = "127.0.0.1"
port = 9998

print("Sending OSC messages...")

client = SimpleUDPClient(ip, port)

count = 0
while True:
    client.send_message("/some/address", count)
    count += 1
    if count >= 1000:
        count = 0
    sleep(0.01)
Ejemplo n.º 25
0
class OscSender(threading.Thread):
    def __init__(self, ip, sending_to_port, ticks_queue,
                 playback_sequence_queue):
        """
        Constructor for OSC_SENDER CLASS

        :param ip: ip address of client ==> 127.0.0.1 (for local host/ inter app communication on same machine)
        :param sending_to_port: the port on which pure data listens for incoming data
        """
        super(OscSender, self).__init__()
        self.setDaemon(True)

        self.ip = ip
        self.sending_to_port = sending_to_port

        self.ticks_queue = ticks_queue
        self.playback_sequence_queue = playback_sequence_queue

        self.client = SimpleUDPClient(self.ip, self.sending_to_port)

    def run(self):
        (start_tick, pitch, velocity, duration) = (None, None, None, None)
        while True:
            if start_tick is None:  # if no note to play, wait for a new note
                (start_tick, pitch, velocity,
                 duration) = self.playback_sequence_queue.get()
            else:  # if note available, wait for the correct tick time and send the note to pd
                current_tick = self.ticks_queue.get()
                if current_tick == start_tick:
                    self.send_to_pd(
                        ["/note/duration", "/note/velocity", "/note/pitch"],
                        [duration, velocity, pitch])
                    #print(f"note played at tick position {current_tick}  ")
                    (start_tick, pitch, velocity, duration) = (None, None,
                                                               None, None)
            #time.sleep(1)

    def send_to_pd(self, message_parameters, message_values):
        """
        sends a list of messages to pd
        Note 1: Messages are sent in the same order as presented in the lists
        Note 2: ALWAYS USE LISTS EVEN IF SENDING A SINGLE PARAM/VALUE

        :param message_parameters: list of str: example ["/note/pitch", /note/duration"]
        :param message_values: list of ints, floats: example [53, 1000]
        """

        if len(message_parameters) != len(message_values):
            raise ValueError(
                "The number of message_types do not match the values")

        else:
            for ix, param in enumerate(message_parameters):
                self.client.send_message(param, message_values[ix])

    def get_ip(self):
        return self.ip

    def get_sending_to_port(self):
        return self.sending_to_port

    def get_client(self):
        return self.client

    def change_ip_port(self, ip, port):
        self.ip = ip
        self.sending_to_port = port
        self.client = SimpleUDPClient(self.ip, self.sending_to_port)
Ejemplo n.º 26
0
 def send_music(self, input):
     client = SimpleUDPClient("127.0.0.1", 5005)
     client.send_message("/typing", input)
Ejemplo n.º 27
0
class UmanoService(object):
    help = "unknown umano service usage"
    option_list = [
        make_option(
            '--name',
            dest='NAME',
            type=str,
            default="",
            help='service name (default "unknown-<uuid>")'
        ),
        make_option(
            '--description',
            dest='DESCRIPTION',
            type=str,
            default="",
            help='service description (default "")'
        ),
    ]

    def __init__(self) -> None:
        super().__init__()
        self.name = "unknown-{}".format(str(uuid4())[:8])
        self.description = "no description"
        self.status = None
        self.status_document = None
        self.host = socket.gethostname()
        self.ip = socket.gethostbyname(self.host)
        self.status = STATUS.IDLE
        self.osc_client = SimpleUDPClient(settings.SUS_EST_VUO_MILLUMIN_HOST, settings.SUS_EST_VUO_MILLUMIN_PORT)

    def create_parser(self):
        parser = OptionParser(option_list=self.option_list)
        parser.set_usage(self.help)
        self.add_arguments(parser)
        return parser

    def add_arguments(self, parser):
        pass

    def run(self):
        parser = self.create_parser()
        options, args = parser.parse_args()

        if options.NAME and 'unknown' in self.name:
            self.name = options.NAME
        if options.DESCRIPTION:
            self.description = options.DESCRIPTION

        self.process_args_and_options(args, options)

        store(
            data_class='ServiceStart',
            data=dict(
                host=self.host,
                ip=self.ip,
                name=self.name,
                description=self.description
            )
        )

        try:
            self.execute(args, options)
        except KeyboardInterrupt:
            self.terminate()

    def terminate(self):
        self.set_status(STATUS.TERMINATED)
        print("Terminate service {} ".format(self.name))
        sys.exit(0)

    def execute(self, args, options):
        raise NotImplementedError()

    def print_help(self):
        parser = self.create_parser()
        parser.print_help()

    def set_status(self, status):
        if isinstance(status, str):
            status = STATUS[status]
        if self.status_document is None:
            self.status_document = store(
                data_class='ServiceStatus',
                data=dict(
                    host=self.host,
                    ip=self.ip,
                    name=self.name,
                    status=self.status.name,
                    description=self.description
                )
            )
        self.status = status
        self.status_document.status = self.status.name
        self.status_document.create_time = datetime.now()
        self.status_document.save()
        self.osc_client.send_message(settings.SERVICE_OSC_STATUS_ADDRESS.format(self.name), self.status.name)

    def process_args_and_options(self, args, options):
        pass

    def log(self, msg="", level=logging.INFO):
        logging.log(msg=msg, level=level)
        print(msg)
        self.osc_client.send_message(settings.SERVICE_OSC_LOG_ADDRESS.format(self.name), msg)
Ejemplo n.º 28
0
from pythonosc.udp_client import SimpleUDPClient

ip = "127.0.0.1"
port = 53000

client = SimpleUDPClient(ip, port)

client.send_message("/new", "text")
client.send_message("/cue/selected/text/format/color", [1, 1, 0, 1])

Ejemplo n.º 29
0
    if not len(args) == 2 or type(args[0]) is not float or type(
            args[1]) is not float:
        return

    # Check that address starts with filter
    if not address[:-1] == "/filter":  # Cut off the last character
        return

    value1 = args[0]
    value2 = args[1]
    filterno = address[-1]
    print(f"Setting filter {filterno} values: {value1}, {value2}")


dispatcher.map("/filter*",
               set_filter)  # Map wildcard address to set_filter function

# Set up server and client for testing
from pythonosc.osc_server import BlockingOSCUDPServer
from pythonosc.udp_client import SimpleUDPClient

server = BlockingOSCUDPServer(("127.0.0.1", 1337), dispatcher)
client = SimpleUDPClient("127.0.0.1", 1337)

# Send message and receive exactly one message (blocking)
client.send_message("/filter1", [1., 2.])
server.handle_request()

client.send_message("/filter8", [6., -2.])
server.handle_request()
Ejemplo n.º 30
0
from pythonosc.dispatcher import Dispatcher
from typing import List, Any

dispatcher = Dispatcher()


def set_data(address: str, *args: List[Any]) -> None:

    # Check that address starts with filter
    if not address[:-1] == "/hargrove":  # Cut off the last character
        return

    value1 = args[0]
    print(f"data: {value1}")


dispatcher.map("/hargrove*", set_data)

# Set up server and client for testing
#from pythonosc.osc_server import BlockingOSCUDPServer
from pythonosc.udp_client import SimpleUDPClient

client = SimpleUDPClient("10.0.0.3", 5001)

# Send message and receive exactly one message (blocking)
client.send_message("/hargrove1", 0.21)
Ejemplo n.º 31
0
def set_filter(address: str, *args: List[Any]) -> None:
    # We expect two float arguments
    if not len(args) == 2 or type(args[0]) is not float or type(args[1]) is not float:
        return

    # Check that address starts with filter
    if not address[:-1] == "/filter":  # Cut off the last character
        return

    value1 = args[0]
    value2 = args[1]
    filterno = address[-1]
    print(f"Setting filter {filterno} values: {value1}, {value2}")


dispatcher.map("/filter*", set_filter)  # Map wildcard address to set_filter function

# Set up server and client for testing
from pythonosc.osc_server import BlockingOSCUDPServer
from pythonosc.udp_client import SimpleUDPClient

server = BlockingOSCUDPServer(("127.0.0.1", 1337), dispatcher)
client = SimpleUDPClient("127.0.0.1", 1337)

# Send message and receive exactly one message (blocking)
client.send_message("/filter1", [1., 2.])
server.handle_request()

client.send_message("/filter8", [6., -2.])
server.handle_request()
Ejemplo n.º 32
0
        cv2.putText(orig_image, "<----> : {:.0f}pix".format(size),
                    (x1, y1 - 15), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 0, 0),
                    2)

        cv2.putText(orig_image,
                    "EAR (L,R): ({:.2f},{:.2f})".format(earL,
                                                        earR), (x1, y2 + 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 255, 255), 2)

        #Detectando e sinalizando a piscada
        setP = 0.26  #Quanto menor mais fechado o olho precisa estar
        #if earL < setP and earR < setP: #forma mais dura, exige os dois olhos fechados
        if earMed < setP:  #sinaliza pela média dos dois olhos
            #Desenha bolinha da piscada e envia msg OSC
            cv2.circle(orig_image, (cx, y1 + 30), 30, (0, 255, 0), -1)
            client.send_message("/piscou", (earL, earR))

        #função que percorre o array landmarks e carimba todos os pontos
        orig_image = drawLandmark_multiple(orig_image, new_bbox, landmark)

    sum += boxes.shape[0]

    #Redução para exibição, caso necessário
    orig_image = cv2.resize(orig_image, (0, 0), fx=0.7, fy=0.7)

    cv2.imshow('Detector Neural de Piscadas (Tecle [q] para sair)', orig_image)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()