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'
This commit is contained in:
Scott Lawson 2017-01-01 12:14:31 -07:00
parent 0f5c05339c
commit c725d395be
2 changed files with 25 additions and 7 deletions

View File

@ -4,6 +4,7 @@ import time
import numpy as np import numpy as np
from pyqtgraph.Qt import QtGui from pyqtgraph.Qt import QtGui
import pyqtgraph as pg import pyqtgraph as pg
from pyqtgraph.dockarea import *
class GUI: class GUI:
@ -11,13 +12,19 @@ class GUI:
curve = [] curve = []
def __init__(self, width=800, height=450, title=''): def __init__(self, width=800, height=450, title=''):
# Create GUI window
self.app = QtGui.QApplication([]) self.app = QtGui.QApplication([])
self.win = pg.GraphicsWindow(title) self.win = pg.GraphicsWindow(title)
self.win.resize(width, height) self.win.resize(width, height)
self.win.setWindowTitle(title) self.win.setWindowTitle(title)
# Create GUI layout
self.layout = QtGui.QVBoxLayout()
self.win.setLayout(self.layout)
def add_plot(self, title): 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([]) self.curve.append([])
def add_curve(self, plot_index, pen=(255, 255, 255)): def add_curve(self, plot_index, pen=(255, 255, 255)):
@ -25,6 +32,7 @@ class GUI:
if __name__ == '__main__': if __name__ == '__main__':
# Example test gui
N = 48 N = 48
gui = GUI(title='Test') gui = GUI(title='Test')
# Sin plot # Sin plot
@ -34,7 +42,6 @@ if __name__ == '__main__':
# Cos plot # Cos plot
gui.add_plot(title='Cos Plot') gui.add_plot(title='Cos Plot')
gui.add_curve(plot_index=1) gui.add_curve(plot_index=1)
while True: while True:
t = time.time() t = time.time()
x = np.linspace(t, 2 * np.pi + t, N) x = np.linspace(t, 2 * np.pi + t, N)

View File

@ -162,7 +162,7 @@ def visualize_energy(y):
p[2, :b] = 255.0 p[2, :b] = 255.0
p[2, b:] = 0.0 p[2, b:] = 0.0
p_filt.update(p) 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[0, :] = gaussian_filter1d(p[0, :], sigma=4.0)
p[1, :] = gaussian_filter1d(p[1, :], sigma=4.0) p[1, :] = gaussian_filter1d(p[1, :], sigma=4.0)
p[2, :] = gaussian_filter1d(p[2, :], 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_gain.update(np.max(mel))
mel = mel / mel_gain.value mel = mel / mel_gain.value
# Visualize the filterbank output # Visualize the filterbank output
# visualize_spectrum(mel) visualization_effect(mel)
# visualize_max(mel)
# visualize_scroll(mel)
visualize_energy(mel)
GUI.app.processEvents() GUI.app.processEvents()
print('FPS {:.0f} / {:.0f}'.format(frames_per_second(), config.FPS)) 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 # Array containing the rolling audio sample window
y_roll = np.random.rand(config.N_ROLLING_HISTORY, samples_per_frame) / 1e16 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__': if __name__ == '__main__':
import pyqtgraph as pg import pyqtgraph as pg
@ -268,6 +267,18 @@ if __name__ == '__main__':
GUI.curve[0][0].setData(x=range(config.N_PIXELS)) GUI.curve[0][0].setData(x=range(config.N_PIXELS))
GUI.curve[0][1].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)) 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 # Initialize LEDs
led.update() led.update()
# Start listening to live audio stream # Start listening to live audio stream