-
Notifications
You must be signed in to change notification settings - Fork 53
/
20.ex-midi-input.py
executable file
·56 lines (45 loc) · 1.61 KB
/
20.ex-midi-input.py
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
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
#------------------------------------------------------------------------
# MIDI input example.
# Listens for a sequence of notes.
# Press ctrl-c to stop listening, and the system will replay the input.
#------------------------------------------------------------------------
from isobar import *
import logging
import time
def main():
midi_in = MidiInputDevice()
notes = []
durations = []
last_note_time = None
print("Listening for notes on %s. Press Ctrl-C to stop." % midi_in.device_name)
try:
while True:
note = midi_in.receive()
if note is not None:
print(" - Read note: %s" % note.note)
notes.append(note)
if last_note_time is not None:
durations.append(time.time() - last_note_time)
last_note_time = time.time()
except KeyboardInterrupt:
pass
if last_note_time:
durations.append(time.time() - last_note_time)
print()
print("----------------------------------------------------")
print("Ctrl-C detected, now playing back")
print("----------------------------------------------------")
timeline = Timeline(60)
timeline.stop_when_done = True
timeline.schedule({
"note": PSequence([note.note for note in notes], 1),
"duration": PSequence(durations, 1)
})
timeline.run()
else:
print()
print("No notes detected")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(message)s")
main()