graphic-eq.py 2.2 KB

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