From c725d395beafd18188ba0671dc3593be6b757bcd Mon Sep 17 00:00:00 2001 From: Scott Lawson Date: Sun, 1 Jan 2017 12:14:31 -0700 Subject: [PATCH] Added ComboBox to GUI for effect selection Added a ComboBox to the GUI to allow the user to change the visualization effect without having to restart Python. The user can now select from one of the following visualizations using the GUI: 'Scroll effect', 'Spectrum effect', 'Energy effect' --- python/gui.py | 11 +++++++++-- python/visualization.py | 21 ++++++++++++++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/python/gui.py b/python/gui.py index d71ee7a..869d5c0 100644 --- a/python/gui.py +++ b/python/gui.py @@ -4,6 +4,7 @@ import time import numpy as np from pyqtgraph.Qt import QtGui import pyqtgraph as pg +from pyqtgraph.dockarea import * class GUI: @@ -11,13 +12,19 @@ class GUI: curve = [] def __init__(self, width=800, height=450, title=''): + # Create GUI window self.app = QtGui.QApplication([]) self.win = pg.GraphicsWindow(title) self.win.resize(width, height) self.win.setWindowTitle(title) + # Create GUI layout + self.layout = QtGui.QVBoxLayout() + self.win.setLayout(self.layout) def add_plot(self, title): - self.plot.append(self.win.addPlot(title=title)) + new_plot = pg.PlotWidget() + self.layout.addWidget(new_plot) + self.plot.append(new_plot) self.curve.append([]) def add_curve(self, plot_index, pen=(255, 255, 255)): @@ -25,6 +32,7 @@ class GUI: if __name__ == '__main__': + # Example test gui N = 48 gui = GUI(title='Test') # Sin plot @@ -34,7 +42,6 @@ if __name__ == '__main__': # Cos plot gui.add_plot(title='Cos Plot') gui.add_curve(plot_index=1) - while True: t = time.time() x = np.linspace(t, 2 * np.pi + t, N) diff --git a/python/visualization.py b/python/visualization.py index dd789b9..0047efc 100644 --- a/python/visualization.py +++ b/python/visualization.py @@ -162,7 +162,7 @@ def visualize_energy(y): p[2, :b] = 255.0 p[2, b:] = 0.0 p_filt.update(p) - p = p_filt.value.astype(int) + p = np.round(p_filt.value) 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) @@ -238,10 +238,7 @@ def microphone_update(stream): mel_gain.update(np.max(mel)) mel = mel / mel_gain.value # Visualize the filterbank output - # visualize_spectrum(mel) - # visualize_max(mel) - # visualize_scroll(mel) - visualize_energy(mel) + visualization_effect(mel) GUI.app.processEvents() print('FPS {:.0f} / {:.0f}'.format(frames_per_second(), config.FPS)) @@ -252,6 +249,8 @@ 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 +visualization_effect = visualize_spectrum +"""Visualization effect to display on the LED strip""" if __name__ == '__main__': import pyqtgraph as pg @@ -268,6 +267,18 @@ if __name__ == '__main__': GUI.curve[0][0].setData(x=range(config.N_PIXELS)) GUI.curve[0][1].setData(x=range(config.N_PIXELS)) GUI.curve[0][2].setData(x=range(config.N_PIXELS)) + # Add ComboBox for effect selection + effect_list = { + 'Scroll effect' : visualize_scroll, + 'Spectrum effect' : visualize_spectrum, + 'Energy effect' : visualize_energy + } + effect_combobox = pg.ComboBox(items=effect_list) + def effect_change(): + global visualization_effect + visualization_effect = effect_combobox.value() + effect_combobox.currentIndexChanged.connect(effect_change) + GUI.layout.addWidget(effect_combobox) # Initialize LEDs led.update() # Start listening to live audio stream