graphic-eq.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from connection import *
  2. from audioHandler import Listener
  3. import numpy as np
  4. import time
  5. import math
  6. import sys, getopt
  7. framerate = 40 #Hz
  8. dataTime = 1/30
  9. agcTime = 10
  10. graphs = True
  11. outDev = True
  12. port = None
  13. try:
  14. opts, args = getopt.getopt(sys.argv[1:],"hsif:t:a:w:",["shadow=","input=","framerate=","time=","agc=","port="])
  15. except getopt.GetoptError:
  16. print('main.py -s (shadow) -i (use input) -f --framerate <f=40>\n -t --time <t=0.033> -a --agc <a=10> -p --port <COM3>')
  17. sys.exit(2)
  18. for opt, arg in opts:
  19. if opt == '-h':
  20. print('main.py -s (shadow) -i (use input) -f --framerate <f=40>\n -t --time <t=0.033> -a --agc <a=10> -p --port <COM3>')
  21. sys.exit()
  22. elif opt in ("-s", "--shadow"):
  23. graphs = False
  24. elif opt in ("-i", "--input"):
  25. outDev = False
  26. elif opt in ("-f", "--framerate"):
  27. framerate = float(arg)
  28. elif opt in ("-a", "--agc"):
  29. agcTime = float(arg)
  30. elif opt in ("-t", "--time"):
  31. dataTime = float(arg)
  32. elif opt in ("-p", "--port"):
  33. port = arg
  34. matrix = SerialConnection()
  35. matrix.open(port)
  36. pitches = [(2**(1/12))**(n/11*12*7-30) * 220 for n in range(11)]
  37. audio = Listener(dataTime, agcTime, not outDev)
  38. audio.start()
  39. nFFT = audio.buffersize
  40. audio.fftSetLimits(nFFT, min(pitches), max(pitches))
  41. audio.agcFftSetLimits(60/60, 300/60)
  42. curVal = [0] * N_COLS
  43. maxVolRow = [0] * N_COLS
  44. maxVolAge = [0] * N_COLS
  45. intervalTime = time.time()
  46. data = [0] * 50
  47. while True:
  48. if audio.hasNewData():
  49. fft = np.copy(audio.fft)
  50. sums = audio.fftGroup(fft, pitches)
  51. data = [0] * 50
  52. for col in range(N_COLS):
  53. curVal[col] = max(sums[col] * len(sums) * 1.5 * 1.1**col, curVal[col]*0.80)
  54. curRow = max(N_ROWS - int(curVal[col]), 0)
  55. if curRow <= maxVolRow[col]:
  56. maxVolRow[col] = curRow
  57. maxVolAge[col] = 60
  58. elif maxVolAge[col] > 0:
  59. maxVolAge[col] -= 1
  60. else:
  61. maxVolRow[col] = N_ROWS
  62. if maxVolRow[col] < N_ROWS:
  63. data[maxVolRow[col]*N_COLS+col] = int(maxVolAge[col]/10)
  64. if curRow < N_ROWS:
  65. for row in range(curRow, N_ROWS):
  66. data[row*N_COLS+col] = 15-curRow-row
  67. data[49] = int(data[49]/2)
  68. matrix.send(b'A' + bytes(data))
  69. time.sleep(1/framerate)
  70. if time.time() - intervalTime > 60:
  71. if not matrix.isConnected():
  72. print("lost serial connection, retrying to connect...")
  73. matrix.connect()
  74. audio.stop()
  75. audio.start()
  76. intervalTime = time.time()