miredis/miredis.py

130 lines
3.1 KiB
Python

#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
miredis
v0.2
Forward midi events to OSC and redis.
by Sam Neurohack
from ProtonPhoton
"""
from libs import log
print()
log.infog('Miredis v0.2')
import argparse
import redis
from libs import midix
import traceback
from pathlib import Path
#myHostName = socket.gethostname()
#print("Name of the localhost is {}".format(myHostName))
#gstt.myIP = socket.gethostbyname(myHostName)
#print("IP address of the localhost is {}".format(gstt.myIP))
#gstt.myIP = "127.0.0.1"
#print('Used IP', gstt.myIP)
argsparser = argparse.ArgumentParser(description="Miredis")
# General Args
argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose")
# Redis Args
argsparser.add_argument("-i","--ip",help="Redis server IP address to forward midi events.",default="127.0.0.1",type=str)
argsparser.add_argument("-p","--port",help="Redis server port number",default="6379",type=str)
# OSC Args
argsparser.add_argument("-o","--oscip",help="OSC server IP address to forward midi events.",default="127.0.0.1",type=str)
argsparser.add_argument("-q","--oscport",help="OSC server port number",default="9000",type=str)
argsparser.add_argument('-link',help="Enable Ableton Link (disabled by default)", dest='link', action='store_true')
argsparser.add_argument("-m","--mode",help="Mode choice : simplex, clitools",default="clitools",type=str)
argsparser.set_defaults(link=False)
args = argsparser.parse_args()
redisIP = args.ip
redisPORT = args.port
midix.oscIP = args.oscip
midix.oscPORT = int(args.oscport)
midix.debug = args.verbose
midix.mode = args.mode
print("Destination OSC server : " +midix.oscIP+ " port "+str(midix.oscPORT))
r = redis.StrictRedis(host=redisIP , port=redisPORT, db=0)
print("Redis server : " +redisIP+ " port "+str(redisPORT))
midix.r = r
# with Ableton Link
if args.link == True:
from libs import alink
alink.Start()
linked = True
else:
print("Link DISABLED")
linked = False
def Osc():
p = r.pubsub()
p.subscribe('updates')
print("Artnet updates subscribed")
while True:
# Handle OSC based changed
OSCframe()
# Handle Artnet change via redis key 'updates' subscription
message = p.get_message()
if message:
messageCC = message['data']
# print(type(messageCC))
#print("Updates said: %s" % messageCC)
if messageCC != 1:
#if ":" in str(messageCC.decode('utf_8')):
messageCC = messageCC.decode('utf_8')
artnetCC = messageCC.split(":")
if len(artnetCC) > 1:
cc(int(artnetCC[0]), round(int(artnetCC[1])/2))
print()
#time.sleep(0.0)
if __name__ == '__main__':
import traceback
import time
midix.check()
midix.loadConf()
midix.toKey("/beats","0.0")
midix.toKey("/bpm",120)
try:
while True:
time.sleep(0.001)
if linked:
alink.BeatEvent()
except Exception:
traceback.print_exc()
#finally: