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 -t --time -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 -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 ("-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, 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(): fft = np.copy(audio.fft) sums = audio.fftGroup(fft, pitches) data = [0] * 50 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/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()