|
@@ -2,28 +2,84 @@ from connection import *
|
|
|
from audioHandler import Listener
|
|
|
import numpy as np
|
|
|
import time
|
|
|
+import math
|
|
|
+import sys, getopt
|
|
|
+
|
|
|
+framerate = 60 #Hz
|
|
|
+dataTime = 1/30
|
|
|
+graphs = True
|
|
|
+outDev = True
|
|
|
+port = None
|
|
|
+
|
|
|
+try:
|
|
|
+ opts, args = getopt.getopt(sys.argv[1:],"hsif:t:b:w:",["shadow=","input=","framerate=","time=","port="])
|
|
|
+except getopt.GetoptError:
|
|
|
+ print('main.py -s (shadow) -i (use input) -f --framerate <n=40>\n -t --time <t=0.033> -p --port <COM3>')
|
|
|
+ sys.exit(2)
|
|
|
+for opt, arg in opts:
|
|
|
+ if opt == '-h':
|
|
|
+ print('main.py -s (shadow) -i (use input) -f --framerate <n=40>\n -t --time <t=0.033> -p --port <COM3>')
|
|
|
+ sys.exit()
|
|
|
+ elif opt in ("-s", "--shadow"):
|
|
|
+ graphs = False
|
|
|
+ elif opt in ("-i", "--input"):
|
|
|
+ outDev = False
|
|
|
+ elif opt in ("-f", "--framerate"):
|
|
|
+ framerate = float(arg)
|
|
|
+ elif opt in ("-t", "--time"):
|
|
|
+ dataTime = float(arg)
|
|
|
+ elif opt in ("-p", "--port"):
|
|
|
+ port = arg
|
|
|
|
|
|
matrix = SerialConnection()
|
|
|
-matrix.open()
|
|
|
+matrix.open(port)
|
|
|
|
|
|
-pitches = [(2**(1/12))**(n/11*12*7-24) * 220 for n in range(11)]
|
|
|
-audio = Listener(1/30, 30)
|
|
|
+pitches = [(2**(1/12))**(n/11*12*7-30) * 220 for n in range(11)]
|
|
|
+audio = Listener(dataTime, 30, not outDev)
|
|
|
audio.start()
|
|
|
+
|
|
|
nFFT = audio.buffersize
|
|
|
audio.fftSetLimits(nFFT, min(pitches), max(pitches))
|
|
|
audio.agcFftSetLimits(60/60, 300/60)
|
|
|
|
|
|
+maxVolRow = [0] * N_COLS
|
|
|
+maxVolAge = [0] * N_COLS
|
|
|
+intervalTime = time.time()
|
|
|
+
|
|
|
while True:
|
|
|
if audio.hasNewData():
|
|
|
- left, right, fft = np.copy(audio.left), np.copy(audio.right), np.copy(audio.fft)
|
|
|
-
|
|
|
+ fft = np.copy(audio.fft)
|
|
|
sums = audio.fftGroup(fft, pitches)
|
|
|
data = [0] * 50
|
|
|
- for row in range(N_ROWS):
|
|
|
- for col in range(N_COLS):
|
|
|
- val = 15 if sums[col] > ((N_ROWS-row)/N_COLS/2) else 0
|
|
|
- data[row*N_COLS+col]= val
|
|
|
- print(data)
|
|
|
+ for col in range(N_COLS):
|
|
|
+ curVal = sums[col] * len(sums) * 1.1**col
|
|
|
+ curRow = max(N_ROWS - int(curVal), 0)
|
|
|
+
|
|
|
+ if curRow <= maxVolRow[col]:
|
|
|
+ maxVolRow[col] = curRow
|
|
|
+ maxVolAge[col] = 30
|
|
|
+ elif maxVolAge[col] > 0:
|
|
|
+ maxVolAge[col] -= 1
|
|
|
+ else:
|
|
|
+ maxVolRow[col] = N_ROWS
|
|
|
+
|
|
|
+ if maxVolRow[col] < N_ROWS:
|
|
|
+ data[maxVolRow[col]*N_COLS+col] = int(maxVolAge[col]/10)
|
|
|
+
|
|
|
+ if curRow < N_ROWS:
|
|
|
+ for row in range(curRow, N_ROWS):
|
|
|
+ data[row*N_COLS+col] = 15-curRow-row
|
|
|
+ data[49] = int(data[49]/2)
|
|
|
+ #print(data)
|
|
|
matrix.send(b'A' + bytes(data))
|
|
|
|
|
|
- time.sleep(1/40)
|
|
|
+ time.sleep(1/30)
|
|
|
+
|
|
|
+ if time.time() - intervalTime > 60:
|
|
|
+ if not matrix.isConnected():
|
|
|
+ print("lost serial connection, retrying to connect...")
|
|
|
+ matrix.connect()
|
|
|
+
|
|
|
+ audio.stop()
|
|
|
+ audio.start()
|
|
|
+ intervalTime = time.time()
|