def effect(y, num_pixels, row_index): """Effect that maps the Mel filterbank frequencies onto the LED strip""" global prev_spectrums if row_index not in prev_spectrums: prev_spectrums[row_index] = np.tile(0.01, num_pixels // 2) if row_index not in r_filts: r_filts[row_index] = dsp.ExpFilter(np.tile(0.01, num_pixels // 2), alpha_decay=0.2, alpha_rise=0.99) if row_index not in b_filts: b_filts[row_index] = dsp.ExpFilter(np.tile(0.01, num_pixels // 2), alpha_decay=0.1, alpha_rise=0.5) if row_index not in common_modes: common_modes[row_index] = dsp.ExpFilter(np.tile(0.01, num_pixels // 2), alpha_decay=0.99, alpha_rise=0.01) y = np.copy(util.interpolate(y, num_pixels // 2)) common_modes[row_index].update(y) diff = y - prev_spectrums[row_index] prev_spectrums[row_index] = np.copy(y) # Color channel mappings r = r_filts[row_index].update(y - common_modes[row_index].value) g = np.abs(diff) b = b_filts[row_index].update(np.copy(y)) # Mirror the color channels for symmetric output r = np.concatenate((r[::-1], r)) g = np.concatenate((g[::-1], g)) b = np.concatenate((b[::-1], b)) output = np.array([r, g, b]) * 255 return output
def __init__(self): self._gain = dsp.ExpFilter(np.tile(0.01, config.N_FFT_BINS), alpha_decay=0.001, alpha_rise=0.99) self._p = np.tile(1.0, (3, config.N_PIXELS // 2)) self._p_filt = dsp.ExpFilter(np.tile(1, (3, config.N_PIXELS // 2)), alpha_decay=0.1, alpha_rise=0.99)
def __init__(self): self._r_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.2, alpha_rise=0.99) self._g_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.05, alpha_rise=0.3) self._b_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.1, alpha_rise=0.5) self._common_mode = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.99, alpha_rise=0.01) self._prev_spectrum = np.tile(0.01, config.N_PIXELS // 2)
def effect(y, num_pixels, row_index): """Effect that expands from the center with increasing sound energy""" p = np.tile(1.0, (3, num_pixels // 2)) p_filt = dsp.ExpFilter(np.tile(1, (3, num_pixels // 2)), alpha_decay=0.1, alpha_rise=0.99) y = np.copy(y) gain.update(y) y /= gain.value # Scale by the width of the LED strip y *= float((num_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 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 p_filt.update(p) p = np.round(p_filt.value) # Apply substantial blur to smooth the edges 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) # Set the new pixel value return np.concatenate((p[:, ::-1], p), axis=1)
def __init__(self): self.ctx = pyaudio.PyAudio() self.stream = self.ctx.open(format=pyaudio.paInt16, channels=1, rate=config.MIC_RATE, input=True, frames_per_buffer=self.frames_per_buffer) self.prev_ovf_time = time.time() self.mel = [] self.bands = [] self.gain = dsp.ExpFilter(np.tile(0.01, config.N_FFT_BINS), alpha_decay=0.001, alpha_rise=0.99)
def update_lamp_effect(mode): global lamp_effect global r_filt, g_filt, b_filt if (mode == 100): lamp_effect = 'BUBBLE' r_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.5, alpha_rise=0.99) g_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.5, alpha_rise=0.3) b_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.5, alpha_rise=0.5) elif (mode == 101): lamp_effect = 'BARS' r_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.1, alpha_rise=0.99) g_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.05, alpha_rise=0.3) b_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.1, alpha_rise=0.5) elif (mode == 102): lamp_effect = 'BARS_COLOR' r_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.1, alpha_rise=0.99) g_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.05, alpha_rise=0.3) b_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.1, alpha_rise=0.5)
def __init__(self): self._fft_window = np.hamming( int(config.MIC_RATE / config.FPS) * config.N_ROLLING_HISTORY) self._mel_gain = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS), alpha_decay=0.01, alpha_rise=0.99) self._mel_smoothing = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS), alpha_decay=0.2, alpha_rise=0.99) # Number of audio samples to read every time frame self._samples_per_frame = int(config.MIC_RATE / config.FPS) # Array containing the rolling audio sample window self._y_roll = np.random.rand(config.N_ROLLING_HISTORY, self._samples_per_frame) / 1e16 self._fft_plot_filter = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS), alpha_decay=0.5, alpha_rise=0.99) self._v_scroll = VScroll() self._v_energy = VEnergy() self._v_spectrum = VSpectrum()
import config import microphone import dsp from midi import MidiConnector from midi import NoteOn from midi import Message count = 0 conn = MidiConnector('/dev/serial0', 38400) _time_prev = time.time() * 1000.0 """The previous time that the frames_per_second() function was called""" _fps = dsp.ExpFilter(val=config.FPS, alpha_decay=0.2, alpha_rise=0.2) """The low-pass filter used to estimate frames-per-second""" def frames_per_second(): """Return the estimated frames per second""" 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)
import config import numpy as np import dsp from scipy.ndimage.filters import gaussian_filter1d gain = dsp.ExpFilter(np.tile(0.01, config.N_FFT_BINS), alpha_decay=0.001, alpha_rise=0.99) def effect(y, num_pixels, row_index): """Effect that expands from the center with increasing sound energy""" p = np.tile(1.0, (3, num_pixels // 2)) p_filt = dsp.ExpFilter(np.tile(1, (3, num_pixels // 2)), alpha_decay=0.1, alpha_rise=0.99) y = np.copy(y) gain.update(y) y /= gain.value # Scale by the width of the LED strip y *= float((num_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 p[0, :r] = 255.0 p[0, r:] = 0.0 p[1, :g] = 255.0 p[1, g:] = 0.0
def __init__(self, configs): self.configs = configs self.visualization_effect = self.visualize_scroll n_pixels = configs['n_pixels'] n_fft_bins = configs['n_fft_bins'] self.r_filt = dsp.ExpFilter(CONFIGS, np.tile(0.01, n_pixels // 2), alpha_decay=0.2, alpha_rise=0.99) self.g_filt = dsp.ExpFilter(CONFIGS, np.tile(0.01, n_pixels // 2), alpha_decay=0.05, alpha_rise=0.3) self.b_filt = dsp.ExpFilter(CONFIGS, np.tile(0.01, n_pixels // 2), alpha_decay=0.1, alpha_rise=0.5) self.common_mode = dsp.ExpFilter(CONFIGS, np.tile(0.01, n_pixels // 2), alpha_decay=0.99, alpha_rise=0.01) self.p_filt = dsp.ExpFilter(CONFIGS, np.tile(1, (3, n_pixels // 2)), alpha_decay=0.1, alpha_rise=0.99) self.p = np.tile(1.0, (3, n_pixels // 2)) self.gain = dsp.ExpFilter(CONFIGS, np.tile(0.01, n_fft_bins), alpha_decay=0.001, alpha_rise=0.99) self._prev_spectrum = np.tile(0.01, n_pixels // 2) ################################################ min_volume_threshold = configs['min_volume_threshold'] mic_rate = configs['mic_rate'] fps = configs['fps'] n_rolling_history = configs['n_rolling_history'] self.fft_plot_filter = dsp.ExpFilter(CONFIGS, np.tile(1e-1, n_fft_bins), alpha_decay=0.5, alpha_rise=0.99) self.mel_gain = dsp.ExpFilter(CONFIGS, np.tile(1e-1, n_fft_bins), alpha_decay=0.01, alpha_rise=0.99) self.mel_smoothing = dsp.ExpFilter(CONFIGS, np.tile(1e-1, n_fft_bins), alpha_decay=0.5, alpha_rise=0.99) self.volume = dsp.ExpFilter(CONFIGS, min_volume_threshold, alpha_decay=0.02, alpha_rise=0.02) self.fft_window = np.hamming(int(mic_rate / fps) * n_rolling_history) self.prev_fps_update = time.time() # Number of audio samples to read every time frame samples_per_frame = int(mic_rate / fps) # Array containing the rolling audio sample window self.y_roll = np.random.rand(n_rolling_history, samples_per_frame) / 1e16 self.default_dsp = dsp.ExpFilter(CONFIGS)
import time import numpy as np from scipy.ndimage.filters import gaussian_filter1d import dsp import led import microphone from config import CONFIGS _time_prev = time.time() * 1000.0 """The previous time that the frames_per_second() function was called""" _fps = dsp.ExpFilter(CONFIGS, val=CONFIGS['fps'], alpha_decay=0.2, alpha_rise=0.2) """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.
import numpy as np import config import dsp mel_trafo, (mel_x, _) = dsp.compute_melmat( num_mel_bands=config.FFT_N_BINS, freq_min=config.MIN_FREQUENCY, freq_max=config.MAX_FREQUENCY, num_fft_bands=int(config.fft_samples_per_window / 2), sample_rate=config.SAMPLE_RATE) # The fft window shape fft_window = np.hamming(config.fft_samples_per_window) mel_smoothing = dsp.ExpFilter(np.tile(1e-1, config.FFT_N_BINS), alpha_decay=0.8, alpha_rise=0.99) def visualize_waveform(_, waveform): interpolated = dsp.interpolate(waveform, config.N_PIXELS) clipped = np.clip(interpolated - 0.5, 0, 1) * 50 zeros = np.zeros(config.N_PIXELS) return np.array([zeros, zeros, zeros, clipped]) def visualize_spectrum(y, _): interpolated = dsp.interpolate(y, config.N_PIXELS) pixels = np.array([ # np.clip(1*np.log(interpolated*10), 0, 1),
from scipy.ndimage.filters import gaussian_filter1d import config import microphone import dsp import led import threading import remote_control import strip_config config = config.Config(filepath=strip_config.CONFIG_FILE, defaults=strip_config.DEFAULT_CONFIG) _time_prev = time.time() * 1000.0 """The previous time that the frames_per_second() function was called""" _fps = dsp.ExpFilter(val=config['FPS'], alpha_decay=0.2, alpha_rise=0.2) """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
import util import numpy as np from scipy.ndimage.filters import gaussian_filter1d import config import dsp r_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.2, alpha_rise=0.99) g_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), alpha_decay=0.05, alpha_rise=0.3) b_filt = dsp.ExpFilter(np.tile(0.01, config.N_PIXELS // 2), 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) p_filt = dsp.ExpFilter(np.tile(1, (3, config.N_PIXELS // 2)), alpha_decay=0.1, alpha_rise=0.99) p = np.tile(1.0, (3, config.N_PIXELS // 2)) gain = dsp.ExpFilter(np.tile(0.01, config.N_FFT_BINS), alpha_decay=0.001, alpha_rise=0.99) _prev_spectrum = np.tile(0.01, config.N_PIXELS // 2) def effect(y): global _prev_spectrum
def b(t): return np.clip(np.sin(f[2] * t + 3. * phi) * width + center, 0., 1.) x = np.tile(0.0, (length, 3)) for i in range(length): x[i][0] = r(i * dt + t) x[i][1] = g(i * dt + t) x[i][2] = b(i * dt + t) return x _time_prev = time.time() * 1000.0 """The previous time that the frames_per_second() function was called""" _fps = dsp.ExpFilter(val=config.FPS, alpha_decay=0.05, alpha_rise=0.05) """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
import time import pyaudio import dsp import config import numpy as np from scipy.ndimage.filters import gaussian_filter1d #N_FFT_BINS = 24 #MIC_RATE = 44100 #FPS = 60 #N_ROLLING_HISTORY = 2 #MIN_FREQUENCY = 200 #MAX_FREQUENCY = 12000 mel_gain = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS), alpha_decay=0.01, alpha_rise=0.99) mel_smoothing = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS), alpha_decay=0.5, alpha_rise=0.99) fft_window = np.hamming( int(config.MIC_RATE / config.FPS) * config.N_ROLLING_HISTORY) # 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 def band_data(audio_samples):
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 import gui _time_prev = time.time() * 1000.0 """The previous time that the frames_per_second() function was called""" _fps = dsp.ExpFilter(val=config.FPS, alpha_decay=0.01, alpha_rise=0.01) """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 -------
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 from tkinter import * import socket import os _time_prev = time.time() * 1000.0 """The previous time that the frames_per_second() function was called""" _fps = dsp.ExpFilter(val=config.FPS, alpha_decay=0.2, alpha_rise=0.2) """The low-pass filter used to estimate frames-per-second""" def set_energy(): global visualization_effect visualization_effect = visualize_energy ve_dsp.set('Energy') btn_enr.configure(background='black', foreground='white') btn_scr.configure(background='deep sky blue', foreground='white') btn_spc.configure(background='deep sky blue', foreground='white') def set_scroll(): global visualization_effect visualization_effect = visualize_scroll
import dsp import config import numpy as np import pyqtgraph as pg from pyqtgraph.Qt import QtGui, QtCore import visualize_energy import visualize_spectrum import led import visualize_scroll fft_plot_filter = dsp.ExpFilter(np.tile(1e-1, config.N_FFT_BINS), alpha_decay=0.5, alpha_rise=0.99) def create(): global mel_curve,r_curve,g_curve,b_curve,app # 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)