forked from jakubthedeveloper/RandomMidiNotesPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplay-random-notes.py
More file actions
46 lines (34 loc) · 1.12 KB
/
play-random-notes.py
File metadata and controls
46 lines (34 loc) · 1.12 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
import rtmidi
import random
import time
midiout = rtmidi.RtMidiOut()
ports = range(midiout.getPortCount())
if ports:
print("Available MIDI output ports:")
for i in ports:
print(str(i) + ': ' + midiout.getPortName(i))
else:
print('No MIDI output ports available')
exit()
midiPort = int(input("Select a port: "))
if (midiPort < 0 or midiPort > len(ports)):
print('Invalid port. Bye.')
exit()
midiChannel = int(input("Select MIDI Channel (default 1): "))
if (midiChannel < 0 or midiChannel > 16):
print('Invalid channel. Bye.')
exit()
midiout.openPort(midiPort)
try:
count = 100
while count > 0:
keyNumber = int(random.random() * 128)
velocity = int(random.random() * 128)
midiout.sendMessage(rtmidi.MidiMessage.noteOn(midiChannel, keyNumber, velocity))
ts = random.random() * 0.5
time.sleep(ts)
midiout.sendMessage(rtmidi.MidiMessage.noteOff(midiChannel, keyNumber))
count = count - 1
except KeyboardInterrupt:
midiout.sendMessage(rtmidi.MidiMessage.allNotesOff(midiChannel))
print("\nWhy did you stop?")