better configuration

This commit is contained in:
Siwat Sirichai 2022-03-31 00:43:46 +07:00
parent ea90d87019
commit 7df9e27007
4 changed files with 99 additions and 26 deletions

View File

@ -14,6 +14,21 @@ import PIL.ImageGrab
from PIL import Image from PIL import Image
import colorsys import colorsys
import win32gui, win32ui, win32con import win32gui, win32ui, win32con
BOARDER_SIZE = 50
TOP_LEDS = 19
RIGHT_LEDS = 11
BUTTOM_LEDS = 19
LEFT_LEDS = 11
MIN_TIME = 0.05
FAST_MODE = True
SATURATION_BOOST_FACTOR = 2
NUM_CLUSTERS = 3
KMEAN_QUALITY = 50
NUM_THREADS = 16
if __name__ == '__main__':
board = telemetrix_rpi_pico.TelemetrixRpiPico()
class size: class size:
width = None width = None
height = None height = None
@ -28,8 +43,7 @@ def sigint_handler(signal=None, frame=None):
board.shutdown() board.shutdown()
exit() exit()
FAST_MODE = True
SATURATION_BOOST_FACTOR = 2.5
def find_dorminant_color(im): def find_dorminant_color(im):
if FAST_MODE: if FAST_MODE:
color = numpy.reshape(numpy.asarray(im),(-1,3)) color = numpy.reshape(numpy.asarray(im),(-1,3))
@ -40,9 +54,9 @@ def find_dorminant_color(im):
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v) r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
return [r*255,g*255,b*255] return [r*255,g*255,b*255]
else: else:
NUM_CLUSTERS = 2
im = im.resize((100, 100))
im = im.resize((KMEAN_QUALITY, KMEAN_QUALITY))
ar = np.asarray(im) ar = np.asarray(im)
shape = ar.shape shape = ar.shape
ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float) ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float)
@ -64,18 +78,6 @@ if __name__ == '__main__':
size.height=pyautogui.size().height size.height=pyautogui.size().height
signal.signal(signal.SIGINT, sigint_handler) signal.signal(signal.SIGINT, sigint_handler)
atexit.register(sigint_handler) atexit.register(sigint_handler)
global NUM_LEDS,BOARDER_SIZE,TOP_LEDS,RIGHT_LEDS,BUTTOM_LEDS,LEFT_LEDS,screenshot,board
board = telemetrix_rpi_pico.TelemetrixRpiPico()
BOARDER_SIZE = 50
TOP_LEDS = 19
RIGHT_LEDS = 11
BUTTOM_LEDS = 19
LEFT_LEDS = 11
MIN_TIME = 0.05
NUM_LEDS = TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS NUM_LEDS = TOP_LEDS+RIGHT_LEDS+BUTTOM_LEDS+LEFT_LEDS
@ -84,7 +86,7 @@ if __name__ == '__main__':
board.neopixel_show() board.neopixel_show()
screenshot = pyautogui.screenshot() screenshot = pyautogui.screenshot()
pool = Pool(8) pool = Pool(NUM_THREADS)
while True: while True:
try: try:
@ -118,5 +120,4 @@ if __name__ == '__main__':
sleep(0.001) sleep(0.001)
print("loop time : "+str(timestamp()-lastTime)) print("loop time : "+str(timestamp()-lastTime))
except Exception as e: except Exception as e:
print(e)
print("Retrying") print("Retrying")

View File

