midi: add virtual port support (linux only, requires mididings)

This commit is contained in:
jean-emmanuel 2017-02-25 18:19:20 +01:00
parent 4a19a39e2b
commit b3916151a7
2 changed files with 45 additions and 4 deletions

View file

@ -3,10 +3,33 @@
from sys import argv, stdin, stdout
import json as JSON
# python -> node communication
def ipcSend(data):
print(JSON.dumps(data))
stdout.flush()
# virtual midi port
if 'virtual' in argv:
try:
import mididings
except:
raise ImportError('virtual midi port requires python-mididings (linux only)')
from threading import Thread
mididings.config(client_name='Open Stage Control', in_ports=['VIRTUAL', 'OpenStageControl_In'], out_ports=['VIRTUAL', 'OpenStageControl_Out'])
t = Thread(target=mididings.run, args=[
[
mididings.PortFilter('VIRTUAL')>>mididings.Port('OpenStageControl_Out'),
mididings.PortFilter('OpenStageControl_In')>>mididings.Port('VIRTUAL')
]
])
t.start()
# pyo import and version check
try:
import pyo
except:
@ -21,10 +44,14 @@ if not pyo.withPortmidi():
raise ImportError('python-pyo must be built with portmidi support')
# get midi devices
_midiOutputs = pyo.pm_get_output_devices()
_midiInputs = pyo.pm_get_input_devices()
# list midi devices
if 'list' in argv:
message = []
@ -35,6 +62,9 @@ if 'list' in argv:
for k in range(len(_midiInputs[0])):
if 'OpenStageControl' in _midiInputs[0][k]:
continue
message.append('%3i: %s' % (_midiInputs[1][k], _midiInputs[0][k]))
message.append('============')
@ -43,6 +73,9 @@ if 'list' in argv:
for k in range(len(_midiOutputs[0])):
if 'OpenStageControl' in _midiOutputs[0][k]:
continue
message.append('%3i: %s' % (_midiOutputs[1][k], _midiOutputs[0][k]))
@ -105,6 +138,7 @@ class MidiRouter(object):
else:
return
if device in self.midiDevicesIn:
ipcSend({'osc':{
@ -152,6 +186,7 @@ class MidiRouter(object):
else:
return
if device in self.midiDevicesOut:
self.dispatcher.send(status, data1, data2, 0, self.midiDevicesOut[device])
@ -168,11 +203,13 @@ for arg in argv:
router = MidiRouter(patch)
# listen for osc->midi commands from node through stdin
while True:
msg = raw_input()
try:
msg = JSON.loads(msg)
msg[1] = msg[1].lower() # lower midi event type
msg[1] = msg[1].lower()
router.sendMidi(*msg)
except:
pass

View file

@ -14,8 +14,12 @@ When running the app, the `-m / -midi` switch must be set; it accepts the follow
- `list`: prints the available midi inputs / outputs
- `device_name:input,output`, where:
- `device_name`
- `input` is the midi input id (midi message sent to open-stage-control)
- `output` is the midi output id (midi message sent from open-stage-control)
- `input` is the midi input number (midi message sent to open-stage-control)
- `output` is the midi output number (midi message sent from open-stage-control)
**Linux only**
- `virtual`: (requires [`mididings`](http://das.nasophon.de/mididings/) to be installed) creates a virtual midi device. `VIRTUAL` ports number are to be used in the `device_name:input,output` declation only; ports named `OpenStageControl_In / OpenStageControl_Out` are to be connected to other ports with ALSA)
Multiple devices can be declared
@ -23,7 +27,7 @@ Multiple devices can be declared
In order to send midi messages, a widget must have at least one `target` formatted as follow;
`midi:device_name` (where `device_name` is one of the declared midi devices (see previous section))
`midi:device_name` (where `device_name` is one of the declared midi devices (see previous section))
## Supported commands