miredis/miredis.py

130 lines
3.1 KiB
Python
Raw Normal View History

2020-10-02 21:33:24 +00:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
miredis
2021-10-11 07:40:24 +00:00
v0.2
2020-10-02 21:33:24 +00:00
2021-04-10 13:21:26 +00:00
Forward midi events to OSC and redis.
2020-10-02 21:33:24 +00:00
by Sam Neurohack
2021-04-10 13:21:26 +00:00
from ProtonPhoton
2020-10-02 21:33:24 +00:00
"""
from libs import log
print()
2021-10-11 07:40:24 +00:00
log.infog('Miredis v0.2')
2020-10-02 21:33:24 +00:00
import argparse
import redis
from libs import midix
import traceback
from pathlib import Path
2020-10-02 21:33:24 +00:00
#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
2021-10-11 07:40:24 +00:00
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)
2020-10-02 21:33:24 +00:00
# OSC Args
2021-10-11 07:40:24 +00:00
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)
2020-10-06 13:37:34 +00:00
argsparser.add_argument('-link',help="Enable Ableton Link (disabled by default)", dest='link', action='store_true')
2020-10-24 23:22:42 +00:00
argsparser.add_argument("-m","--mode",help="Mode choice : simplex, clitools",default="clitools",type=str)
2020-10-06 13:37:34 +00:00
argsparser.set_defaults(link=False)
2020-10-02 21:33:24 +00:00
args = argsparser.parse_args()
redisIP = args.ip
redisPORT = args.port
midix.oscIP = args.oscip
midix.oscPORT = int(args.oscport)
midix.debug = args.verbose
2020-10-24 23:22:42 +00:00
midix.mode = args.mode
2020-10-02 21:33:24 +00:00
2021-10-11 07:40:24 +00:00
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
2020-10-06 13:37:34 +00:00
# with Ableton Link
if args.link == True:
from libs import alink
2020-10-02 21:33:24 +00:00
2020-10-06 13:37:34 +00:00
alink.Start()
linked = True
else:
print("Link DISABLED")
linked = False
2020-10-02 21:33:24 +00:00
2020-10-09 12:57:03 +00:00
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)
2020-10-02 21:33:24 +00:00
if __name__ == '__main__':
import traceback
import time
2020-10-09 12:57:03 +00:00
2020-10-02 21:33:24 +00:00
midix.check()
midix.loadConf()
2020-10-06 18:03:48 +00:00
midix.toKey("/beats","0.0")
midix.toKey("/bpm",120)
2020-10-02 21:33:24 +00:00
try:
while True:
2020-10-06 13:37:34 +00:00
time.sleep(0.001)
if linked:
alink.BeatEvent()
2020-10-02 21:33:24 +00:00
except Exception:
traceback.print_exc()
#finally:
2020-10-02 21:33:24 +00:00