-
Notifications
You must be signed in to change notification settings - Fork 0
/
midi.py
70 lines (53 loc) · 1.82 KB
/
midi.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
#!/usr/bin/env python3
# coding: utf-8
# Midi
from mido import Message, open_output, get_output_names
from time import sleep
#from IPython import embed
# Config
import config
NOTAS = ['C', 'Csharp', 'D', 'Dsharp', 'E', 'F', 'Fsharp', 'G', 'Gsharp', 'A', 'Asharp', 'B']
ACORDES = {
'maj': [0, 4, 7],
'maj-inv': [-5, 0, 4], # una inversion mas "abierta"
'min': [0, 3, 7],
'min-inv': [-5, 0, 3],
'sus2': [0, 2, 7],
'sus2-inv': [-5, 0, 2],
'sus4': [0, 5, 7],
'sus4-inv': [-5, 0, 5],
}
DEVICE = None
DEVICES_NAMES = get_output_names()
DEVICES_LIST = ["%d: %s" % (DEVICES_NAMES.index(x), x) for x in DEVICES_NAMES]
def load():
""" See if requested MIDI DEVICE is available """
global DEVICE
if not config.CONFIG['midi-device']['name'] in DEVICES_NAMES:
print("MIDI DEVICE NOT AVAILABLE (%s)" % config.CONFIG['midi-device']['name'])
return False
DEVICE = open_output(config.CONFIG['midi-device']['name'])
return True
def play_chord(midi_out, root_note, chord, sustain):
""" Plays chords (multiple notes) """
notes = [root_note+x for x in chord]
for note in notes:
msg = Message('note_on', note=note, velocity=127)
midi_out.send(msg)
sleep(sustain)
midi_out.reset()
""" # no anda!
for n in notes:
Message('note_off', note=n, velocity=127)
midi_out.send(msg)
"""
def play_note(midi_out, channel, note, velocity, sustain):
""" Plays notes """
msg_on = Message('note_on', channel=channel, note=note, velocity=velocity)
msg_off = Message('note_off', channel=channel, note=note, velocity=velocity)
#msg_on = Message('note_on', note=note, velocity=velocity)
#msg_off = Message('note_off', note=note, velocity=velocity)
#embed()
midi_out.send(msg_on)
sleep(sustain)
midi_out.send(msg_off)