-
Notifications
You must be signed in to change notification settings - Fork 15
/
midi_devices.py
294 lines (230 loc) · 9.77 KB
/
midi_devices.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# Copyright (C) 2019 JPfeP
#
# ##### END GPL LICENSE BLOCK #####
import bpy
from bpy.utils import register_class, unregister_class
from bpy.app.handlers import persistent
from bpy_extras.io_utils import ImportHelper
import rtmidi
from rtmidi.midiutil import open_midiinput
from rtmidi.midiutil import open_midioutput
import platform
from . import g_vars
midi_in_list = []
midi_out_list = []
midi_in_sys_list = []
midi_out_sys_list = []
midi_in_pt = None
midi_out_pt = None
def update_midi_mode(self, context):
update_midi_in(self, context)
update_midi_out(self, context)
def update_midi_in(self, context):
global midi_in_pt
if g_vars.midi_update_inh:
return
prefs = bpy.context.preferences.addons['AddRoutes'].preferences
bcw = bpy.context.window_manager
if bcw.addroutes_midi_settings == 'Project':
bcw.addroutes_midi_in_device = bcw.addroutes_midi_in_enum
midiin_dev = bcw.addroutes_midi_in_device
else:
prefs.midi_in_device = bcw.addroutes_sys_midi_in_enum
midiin_dev = prefs.midi_in_device
try:
midi_in_pt = midiin_dev
set_midiin(None, None)
bcw.addroutes_midi_in_alert = False
except:
bcw.addroutes_midi_in_alert = True
def update_midi_out(self, context):
global midi_out_pt
if g_vars.midi_update_inh:
return
prefs = bpy.context.preferences.addons['AddRoutes'].preferences
bcw = bpy.context.window_manager
if bcw.addroutes_midi_settings == 'Project':
bcw.addroutes_midi_out_device = bcw.addroutes_midi_out_enum
midiout_dev = bcw.addroutes_midi_out_device
else:
prefs.midi_out_device = bcw.addroutes_sys_midi_out_enum
midiout_dev = prefs.midi_out_device
try:
midi_out_pt = midiout_dev
set_midiout(None, None)
bcw.addroutes_midi_out_alert = False
except:
bcw.addroutes_midi_out_alert = True
def set_midiin(self, context):
global midi_in_pt
port = midi_in_pt
if g_vars.midiin.is_port_open():
g_vars.midiin.close_port()
g_vars.midiin.delete()
g_vars.midiin = rtmidi.MidiIn()
if port != "None":
if port != "Virtual Port":
g_vars.midiin, portname = open_midiinput(port=port, interactive=False, client_name="Blender In")
else:
g_vars.midiin, portname = open_midiinput(use_virtual=True, interactive=False, client_name="Blender Virtual In" )
print("MIDI Input: " + portname)
def set_midiout(self, context):
global midi_out_pt
port = midi_out_pt
if g_vars.midiout.is_port_open():
g_vars.midiout.close_port()
g_vars.midiout.delete()
g_vars.midiout = rtmidi.MidiOut()
if port != "None":
if port != "Virtual Port":
g_vars.midiout, portname = open_midioutput(port=port, interactive=False, client_name="Blender Out")
else:
g_vars.midiout, portname = open_midioutput(use_virtual=True, interactive=False, client_name="Blender Virtual Out")
print("MIDI Output: " + portname)
def refresh_devices(self, context):
global midi_in_list, midi_out_list, midi_in_sys_list, midi_out_sys_list
bcw = bpy.context.window_manager
prefs = bpy.context.preferences.addons['AddRoutes'].preferences
# for input
m_in_ports = g_vars.midiin.get_ports()
targets = [(bcw.addroutes_midi_in_device, midi_in_list), (prefs.midi_in_device, midi_in_sys_list)]
for midiin_dev, mlist in targets:
mlist.clear()
if (midiin_dev in m_in_ports) is False and midiin_dev != "None" and midiin_dev != "Virtual Port" and midiin_dev !="":
a = (midiin_dev, midiin_dev, midiin_dev)
mlist.append(a)
b = ("None", "None", "None")
mlist.append(b)
if platform.system() != "Windows":
c = ("Virtual Port", "Virtual Port", "Virtual Port")
mlist.append(c)
for i in m_in_ports:
d = (i, i, i)
mlist.append(d)
# for output
m_out_ports = g_vars.midiout.get_ports()
targets = [(bcw.addroutes_midi_out_device, midi_out_list), (prefs.midi_out_device, midi_out_sys_list)]
for midiout_dev, moutlist in targets:
moutlist.clear()
if (midiout_dev in m_out_ports) is False and midiout_dev != "None" and midiout_dev != "Virtual Port" and midiout_dev != "":
a = (midiout_dev, midiout_dev, midiout_dev)
moutlist.append(a)
b = ("None", "None", "None")
moutlist.append(b)
c = ("Virtual Port", "Virtual Port", "Virtual Port")
moutlist.append(c)
for i in m_out_ports:
d = (i, i, i)
moutlist.append(d)
def refresh_midi_in_devices(self, context):
global midi_in_list
return midi_in_list
def refresh_midi_out_devices(self, context):
global midi_out_listm
return midi_out_list
def refresh_sys_midi_in_devices(self, context):
global midi_in_sys_list
return midi_in_sys_list
def refresh_sys_midi_out_devices(self, context):
global midi_out_sys_list
return midi_out_sys_list
class AddRoutes_RefreshDevices(bpy.types.Operator):
"""Refresh the list of MIDI devices"""
bl_idname = "addroutes.refresh_devices"
bl_label = "Refresh MIDI devices"
def execute(self, context):
bcw = bpy.context.window_manager
prefs = bpy.context.preferences.addons['AddRoutes'].preferences
refresh_devices(self, context)
g_vars.midi_update_inh = True
try:
bcw.addroutes_midi_in_enum = bcw.addroutes_midi_in_device
except:
pass
try:
bcw.addroutes_midi_out_enum = bcw.addroutes_midi_out_device
except:
pass
try:
bcw.addroutes_sys_midi_in_enum = prefs.midi_in_device
except:
pass
try:
bcw.addroutes_sys_midi_out_enum = prefs.midi_out_device
except:
pass
g_vars.midi_update_inh = False
update_midi_mode(self, context)
return{'FINISHED'}
class AddRoutes_StartMidi(bpy.types.Operator):
"""Start the MIDI service"""
bl_idname = "addroutes.start_midi"
bl_label = "Start the MIDI service"
def execute(self, context):
update_midi_in(self, context)
update_midi_out(self, context)
return{'FINISHED'}
cls = (
AddRoutes_RefreshDevices,
AddRoutes_StartMidi,
)
def register():
bpy.types.WindowManager.addroutes_midi_settings = bpy.props.EnumProperty(
name="MIDI configuration",
items=[('System', 'System', 'System', 0),
('Project', 'Project', 'Project', 1)],
update=update_midi_mode
)
bpy.types.WindowManager.addroutes_midi_in_device = bpy.props.StringProperty(default="None")
bpy.types.WindowManager.addroutes_midi_out_device = bpy.props.StringProperty(default="None")
bpy.types.WindowManager.addroutes_midi_in_enum = bpy.props.EnumProperty(name="MIDI In Ports",
items=refresh_midi_in_devices,
update=update_midi_in)
bpy.types.WindowManager.addroutes_midi_out_enum = bpy.props.EnumProperty(name="MIDI Out Ports",
items=refresh_midi_out_devices,
update=update_midi_out)
bpy.types.WindowManager.addroutes_sys_midi_in_enum = bpy.props.EnumProperty(name="MIDI In Ports",
items=refresh_sys_midi_in_devices,
update=update_midi_in)
bpy.types.WindowManager.addroutes_sys_midi_out_enum = bpy.props.EnumProperty(name="MIDI Out Ports",
items=refresh_sys_midi_out_devices,
update=update_midi_out)
bpy.types.WindowManager.addroutes_midi_in_alert = bpy.props.BoolProperty()
bpy.types.WindowManager.addroutes_midi_out_alert = bpy.props.BoolProperty()
bpy.types.WindowManager.addroutes_midi_debug = bpy.props.BoolProperty(
description='Debug in/out MIDI messages. Warning : Can be slow !',
update=g_vars.debugcopy
)
for c in cls:
register_class(c)
def unregister():
del bpy.types.WindowManager.addroutes_midi_debug
del bpy.types.WindowManager.addroutes_midi_in_alert
del bpy.types.WindowManager.addroutes_midi_out_alert
del bpy.types.WindowManager.addroutes_midi_out_device
del bpy.types.WindowManager.addroutes_midi_in_device
del bpy.types.WindowManager.addroutes_midi_settings
del bpy.types.WindowManager.addroutes_sys_midi_in_enum
del bpy.types.WindowManager.addroutes_sys_midi_out_enum
del bpy.types.WindowManager.addroutes_midi_in_enum
del bpy.types.WindowManager.addroutes_midi_out_enum
for c in cls:
unregister_class(c)
if __name__ == "__main__":
register()