jamidi/client.py

214 lines
4.4 KiB
Python

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
NozoidUI Client v0.0.1
Input : local midi instruments
Output : nozoidtUI server
Websocket INSTALLER
https://files.pythonhosted.org/packages/8b/0f/52de51b9b450ed52694208ab952d5af6ebbcbce7f166a48784095d930d8c/websocket_client-0.57.0.tar.gz
'''
print ""
print ""
print "NozoidUI Client"
print "v0.0.1"
from multiprocessing import Process, Queue, TimeoutError
import subprocess
import sys
import traceback
import os
import time
from rtmidi.midiconstants import (CHANNEL_PRESSURE, CONTROLLER_CHANGE, NOTE_ON, NOTE_OFF,
PITCH_BEND, POLY_PRESSURE, PROGRAM_CHANGE)
import midi3
# from websocket_server import WebsocketServer
#import socket
import types, time
import websocket
try:
import thread
except ImportError:
import _thread as thread
import time
debug = 1
#
# webUI server
#
serverIP = "xrkia.org"
# serverIP = "127.0.0.1"
# serverIP = "10.8.0.46"
wsPORT = 8081
#
# Midi part
#
nozmidi = "BCR2000 Port 1"
# nozmidi = "Arturia BeatStep"
# nozmidi = "Virtual Midi A"
# nozmidi = "Virtual Sequencer"
# nowmidi = "IAC Driver Sequencer Bus 1"
# nozmidi = "UM-ONE:UM-ONE MIDI 1 20:0"
midichanOCS2 = 2
midichanMMO3 = 1
# resetMMO3 = [64,64,0,32,96] # un truc comme ca pour les valeurs de reset ?
resetMMO3 = [0] * 32
resetOCS2 = [0] * 32
# /cc cc number value
def cc(midichannel, ccnumber, value, mididest):
print "NozoidUI Sending Midi channel", midichannel, "cc", ccnumber, "value", value, "to", mididest
#if mididest == "BCR2000 Port 1":
midi3.MidiMsg([CONTROLLER_CHANGE+midichannel-1, ccnumber, value], mididest)
# /reset nozoids with "default" values
def reset(nozoid):
print ""
print "reseting", nozoid
if nozoid == "mmo3":
for ccnumber in xrange(0,32):
midi3.MidiMsg([CONTROLLER_CHANGE+midichanMMO3-1, ccnumber, resetMMO3[ccnumber]], nozmidi)
sendWSall("/mmo3/cc/"+str(ccnumber)+" "+str(resetMMO3[ccnumber]))
else:
for ccnumber in xrange(0,32):
midi3.MidiMsg([CONTROLLER_CHANGE+midichanOCS2-1, ccnumber, resetOCS2[ccnumber]], nozmidi)
sendWSall("/ocs2/cc/"+str(ccnumber)+" "+str(resetMMO3[ccnumber]))
#
# Websocket part
#
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
def on_open(ws):
def run(*args):
try:
while True:
time.sleep(1)
except Exception:
traceback.print_exc()
finally:
ws.close()
print("thread terminating...")
thread.start_new_thread(run, ())
def on_message(ws, message):
#
print ""
print(message)
if len(message) > 200:
message = message[:200]+'..'
oscpath = message.split(" ")
if debug > 0:
#print "Client got from WS", client['id'], "said :", message, "splitted in an oscpath :", oscpath
print "Client got from WS said :", message, "splitted in an oscpath :", oscpath
wscommand = oscpath[0].split("/")
print "WS command was :",wscommand
if len(oscpath) == 1:
args[0] = "noargs"
#print "noargs command"
elif wscommand[2] == "cc":
if wscommand[1] == "ocs2":
print "Incoming OCS-2 WS"
cc(midichanOCS2, int(wscommand[3]), int(oscpath[1]), nozmidi)
else:
print "Incoming MMO-3 WS"
cc(midichanMMO3, int(wscommand[3]), int(oscpath[1]), nozmidi)
elif wscommand[2] == "reset":
if wscommand[1] == "ocs2":
reset("ocs2")
else:
reset("mmo3")
# if needed a loop back : WS Client -> server -> WS Client
# sendWSall(message)
print "Running...."
# Main loop do nothing. Maybe do the webui server ?
try:
print ""
print "Connecting to NozoidUI server..."
print "at", serverIP, "port",wsPORT
#websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://"+str(serverIP)+":"+str(wsPORT),
on_message = on_message,
on_error = on_error,
on_close = on_close)
midi3.ws = ws
midi3.wsmode = True
print "Midi Configuration..."
print "Midi Destination", nozmidi
midi3.check()
ws.on_open = on_open
ws.run_forever()
except KeyboardInterrupt:
pass
# Gently stop on CTRL C
print "Fin de NozoidUI."