jamidi/libs/WScom.py

187 lines
4.7 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
WScom for jamidi v0.1b
WScom.Start(serverIP, wsPORT)
WScom.runforever()
handler : message_received(client, wserver, message)
Launch :
print("Launching WS Server", serverIP,':', UDPORT)
WScom.Start(serverIP, wsPORT)
try:
WScom.runforever()
except Exception:
traceback.print_exc()
'''
import midi3
#import socket
import types, json
import _thread, time
from websocket_server import WebsocketServer
import gstt
import UDPcom, OSCom
#
# Server part
#
def Start(serverIP, wsPORT):
global wserver
wserver = WebsocketServer(wsPORT,host=serverIP)
midi3.ws = wserver
wserver.set_fn_new_client(new_client)
wserver.set_fn_client_left(client_left)
wserver.set_fn_message_received(message_received)
def runforever():
wserver.run_forever()
# Called for every WS client connecting (after handshake)
def new_client(client, wserver):
print(midi3.GetTime(),"New WS client connected and was given id %d" % client['id'])
#sendWSall("/status Hello %d" % client['id'])
if gstt.current == True:
sendallcurrentccvalues("mmo3")
sendallcurrentccvalues("ocs2")
gstt.Players+=1
sendWSall("/status Hello %d" %(client['id']))
if gstt.Players > 1:
#sendWSall("/gstt.Players %d" %(Players))
sendWSall("/players (players:%d)" %(gstt.Players))
else:
sendWSall("/players (player:%d)" %(gstt.Players))
# Called for every WS client disconnecting
def client_left(client, wserver):
try:
print(midi3.GetTime(),"WS Client(%d) disconnected" % client['id'])
gstt.Players-=1
sendWSall("/players %d" %(gstt.Players))
except:
print("Something weird if coming from",client,"on the wire...")
pass
# Called for each WS received message.
def message_received(client, wserver, message):
print("")
if len(message) > 200:
message = message[:200]+'..'
wspath = message.split(" ")
if gstt.debug > 0:
print(midi3.GetTime(),"Main got from WS", client['id'], "said :", message, "splitted in an wspath :", wspath)
else:
print(midi3.GetTime(),"Main got WS Client", client['id'], "said :", message)
wscommand = wspath[0].split("/")
# gstt.debug
if gstt.debug > 0:
print("wscommand :",wscommand)
# noarg
if len(wspath) == 1:
args[0] = "noargs"
#print "noargs command"
# CC : /device/cc/2 127
elif wscommand[2] == "cc":
ccvr=int(wscommand[3]) #cc variable
ccvl=int(wspath[1]) #cc value
if gstt.debug > 0:
print("ccvr=%d/ccvl=%d"%(ccvr,ccvl))
if wscommand[1] == "ocs2":
gstt.crtvalueOCS2[ccvr]=ccvl
else:
gstt.crtvalueMMO3[ccvr]=ccvl
for mididevice in midi3.findJamDevices(wscommand[1]):
midi3.cc(gstt.Confs[wscommand[1]][0]["midichan"], ccvr, ccvl, mididevice)
# RESET : /device/reset 1
elif wscommand[2] == "reset":
if wscommand[1] == "ocs2":
reset("ocs2")
else:
reset("mmo3")
# NOTEON : /device/noteon note velocity
elif wscommand[2] == "noteon":
for mididevice in midi3.findJamDevices(wscommand[1]):
midi3.NoteOn(int(wspath[1]), int(wspath[2]), mididevice)
#midi3.NoteOn(int(wspath[1]), int(wspath[2]), gstt.Confs[wscommand[1]][0]["mididevice"])
# NOTEOFF /device/noteoff note
elif wscommand[2] == "noteoff":
for mididevice in midi3.findJamDevices(wscommand[1]):
midi3.NoteOff(int(wspath[1]), mididevice)
#midi3.NoteOff(int(wspath[1]), gstt.Confs[wscommand[1]][0]["mididevice"])
# Loop back : WS Client -> server -> WS Client
sendWSall(message)
# Send through websocket.
# Different websocket library for client (websocket) or server (websocket_server.
# ws object is added here by main.py or client.py startup : midi3.ws =
def send(message):
if gstt.clientmode == True:
midi3.ws.send(message)
else:
wserver.send_message_to_all(msg = message)
def sendWSall(message):
if gstt.broadcast == True:
if gstt.debug >0:
print(midi3.GetTime(),"WS sending to all %s" % (message))
wserver.send_message_to_all(message)
# /send all current cc values
def sendallcurrentccvalues(nozoid):
if gstt.broadcast == True:
#print ""
print(midi3.GetTime(),"sending all current cc values of", nozoid)
if nozoid == "mmo3":
for ccnumber in range(0,32):
sendWSall("/mmo3/cc/"+str(ccnumber)+" "+str(gstt.crtvalueMMO3[ccnumber]))
else:
for ccnumber in range(0,32):
sendWSall("/ocs2/cc/"+str(ccnumber)+" "+str(gstt.crtvalueOCS2[ccnumber]))