2016-11-08 02:45:52 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
from __future__ import division
|
|
|
|
import time
|
|
|
|
import numpy as np
|
|
|
|
from scipy.ndimage.filters import gaussian_filter1d
|
|
|
|
import config
|
|
|
|
import microphone
|
|
|
|
import dsp
|
|
|
|
import led
|
|
|
|
|
|
|
|
_time_prev = time.time() * 1000.0
|
|
|
|
"""The previous time that the frames_per_second() function was called"""
|
|
|
|
|
2016-11-13 10:11:15 +01:00
|
|
|
_fps = dsp.ExpFilter(val=config.FPS, alpha_decay=0.002, alpha_rise=0.002)
|
2016-11-08 02:45:52 +01:00
|
|
|
"""The low-pass filter used to estimate frames-per-second"""
|
|
|
|
|
|
|
|
|
|
|
|
def frames_per_second():
|
|
|
|
"""Return the estimated frames per second
|
|
|
|
|
|
|
|
Returns the current estimate for frames-per-second (FPS).
|
|
|
|
FPS is estimated by measured the amount of time that has elapsed since
|
|
|
|
this function was previously called. The FPS estimate is low-pass filtered
|
|
|
|
to reduce noise.
|
|
|
|
|
|
|
|
This function is intended to be called one time for every iteration of
|
|
|
|
the program's main loop.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
fps : float
|
|
|
|
Estimated frames-per-second. This value is low-pass filtered
|
|
|
|
to reduce noise.
|
|
|
|
"""
|
|
|
|
global _time_prev, _fps
|
|
|
|
time_now = time.time() * 1000.0
|
|
|
|
dt = time_now - _time_prev
|
|
|
|
_time_prev = time_now
|
|
|
|
if dt == 0.0:
|
|
|
|
return _fps.value
|
|
|
|
return _fps.update(1000.0 / dt)
|
|
|
|
|
|
|
|
|
|
|
|
def interpolate(y, new_length):
|
|
|
|
"""Intelligently resizes the array by linearly interpolating the values
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
y : np.array
|
|
|
|
Array that should be resized
|
|
|
|
|
|
|
|
new_length : int
|
|
|
|
The length of the new interpolated array
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
z : np.array
|
|
|
|
New array with length of new_length that contains the interpolated
|
|
|
|
values of y.
|
|
|
|
"""
|
|
|
|
if len(y) == new_length:
|
|
|
|
return y
|
|
|
|
x_old = np.linspace(0, 1, len(y))
|
|
|
|
x_new = np.linspace(0, 1, new_length)
|
|
|
|
z = np.interp(x_new, x_old, y)
|
|
|
|
return z
|
|
|
|
|
|
|
|
|
2016-11-13 10:11:15 +01:00
|
|
|
r_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2),
|
2017-01-05 07:12:12 +01:00
|
|
|
alpha_decay=0.2, alpha_rise=0.99)
|
2016-11-13 10:11:15 +01:00
|
|
|
g_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2),
|
2017-01-05 07:12:12 +01:00
|
|
|
alpha_decay=0.05, alpha_rise=0.3)
|
2016-11-13 10:11:15 +01:00
|
|
|
b_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2),
|
2017-01-05 07:12:12 +01:00
|
|
|
alpha_decay=0.1, alpha_rise=0.5)
|
|
|
|
common_mode = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2),
|
|
|
|
alpha_decay=0.99, alpha_rise=0.01)
|
2016-11-22 05:11:45 +01:00
|
|
|
p_filt = dsp.ExpFilter(np.tile(1, (3, config.N_PIXELS // 2)),
|
2017-01-04 04:16:45 +01:00
|
|
|
alpha_decay=0.1, alpha_rise=0.99)
|
2016-12-29 23:51:38 +01:00
|
|
|
p = np.tile(1.0, (3, config.N_PIXELS // 2))
|
2016-11-22 05:11:45 +01:00
|
|
|
gain = dsp.ExpFilter(np.tile(0.01, config.N_FFT_BINS),
|
|
|
|
alpha_decay=0.001, alpha_rise=0.99)
|
|
|
|
|
|
|
|
|
|
|
|
def visualize_scroll(y):
|
2016-12-29 23:51:38 +01:00
|
|
|
"""Effect that originates in the center and scrolls outwards"""
|
2016-11-22 05:11:45 +01:00
|
|
|
global p
|
2017-01-05 07:12:12 +01:00
|
|
|
y = np.copy(y)**2.0
|
2016-11-22 05:11:45 +01:00
|
|
|
gain.update(y)
|
|
|
|
y /= gain.value
|
|
|
|
y *= 255.0
|
|
|
|
r = int(max(y[:len(y) // 3]))
|
|
|
|
g = int(max(y[len(y) // 3: 2 * len(y) // 3]))
|
|
|
|
b = int(max(y[2 * len(y) // 3:]))
|
2017-01-04 01:33:28 +01:00
|
|
|
# Scrolling effect window
|
2016-11-22 05:11:45 +01:00
|
|
|
p = np.roll(p, 1, axis=1)
|
2016-12-25 06:54:06 +01:00
|
|
|
p *= 0.98
|
2017-01-04 04:16:45 +01:00
|
|
|
p = gaussian_filter1d(p, sigma=0.2)
|
2017-01-04 01:33:28 +01:00
|
|
|
# Create new color originating at the center
|
2016-11-22 05:11:45 +01:00
|
|
|
p[0, 0] = r
|
|
|
|
p[1, 0] = g
|
|
|
|
p[2, 0] = b
|
2016-12-29 23:51:38 +01:00
|
|
|
# Update the LED strip
|
2017-01-05 07:12:12 +01:00
|
|
|
return np.concatenate((p[:, ::-1], p), axis=1)
|
2016-11-22 05:11:45 +01:00
|
|
|
|
|
|
|
|
|
|
|
def visualize_energy(y):
|
2016-12-29 23:51:38 +01:00
|
|
|
"""Effect that expands from the center with increasing sound energy"""
|
2016-11-22 05:11:45 +01:00
|
|
|
global p
|
2017-01-05 07:12:12 +01:00
|
|
|
y = np.copy(y)
|
2016-11-22 05:11:45 +01:00
|
|
|
gain.update(y)
|
|
|
|
y /= gain.value
|
2017-01-04 01:33:28 +01:00
|
|
|
# Scale by the width of the LED strip
|
|
|
|
y *= float((config.N_PIXELS // 2) - 1)
|
|
|
|
# Map color channels according to energy in the different freq bands
|
|
|
|
scale = 0.9
|
|
|
|
r = int(np.mean(y[:len(y) // 3]**scale))
|
|
|
|
g = int(np.mean(y[len(y) // 3: 2 * len(y) // 3]**scale))
|
|
|
|
b = int(np.mean(y[2 * len(y) // 3:]**scale))
|
|
|
|
# Assign color to different frequency regions
|
2016-12-29 23:51:38 +01:00
|
|
|
p[0, :r] = 255.0
|
|
|
|
p[0, r:] = 0.0
|
|
|
|
p[1, :g] = 255.0
|
|
|
|
p[1, g:] = 0.0
|
|
|
|
p[2, :b] = 255.0
|
|
|
|
p[2, b:] = 0.0
|
2016-11-22 05:11:45 +01:00
|
|
|
p_filt.update(p)
|
2017-01-01 20:14:31 +01:00
|
|
|
p = np.round(p_filt.value)
|
2017-01-04 01:33:28 +01:00
|
|
|
# Apply substantial blur to smooth the edges
|
2016-11-22 05:11:45 +01:00
|
|
|
p[0, :] = gaussian_filter1d(p[0, :], sigma=4.0)
|
|
|
|
p[1, :] = gaussian_filter1d(p[1, :], sigma=4.0)
|
|
|
|
p[2, :] = gaussian_filter1d(p[2, :], sigma=4.0)
|
2017-01-04 01:33:28 +01:00
|
|
|
# Set the new pixel value
|
2017-01-05 07:12:12 +01:00
|
|
|
return np.concatenate((p[:, ::-1], p), axis=1)
|
2016-11-22 05:11:45 +01:00
|
|
|
|
2017-01-05 08:21:09 +01:00
|
|
|
|
|
|
|
_prev_spectrum = np.tile(0.01, config.N_PIXELS // 2)
|
|
|
|
|
|
|
|
|
2016-11-22 05:11:45 +01:00
|
|
|
def visualize_spectrum(y):
|
2016-12-29 23:51:38 +01:00
|
|
|
"""Effect that maps the Mel filterbank frequencies onto the LED strip"""
|
2017-01-05 09:13:14 +01:00
|
|
|
global _prev_spectrum
|
2017-01-04 01:33:28 +01:00
|
|
|
y = np.copy(interpolate(y, config.N_PIXELS // 2))
|
2017-01-05 07:12:12 +01:00
|
|
|
common_mode.update(gaussian_filter1d(y, sigma=2.0))
|
2017-01-05 09:13:39 +01:00
|
|
|
diff = y - _prev_spectrum
|
2017-01-05 08:21:09 +01:00
|
|
|
_prev_spectrum = np.copy(y)
|
2017-01-05 07:12:12 +01:00
|
|
|
r = gaussian_filter1d(y, sigma=0.5) - common_mode.value
|
|
|
|
# g = gaussian_filter1d(y, sigma=0.5) - common_mode.value
|
|
|
|
b = gaussian_filter1d(y, sigma=0.0) - common_mode.value
|
2017-01-04 01:33:28 +01:00
|
|
|
# Update temporal filters
|
2017-01-05 07:12:12 +01:00
|
|
|
r = r_filt.update(r)
|
|
|
|
# g = g_filt.update(g)
|
|
|
|
g = np.abs(diff)
|
|
|
|
b = b_filt.update(b)
|
|
|
|
# Mirror the color channels for symmetric output
|
|
|
|
pixel_r = np.concatenate((r[::-1], r))
|
|
|
|
pixel_g = np.concatenate((g[::-1], g))
|
|
|
|
pixel_b = np.concatenate((b[::-1], b))
|
|
|
|
output = np.array([pixel_r, pixel_g, pixel_b]) * 255.0
|
|
|
|
return output
|
2016-11-22 05:11:45 +01:00
|
|
|
|
|
|
|
|
2017-01-05 07:12:12 +01:00
|
|
|
fft_plot_filter = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS),
|
|
|
|
alpha_decay=0.5, alpha_rise=0.99)
|
2016-11-22 05:11:45 +01:00
|
|
|
mel_gain = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS),
|
|
|
|
alpha_decay=0.01, alpha_rise=0.99)
|
2017-01-05 07:12:12 +01:00
|
|
|
mel_smoothing = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS),
|
|
|
|
alpha_decay=0.5, alpha_rise=0.99)
|
2016-11-08 02:45:52 +01:00
|
|
|
volume = dsp.ExpFilter(config.MIN_VOLUME_THRESHOLD,
|
|
|
|
alpha_decay=0.02, alpha_rise=0.02)
|
|
|
|
|
2017-01-04 01:33:28 +01:00
|
|
|
# Keeps track of the number of buffer overflows
|
|
|
|
# Lots of buffer overflows could mean that FPS is set too high
|
|
|
|
buffer_overflows = 1
|
2016-11-13 10:11:15 +01:00
|
|
|
|
2017-01-04 01:46:19 +01:00
|
|
|
|
2016-11-08 02:45:52 +01:00
|
|
|
def microphone_update(stream):
|
2016-11-12 00:52:57 +01:00
|
|
|
global y_roll, prev_rms, prev_exp
|
2016-12-29 23:51:38 +01:00
|
|
|
# Retrieve and normalize the new audio samples
|
2017-01-04 01:33:28 +01:00
|
|
|
try:
|
|
|
|
y = np.fromstring(stream.read(samples_per_frame), dtype=np.int16)
|
|
|
|
except IOError:
|
|
|
|
y = y_roll[config.N_ROLLING_HISTORY - 1, :]
|
|
|
|
global buffer_overflows
|
|
|
|
print('Buffer overflows: {0}'.format(buffer_overflows))
|
|
|
|
buffer_overflows += 1
|
|
|
|
# Normalize samples between 0 and 1
|
2016-11-08 02:45:52 +01:00
|
|
|
y = y / 2.0**15
|
|
|
|
# Construct a rolling window of audio samples
|
|
|
|
y_roll = np.roll(y_roll, -1, axis=0)
|
|
|
|
y_roll[-1, :] = np.copy(y)
|
|
|
|
y_data = np.concatenate(y_roll, axis=0)
|
|
|
|
volume.update(np.nanmean(y_data ** 2))
|
|
|
|
|
|
|
|
if volume.value < config.MIN_VOLUME_THRESHOLD:
|
|
|
|
print('No audio input. Volume below threshold. Volume:', volume.value)
|
2016-11-22 05:11:45 +01:00
|
|
|
led.pixels = np.tile(0, (3, config.N_PIXELS))
|
|
|
|
led.update()
|
2016-11-08 02:45:52 +01:00
|
|
|
else:
|
2016-12-29 23:51:38 +01:00
|
|
|
# Transform audio input into the frequency domain
|
2016-11-08 02:45:52 +01:00
|
|
|
XS, YS = dsp.fft(y_data, window=np.hamming)
|
2016-12-29 23:51:38 +01:00
|
|
|
# Remove half of the FFT data because of symmetry
|
2016-11-13 10:11:15 +01:00
|
|
|
YS = YS[:len(YS) // 2]
|
|
|
|
XS = XS[:len(XS) // 2]
|
2016-12-29 23:51:38 +01:00
|
|
|
# Construct a Mel filterbank from the FFT data
|
2017-01-05 07:12:12 +01:00
|
|
|
mel = np.atleast_2d(np.abs(YS)).T * dsp.mel_y.T
|
2016-12-29 23:51:38 +01:00
|
|
|
# Scale data to values more suitable for visualization
|
2017-01-05 07:12:12 +01:00
|
|
|
mel = np.mean(mel, axis=0)
|
|
|
|
mel = mel**2.0
|
|
|
|
# Gain normalization
|
|
|
|
mel_gain.update(np.max(gaussian_filter1d(mel, sigma=1.0)))
|
2016-11-08 02:45:52 +01:00
|
|
|
mel = mel / mel_gain.value
|
2017-01-05 07:12:12 +01:00
|
|
|
mel = mel_smoothing.update(mel)
|
|
|
|
# Map filterbank output onto LED strip
|
|
|
|
output = visualization_effect(mel)
|
|
|
|
led.pixels = output
|
|
|
|
led.update()
|
2017-01-06 05:42:48 +01:00
|
|
|
if config.USE_GUI:
|
|
|
|
# Plot filterbank output
|
|
|
|
x = np.linspace(config.MIN_FREQUENCY, config.MAX_FREQUENCY, len(mel))
|
|
|
|
mel_curve.setData(x=x, y=fft_plot_filter.update(mel))
|
|
|
|
# Plot the color channels
|
|
|
|
r_curve.setData(y=led.pixels[0])
|
|
|
|
g_curve.setData(y=led.pixels[1])
|
|
|
|
b_curve.setData(y=led.pixels[2])
|
2017-01-21 19:53:50 +01:00
|
|
|
if config.USE_GUI:
|
|
|
|
app.processEvents()
|
2017-01-04 01:33:28 +01:00
|
|
|
if config.DISPLAY_FPS:
|
|
|
|
print('FPS {:.0f} / {:.0f}'.format(frames_per_second(), config.FPS))
|
2016-11-08 02:45:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
# Number of audio samples to read every time frame
|
|
|
|
samples_per_frame = int(config.MIC_RATE / config.FPS)
|
|
|
|
|
|
|
|
# Array containing the rolling audio sample window
|
|
|
|
y_roll = np.random.rand(config.N_ROLLING_HISTORY, samples_per_frame) / 1e16
|
|
|
|
|
2017-01-05 07:12:12 +01:00
|
|
|
visualization_effect = visualize_spectrum
|
2017-01-01 20:14:31 +01:00
|
|
|
"""Visualization effect to display on the LED strip"""
|
2016-11-08 02:45:52 +01:00
|
|
|
|
2017-01-05 07:12:12 +01:00
|
|
|
|
2016-11-08 02:45:52 +01:00
|
|
|
if __name__ == '__main__':
|
2017-01-04 01:33:28 +01:00
|
|
|
if config.USE_GUI:
|
|
|
|
import pyqtgraph as pg
|
2017-01-04 04:16:45 +01:00
|
|
|
from pyqtgraph.Qt import QtGui, QtCore
|
2017-01-05 07:12:12 +01:00
|
|
|
# Create GUI window
|
|
|
|
app = QtGui.QApplication([])
|
|
|
|
view = pg.GraphicsView()
|
|
|
|
layout = pg.GraphicsLayout(border=(100,100,100))
|
|
|
|
view.setCentralItem(layout)
|
|
|
|
view.show()
|
|
|
|
view.setWindowTitle('Visualization')
|
|
|
|
view.resize(800,600)
|
|
|
|
# Mel filterbank plot
|
|
|
|
fft_plot = layout.addPlot(title='Filterbank Output', colspan=3)
|
|
|
|
fft_plot.setRange(yRange=[-0.1, 1.2])
|
|
|
|
fft_plot.disableAutoRange(axis=pg.ViewBox.YAxis)
|
|
|
|
x_data = np.array(range(1, config.N_FFT_BINS + 1))
|
|
|
|
mel_curve = pg.PlotCurveItem()
|
|
|
|
mel_curve.setData(x=x_data, y=x_data*0)
|
|
|
|
fft_plot.addItem(mel_curve)
|
|
|
|
# Visualization plot
|
|
|
|
layout.nextRow()
|
|
|
|
led_plot = layout.addPlot(title='Visualization Output', colspan=3)
|
|
|
|
led_plot.setRange(yRange=[-5, 260])
|
|
|
|
led_plot.disableAutoRange(axis=pg.ViewBox.YAxis)
|
|
|
|
# Pen for each of the color channel curves
|
|
|
|
r_pen = pg.mkPen((255, 30, 30, 200), width=4)
|
|
|
|
g_pen = pg.mkPen((30, 255, 30, 200), width=4)
|
|
|
|
b_pen = pg.mkPen((30, 30, 255, 200), width=4)
|
|
|
|
# Color channel curves
|
|
|
|
r_curve = pg.PlotCurveItem(pen=r_pen)
|
|
|
|
g_curve = pg.PlotCurveItem(pen=g_pen)
|
|
|
|
b_curve = pg.PlotCurveItem(pen=b_pen)
|
|
|
|
# Define x data
|
|
|
|
x_data = np.array(range(1, config.N_PIXELS + 1))
|
|
|
|
r_curve.setData(x=x_data, y=x_data*0)
|
|
|
|
g_curve.setData(x=x_data, y=x_data*0)
|
|
|
|
b_curve.setData(x=x_data, y=x_data*0)
|
|
|
|
# Add curves to plot
|
|
|
|
led_plot.addItem(r_curve)
|
|
|
|
led_plot.addItem(g_curve)
|
|
|
|
led_plot.addItem(b_curve)
|
|
|
|
# Frequency range label
|
|
|
|
freq_label = pg.LabelItem('')
|
|
|
|
# Frequency slider
|
|
|
|
def freq_slider_change(tick):
|
|
|
|
minf = freq_slider.tickValue(0)**2.0 * (config.MIC_RATE / 2.0)
|
|
|
|
maxf = freq_slider.tickValue(1)**2.0 * (config.MIC_RATE / 2.0)
|
|
|
|
t = 'Frequency range: {:.0f} - {:.0f} Hz'.format(minf, maxf)
|
|
|
|
freq_label.setText(t)
|
|
|
|
config.MIN_FREQUENCY = minf
|
|
|
|
config.MAX_FREQUENCY = maxf
|
|
|
|
dsp.create_mel_bank()
|
|
|
|
freq_slider = pg.TickSliderItem(orientation='bottom', allowAdd=False)
|
|
|
|
freq_slider.addTick((config.MIN_FREQUENCY / (config.MIC_RATE / 2.0))**0.5)
|
|
|
|
freq_slider.addTick((config.MAX_FREQUENCY / (config.MIC_RATE / 2.0))**0.5)
|
|
|
|
freq_slider.tickMoveFinished = freq_slider_change
|
|
|
|
freq_label.setText('Frequency range: {} - {} Hz'.format(
|
|
|
|
config.MIN_FREQUENCY,
|
|
|
|
config.MAX_FREQUENCY))
|
|
|
|
# Effect selection
|
|
|
|
active_color = '#16dbeb'
|
|
|
|
inactive_color = '#FFFFFF'
|
|
|
|
def energy_click(x):
|
|
|
|
global visualization_effect
|
|
|
|
visualization_effect = visualize_energy
|
|
|
|
energy_label.setText('Energy', color=active_color)
|
|
|
|
scroll_label.setText('Scroll', color=inactive_color)
|
|
|
|
spectrum_label.setText('Spectrum', color=inactive_color)
|
|
|
|
def scroll_click(x):
|
|
|
|
global visualization_effect
|
|
|
|
visualization_effect = visualize_scroll
|
|
|
|
energy_label.setText('Energy', color=inactive_color)
|
|
|
|
scroll_label.setText('Scroll', color=active_color)
|
|
|
|
spectrum_label.setText('Spectrum', color=inactive_color)
|
|
|
|
def spectrum_click(x):
|
2017-01-04 01:33:28 +01:00
|
|
|
global visualization_effect
|
2017-01-05 07:12:12 +01:00
|
|
|
visualization_effect = visualize_spectrum
|
|
|
|
energy_label.setText('Energy', color=inactive_color)
|
|
|
|
scroll_label.setText('Scroll', color=inactive_color)
|
|
|
|
spectrum_label.setText('Spectrum', color=active_color)
|
|
|
|
# Create effect "buttons" (labels with click event)
|
|
|
|
energy_label = pg.LabelItem('Energy')
|
|
|
|
scroll_label = pg.LabelItem('Scroll')
|
|
|
|
spectrum_label = pg.LabelItem('Spectrum')
|
|
|
|
energy_label.mousePressEvent = energy_click
|
|
|
|
scroll_label.mousePressEvent = scroll_click
|
|
|
|
spectrum_label.mousePressEvent = spectrum_click
|
|
|
|
energy_click(0)
|
|
|
|
# Layout
|
|
|
|
layout.nextRow()
|
|
|
|
layout.addItem(freq_label, colspan=3)
|
|
|
|
layout.nextRow()
|
|
|
|
layout.addItem(freq_slider, colspan=3)
|
|
|
|
layout.nextRow()
|
|
|
|
layout.addItem(energy_label)
|
|
|
|
layout.addItem(scroll_label)
|
|
|
|
layout.addItem(spectrum_label)
|
2016-11-08 02:45:52 +01:00
|
|
|
# Initialize LEDs
|
|
|
|
led.update()
|
|
|
|
# Start listening to live audio stream
|
|
|
|
microphone.start_stream(microphone_update)
|