-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidi-mido-1.py
54 lines (41 loc) · 1.38 KB
/
midi-mido-1.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
# Import python modules
from mido.messages import Message
import fluidsynth
from time import sleep
# activate (instantiate) the fluidsynth object in python
fs = fluidsynth.Synth()
fs.start() # you may need to use 'start(driver=dsound)' driver in Windows or 'start(driver=alsa)' in linux
# locate the sf2 file and load into the fluidsynth object
sfid = fs.sfload(r'GeneralUser GS v1.471.sf2') # replace path as needed
# select a sound to play
fs.program_select(0, sfid, 0, 0)
# Make a midi Message object and call it msg
# This message will be a 'not on' type, with note number 60
msg = Message('note_on', note=50)
# Amend other Message Object parameters
msg.velocity = 90
# Print out the contents of the message object msg
print(msg)
# Parse the Message and play on fluidsynth
for t in range(10):
if msg.type == "note_on":
adjusted_note = msg.note + t
fs.noteon(msg.channel, adjusted_note, msg.velocity)
sleep(t / 10)
fs.noteoff(msg.channel, adjusted_note)
# Collapse the fs object
fs.delete()
"""
ALTERNATIVE TO FLUIDSYNTH
portname = "INSERT PORT NAME HERE"
with mido.open_output(portname, autoreset=True) as port:
print(f'Using {port}')
on = Message('note_on', note=note)
print(f'Sending {on}')
port.send(on)
time.sleep(0.05)
off = Message('note_off', note=note)
print(f'Sending {off}')
port.send(off)
time.sleep(0.1)
"""