File: send.py

package info (click to toggle)
python-mido 1.3.3-0.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 920 kB
  • sloc: python: 4,006; makefile: 127; sh: 4
file content (44 lines) | stat: -rwxr-xr-x 886 bytes parent folder | download
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
#!/usr/bin/env python3

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

"""
Send random notes to the output port.
"""

import random
import sys
import time

import mido
from mido import Message

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

# A pentatonic scale
notes = [60, 62, 64, 67, 69, 72]

try:
    with mido.open_output(portname, autoreset=True) as port:
        print(f'Using {port}')
        while True:
            note = random.choice(notes)

            on = Message('note_on', note=note)
            print(f'Sending {on}')
            port.send(on)
            time.sleep(0.05)

            off = Message('note_off', note=note)
            print(f'Sending {off}')
            port.send(off)
            time.sleep(0.1)
except KeyboardInterrupt:
    pass

print()