You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redilysis_lines.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # -*- mode: Python -*-
  4. '''
  5. redilysis_lines
  6. v0.1.0
  7. Add a line on every frame and scroll
  8. see https://git.interhacker.space/teamlaser/redilysis for more informations
  9. about the redilysis project
  10. LICENCE : CC
  11. by cocoa
  12. '''
  13. from __future__ import print_function
  14. import argparse
  15. import ast
  16. import os
  17. import math
  18. import random
  19. import redis
  20. import sys
  21. import time
  22. name = "generator::redilysis_lines"
  23. def debug(*args, **kwargs):
  24. if( verbose == False ):
  25. return
  26. print(*args, file=sys.stderr, **kwargs)
  27. def msNow():
  28. return time.time()
  29. CHAOS = 1
  30. REDIS_FREQ = 33
  31. # General Args
  32. argsparser = argparse.ArgumentParser(description="Redilysis filter")
  33. argsparser.add_argument("-v","--verbose",action="store_true",help="Verbose")
  34. # Redis Args
  35. argsparser.add_argument("-i","--ip",help="IP address of the Redis server ",default="127.0.0.1",type=str)
  36. argsparser.add_argument("-p","--port",help="Port of the Redis server ",default="6379",type=str)
  37. argsparser.add_argument("-F","--redis-freq",help="Query Redis every x (in milliseconds). Default:{}".format(REDIS_FREQ),default=REDIS_FREQ,type=int)
  38. # General args
  39. argsparser.add_argument("-n","--nlines",help="number of lines on screen",default=60,type=int)
  40. argsparser.add_argument("-x","--centerX",help="geometrical center X position",default=400,type=int)
  41. argsparser.add_argument("-y","--centerY",help="geometrical center Y position",default=400,type=int)
  42. argsparser.add_argument("-W","--max-width",help="geometrical max width",default=800,type=int)
  43. argsparser.add_argument("-H","--max-height",help="geometrical max height",default=800,type=int)
  44. argsparser.add_argument("-f","--fps",help="Frame Per Second",default=30,type=int)
  45. args = argsparser.parse_args()
  46. verbose = args.verbose
  47. ip = args.ip
  48. port = args.port
  49. fps = args.fps
  50. centerX = args.centerX
  51. centerY = args.centerY
  52. redisFreq = args.redis_freq / 1000
  53. maxWidth = args.max_width
  54. maxHeight = args.max_height
  55. nlines = args.nlines
  56. optimal_looptime = 1 / fps
  57. redisKeys = ["spectrum_120","spectrum_10"]
  58. debug(name,"Redis Keys:{}".format(redisKeys))
  59. redisData = {}
  60. redisLastHit = msNow() - 99999
  61. r = redis.Redis(
  62. host=ip,
  63. port=port)
  64. white = 16777215
  65. lineList = []
  66. scroll_speed = int(maxHeight / nlines )
  67. line_length = int(maxWidth / 10)
  68. line_pattern = []
  69. def rgb2int(rgb):
  70. #debug(name,"::rgb2int rbg:{}".format(rgb))
  71. return int('0x%02x%02x%02x' % tuple(rgb),0)
  72. def spectrum_10( ):
  73. delList = []
  74. spectrum = ast.literal_eval(redisData["spectrum_10"])
  75. debug( name, "spectrum:{}".format(spectrum))
  76. # scroll lines
  77. for i,line in enumerate(lineList):
  78. skip_line = False
  79. new_y = int(line[0][1] + scroll_speed)
  80. if( new_y >= maxHeight ):
  81. debug(name,"{} > {}".format(new_y,maxHeight))
  82. debug(name,"delete:{}".format(i))
  83. delList.append(i)
  84. continue
  85. for j,point in enumerate(line):
  86. line[j][1] = new_y
  87. lineList[i] = line
  88. for i in delList:
  89. del lineList[i]
  90. # new line
  91. currentLine = []
  92. for i in range(0,10):
  93. x = int(i * line_length)
  94. y = 0
  95. # get frequency level
  96. level = spectrum[i]
  97. # get color
  98. comp = int(255*level)
  99. color = rgb2int( (comp,comp,comp))
  100. # new point
  101. currentLine.append( [x,y,color] )
  102. # add line to list
  103. lineList.append( currentLine)
  104. def refreshRedis():
  105. global redisLastHit
  106. global redisData
  107. # Skip if cache is sufficent
  108. diff = msNow() - redisLastHit
  109. if diff < redisFreq :
  110. #debug(name, "refreshRedis not updating redis, {} < {}".format(diff, redisFreq))
  111. pass
  112. else:
  113. #debug(name, "refreshRedis updating redis, {} > {}".format(diff, redisFreq))
  114. redisLastHit = msNow()
  115. for key in redisKeys:
  116. redisData[key] = r.get(key).decode('ascii')
  117. #debug(name,"refreshRedis key:{} value:{}".format(key,redisData[key]))
  118. # Only update the TTLs
  119. if 'bpm' in redisKeys:
  120. redisData['bpm_pttl'] = r.pttl('bpm')
  121. #debug(name,"refreshRedis key:bpm_ttl value:{}".format(redisData["bpm_pttl"]))
  122. #debug(name,"redisData:{}".format(redisData))
  123. return True
  124. def linelistToPoints( lineList ):
  125. pl = []
  126. for i,line in enumerate(lineList):
  127. # add a blank point
  128. pl.append([ line[0][0], line[0][1], 0 ])
  129. # append all the points of the line
  130. pl += line
  131. #debug(name,"pl:{}".format(pl))
  132. debug(name,"pl length:{}".format(len(pl)))
  133. return pl
  134. try:
  135. while True:
  136. refreshRedis()
  137. start = time.time()
  138. # Do the thing
  139. pointsList = spectrum_10()
  140. print( linelistToPoints(lineList), flush=True )
  141. looptime = time.time() - start
  142. # debug(name+" looptime:"+str(looptime))
  143. if( looptime < optimal_looptime ):
  144. time.sleep( optimal_looptime - looptime)
  145. # debug(name+" micro sleep:"+str( optimal_looptime - looptime))
  146. except EOFError:
  147. debug(name+" break")# no more information