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