forked from maralorn/pythonlights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk3x_lbb.py
47 lines (36 loc) · 1.33 KB
/
k3x_lbb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pythonlights
import sound
import numpy as np
ctrl = pythonlights.LEDControl()
listener = sound.Listener()
ones = np.ones(3)
#peaks = ones * 50000000/256 # Nur ein Schätzwert
peaks=0;
longmean = shortmean = ones * 128 # long zum regeln, short zum glätten
#
sound.INPUT_BLOCK_TIME = 0.1
a = int(250*sound.INPUT_BLOCK_TIME) # Anzahl der Array Element um 250Hz abdecken
while True:
# Dieser Aufruf blockt, bis genug sample da ist.
spectrum = listener.get_spectrum()
# blau 0-250 Hz, grün 250-500 Hz, rot 500-1000 Hz
colors = np.array([np.sum(spectrum[0:a]),np.sum(spectrum[a:a*6]),np.sum(spectrum[a*6:a*15])-np.sum(spectrum[a*2:a*6])])
peaks*=0.99;
peaks = np.max(np.array([colors[0],colors[1],colors[2],peaks]));
# Normalisiere auf anzeigbare Intensitäten
colors /= (peaks/255+1);
colors *= 0.5;
colors += 128
print colors;
colors = np.clip(colors,128,255)
# gleitende Mittelwerte
shortmean = 0.5 * colors + 0.5 * shortmean
longmean = 0.01 * colors + 0.99 * longmean
# Korrektur der Maximalwerte um damit die Intesitäten im Mittel um 50% schwanken.
# peaks *= 0.01*longmean/128 + 0.99
# Einstellen und senden der berechneten Farbe.
color = pythonlights.Color(shortmean.astype(int))
ctrl.set_all(color)
ctrl.send()