File: mido_connect.py

package info (click to toggle)
python-mido 1.3.3-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 920 kB
  • sloc: python: 4,006; makefile: 127; sh: 4
file content (48 lines) | stat: -rw-r--r-- 1,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3

# SPDX-FileCopyrightText: 2013 Ole Martin Bjorndalen <ombdalen@gmail.com>
#
# SPDX-License-Identifier: MIT

"""
Forward all messages from one or more ports to server.
"""
import argparse

import mido


def parse_args():
    parser = argparse.ArgumentParser(description=__doc__)
    arg = parser.add_argument

    arg('address',
        metavar='ADDRESS',
        help='host:port to connect to')

    arg('ports',
        metavar='PORT',
        nargs='+',
        help='input ports to listen to')

    return parser.parse_args()


def main():
    args = parse_args()

    try:
        hostname, port = mido.sockets.parse_address(args.address)
        ports = [mido.open_input(name) for name in args.ports]

        with mido.sockets.connect(hostname, port) as server_port:
            print('Connected.')
            for message in mido.ports.multi_receive(ports):
                print(f'Sending {message}')
                server_port.send(message)
    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    main()