From 7df9e2700780a14d6d961ca1b3b3f77e8dda007f Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 31 Mar 2022 00:43:46 +0700 Subject: [PATCH] better configuration --- ambilight_mulitprocessing.py | 37 +++++++------ beat_detection.py => beat_detection_gpu.py | 19 +++++-- beat_detection_rainbow_gpu.py | 64 ++++++++++++++++++++++ test.py | 5 +- 4 files changed, 99 insertions(+), 26 deletions(-) rename beat_detection.py => beat_detection_gpu.py (67%) create mode 100644 beat_detection_rainbow_gpu.py diff --git a/ambilight_mulitprocessing.py b/ambilight_mulitprocessing.py index 9a35ffd..7745c77 100644 --- a/ambilight_mulitprocessing.py +++ b/ambilight_mulitprocessing.py @@ -14,6 +14,21 @@ import PIL.ImageGrab from PIL import Image import colorsys 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: width = None height = None @@ -28,8 +43,7 @@ def sigint_handler(signal=None, frame=None): board.shutdown() exit() -FAST_MODE = True -SATURATION_BOOST_FACTOR = 2.5 + def find_dorminant_color(im): if FAST_MODE: 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) return [r*255,g*255,b*255] else: - NUM_CLUSTERS = 2 + - im = im.resize((100, 100)) + im = im.resize((KMEAN_QUALITY, KMEAN_QUALITY)) ar = np.asarray(im) shape = ar.shape ar = ar.reshape(np.product(shape[:2]), shape[2]).astype(float) @@ -64,18 +78,6 @@ if __name__ == '__main__': size.height=pyautogui.size().height signal.signal(signal.SIGINT, 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 @@ -84,7 +86,7 @@ if __name__ == '__main__': board.neopixel_show() screenshot = pyautogui.screenshot() - pool = Pool(8) + pool = Pool(NUM_THREADS) while True: try: @@ -118,5 +120,4 @@ if __name__ == '__main__': sleep(0.001) print("loop time : "+str(timestamp()-lastTime)) except Exception as e: - print(e) print("Retrying") diff --git a/beat_detection.py b/beat_detection_gpu.py similarity index 67% rename from beat_detection.py rename to beat_detection_gpu.py index 2edd016..1545111 100644 --- a/beat_detection.py +++ b/beat_detection_gpu.py @@ -1,6 +1,6 @@ import scipy.signal as signal import pyaudio -from matplotlib import pyplot as plt +from matplotlib import pyplot import numpy as np from time import sleep, time import cupy @@ -24,24 +24,31 @@ board.neopixel_clear(auto_show=True) SAMPLE_SIZE = 4096 SAMPLE_RATE = 48000 +LOWPASS_CUTOFF = 50 +AMPLITUDE_MULTIPLIER = 0.1 audio = pyaudio.PyAudio() audioStream = audio.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True, frames_per_buffer=SAMPLE_SIZE) +pastBassSignal = [0]*100 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.fft(sample,10000) - freqs = cupy.fft.fftfreq(len(freq_dom)) - power_bass = cupy.sum(cupy.abs(freq_dom[0:30]))/cupy.sum(cupy.abs(freq_dom))*power - power_bass = max(0,power_bass-275) + 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) - 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: board.neopixel_clear(auto_show=True) audioStream.stop_stream() diff --git a/beat_detection_rainbow_gpu.py b/beat_detection_rainbow_gpu.py new file mode 100644 index 0000000..880b46c --- /dev/null +++ b/beat_detection_rainbow_gpu.py @@ -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() \ No newline at end of file diff --git a/test.py b/test.py index e35637f..57f7e3a 100644 --- a/test.py +++ b/test.py @@ -1,6 +1,6 @@ import cupy import colorsys - +from matplotlib impl plot 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) @@ -8,4 +8,5 @@ SATURATION_BOOST_FACTOR = 1.5 s = min(1,s*SATURATION_BOOST_FACTOR) print([h,s,v]) r,g,b = colorsys.hsv_to_rgb(h=h,s=s,v=v) -print([r,g,b]) \ No newline at end of file +print([r,g,b]) +