forked from ideoforms/isobar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20.ex-midi-input.py
executable file
·51 lines (40 loc) · 1.34 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
#!/usr/bin/env python3
#------------------------------------------------------------------------
# MIDI input example
#------------------------------------------------------------------------
import isobar as iso
import logging
import time
logging.basicConfig(level=logging.INFO, format="[%(asctime)s] %(message)s")
midi_in = iso.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 = iso.Timeline(60)
timeline.stop_when_done = True
timeline.schedule({
"note": iso.PSequence([note.note for note in notes], 1),
"duration": iso.PSequence(durations, 1)
})
timeline.run()
else:
print()
print("No notes detected")