File: input_filter.py

package info (click to toggle)
python-mido 1.2.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 852 kB
  • sloc: python: 3,753; makefile: 136; sh: 4
file content (37 lines) | stat: -rwxr-xr-x 860 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
#!/usr/bin/python3
"""
Receive messages on the input port and print messages with a specific
channel (defaults to 0).

Usage:

    python example_input_filter.py portname [CHANNEL] [...]
"""
from __future__ import print_function
import sys
import mido


def accept_notes(port):
    """Only let note_on and note_off messages through."""
    for message in port:
        if message.type in ('note_on', 'note_off'):
            yield message


if len(sys.argv) > 1:
    portname = sys.argv[1]
else:
    portname = None   # Use default port

try:
    with mido.open_input(portname) as port:
        print('Using {}'.format(port))

        print("Ignoring everything but 'note_on' and 'note_off'.")
        print('Waiting for notes...')

        for message in accept_notes(port):
            print('Received {}'.format(message))
except KeyboardInterrupt:
    pass