host_python/equalizer: Ratelimit updates

This commit is contained in:
Petr Baudis 2012-06-30 23:42:54 +02:00
parent a0e7bca3ae
commit 683c89f586

View file

@ -5,6 +5,8 @@ import pyaudio
import struct import struct
import math import math
import numpy as np import numpy as np
import time
from datetime import datetime
import ledbar import ledbar
@ -31,7 +33,7 @@ stream = p.open(format = FORMAT,
frames_per_buffer = CHUNK_SIZE) frames_per_buffer = CHUNK_SIZE)
def get_color(volume): def get_color(volume):
p = 1-15/volume p = 1-15/(volume)
if p <= 0: return (0, 0, 0) if p <= 0: return (0, 0, 0)
p *= p p *= p
if p <= 0.4: return (0, 0, p*2.5) if p <= 0.4: return (0, 0, p*2.5)
@ -44,10 +46,17 @@ history = []
window = np.array([0.5*(1-math.cos(2*math.pi*i/(SAMPLE_SIZE-1))) for i in xrange(SAMPLE_SIZE)]) window = np.array([0.5*(1-math.cos(2*math.pi*i/(SAMPLE_SIZE-1))) for i in xrange(SAMPLE_SIZE)])
work = True work = True
nexttrig = 0
try: try:
while work: while work:
try: data = stream.read(CHUNK_SIZE) try: data = stream.read(CHUNK_SIZE)
except IOError: continue except IOError: continue
nowtrig = datetime.now().microsecond / 50000
if (nowtrig == nexttrig):
continue
else:
nexttrig = nowtrig
if len(data) == 0: break if len(data) == 0: break
indata = np.array(struct.unpack('%dh'%CHUNK_SIZE,data)) indata = np.array(struct.unpack('%dh'%CHUNK_SIZE,data))
history.append(indata) history.append(indata)
@ -79,6 +88,7 @@ try:
c = get_color(volumes[pixel]) c = get_color(volumes[pixel])
l.set_pixel(pixel, c[0], c[1], c[2]) l.set_pixel(pixel, c[0], c[1], c[2])
work = l.update() work = l.update()
# time.sleep(0.05)
finally: finally:
stream.close() stream.close()
p.terminate() p.terminate()