-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_gui.py
1181 lines (927 loc) · 50.7 KB
/
config_gui.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import json
import pyaudio
import os
import time
import asyncio
import pyi_splash
import pyi_splash
from pywizlight import wizlight
from PyQt5.QtCore import QProcess, QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from pycaw.utils import AudioUtilities, AudioDeviceState
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QVBoxLayout, QHBoxLayout, QWidget, QLabel, QLineEdit,
QPushButton, QGroupBox, QFormLayout, QScrollArea, QTabWidget, QTextEdit, QComboBox, QMessageBox, QColorDialog,
)
class LightStateFetcher:
def __init__(self, ip, update_callback):
self.light = wizlight(ip)
self.update_callback = update_callback
self.running = True
async def fetch_state(self):
"""Fetch the state of the WiZ light periodically."""
while self.running:
if self.update_callback: # Ensure the callback is defined
if self.running: # Check if the visualizer is running
try:
await self.light.updateState()
rgb = self.light.state.get_rgb()
if rgb:
self.update_callback(rgb) # Emit signal to update light icon
except Exception as e:
print(f"Error fetching light state: {e}")
await asyncio.sleep(0.01) # Poll every 10ms, adjust as necessary
def stop(self):
"""Stop the fetch loop."""
self.running = False
def calibrate_silence_threshold(self):
self.calibration_process = QProcess(self)
self.calibration_process.setProgram("wiz_visualizer_freq")
self.calibration_process.setArguments(["--calibrate", f"--device={self.config.get('audio_device', 'default')}"])
self.calibration_process.readyReadStandardOutput.connect(self.handle_calibration_output)
self.calibration_process.finished.connect(self.handle_calibration_finished)
self.calibration_process.start()
def handle_calibration_output(self):
output = self.calibration_process.readAllStandardOutput().data().decode()
print(output) # Or display in a text widget
def handle_calibration_finished(self):
QMessageBox.information(self, "Calibration Complete", "Calibration completed successfully.")
self.config = self.load_config("config.json")
self.populate_settings(self.config)
class CalibrationThread(QThread):
# Signal to notify when calibration is done
calibration_done = pyqtSignal(str)
def __init__(self, duration, device, parent=None):
super().__init__(parent)
self.duration = duration
self.device = device
def run(self):
try:
import subprocess
# Run the calibration process in the background
print(f"Running calibration for {self.duration} seconds with device {self.device}")
result = subprocess.run(
["wiz_visualizer_freq.exe", "--calibrate", f"--duration={self.duration}", f"--device={self.device}"],
capture_output=True,
text=True
)
# Emit the signal to notify that calibration is done
if result.returncode == 0:
self.calibration_done.emit("Calibration completed successfully.")
else:
self.calibration_done.emit(f"Calibration failed: {result.stderr}")
except Exception as e:
self.calibration_done.emit(f"An error occurred during calibration: {str(e)}")
class LightUpdateThread(QThread):
update_signal = pyqtSignal(tuple) # Signal to update the UI
def __init__(self, ip):
super().__init__()
self.ip = ip
self.fetcher = None
def run(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
self.fetcher = LightStateFetcher(self.ip, self.emit_update)
loop.run_until_complete(self.fetcher.fetch_state())
def emit_update(self, rgb):
"""Emit the updated RGB values as a signal."""
self.update_signal.emit(rgb)
def stop(self):
"""Stop the fetcher."""
if self.fetcher:
self.fetcher.stop()
def load_icon():
"""
Load the program's icon dynamically, considering both development and packaged environments.
"""
if getattr(sys, '_MEIPASS', False): # If running as a packaged app
base_path = sys._MEIPASS
else:
base_path = os.path.abspath(".")
icon_path = os.path.join(base_path, 'icon', 'freq.ico')
print(f"Trying to load icon from: {icon_path}") # Debug statement
return QIcon(icon_path)
def get_default_input_device():
"""
Get the name and index of the default audio input device (recording device) using PyAudio.
"""
p = pyaudio.PyAudio()
try:
# Get default input device info (recording device)
default_device_index = p.get_default_input_device_info()["index"]
default_device_info = p.get_device_info_by_index(default_device_index)
print("Default input device info:", default_device_info) # Debug statement
return default_device_index, default_device_info["name"]
except Exception as e:
print(f"Error retrieving default input device: {e}")
return None, "Unknown"
finally:
p.terminate()
def load_stylesheet(app, theme_name="dark"):
"""
Load a stylesheet based on the theme name from the 'themes' folder.
"""
if getattr(sys, 'frozen', False): # If running as a packaged app
base_path = os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'themes')
else:
base_path = os.path.join(os.path.abspath("."), 'themes')
theme_path = os.path.join(base_path, f"{theme_name}.qss")
print(f"Trying to load stylesheet from: {theme_path}") # Debug statement
try:
with open(theme_path, "r") as f:
stylesheet = f.read()
app.setStyleSheet(stylesheet)
except FileNotFoundError:
print(f"Stylesheet not found: {theme_path}")
def load_theme_effects(theme_name):
"""
Load theme effects from the JSON settings based on the theme name.
"""
if getattr(sys, 'frozen', False): # If running as a packaged app
base_path = os.path.join(os.path.dirname(os.path.abspath(sys.executable)), 'themes')
else:
base_path = os.path.join(os.path.abspath("."), 'themes')
effects_path = os.path.join(base_path, 'theme_effects.json')
print(f"Trying to load theme effects from: {effects_path}") # Debug statement
try:
with open(effects_path, "r") as f:
effects = json.load(f)
return effects.get(theme_name, {})
except FileNotFoundError:
print("Theme effects file not found.")
return {}
except json.JSONDecodeError:
print("Error parsing theme effects file.")
return {}
class ConfigEditor(QMainWindow):
def __init__(self, theme_name='dark'):
super().__init__()
self.setWindowTitle("Frequency Config Editor")
self.setGeometry(100, 100, 400, 600)
self.setWindowIcon(load_icon())
# Define a dictionary to map configuration keys to display names
self.display_names = {
'reversal_interval': 'Reverse Interval',
'sample_rate': 'Sample Rate',
'frames_per_buffer': 'Frames Per Buffer',
'num_channels': 'Number of Channels',
'udp_port': 'UDP Port',
'min_update_interval_ms': 'Minimum Update Interval (ms)',
'frequency_sensitivity_threshold': 'Frequency Sensitivity Threshold',
'dynamic_threshold': 'Dynamic Threshold',
'target_brightness': 'Target Brightness',
'current_brightness': 'Current Brightness',
'is_dimmed': 'Is Dimmed',
'hysteresis_counter': 'Hysteresis Counter'
# Add more mappings as needed
}
# Load configuration files
self.default_config = self.load_config('default.json')
self.config = self.load_config('config.json') # Load user config here
self.visualizer_running = False # Track if the visualizer is running or not
# Setup UI
self.init_ui()
# Load saved config when the program starts
self.load_config('config.json')
# QProcess to manage the C++ program
self.process = QProcess(self)
# Populate UI with loaded configuration
self.populate_settings(self.config)
def calibrate_silence_threshold(self):
reply = QMessageBox.question(
self,
'Calibrate Silence Threshold',
'Ensure no audio is playing and the environment is silent. Do you want to proceed with calibration?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.Yes:
# Get the calibration duration from the input field or default to 5 seconds
calibration_duration = int(self.calibration_duration_input.text()) if self.calibration_duration_input.text().isdigit() else 5
audio_device = self.config.get("audio_device", "default")
# Create the calibration thread
self.calibration_thread = CalibrationThread(calibration_duration, audio_device)
# Connect the thread's signal to the update method
self.calibration_thread.calibration_done.connect(self.on_calibration_done)
# Start the calibration thread
self.calibration_thread.start()
# Inform the user that the calibration has started
QMessageBox.information(self, "Calibration", "Calibration has started. Please wait...")
def on_calibration_done(self, message):
# Show a message box when calibration is complete or failed
QMessageBox.information(self, "Calibration Status", message)
# Optionally, reload the config and update the GUI with the new threshold
self.config = self.load_config("config.json")
self.populate_settings(self.config)
def populate_settings(self, config):
"""
Populate UI with loaded configuration.
"""
# Update audio device input
default_device = config.get('audio_device', '')
self.audio_device_input.setCurrentText(default_device)
# Update lights configuration
for i, light in enumerate(config.get('lights', [])):
light_ip_input = getattr(self, f'light_{i + 1}_ip', None)
light_effect_input = getattr(self, f'light_{i + 1}_effect', None)
light_colors_input = getattr(self, f'light_{i + 1}_colors', None)
if light_ip_input and light_effect_input and light_colors_input:
light_ip_input.setText(light['ip'])
light_effect_input.setCurrentText(light['effect'])
colors_str = ";".join(",".join(map(str, color)) for color in light['colors'])
light_colors_input.setText(colors_str)
# Update general settings
for key, value in config.get('general_settings', {}).items():
general_input = getattr(self, f'general_{key}', None)
if general_input:
if isinstance(general_input, QComboBox):
general_input.setCurrentText(str(value))
elif isinstance(general_input, QLineEdit):
general_input.setText(str(value))
# Update advanced settings
for key, value in config.get('advanced_settings', {}).items():
advanced_input = getattr(self, f'advanced_{key}', None)
if advanced_input:
if isinstance(advanced_input, QComboBox):
advanced_input.setCurrentText(str(value))
elif isinstance(advanced_input, QLineEdit):
advanced_input.setText(str(value))
def load_config(self, filename):
"""
Load a configuration file.
Prioritizes reading from the executable's directory in packaged mode.
"""
# Determine the base path for the executable or script
if getattr(sys, 'frozen', False): # Running as a packaged executable
base_path = os.path.dirname(sys.executable) # Directory of the .exe
else: # Running in development mode
base_path = os.path.dirname(os.path.abspath(__file__)) # Script directory
# Construct the full path to the configuration file
config_path = os.path.join(base_path, filename)
try:
with open(config_path, 'r') as file:
return json.load(file) # Assuming the config files are in JSON format
except FileNotFoundError:
print(f"Configuration file '{filename}' not found at {config_path}.")
return {} # Return an empty dict or handle as appropriate
except json.JSONDecodeError:
print(f"Error decoding JSON from the file '{filename}'.")
return {} # Return an empty dict or handle as appropriate
# Audio Device
content_layout.addWidget(QLabel("Audio Device:"))
self.audio_device_input = QComboBox()
content_layout.addWidget(self.audio_device_input)
# Initialize device name input field
self.device_name_input = QLineEdit(self)
self.device_name_input.setPlaceholderText("Manually enter device name")
content_layout.addWidget(self.device_name_input)
# Refresh devices button
self.refresh_devices_button = QPushButton("Refresh Devices", self)
self.refresh_devices_button.clicked.connect(self.refresh_audio_devices)
content_layout.addWidget(self.refresh_devices_button)
# Color input fields for lights
for i in range(len(self.config['lights'])):
light_layout = QVBoxLayout()
light_ip_label = QLabel(f"Light {i + 1} IP:")
light_ip_input = QLineEdit(self)
light_ip_input.setPlaceholderText("Enter light IP")
light_effect_label = QLabel(f"Light {i + 1} Effect:")
light_effect_input = QComboBox()
light_effect_input.addItems(["CHANGE_COLOR", "ANOTHER_EFFECT"]) # Add your actual effects here
# Adding color input fields
light_colors_label = QLabel(f"Light {i + 1} Colors (R,G,B):")
light_colors_input = QLineEdit(self)
light_colors_input.setPlaceholderText("Enter colors as R,G,B;R,G,B")
# Add the fields to the layout
light_layout.addWidget(light_ip_label)
light_layout.addWidget(light_ip_input)
light_layout.addWidget(light_effect_label)
light_layout.addWidget(light_effect_input)
light_layout.addWidget(light_colors_label)
light_layout.addWidget(light_colors_input)
# Store the input fields in attributes for later access
setattr(self, f'light_{i + 1}_ip', light_ip_input)
setattr(self, f'light_{i + 1}_effect', light_effect_input)
setattr(self, f'light_{i + 1}_colors', light_colors_input)
content_layout.addLayout(light_layout)
# Finalize layout setup
central_widget = QWidget(self)
central_widget.setLayout(content_layout)
self.setCentralWidget(central_widget)
# Other initializations...
self.audio_device_combo = QtWidgets.QComboBox(self)
p = pyaudio.PyAudio()
self.audio_device_combo.clear() # Clear previous entries
self.audio_device_combo.addItem("Select an audio device") # Placeholder
max_width = max([self.audio_device_input.fontMetrics().width(device_info['name']) for device_info in devices])
self.audio_device_input.setMinimumWidth(max_width + 20) # Adjust for padding
self.device_name_input = QtWidgets.QLineEdit(self)
self.device_name_input.setPlaceholderText("Manually enter device name")
self.refresh_devices_button = QtWidgets.QPushButton("Refresh Devices", self)
self.refresh_devices_button.clicked.connect(self.refresh_audio_devices)
# Layout setup
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.audio_device_combo)
layout.addWidget(self.device_name_input)
layout.addWidget(self.refresh_devices_button)
def refresh_audio_devices(self):
"""
Refresh the list of audio devices, showing the index and name in the dropdown,
but saving only the device name in the config.
"""
self.audio_device_input.clear() # Clear previous entries
self.audio_device_input.addItem("Select an audio device") # Placeholder
p = pyaudio.PyAudio()
for i in range(p.get_device_count()):
device_info = p.get_device_info_by_index(i)
# Format as "[index] Device Name" for the dropdown
self.audio_device_input.addItem(f"[{i}] {device_info['name']}")
# Update the default device label
self.update_default_device_label()
p.terminate()
def update_default_device_label(self):
"""
Update the default device label with the index and name of the default input device
when the user refreshes the device list.
"""
default_device_index, default_device_name = get_default_input_device()
if default_device_index is not None:
self.default_device_label.setText(f"Default Input Device: {default_device_name} (Index: {default_device_index})")
else:
self.default_device_label.setText(f"Default Input Device: Unknown (Index: -1)")
def populate_audio_devices(self):
"""
Populate the audio devices dropdown with available devices and select the saved device from config.
"""
self.audio_device_input.clear() # Clear previous entries
self.audio_device_input.addItem("Select an audio device") # Placeholder
p = pyaudio.PyAudio()
saved_device_index = self.config.get('audio', {}).get('device_index', None) # Load saved device index
saved_device_name = self.config.get('audio', {}).get('audio_device', '') # Load saved device name
selected_device_name = None # To hold the name of the selected device
selected_device_index = None # To hold the selected device index
# Populate the devices in the dropdown
for i in range(p.get_device_count()):
device_info = p.get_device_info_by_index(i)
device_name = device_info["name"]
self.audio_device_input.addItem(f"[{i}] {device_name}")
# If we find a match with the saved index, select this device
if saved_device_index == i:
selected_device_index = i
selected_device_name = device_name # Store the matching name
# After populating, we ensure the correct device is selected based on index and name
if selected_device_index is not None:
# Set the selected index in the dropdown
index_to_select = self.audio_device_input.findText(f"[{selected_device_index}] {selected_device_name}")
if index_to_select >= 0:
self.audio_device_input.setCurrentIndex(index_to_select) # Select the device
p.terminate()
def init_ui(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
# Create a horizontal layout for the top section (including the label and the light icon)
top_layout = QHBoxLayout()
# Add the default device label first
self.default_device_label = QLabel("Default Output Device: Fetching...")
# Add some stretch to push the light icon to the right
top_layout.addWidget(self.default_device_label)
# Add the stretch to push the light icon to the right side
top_layout.addStretch()
# Light icon
self.light_icon = QLabel(self)
self.light_icon.setFixedSize(20, 20) # Set the size of the mini icon
self.light_icon.setStyleSheet("background-color: rgb(169, 169, 169); "
"border: 1px solid black; "
"border-radius: 10px;") # Greyed-out icon initially
# Add the light icon to the layout
top_layout.addWidget(self.light_icon)
# Optionally, adjust the spacing between the icon and label
top_layout.setSpacing(10) # Adjust the spacing between the light icon and the label
# Add this layout to the main layout
main_layout.addLayout(top_layout)
# Fetch and set the default device name
default_device = get_default_input_device()
self.default_device_label.setText(f"Default Output Device: {default_device}")
# Add Reset Button
reset_button = QPushButton("Reset to Default")
reset_button.clicked.connect(self.reset_to_default)
main_layout.addWidget(reset_button)
# Create a tab widget
self.tabs = QTabWidget()
main_layout.addWidget(self.tabs)
# Create the configuration tab
config_tab = QWidget()
self.setup_config_tab(config_tab)
self.tabs.addTab(config_tab, "Configuration")
# Create the help tab
help_tab = QWidget()
self.setup_help_tab(help_tab)
self.tabs.addTab(help_tab, "Help")
# Call populate_audio_devices to auto-select saved device
self.populate_audio_devices()
# Start and Stop buttons
self.start_button = QPushButton("Start Program")
self.start_button.clicked.connect(self.start_program)
main_layout.addWidget(self.start_button)
self.stop_button = QPushButton("Stop Program")
self.stop_button.clicked.connect(self.stop_program)
main_layout.addWidget(self.stop_button)
# Get the first light's IP from the configuration (default to '192.168.1.73' if no lights are configured)
self.first_light_ip = self.config.get('lights', [{}])[0].get('ip', '192.168.1.73')
# Initialize the LightUpdateThread with the first light's IP
self.light_thread = LightUpdateThread(self.first_light_ip) # Use the IP of the first light
self.light_thread.update_signal.connect(self.update_light_icon)
self.light_thread.start()
def set_light_icon_active(self):
"""Set the light icon to active (shows current color)"""
self.light_icon.setStyleSheet("background-color: rgb(0, 255, 0); "
"border: 1px solid black; "
"border-radius: 10px;") # Active color (green in this case)
def set_light_icon_grey(self):
"""Set the light icon to grey when the visualizer is not running"""
self.light_icon.setStyleSheet("background-color: rgb(169, 169, 169); "
"border: 1px solid black; "
"border-radius: 10px;") # Grey color
def update_light_icon(self, rgb):
"""Update the light icon with the new RGB values."""
if self.visualizer_running:
r, g, b = rgb
self.light_icon.setStyleSheet(f"background-color: rgb({r}, {g}, {b}); "
"border: 1px solid black; "
"border-radius: 10px;") # Update color and keep rounded shape
else:
self.set_light_icon_grey() # Ensure the icon is grey if visualizer isn't running
def update_light_icon_from_state(self):
"""Fetch and update the light icon based on the actual light state."""
ip = "192.168.1.73" # Replace with your light's IP
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# Create a LightStateFetcher object to fetch the light's state
light_state_fetcher = LightStateFetcher(ip, self.update_light_icon)
loop.run_until_complete(light_state_fetcher.fetch_state())
def update_light_ip(self, new_ip):
"""
Update the light IP dynamically when the user saves the config.
Stops the current light update thread and starts a new one with the updated IP.
"""
# Stop the current thread if it's running
if self.light_thread is not None:
self.light_thread.stop()
# Update the IP in the config and UI (optional)
self.first_light_ip = new_ip
self.config['lights'][0]['ip'] = new_ip # Update the IP of the first light in the config
# Restart the LightUpdateThread with the new IP
self.light_thread = LightUpdateThread(self.first_light_ip)
self.light_thread.update_signal.connect(self.update_light_icon)
self.light_thread.start()
def reset_to_default(self):
reply = QMessageBox.question(
self,
'Reset to Default',
'Are you sure you want to reset to default settings?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.Yes:
try:
# Determine the base path for finding the default.json and saving config.json
if getattr(sys, 'frozen', False): # Running as a packaged executable
base_path = os.path.dirname(sys.executable) # Directory of the .exe
else: # Running in development mode
base_path = os.path.dirname(os.path.abspath(__file__)) # Script directory
default_path = os.path.join(base_path, 'default.json')
config_path = os.path.join(base_path, 'config.json')
# Read default configuration
with open(default_path, 'r') as default_file:
self.config = json.load(default_file)
# Write default configuration to config.json
with open(config_path, 'w') as config_file:
json.dump(self.config, config_file, indent=4)
print("Configuration reset to default values.")
# Refresh the UI to reflect default values
self.audio_device_input.setCurrentText(self.config.get('audio_device', ''))
# Define available light effects
available_effects = ["CHANGE_COLOR", "ADJUST_BRIGHTNESS", "TURN_OFF_ON"]
for i, light in enumerate(self.config.get('lights', [])):
light_ip_input = getattr(self, f'light_{i + 1}_ip', None)
light_effect_input = getattr(self, f'light_{i + 1}_effect', None)
light_colors_input = getattr(self, f'light_{i + 1}_colors', None)
if light_ip_input and light_effect_input and light_colors_input:
light_ip_input.setText(light['ip'])
effect_input = QComboBox()
effect_input.addItems(available_effects) # Add available effects to ComboBox
effect_input.setCurrentText(light['effect']) # Set the currently saved effect
colors_str = ";".join(",".join(map(str, color)) for color in light['colors'])
light_colors_input.setText(colors_str)
red_color_label = getattr(self, f'light_{i + 1}_red_color_label', None)
green_color_label = getattr(self, f'light_{i + 1}_green_color_label', None)
blue_color_label = getattr(self, f'light_{i + 1}_blue_color_label', None)
if red_color_label and green_color_label and blue_color_label:
red_color_label.setStyleSheet(
f"background-color: rgb({light['colors'][0][0]},{light['colors'][0][1]},{light['colors'][0][2]});"
)
green_color_label.setStyleSheet(
f"background-color: rgb({light['colors'][1][0]},{light['colors'][1][1]},{light['colors'][1][2]});"
)
blue_color_label.setStyleSheet(
f"background-color: rgb({light['colors'][2][0]},{light['colors'][2][1]},{light['colors'][2][2]});"
)
for key, value in self.config.get('advanced_settings', {}).items():
advanced_input = getattr(self, f'advanced_{key}', None)
if advanced_input:
if isinstance(advanced_input, QComboBox):
advanced_input.setCurrentText(str(value))
elif isinstance(advanced_input, QLineEdit):
advanced_input.setText(str(value))
for key, value in self.config.get('general_settings', {}).items():
general_input = getattr(self, f'general_{key}', None)
if general_input:
if isinstance(general_input, QComboBox):
general_input.setCurrentText(str(value))
elif isinstance(general_input, QLineEdit):
general_input.setText(str(value))
# Refresh the lights layout to reflect changes
self.populate_lights()
except FileNotFoundError:
print(f"Default configuration file '{default_path}' not found.")
except json.JSONDecodeError:
print(f"Error decoding JSON from '{default_path}'.")
def setup_config_tab(self, tab):
layout = QVBoxLayout(tab)
# Create a scroll area for the configuration settings
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
# Create a content widget for the scroll area
content_widget = QWidget()
content_layout = QVBoxLayout(content_widget)
self.calibration_duration_input = QLineEdit(self)
self.calibration_duration_input.setPlaceholderText("Calibration duration (default: 5 seconds)")
content_layout.addWidget(QLabel("Calibration Duration:"))
content_layout.addWidget(self.calibration_duration_input)
# Inside setup_config_tab
calibrate_button = QPushButton("Calibrate Silence Threshold")
calibrate_button.clicked.connect(self.calibrate_silence_threshold)
content_layout.addWidget(calibrate_button)
# Audio Device
content_layout.addWidget(QLabel("Audio Device:"))
self.audio_device_input = QComboBox()
content_layout.addWidget(self.audio_device_input)
# Initialize device name input field
self.device_name_input = QLineEdit(self)
self.device_name_input.setPlaceholderText("Manually enter device name")
content_layout.addWidget(self.device_name_input)
# Refresh devices button
self.refresh_devices_button = QPushButton("Refresh Devices", self)
self.refresh_devices_button.clicked.connect(self.refresh_audio_devices)
content_layout.addWidget(self.refresh_devices_button)
self.populate_audio_devices() # Call to populate audio devices
# Lights Configuration
lights_group = QGroupBox("Lights Configuration")
self.lights_layout = QFormLayout()
self.populate_lights()
lights_group.setLayout(self.lights_layout)
content_layout.addWidget(lights_group)
# Buttons to add and remove lights
add_light_button = QPushButton("Add Light")
add_light_button.clicked.connect(self.add_light)
content_layout.addWidget(add_light_button)
# Add Remove Light button
remove_light_button = QPushButton("Remove Light")
remove_light_button.clicked.connect(self.remove_light)
content_layout.addWidget(remove_light_button)
# General Settings
general_group = QGroupBox("General Settings")
general_layout = QFormLayout()
for key, value in self.config['general_settings'].items():
# Get the display name from the dictionary, or format it if not found
label = self.display_names.get(key, key.replace('_', ' ').capitalize())
if isinstance(value, bool):
# Create a dropdown for boolean values
combo = QComboBox()
combo.addItems(["True", "False"])
combo.setCurrentText(str(value)) # Set the current value
general_layout.addRow(f"{label}:", combo)
setattr(self, f'general_{key}', combo) # Store for saving later
else:
# Handle non-boolean values
value_input = QLineEdit(str(value))
general_layout.addRow(f"{label}:", value_input)
setattr(self, f'general_{key}', value_input) # Store for saving later
general_group.setLayout(general_layout)
content_layout.addWidget(general_group)
# Advanced Settings
self.advanced_group = QGroupBox("Advanced Settings")
self.advanced_layout = QFormLayout()
for key, value in self.config['advanced_settings'].items():
label = self.display_names.get(key, key.replace('_', ' ').capitalize())
if isinstance(value, bool):
combo = QComboBox()
combo.addItems(["True", "False"])
combo.setCurrentText(str(value))
self.advanced_layout.addRow(f"{label}:", combo)
setattr(self, f'advanced_{key}', combo) # Store for saving later
else:
value_input = QLineEdit(str(value))
self.advanced_layout.addRow(f"{label}:", value_input)
setattr(self, f'advanced_{key}', value_input) # Store for saving later
self.advanced_group.setLayout(self.advanced_layout)
self.advanced_group.setVisible(False) # Start hidden
content_layout.addWidget(self.advanced_group)
# Button to toggle advanced settings visibility
self.toggle_advanced_button = QPushButton("Show Advanced Settings")
self.toggle_advanced_button.clicked.connect(self.toggle_advanced_settings)
content_layout.addWidget(self.toggle_advanced_button)
# Save Button
save_button = QPushButton("Save Config")
save_button.clicked.connect(self.save_config)
content_layout.addWidget(save_button)
# Set the layout to the content widget and set it to the scroll area
content_widget.setLayout(content_layout)
scroll_area.setWidget(content_widget)
# Add the scroll area to the configuration tab
layout.addWidget(scroll_area)
def populate_lights(self):
# Clear existing rows in the layout to prevent duplicates
while self.lights_layout.rowCount() > 0:
self.lights_layout.removeRow(0)
# Define available light effects
available_effects = ["CHANGE_COLOR", "ADJUST_BRIGHTNESS", "TURN_OFF_ON"]
for i, light in enumerate(self.config.get('lights', [])):
ip_input = QLineEdit(light['ip'])
# Create a ComboBox for selecting light effect
effect_input = QComboBox()
effect_input.addItems(available_effects) # Add available effects to ComboBox
effect_input.setCurrentText(light['effect']) # Set the currently saved effect
# Initialize color boxes
red_color_label = QLabel()
red_color_label.setFixedSize(50, 20)
red_color_label.setStyleSheet("background-color: rgb({},{},{});".format(*light['colors'][0]))
red_color_label.mousePressEvent = lambda event, index=i: self.open_color_picker(index, 'red', red_color_label)
green_color_label = QLabel()
green_color_label.setFixedSize(50, 20)
green_color_label.setStyleSheet("background-color: rgb({},{},{});".format(*light['colors'][1]))
green_color_label.mousePressEvent = lambda event, index=i: self.open_color_picker(index, 'green', green_color_label)
blue_color_label = QLabel()
blue_color_label.setFixedSize(50, 20)
blue_color_label.setStyleSheet("background-color: rgb({},{},{});".format(*light['colors'][2]))
blue_color_label.mousePressEvent = lambda event, index=i: self.open_color_picker(index, 'blue', blue_color_label)
red_button = QPushButton("Select Red Color")
green_button = QPushButton("Select Green Color")
blue_button = QPushButton("Select Blue Color")
# Connect buttons to open color picker
red_button.clicked.connect(lambda checked, index=i: self.open_color_picker(index, 'red', red_color_label))
green_button.clicked.connect(lambda checked, index=i: self.open_color_picker(index, 'green', green_color_label))
blue_button.clicked.connect(lambda checked, index=i: self.open_color_picker(index, 'blue', blue_color_label))
self.lights_layout.addRow(f"Light {i + 1} IP Address:", ip_input)
self.lights_layout.addRow(f"Light {i + 1} Effect:", effect_input)
self.lights_layout.addRow("Red Color:", red_color_label)
self.lights_layout.addRow(red_button)
self.lights_layout.addRow("Green Color:", green_color_label)
self.lights_layout.addRow(green_button)
self.lights_layout.addRow("Blue Color:", blue_color_label)
self.lights_layout.addRow(blue_button)
setattr(self, f'light_{i + 1}_ip', ip_input)
setattr(self, f'light_{i + 1}_effect', effect_input)
def open_config_editor(self):
current_theme = self.theme_combo.currentText()
# Determine if running in packaged mode or development mode
if getattr(sys, 'frozen', False):
# If frozen (packaged mode), use sys._MEIPASS for extracting files
base_path = sys._MEIPASS
else:
# If running in development mode, use current directory
base_path = os.path.dirname(os.path.abspath(__file__))
# Define the path to the script (config_gui.py)
script_path = os.path.join(base_path, 'config_gui.py')
# Call the script with the theme as a command-line argument
subprocess.Popen([sys.executable, script_path, current_theme])
def open_color_picker(self, index, color_component, color_label):
color = QColorDialog.getColor()
if color.isValid():
rgb = [color.red(), color.green(), color.blue()]
if color_component == 'red':
self.config['lights'][index]['colors'][0] = rgb
color_label.setStyleSheet(f"background-color: rgb({rgb[0]}, {rgb[1]}, {rgb[2]});")
elif color_component == 'green':
self.config['lights'][index]['colors'][1] = rgb
color_label.setStyleSheet(f"background-color: rgb({rgb[0]}, {rgb[1]}, {rgb[2]});")
elif color_component == 'blue':
self.config['lights'][index]['colors'][2] = rgb
color_label.setStyleSheet(f"background-color: rgb({rgb[0]}, {rgb[1]}, {rgb[2]});")
def add_light(self):
light_number = len(self.config['lights']) + 1
# Set default colors for the new light, change as needed
default_colors = [[255, 0, 0], [0, 255, 0], [0, 0, 255]] # Example RGB colors
new_light = {
"ip": f"192.168.1.{light_number + 70}", # Example IP generation
"effect": "CHANGE_COLOR",
"colors": default_colors # Add the colors array
}
self.config['lights'].append(new_light)
self.populate_lights() # Refresh the UI to show the new light
def remove_light(self, index):
reply = QMessageBox.question(
self,
'Remove Light',
'Are you sure you want to remove this light?',
QMessageBox.Yes | QMessageBox.No,
QMessageBox.No
)
if reply == QMessageBox.Yes:
if 0 <= index < len(self.config['lights']):
# Remove the light from the configuration
del self.config['lights'][index]
# Refresh the lights layout to reflect changes
self.populate_lights()
print(f"Light {index + 1} removed.")
def setup_help_tab(self, tab):
layout = QVBoxLayout(tab)
# Add help text
help_text = QTextEdit()
help_text.setReadOnly(True)
help_text.setPlainText(
"Configuration Guide\n\n"
"Audio Device\n"
"audio_device: Sets the input audio device for capturing sound. Default: "
"\"alsa_output.pci-0000_00_1b.0.analog-stereo.monitor\"\n\n"
"Lights Setup\n"
"lights: An array defining each light's configuration.\n\n"
"Network and IP Settings\n"
"ip: The IP address for connecting to your light. Example: \"192.168.1.65\"\n"
"UDP_PORT: The port used for sending commands to the lights. Default: 38899\n\n"
"Lighting Effects\n"
"effect: Choose an effect type to control light behavior. Options: \"CHANGE_COLOR\", "
"\"ADJUST_BRIGHTNESS\", \"TURN_OFF_ON\"\n"
"colors: Specify RGB color values for lights in a sequence. Example: "
"[[255, 0, 0], [0, 255, 0], [0, 0, 255]]\n\n"
"Audio Processing Settings\n"
"SAMPLE_RATE: Sets the audio sample rate in Hz. Default: 48000\n"
"FRAMES_PER_BUFFER: Number of frames processed per buffer, affecting audio smoothness. Default: 256\n"
"NUM_CHANNELS: Number of audio channels used. Default: 2\n\n"
"Visualizer Tuning Options\n"
"MIN_UPDATE_INTERVAL_MS: (Caution: may cause the light to go offline if set too low) Minimum time between updates, in milliseconds. Default: 100\n"
"FREQUENCY_SENSITIVITY_THRESHOLD: Sensitivity threshold for frequency response. Default: 2.5 (ow = less sensitive to freq, high = more sensitive)\n"
"dynamic_threshold: Base level for automatic threshold adjustment. Default: 0.0\n\n"
"Brightness and Dimming\n"
"target_brightness: Desired brightness level for the lights. Default: 255\n"
"current_brightness: Current set brightness level. Default: 255\n"
"is_dimmed: Indicates whether lights are in a dimmed state. Default: false\n"
"dimming_factor: Adjusts the rate of light dimming. Default: 0.001\n"
"gradual_brightness_recovery: Enable gradual brightness recovery after dimming. Default: true\n"
"brightness_multiplier: Adjusts brightness based on audio intensity. Default: 2\n\n"
"Color and Pattern Settings\n"
"reversal_interval: Time interval (ms) for reversing colors. Default: 5000\n"
"reverse_colors: Enable reversing of the color sequence. Default: false\n"
"random_reversal_interval: Use random intervals for color reversals. Default: false\n"
"enable_interpolation: Enable smooth color transitions between changes. Default: true\n\n"
"Beat Detection\n"
"enable_beat_detection: Enable detection of beats for synchronized lighting. Default: false\n\n"
"Additional Tuning Options\n"
"off_effect_delay_ms: Delay (ms) for turning lights off and on. Default: 50\n"
"hysteresis_counter: Initial counter value for visual stabilizing effects. Default: 0\n"
"hysteresis_limit: Maximum count before adjusting lights based on audio data. Default: 50\n"
"recent_energies_size: Buffer size for tracking recent audio energies. Default: 50\n"
"sensitivity_multiplier: Adjusts the overall sensitivity to sound. Default: 1.6\n"
"enable_smoothing: Smooths audio input for more even lighting. Default: false\n"
"enable_silence_threshold: Enable or disable processing silent audio. Default: true\n"
"silence_threshold: Threshold for silence detection, higher values ensure no silence is processed. Default: 0.02\n"