@ -1,6 +1,6 @@
import scipy.signal as signal import scipy.signal as signal
import pyaudio import pyaudio
from matplotlib import pyplot as plt from matplotlib import pyplot
import numpy as np import numpy as np
from time import sleep, time from time import sleep, time
import cupy import cupy
@ -24,24 +24,31 @@ board.neopixel_clear(auto_show=True)
SAMPLE_SIZE = 4096 SAMPLE_SIZE = 4096
SAMPLE_RATE = 48000 SAMPLE_RATE = 48000
LOWPASS_CUTOFF = 50
AMPLITUDE_MULTIPLIER = 0.1
audio = pyaudio.PyAudio() audio = pyaudio.PyAudio()
audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True, frames_per_buffer=SAMPLE_SIZE) audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True, frames_per_buffer=SAMPLE_SIZE)
pastBassSignal = [0]*100
while True: while True:
data = audioStream.read(SAMPLE_SIZE) data = audioStream.read(SAMPLE_SIZE)
sample = cupy.frombuffer(data, dtype=np.int16) sample = cupy.frombuffer(data, dtype=np.int16)
power = cupy.sum(cupy.abs(sample))/SAMPLE_SIZE power = cupy.sum(cupy.abs(sample))/SAMPLE_SIZE
if power > 1000: if power > 1000:
freq_dom = cupy.fft.fft(sample,10000) freq_dom = cupy.fft.rfft(sample,10000)
freqs = cupy.fft.fftfreq(len(freq_dom)) freqs = cupy.fft.rfftfreq(len(freq_dom))
power_bass = cupy.sum(cupy.abs(freq_dom[0:30]))/cupy.sum(cupy.abs(freq_dom))*power power_bass = cupy.sum(cupy.abs(freq_dom[0:LOWPASS_CUTOFF]))/cupy.sum(cupy.abs(freq_dom))*power
power_bass = max(0,power_bass-275) power_bass = max(0,power_bass-250)
if len(pastBassSignal)>100:
pastBassSignal.pop(0)
pastBassSignal.append(power_bass)
print(power_bass) print(power_bass)
idmax = cupy.argmax(cupy.abs(freq_dom)) idmax = cupy.argmax(cupy.abs(freq_dom))
freqmax = abs(freqs[idmax]*SAMPLE_RATE) freqmax = abs(freqs[idmax]*SAMPLE_RATE)
board.neopixel_fill(r=int(min(255,power_bass)),g=0,b=0,auto_show=True) board.neopixel_fill(r=int(min(255,power_bass*AMPLITUDE_MULTIPLIER)),g=0,b=0,auto_show=True)
else: else:
board.neopixel_clear(auto_show=True) board.neopixel_clear(auto_show=True)
audioStream.stop_stream() audioStream.stop_stream()

View File

@ -0,0 +1,64 @@
import scipy.signal as signal
import pyaudio
from matplotlib import pyplot
import numpy as np
from time import sleep, time
import cupy
import atexit
import signal
from telemetrix_rpi_pico import telemetrix_rpi_pico
import colorsys
def sigint_handler(signal=None, frame=None):
print ('KeyboardInterrupt is caught')
board.neopixel_clear()
sleep(0.75)
board.shutdown()
exit()
signal.signal(signal.SIGINT, sigint_handler)
atexit.register(sigint_handler)
NUM_LEDS = 60
board = telemetrix_rpi_pico.TelemetrixRpiPico()
board.set_pin_mode_neopixel(pin_number=2,num_pixels=NUM_LEDS)
board.neopixel_clear(auto_show=True)
SAMPLE_SIZE = 4096
SAMPLE_RATE = 48000
LOWPASS_CUTOFF = 50
AMPLITUDE_MULTIPLIER = 0.1
VELOCITY = 6
audio = pyaudio.PyAudio()
audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True, frames_per_buffer=SAMPLE_SIZE)
pastBassSignal = [0]*100
timecounter = 0
while True:
data = audioStream.read(SAMPLE_SIZE)
sample = cupy.frombuffer(data, dtype=np.int16)
power = cupy.sum(cupy.abs(sample))/SAMPLE_SIZE
if power > 1000:
freq_dom = cupy.fft.rfft(sample,10000)
freqs = cupy.fft.rfftfreq(len(freq_dom))
power_bass = cupy.sum(cupy.abs(freq_dom[0:LOWPASS_CUTOFF]))/cupy.sum(cupy.abs(freq_dom))*power
power_bass = max(0,power_bass-250)
if len(pastBassSignal)>100:
pastBassSignal.pop(0)
pastBassSignal.append(power_bass)
print(power_bass)
idmax = cupy.argmax(cupy.abs(freq_dom))
freqmax = abs(freqs[idmax]*SAMPLE_RATE)
brightness = int(min(255,power_bass*AMPLITUDE_MULTIPLIER))
for j in range(0,NUM_LEDS):
r, g, b = colorsys.hsv_to_rgb(((-timecounter*VELOCITY+j*4)%360)/360,1,1)
board.neo_pixel_set_value(j,r=int(r*brightness),g=int(g*brightness),b=int(b*brightness))
timecounter+=1
board.neopixel_show()
else:
board.neopixel_clear(auto_show=True)
audioStream.stop_stream()
audioStream.close()
audio.terminate()

View File

@ -1,6 +1,6 @@
import cupy import cupy
import colorsys import colorsys
from matplotlib impl plot
peak = [124.3,231.5, 64.4] peak = [124.3,231.5, 64.4]
h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255) h,s,v = colorsys.rgb_to_hsv(r=peak[0]/255,g=peak[1]/255,b=peak[2]/255)
@ -9,3 +9,4 @@ s = min(1,s*SATURATION_BOOST_FACTOR)
print([h,s,v]) print([h,s,v])
r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v) r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v)
print([r,g,b]) print([r,g,b])