Skip to content

Commit

Permalink
Update implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
segfault16 committed May 28, 2020
1 parent cba67d4 commit 6b29793
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,32 +899,26 @@ def strandTest(dev, num_pixels):
t = t + dt
time.sleep(dt)

def startMIDIThread():
def startMIDIThread(callback):
global midiThread
midiThread = threading.Thread(target=processMidi)
midiThread.start()
midiThread = threading.Thread(target=processMidi, args=([callback])).start()

def processMidi():
def processMidi(callback):
global stop_signal
global serverconfig
global proj
global midiCtrlPortOut
while not stop_signal:
for msg in midiCtrlPortIn.iter_pending():
handleMidiIn(msg)
outPortName = serverconfig.getConfiguration(serverconfiguration.CONFIG_MIDI_CTRL_PORT_OUT)
if outPortName is not None and (midiCtrlPortOut is None or midiCtrlPortOut.closed):
try:
midiCtrlPortOut = mido.open_output(outPortName)
except Exception as e:
logger.error("Error creating MIDI out port {}: {}".format(outPortName, e))
callback(msg)
time.sleep(0.01)

def handleMidiIn(msg: mido.Message):
global proj
global midiController
global serverconfig
global midiCtrlPortOut
# if serverconfig.getConfiguration(serverconfiguration.CONFIG_MIDI_CTRL_PORT_OUT) and (midiCtrlPortOut is None or midiCtrlPortOut.closed):
# try:
# midiCtrlPortOut = mido.open_output(serverconfig.getConfiguration(serverconfiguration.CONFIG_MIDI_CTRL_PORT_OUT))
# except Exception as e:
# logger.error(e)
for c in midiController:
c = c # type: midi_full.MidiProjectController
c.handleMidiMsg(msg, serverconfig, proj)


def handleMidiOut(msg: mido.Message):
global midiBluetooth
Expand Down Expand Up @@ -1043,7 +1037,10 @@ def handleMidiOut(msg: mido.Message):
from audioled_controller import bluetooth
fullMidiController = midi_full.MidiProjectController(callback=handleMidiOut)
midiController.append(fullMidiController)
midiBluetooth = bluetooth.MidiBluetoothService(callback=handleMidiIn, advertiseName=midiAdvertiseName)
midiBluetooth = bluetooth.MidiBluetoothService(
callback=lambda msg: fullMidiController.handleMidiMsg(msg, serverconfig, proj),
advertiseName=midiAdvertiseName
)
except Exception as e:
logger.warning("Ignoring Bluetooth error. Bluetooth not available on all plattforms")
logger.error(e)
Expand All @@ -1062,7 +1059,7 @@ def handleMidiOut(msg: mido.Message):
inPortName,
virtual=True)
logger.info("Added virtual MIDI port {}".format(inPortName))
startMIDIThread()
startMIDIThread(callback=lambda msg: fullMidiController.handleMidiMsg(msg, serverconfig, proj))
if outPortName:
try:
midiCtrlPortOut = mido.open_output(outPortName)
Expand All @@ -1072,10 +1069,11 @@ def handleMidiOut(msg: mido.Message):
logger.info("Virtual Port Controller is disabled")

if serverconfig.getConfiguration(serverconfiguration.CONFIG_GRPC_ENABLED):
logger.info("Creating GRPC server")
fullMidiController = midi_full.MidiProjectController(callback=handleMidiOut)
logger.info("Creating GRPC server for {}".format(fullMidiController))
midiController.append(fullMidiController)
server, midiGRPCService = grpc_server.create_server(handleMidiIn)
server, midiGRPCService = grpc_server.create_server(
callback=lambda msg: fullMidiController.handleMidiMsg(msg, serverconfig, proj))
server.add_insecure_port('localhost:5001')
server.start()
else:
Expand Down

0 comments on commit 6b29793

Please sign in to comment.