renaming folders and files to python format

This commit is contained in:
dezeyer
2019-05-11 14:28:42 +00:00
parent ade378d128
commit 798775805c
19 changed files with 509 additions and 268 deletions
+249
View File
@@ -0,0 +1,249 @@
from Utils.BaseEffect import BaseEffect
from Utils.EffectParameter import slider, colorpicker
import time
import sys
import numpy
from time import perf_counter, sleep
from random import randint, shuffle
class MusikEffect(BaseEffect):
name = "musikEffect"
desc = "LED-Band *sollte* nach musik blinken"
# Something that will be used to show descriptions and value options
# of the parameters the effect will accept, in a way, that eg the webclient can decide,
# if the parameters can be toggeled by a button/checkbox/slider/whatever
effectParameters: list = [
# radio(\
# "Shuffle LED to Freq Order",\
# "Off -> Mapping ",\
# [\
# [0,255,255,"red"],\
# [0,255,255,"green"],\
# [0,255,255,"blue"]\
# ]\
# ),\
# slider(\
# "Effect Brightnes",\
# "Choose a brightness for your LED's",\
# [\
# [0,100,100,"brightness"],\
# ]\
# )\
]
def init(self):
self.fft_random_keys = [0,1,2,3]
self.fft_random = [0,0,0,0]
self.y_max_freq_avg_list = []
self.low_freq_avg_list = []
self.bpm_list = []
self.prev_beat = 0 # timestamp
# used by strobe()
self.lastmode = 0
return
#loop effect as long as not stopped
def effect(self):
self.plot_audio_and_detect_beats()
#self.freqtocolor()
#if(time.time() - self.lastTime >= 0.002):
#for RGBStrip in self.effectRGBStrips():
# r= RGBStrip.red-1
# if(r<0):
# r=0
# g= RGBStrip.green-1
# if(g<0):
# g=0
# b= RGBStrip.blue-1
# if(b<0):
# b=0
# RGBStrip.RGB(r,g,b)
#self.lastTime = time.time()
sleep(0.00833333333)
def end(self):
pass
def plot_audio_and_detect_beats(self):
if not self.hasNewFttValues:
return
# get x and y values from FFT
xs, ys = self.ftt
# calculate average for all frequency ranges
y_avg = numpy.mean(ys)
#low_freq = numpy.mean(ys[20:47])
#mid_freq = numpy.mean(ys[88:115])
#hig_freq = numpy.mean(ys[156:184])
low_freq = numpy.mean(ys[0:int(ys.__len__() / 3 * 1)])
mid_freq = numpy.mean(ys[int(ys.__len__() / 3 * 1):int(ys.__len__() / 3 * 2)])
hig_freq = numpy.mean(ys[int(ys.__len__() / 3 * 2):int(ys.__len__() / 3 * 3)])
#get the maximum of all freq
if len(self.y_max_freq_avg_list) < 250 or y_avg > 10 and numpy.amax([low_freq,mid_freq,hig_freq])/2 > numpy.amin(self.y_max_freq_avg_list):
self.y_max_freq_avg_list.append(numpy.amax([low_freq,mid_freq,hig_freq]))
y_max = numpy.amax(self.y_max_freq_avg_list)
#y_max = numpy.mean([numpy.amax(ys),y_max])
#print(low_freq,mid_freq,hig_freq,y_max)
for i,item in enumerate([low_freq,mid_freq,hig_freq]):
if item is None:
item = 0
low = round(low_freq/(y_max+10)*255)
mid = round(mid_freq/(y_max+10)*255)
hig = round(hig_freq/(y_max+10)*255)
#print(low,mid,hig,y_max, numpy.amax(ys), y_avg)
#print("------")
self.fft_random[self.fft_random_keys[0]] = low
self.fft_random[self.fft_random_keys[1]] = mid
self.fft_random[self.fft_random_keys[2]] = hig
self.fft_random[self.fft_random_keys[3]] = 0
# calculate low frequency average
#low_freq = [ys[i] for i in range(len(xs)) if xs[i] < 1000]
low_freq = ys[0:47]
low_freq_avg = numpy.mean(low_freq)
if len(self.low_freq_avg_list) < 250 or low_freq_avg > numpy.amin(self.low_freq_avg_list)/2:
self.low_freq_avg_list.append(low_freq_avg)
cumulative_avg = numpy.mean(self.low_freq_avg_list)
bass = low_freq[:int(len(low_freq)/2)]
bass_avg = numpy.mean(bass)
#print("bass: {:.2f} vs cumulative: {:.2f}".format(bass_avg, cumulative_avg))
# check if there is a beat
# song is pretty uniform across all frequencies
if (y_avg > y_avg/5 and (bass_avg > cumulative_avg * 1.8 or (low_freq_avg < y_avg * 1.2 and bass_avg > cumulative_avg))):
#self.prev_beat
curr_time = perf_counter()
# print(curr_time - self.prev_beat)
if curr_time - self.prev_beat > 60/360*2: # 180 BPM max
shuffle(self.fft_random_keys)
# change the button color
#self.beats_idx += 1
#self.strobe()
#print("beat {}".format(self.beats_idx))
#print("bass: {:.2f} vs cumulative: {:.2f}".format(bass_avg, cumulative_avg))
#print(self.fft_random)
# change the button text
bpm = int(60 / (curr_time - self.prev_beat))
if len(self.bpm_list) < 4:
if bpm > 60:
self.bpm_list.append(bpm)
else:
bpm_avg = int(numpy.mean(self.bpm_list))
if abs(bpm_avg - bpm) < 35:
self.bpm_list.append(bpm)
print("bpm: {:d}".format(bpm_avg))
# reset the timer
self.prev_beat = curr_time
if y_avg > 10:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(
self.fft_random[0],
self.fft_random[1],
self.fft_random[2]
)
"""
rgbStrip: RGBStrip
for rgbStrip in self.effectRGBStrips():
for i in range(rgbStrip.STRIP_LENGHT):
if rgbStrip.STRIP_LENGHT-1-10 is not i:
rgbStrip.WS2812b(rgbStrip.STRIP_LENGHT-i-1, rgbStrip.red[rgbStrip.STRIP_LENGHT-i-2], rgbStrip.green[rgbStrip.STRIP_LENGHT-i-2], rgbStrip.blue[rgbStrip.STRIP_LENGHT-i-2])
else:
rgbStrip.WS2812b(rgbStrip.STRIP_LENGHT-i-1, self.fft_random[0], self.fft_random[1], self.fft_random[2])
rgbStrip.show()
"""
# shorten the cumulative list to account for changes in dynamics
if len(self.low_freq_avg_list) > 500:
self.low_freq_avg_list = self.low_freq_avg_list[250:]
#print("REFRESH!!")
# shorten the cumulative list to account for changes in dynamics
if len(self.y_max_freq_avg_list ) > 500:
self.y_max_freq_avg_list = self.y_max_freq_avg_list[250:]
print("--REFRESH y_max_freq_avg_list")
# keep two 8-counts of BPMs so we can maybe catch tempo changes
if len(self.bpm_list) > 24:
self.bpm_list = self.bpm_list[8:]
# reset song data if the song has stopped
if y_avg < 10:
self.bpm_list = []
self.low_freq_avg_list = []
print("new song")
self.off()
self.hasNewFttValues = False
# print(self.bpm_list)250
def strobe(self):
x = randint(0,5)
while x is self.lastmode:
x = randint(0,5)
self.lastmode = x
r = 255#randint(0,255)
g = 255#randint(0,255)
b = 255#randint(0,255)
if x is 0:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(r,g,0)
if x is 1:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,g,b)
if x is 2:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(r,0,b)
if x is 3:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(r,0,0)
if x is 4:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,g,0)
if x is 5:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,0,b)
if x is 6:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(r,g,b)
def off(self):
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,0,0)
def left_rotate(self,arr):
if not arr:
return arr
left_most_element = arr[0]
length = len(arr)
for i in range(length - 1):
arr[i], arr[i + 1] = arr[i + 1], arr[i]
arr[length - 1] = left_most_element
return arr
+184
View File
@@ -0,0 +1,184 @@
from Utils.BaseEffect import BaseEffect
from Utils.RGBStrip import RGBStrip
from Utils.EffectParameter import slider, colorpicker
import time
import sys
import numpy
from time import perf_counter, sleep
from random import randint, shuffle
import typing
import matplotlib
import matplotlib.pyplot as plt
#matplotlib.use('TkAgg') # THIS MAKES IT FAST!
matplotlib.get_backend()
class MusikEffect2(BaseEffect):
name = "musikEffect2"
desc = "LED-Band *sollte* nach musik blinken"
# Something that will be used to show descriptions and value options
# of the parameters the effect will accept, in a way, that eg the webclient can decide,
# if the parameters can be toggeled by a button/checkbox/slider/whatever
"""radio(
"Shuffle LED to Freq Order",
"Off -> Mapping ",
[
[0,255,255,"red"],
[0,255,255,"green"],
[0,255,255,"blue"]
]
),"""
effectParameters: list = [
slider(
"Effect Brightnes",
"Choose a brightness for your LED's",
[
[0,100,100,"brightness"],
]
)
]
def init(self):
self.fft_random_keys = [0,1,2,3]
self.fft_random = [0,0,0,0]
self.y_max_freq_avg_list = [0]
self.low_freq_avg_list = [0]
self.bpm_list = []
self.prev_beat = 0 # timestamp
'''
self.fig = plt.gcf()
self.fig.show()
'''
return
#loop effect as long as not stopped
def effect(self):
'''plt.clf()
#plt.ylim(-.5, numpy.amax(self.y_max_freq_avg_list))
plt.xlabel('xs')
plt.ylabel('ys')
plt.plot(self.ftt[0],self.ftt[1])
self.fig.canvas.draw()
self.fig.canvas.flush_events()'''
self.plot_audio_and_detect_beats()
#self.freqtocolor()
#if(time.time() - self.lastTime >= 0.002):
#for RGBStrip in self.effectRGBStrips():
# r= RGBStrip.red-1
# if(r<0):
# r=0
# g= RGBStrip.green-1
# if(g<0):
# g=0
# b= RGBStrip.blue-1
# if(b<0):
# b=0
# RGBStrip.RGB(r,g,b)
#self.lastTime = time.time()
sleep(0.00833333333)
def end(self):
pass
def plot_audio_and_detect_beats(self):
if not self.hasNewFttValues:
return
# get x and y values from FFT
xs, ys = self.ftt
amax = numpy.amax(ys)
if amax > 0:
self.y_max_freq_avg_list.append(amax)
if len(self.y_max_freq_avg_list ) > 50000:
self.y_max_freq_avg_list = self.y_max_freq_avg_list[25000:]
calc = numpy.amax(ys) / numpy.amax(self.y_max_freq_avg_list) * 765
"""print(amax)
print(numpy.amax(self.y_max_freq_avg_list))
print(calc)
print("-----------")
"""
c: self.Color = self.pitchRainbow(calc)
rgbStrip: RGBStrip
for rgbStrip in self.effectRGBStrips():
"""
for i in range(rgbStrip.STRIP_LENGHT):
if rgbStrip.STRIP_LENGHT-1-10 is not i:
rgbStrip.WS2812b(rgbStrip.STRIP_LENGHT-i-1, rgbStrip.red[rgbStrip.STRIP_LENGHT-i-2], rgbStrip.green[rgbStrip.STRIP_LENGHT-i-2], rgbStrip.blue[rgbStrip.STRIP_LENGHT-i-2])
else:
rgbStrip.WS2812b(rgbStrip.STRIP_LENGHT-i-1, c.red, c.green, c.blue)
rgbStrip.show()
"""
rgbStrip.RGB(int(c.red),int(c.green),int(c.blue))
# shorten the cumulative list to account for changes in dynamics
def pitchRainbow(self,p: float):
if p < 1:
return self.Color(0,0,0)
elif p < 255:
return self.Color(255 - p % 255,p % 255,0)
elif p < 510:
return self.Color(0,255 - p % 255,p % 255)
else:
return self.Color(p % 255,0,255 - p % 255)
def pitchConv(self,p: float):
r:float
g:float
b:float
if p < 40:
return self.Color(255,0,0)
elif p >= 40 and p <= 77 :
b = (p - 40) * (255/37.0000)
return self.Color(255,0,b)
elif p > 77 and p <= 205:
r = 255 - ((p - 78) * 2)
return self.Color(r,0,255)
elif p >= 206 and p <= 238:
g = (p - 206) * (255/32.0000)
return self.Color(0,g,255)
elif p <= 239 and p <= 250:
r = (p - 239) * (255/11.0000)
return self.Color(r, 255, 255)
elif p >= 251 and p <= 270:
return self.Color(255, 255, 255)
elif p >= 271 and p <= 398:
rb = 255-((p-271)*2)
return self.Color(rb, 255, rb)
elif p >= 398 and p <= 650:
return self.Color(0, 255-(p-398), (p-398))
else:
return self.Color(255, 0, 0)
class Color:
red: float
green: float
blue: float
def __init__(self,red:float,green:float,blue:float) -> None:
self.red = red
self.green = green
self.blue = blue
+22
View File
@@ -0,0 +1,22 @@
from Utils.BaseEffect import BaseEffect
import time
class OffEffect(BaseEffect):
name = "offEffect"
desc = "LED-Band *sollte* nicht an sein"
def effect(self):
time.sleep(1)
return
# for overriding by the effect, when a strip is added
def onRGBStripAdded(self,rgbStrip):
rgbStrip.RGB(0,0,0,)
return
# for overriding by the effect, when a strip is added
def onEffectParameterValuesUpdated(self):
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,0,0)
return
+66
View File
@@ -0,0 +1,66 @@
from Utils.BaseEffect import BaseEffect
from Utils.EffectParameter import slider, colorpicker
import time
class OnEffect(BaseEffect):
name = "onEffect"
desc = "LED-Band *sollte* an sein"
# Something that will be used to show descriptions and value options
# of the parameters the effect will accept, in a way, that eg the webclient can decide,
# if the parameters can be toggeled by a button/checkbox/slider/whatever
effectParameters = [
colorpicker(\
"Effect Color",\
"Choose a color for your LED's",\
[\
[0,255,255,"red"],\
[0,255,255,"green"],\
[0,255,255,"blue"]\
]\
),\
slider(\
"Effect Brightnes",\
"Choose a brightness for your LED's",\
[\
[0,100,100,"brightness"],\
]\
)\
]
def init(self):
return
#loop effect as long as not stopped
def effect(self):
time.sleep(1)
return
# for overriding by the effect, when a strip is added
def onRGBStripAdded(self,rgbStrip):
rgbStrip.RGB(\
# colorpicker red currentvalue
self.effectParameterValues[0][0],\
# colorpicker green currentvalue
self.effectParameterValues[0][1],\
# colorpicker blue currentvalue
self.effectParameterValues[0][2],\
# slider brightness currentvalue
self.effectParameterValues[1][0]\
)
return
# for overriding by the effect, when a params are updated
def onEffectParameterValuesUpdated(self):
for RGBStrip in self.effectRGBStrips():
#print(self.effectParameterValues)
RGBStrip.RGB(\
# colorpicker red currentvalue
self.effectParameterValues[0][0],\
# colorpicker green currentvalue
self.effectParameterValues[0][1],\
# colorpicker blue currentvalue
self.effectParameterValues[0][2],\
# slider brightness currentvalue
self.effectParameterValues[1][0]\
)
+159
View File
@@ -0,0 +1,159 @@
from Utils.BaseEffect import BaseEffect
import time
class RainbowEffect(BaseEffect):
name = "rainbowEffect"
desc = "LED-Band *sollte* rainbowEffect sein"
def init(self):
self.i=0
self.speed = 1
self.helligkeit = 100
#loop effect as long as not stopped
def effect(self):
"""Draw rainbow that fades across all pixels at once."""
for j in range(256*75):
for rgbStrip in self.effectRGBStrips():
for i in range(rgbStrip.STRIP_LENGHT):
color = self.wheel((i+j) & 255)
rgbStrip.WS2812b(i, color[0],color[1],color[2])
for rgbStrip in self.effectRGBStrips():
rgbStrip.show()
time.sleep(100/1000.0)
"""Draw rainbow that uniformly distributes itself across all pixels.
for j in range(256*iterations):
for i in range(rgbStrip.STRIP_LENGHT):
color = self.wheel(((i * 256 // rgbStrip.STRIP_LENGHT) + j) & 255)
rgbStrip.WS2812b(i, color[0],color[1],color[2])
#strip.show()
time.sleep(wait_ms/1000.0)"""
"""Rainbow movie theater light style chaser animation.
for j in range(256):
for q in range(3):
for rgbStrip in self.effectRGBStrips():
for i in range(0, rgbStrip.STRIP_LENGHT, 3):
color=self.wheel((i+j) % 255)
rgbStrip.WS2812b(i+q, color[0],color[1],color[2])
for rgbStrip in self.effectRGBStrips():
rgbStrip.show()
time.sleep(50/1000.0)
for rgbStrip in self.effectRGBStrips():
for i in range(0, rgbStrip.STRIP_LENGHT, 3):
rgbStrip.WS2812b(i+q, 0,0,0)"""
#if self.i < 3*255*self.speed:
# c = self.wheel_color(self.i,self.speed)
# #print("r: "+ str(round(c[0]/100*helligkeit,2))+" g: "+str(round(c[1]/100*helligkeit,2))+" b: "+str(round(c[2]/100*helligkeit,2)))
# for rgbStrip in self.effectRGBStrips():
# #print(c[0],c[1],c[2])
# rgbStrip.RGB(c[0],c[1],c[2],self.helligkeit)
# time.sleep(0.01)
# self.i =self.i +1
#else:
# self.i=0
# for overriding by the effect, when a strip is added
def onRGBStripAdded(self,rgbStrip):
return
# for overriding by the effect, when a strip is added
def onEffectParameterValuesUpdated(self):
return
def wheel_color(self,position,speed = 5):
"""Get color from wheel value (0 - 765)"""
if position < 0:
position = 0
if position > 765*speed:
position = 765*speed
if position < (255*speed):
r = (255*speed) - position % (255*speed)
g = position % (255*speed)
b = 0
elif position < (510*speed):
g = (255*speed) - position % (255*speed)
b = position % (255*speed)
r = 0
else:
b = (255*speed) - position % (255*speed)
r = position % (255*speed)
g = 0
return [r/speed, g/speed, b/speed]
# Define functions which animate LEDs in various ways.
#https://github.com/jgarff/rpi_ws281x/blob/master/python/examples/SK6812_strandtest.py
def colorWipe(self, rgbStrip, red, green, blue, wait_ms=50):
"""Wipe color across display a pixel at a time."""
for i in range(rgbStrip.STRIP_LENGHT):
rgbStrip.WS2812b(i, red,green,blue)
time.sleep(wait_ms/1000.0)
def theaterChase(self, rgbStrip , red, green, blue, wait_ms=50, iterations=10):
"""Movie theater light style chaser animation."""
for j in range(iterations):
for q in range(3):
for i in range(0, rgbStrip.STRIP_LENGHT, 3):
rgbStrip.WS2812b(i+q, red,green,blue)
#strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, rgbStrip.STRIP_LENGHT, 3):
rgbStrip.WS2812b(i+q, red,green,blue)
def wheel(self,pos):
"""Generate rainbow colors across 0-255 positions."""
if pos < 85:
return [pos * 3, 255 - pos * 3, 0]
elif pos < 170:
pos -= 85
return [255 - pos * 3, 0, pos * 3]
else:
pos -= 170
return [0, pos * 3, 255 - pos * 3]
def rainbow(self, rgbStrip, wait_ms=20, iterations=1):
"""Draw rainbow that fades across all pixels at once."""
for j in range(256*iterations):
for i in range(rgbStrip.STRIP_LENGHT):
color = self.wheel((i+j) & 255)
rgbStrip.WS2812b(i, color[0],color[1],color[2])
#strip.show()
time.sleep(wait_ms/1000.0)
def rainbowCycle(self,rgbStrip, wait_ms=20, iterations=5):
"""Draw rainbow that uniformly distributes itself across all pixels."""
for j in range(256*iterations):
for i in range(rgbStrip.STRIP_LENGHT):
color = self.wheel(((i * 256 // rgbStrip.STRIP_LENGHT) + j) & 255)
rgbStrip.WS2812b(i, color[0],color[1],color[2])
#strip.show()
time.sleep(wait_ms/1000.0)
def theaterChaseRainbow(self,rgbStrip, wait_ms=50):
"""Rainbow movie theater light style chaser animation."""
for j in range(256):
for q in range(3):
for i in range(0, rgbStrip.STRIP_LENGHT, 3):
color=self.wheel((i+j) % 255)
rgbStrip.WS2812b(i+q, color[0],color[1],color[2])
#strip.show()
time.sleep(wait_ms/1000.0)
for i in range(0, rgbStrip.STRIP_LENGHT, 3):
rgbStrip.WS2812b(i+q, 0,0,0)
# def wheel_color_2(self,r=255,g=0,b=0):
# if r<255:
# r=r+1
# elif g<255:
# g=g+1
# elif r=255:
# elif b<255:
# b=b+1
+41
View File
@@ -0,0 +1,41 @@
from Utils.BaseEffect import BaseEffect
from random import randint
import time
class StrobeEffect(BaseEffect):
name = "strobeEffect"
desc = "*Strobe*"
def init(self):
self.state = True
#loop effect as long as not stopped
def effect(self):
y = -1
x = -1
if self.state:
while x is y:
x = randint(0,2)
if x is 0:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(255,255,0)
if x is 1:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,255,255)
if x is 2:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(255,0,255)
self.state = False
else:
for RGBStrip in self.effectRGBStrips():
RGBStrip.RGB(0,0,0)
self.state = True
time.sleep(0.01)
# for overriding by the effect, when a strip is added
def onRGBStripAdded(self,rgbStrip):
return
# for overriding by the effect, when a strip is added
def onEffectParameterValuesUpdated(self):
return
View File