-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmidi-Chord2-control-default.py
More file actions
78 lines (69 loc) · 2.23 KB
/
midi-Chord2-control-default.py
File metadata and controls
78 lines (69 loc) · 2.23 KB
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
# This program controls the QU-Bit Chord module.
import sys
import mido
import random
import time
import threading
from music_voicegen import MusicVoiceGen
def duration(x=1, y=4):
return random.randint(x, y)
def velo():
global velocity
return velocity.rand()
def play_chord(pitch, velocity=127, duration=1):
global outport
nv = velo()
msg = mido.Message('note_on', note=pitch, channel=0, velocity=nv)
outport.send(msg)
time.sleep(duration)
msg = mido.Message('note_off', note=pitch, channel=0, velocity=nv)
outport.send(msg)
def note_stream_thread():
global x, y, voice, stop_threads
while not stop_threads:
pitch = voice.rand()
play_chord(pitch, duration=duration(x, y))
if __name__ == "__main__":
pitches = [
12, # c0
14, # d
16, # e
17, # f
19, # g
21, # a
23, # b
]
voice = MusicVoiceGen(
pitches=pitches,
intervals=[-3,-2,-1,1,2,3],
)
velocity = MusicVoiceGen(
pitches=[ i for i in range(0,128) ],
intervals=[ i for i in range(-20,21) if (i != 0) and (i % 4 == 0) ],
)
velocity.context(context=[64]) # start in the middle of the range
# time between clocks at 24 PPQN per beat and 100 BPM
interval = 60 / (100 * 24)
stop_threads = False
port_name = sys.argv[1] if len(sys.argv) > 1 else 'MIDIThing2'
# note duration length range, between x and y inclusive
x = sys.argv[2] if len(sys.argv) > 2 else 4
y = sys.argv[3] if len(sys.argv) > 3 else 8
with mido.open_output(port_name) as outport:
print(outport)
note_thread = threading.Thread(target=note_stream_thread, daemon=True)
note_thread.start()
outport.send(mido.Message('start'))
try:
while True:
time.sleep(interval) # keep main thread alive and respond to interrupts
except KeyboardInterrupt:
outport.send(mido.Message('stop'))
print("\nSignaling threads to stop...")
stop_threads = True
note_thread.join()
print("All threads stopped.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
outport.close()