Last active
August 29, 2015 14:02
-
-
Save tkhduracell/fb36ee3f441c64bf8447 to your computer and use it in GitHub Desktop.
Using LightPack to visualize SteroMix (music and games) on Windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lightpack | |
import time | |
import random | |
import pyaudio # from http://people.csail.mit.edu/hubert/pyaudio/ | |
import audioop | |
import sys | |
import math | |
def list_devices(): | |
# List all audio input devices | |
p = pyaudio.PyAudio() | |
i = 0 | |
n = p.get_device_count() | |
while i < n: | |
dev = p.get_device_info_by_index(i) | |
if dev['maxInputChannels'] > 0: | |
print("%d - %s" % (i, dev['name'])) | |
i += 1 | |
def soundlight(lpack): | |
chunk = 1024 * 2 | |
scale = 200 # Change if too dim/bright | |
exponent = 2 # Change if too little/too much difference between loud and quiet sounds | |
# Set up color schemes | |
steps = 512 | |
colors = [] | |
colors.extend([(1-x,0.5 + x,0) for x in linspace(0,0.5, steps)]) | |
colors.extend([(0,1-x,0.5 + x) for x in linspace(0,0.5,steps)]) | |
colors.extend([(0.5 + x,0,1-x) for x in linspace(0,0.5, steps)]) | |
values = [125 for x in range(0,8)] | |
adjust = 0.3 | |
diff = 50 | |
limit = 255 | |
lower_limit = diff * len(values) | |
upper_limit = (limit-(diff*0.7)) * len(values) | |
# CHANGE THIS TO CORRECT INPUT DEVICE | |
# Enable stereo mixing in your sound card | |
# to make you sound output an input | |
# Use list_devices() to list all your input devices | |
device = 2 | |
debug = True | |
try: | |
p = pyaudio.PyAudio() | |
stream = p.open(format = pyaudio.paInt16, channels = 1, rate = 44100, input = True, frames_per_buffer = chunk, input_device_index = device) | |
except Error: | |
print("asdad") | |
counter = 0 | |
try: | |
print("Starting, use Ctrl+C to stop") | |
while True: | |
data = stream.read(chunk) | |
rms = audioop.rms(data, 2) | |
# Get audio level | |
level = min(rms / (2.0 ** 16) * scale, 1.0) | |
level = level**exponent | |
level = int(level * 255) | |
# get current color scheme | |
c = colors[counter] | |
lpack.setColorToAll(int(level * c[0]), int(level * c[1]), int(level * c[2])) | |
if debug: | |
print("{:<3d}({:<3d},{:<3d},{:<3d})" | |
.format(level, int(level * c[0]), int(level * c[1]), int(level * c[2])), end="") | |
# compute and adjust scaling of signal, based on previous values | |
values.pop() | |
values.insert(0, level) | |
values_sum = sum(values) | |
if values_sum < lower_limit: | |
scale *= (1+adjust) | |
values = [125 for x in range(0,8)] | |
if debug: print(str(scale)+"+") | |
elif values_sum > upper_limit: | |
scale *= (1-adjust) | |
values = [125 for x in range(0,8)] | |
if debug: print(str(scale)+"-") | |
else: | |
if debug: print("") | |
counter = (counter + 1) % len(colors) | |
except KeyboardInterrupt: | |
print("values:") | |
for x in values: | |
print(x) | |
print("Scale: ") | |
print(scale) | |
finally: | |
print("Stopping..") | |
stream.close() | |
p.terminate() | |
def main(): | |
lpack = lightpack.lightpack('127.0.0.1', 3636, [2,4,5,3,6,7,8,9], '{d1eb3ea1-6f8b-4c02-9865-2731b7e407cd}') | |
lpack.connect() | |
lpack.lock() | |
lpack.turnOn() | |
lpack.setSmooth(100) | |
lpack.setBrightness(100) | |
soundlight(lpack) | |
def linspace(start, stop, n): | |
if n == 1: | |
yield stop | |
return | |
h = (stop - start) / (n - 1) | |
for i in range(n): | |
yield start + h * i | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Beat detection would be nice!