Last active
August 29, 2015 14:09
-
-
Save edy555/c70e33a01662e15be6bd to your computer and use it in GitHub Desktop.
Sound Spectrum Display using Laser Projector and OpenLASE
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
#!/usr/bin/env python | |
import pyaudio | |
import pylase as ol | |
import numpy as np | |
import sys | |
import threading | |
chunk = 1024 | |
#chunk = 2048 | |
CHANNELS = 2 | |
DISPLAY_SIZE = 128 | |
#DISPLAY_SIZE = 256 | |
def spectrum_linear(data): | |
#d = data * np.hanning(chunk) | |
d = data * np.blackman(chunk) | |
ps = np.fft.fft(d) / chunk | |
return np.abs(ps)[0:chunk/2] | |
def spectrum_log(data): | |
d = data * np.blackman(chunk) | |
ps = np.fft.fft(d) / chunk | |
ps = np.log(np.abs(ps)**2)[0:chunk/2] | |
ps += 3 | |
ps[ps < 0] = 0 | |
return ps | |
ol.init() | |
import jack | |
jack.attach("ctrl") | |
jack.activate() | |
jack.connect("libol:out_x", "system:playback_4") | |
jack.connect("libol:out_y", "system:playback_3") | |
jack.connect("libol:out_x", "system:playback_2") | |
jack.connect("libol:out_y", "system:playback_1") | |
jack.connect("libol:out_g", "system:playback_5") | |
data = None | |
evt = threading.Event() | |
def capture(): | |
paud = pyaudio.PyAudio() | |
di = paud.get_default_input_device_info() | |
rate = int(di['defaultSampleRate']) | |
print "Audio source: %s %dHz\n" % (di['name'],rate) | |
stream = paud.open(format = pyaudio.paInt16, channels = CHANNELS, rate = rate, | |
input = True, frames_per_buffer = chunk) | |
while True: | |
global data | |
d = stream.read(chunk) | |
d = np.fromstring(d, dtype=np.int16) | |
data = d - np.average(d) | |
evt.set() | |
def draw(): | |
while True: | |
evt.wait() | |
left = data[0::2] | |
right = data[1::2] | |
evt.clear() | |
psd_l = spectrum_log(left)[16:DISPLAY_SIZE] | |
psd_r = spectrum_log(right)[16:DISPLAY_SIZE] | |
ol.loadIdentity() | |
ol.scale((0.08, -0.8)) | |
y = 1 | |
ystep = -2.0 / len(psd_r) | |
ol.begin(ol.LINESTRIP) | |
ol.vertex((0, y), ol.C_WHITE) | |
for p in psd_r: | |
y += ystep | |
x = p | |
ol.vertex((x, y), ol.C_WHITE) | |
ol.vertex((0, y), ol.C_WHITE) | |
ol.scale((-1, 1)) | |
y = 1 | |
ystep = -2.0 / len(psd_l) | |
ol.vertex((0, y), ol.C_WHITE) | |
for p in psd_l: | |
y += ystep | |
x = p | |
ol.vertex((x, y), ol.C_WHITE) | |
ol.vertex((0, y), ol.C_WHITE) | |
ol.vertex((0, 1), ol.C_WHITE) | |
ol.end() | |
ol.renderFrame(120) | |
thd = threading.Thread(target=draw, name="draw", args=()) | |
#thd = threading.Thread(target=capture, name="capture", args=()) | |
thd.daemon = True | |
thd.start() | |
capture() | |
#draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment