jamidi/libs/UDPcom.py

185 lines
4.6 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
# -*- mode: Python -*-
'''
UDPcom for jamidi v0.1b
UDPcom.Start(serverIP, UDPORT)
Handler : udp_thread()
Launch :
print("Launching UDP Server", serverIP,':', wsPORT)
UDPcom.Start(serverIP, UDPORT)
Read below for :
- MIDI NOTES use ;nmonv
- MIDI CCs use ;cmnd
'''
import midi3
#import socket
import types, json
import socket
import _thread, time
import WScom, OSCom
import gstt
import time
def GetTime():
return time.strftime("%a, %d %b %Y %H:%M:%S", time.localtime())
#
# SERVER part
#
def udp_thread():
while True:
payload, client_address = sock.recvfrom(1024)
udpath = payload.decode('utf_8')
if gstt.debug > 1:
print(GetTime(),"UDP got", udpath, "from", str(client_address))
#print(udpath[0:1], " ",udpath[1:2], " ",udpath[2:3], " ",udpath[3:4], " " )
if udpath[0:1] == "n":
Note(udpath)
if udpath[0:1] == "c":
CC(udpath)
if udpath[0:1] == "f":
FullCC(udpath)
time.sleep(0.005)
def Start(serverIP, UDPORT):
global sock
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server = ( serverIP,UDPORT)
sock.bind(server)
_thread.start_new_thread(udp_thread, ())
'''
MIDI NOTES use ;nmonv
m : midi channel 0-F (0-15)
o : octave 0-8
n : Note A-G. For note with # : a-g
v : velocity 0-Z will output (v/36)*127
'''
def Note(udpnote):
if gstt.debug>0:
print()
print(GetTime(),'UDPNote from ORCA received', udpnote, udpnote[1:1])
midichannel = int(udpnote[1:2],36)
octave = int(udpnote[2:3],36)
note = udpnote[3:4]
velocity = (int(udpnote[4:5],36)/36)*127
#if gstt.debug>0:
print(GetTime(),'UDPNote from ORCA received:','midichannel:',midichannel,'octave:',octave,'note:',note,'velocity:',velocity)
#if octave < 9 or midichannel < 16 or int(note,36) < 10 or int(note,36) > 16:
if octave < 9 and midichannel < 16 and int(note,36) >= 10 and int(note,36) <= 16:
if note.istitle() == True:
notename = str(note.upper())+ str(octave)
else:
notename = str(note.upper())+ "#"+ str(octave)
if gstt.debug > 0:
print(GetTime(),"Incoming note", notename, "=", midi3.note2midi(notename), "velocity", velocity, "for channel", midichannel)
for mididevice in midi3.findJamDevices(gstt.oscname):
midi3.NoteOn(midi3.note2midi(notename), int(velocity), mididevice, midichannel-1)
# if sending note back to WS users :
#WScom.sendWSall("/"+midi3.findJamName(gstt.oscname, midichannel)+"/noteon "+str(midi3.note2midi(notename)))
else:
print(GetTime(),"Note", midichannel, octave, note, velocity,"had offchart parameters.")
'''
MIDI CCs base 36 use ;cmnd
m : midi channel 0-F (0-15)
n : number 0-Z (0-35)
d : data 0-Z will output (d/36)*127
'''
def CC(udpcc):
print()
midichannel = int(udpcc[1:2],36)
#midichannel = base36.index(udpcc[1:2].upper())
ccvr = int(udpcc[2:3],36)
ccvl = int((int(udpcc[3:4],36)/36)*127)
if midichannel < 16:
if gstt.debug > 0:
print(GetTime(),"ccvr=%d/ccvl=%d"%(ccvr,ccvl))
if gstt.oscname == "ocs2":
gstt.crtvalueOCS2[ccvr]=ccvl
else:
gstt.crtvalueMMO3[ccvr]=ccvl
for mididevice in midi3.findJamDevices(gstt.oscname):
midi3.cc(midichannel, ccvr, ccvl, mididevice)
#midi3.cc(gstt.Confs[gstt.oscname][0]["midichan"], ccvr, ccvl, mididevice)
WScom.sendWSall("/"+midi3.findJamName(mididevice, midichannel)+"/cc/"+str(ccvr)+" "+str(ccvl))
else:
print(GetTime(),"Bad midichannel")
'''
MIDI Full CCs all 128 channels and data use ;fmnndd
m : midi channel 0-F (0-15)
nn : number 0-3J (0-127)
dd : data 0-3J (0-127)
'''
def FullCC(udpcc):
print()
midichannel = int(udpcc[1:2],36)
#midichannel = base36.index(udpcc[1:2].upper())
ccvr = int(udpcc[2:4],36)
ccvl = int(udpcc[4:6],36)
if midichannel < 16:
if gstt.debug > 0:
print(GetTime(),"ccvr=%d/ccvl=%d"%(ccvr,ccvl))
if gstt.oscname == "ocs2":
gstt.crtvalueOCS2[ccvr]=ccvl
else:
gstt.crtvalueMMO3[ccvr]=ccvl
for mididevice in midi3.findJamDevices(gstt.oscname):
midi3.cc(midichannel, ccvr, ccvl, mididevice)
WScom.sendWSall("/"+midi3.findJamName(mididevice, midichannel)+"/cc/"+str(ccvr)+" "+str(ccvl))
else:
print(GetTime(),"Bad midichannel")