-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiclock.py
210 lines (161 loc) · 4.95 KB
/
midiclock.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import sys
import math
import collections
import os
import signal
import play
import time
import thread_ex
import multiprocessing
from collections import deque
from rtmidi.midiconstants import NOTE_OFF, NOTE_ON
import theory
from rtmidi.midiconstants import (TIMING_CLOCK, SONG_CONTINUE, SONG_START, SONG_STOP)
import rtmidi
from rtmidi.midiutil import open_midiinput
def signal_handler(sig, frame):
print('cleanup')
clear()
# m_in.close_port()
# del m_in
sys.exit(0)
def clear():
global event_q
notes_off = [[NOTE_OFF, i, 0] for i in range(128)]
for off in notes_off:
event_q.put(off)
# unplay([i for i in range(128)])
# def unplay(chord_notes):
# notes_off = [[NOTE_OFF, i, 0] for i in chord_notes]
# for off in notes_off:
# midiout.send_message(off)
#
# def play(chord_notes, v=100):
# notes = [[NOTE_ON, i, v] for i in chord_notes]
# for note in notes:
# midiout.send_message(note)
bpm = 120.0
sync = False
running = True
samples = deque(maxlen=24)
last_clock = None
# threads = []
procs = []
event_q = multiprocessing.Queue()
def print_tracks(tracks):
w,_ = os.get_terminal_size()
for track in tracks:
notes_dict = collections.defaultdict(list)
for notes, start_time in track:
a = '-'.join(map(lambda e: theory.note_to_letter(e[1]), notes))
notes_dict[a].append(start_time)
m = 0
mn = 0
for k, v in notes_dict.items():
mn = max(mn, len(k))
m = max(m, *v)
def lerp(a, b, c):
# return math.floor((a / b) * c)
return int((a / b) * c)
w2 = w - 1 - mn - 1
print(''.ljust(mn), '|', sep='',end='')
for i in range(1,int(m / 4)):
print(' ' * lerp(4,m, w2), '|', sep='', end='')
print()
for k, v in notes_dict.items():
i = iter(v)
pairs = []
for start in i:
stop = next(i)
pairs.append((start, stop))
print(k.ljust(mn), '|', sep='',end='')
for start, stop in pairs:
start_i = lerp(start, m, w2)
stop_i = lerp(stop, m, w2)
print(
# (start, start_i, stop, stop_i, m, w2),
' ' * (start_i), '[', '-'*(stop_i - start_i - 1), ']', sep='', end='')
print()
print()
def on_start(event_q):
print(time.time(), 'start')
base = 65
while True:
scale = theory.major_scale(base)
progression = [
(1, theory.minor_seventh_chord),
(4, theory.minor_seventh_chord),
(5, theory.minor_seventh_chord),
(1, theory.minor_seventh_chord),
]
chords = [f(scale[i - 1]) for i, f in progression]
scale = theory.major_scale(base + 7)
chords_2 = [f(scale[i - 1]) for i, f in progression]
tracks = [
[
*play.space(chords, 1, 100),
*play.space(chords_2, 1, 100, start=16),
],
# [
# *play.space(chords_2, 2, 100),
# ]
]
tracks = [sorted(track, key=lambda e: e[1]) for track in tracks]
print_tracks(tracks)
play.play_midi_events(tracks, bpm, event_q)
def on_stop():
print(time.time(), 'stop')
clear()
def send_events(q):
midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()
if available_ports:
midiout.open_port(0)
else:
midiout.open_virtual_port("My virtual output")
while True:
event = q.get()
if event[0] != 128:
print('new event', event)
midiout.send_message(event)
# play.play_midi_events(midiout)
def kill_procs(procs):
print('killing procs', procs)
for p in procs:
p.terminate()
def cb(event, _):
global last_clock, bpm, sync, running, samples, procs, event_q
msg, _ = event
multiprocessing.Process(target = send_events, args = (event_q,)).start()
if msg[0] == TIMING_CLOCK:
now = time.time()
if last_clock is not None:
samples.append(now - last_clock)
last_clock = now
if len(samples) >= 2:
bpm = 2.5 / (sum(samples) / len(samples))
sync = True
elif msg[0] in (SONG_CONTINUE, SONG_START):
kill_procs(procs)
running = True
p = thread_ex.thread_with_exception(on_start)
p = multiprocessing.Process(target=on_start, args=(event_q, ))
p.start()
procs.append(p)
elif msg[0] == SONG_STOP:
running = False
kill_procs(procs)
on_stop()
def main():
try:
m_in, _ = open_midiinput(use_virtual=True)
except (EOFError, KeyboardInterrupt):
return 1
m_in.set_callback(cb)
m_in.ignore_types(timing=False)
signal.signal(signal.SIGINT, signal_handler)
print("Waiting for clock sync...")
while True:
pass
if __name__ == '__main__':
main()