-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioBoxDialog.py
More file actions
247 lines (196 loc) · 8.37 KB
/
AudioBoxDialog.py
File metadata and controls
247 lines (196 loc) · 8.37 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AppletDialog.py
MIT License (c) Faure Systems <dev at faure dot systems>
Dialog to control PluginProps app running on Raspberry.
"""
import os
import codecs
import configparser
from constants import *
from AudioBoxSettingsDialog import AudioBoxSettingsDialog
from AudioOutputNotFoundDialog import AudioOutputNotFoundDialog
from AppletDialog import AppletDialog
from LedWidget import LedWidget
from PyQt5.QtGui import QIcon
from PyQt5.QtMultimedia import QAudioDeviceInfo, QAudioOutput, QAudio, QAudioFormat
from PyQt5.QtCore import Qt, pyqtSignal, pyqtSlot, QSize, QPoint, QBuffer, QIODevice, QByteArray, QFile, QIODevice
from PyQt5.QtWidgets import QHBoxLayout, QVBoxLayout, QPushButton, QComboBox
class AudioBoxDialog(AppletDialog):
aboutToClose = pyqtSignal()
publishMessage = pyqtSignal(str, str)
switchLed = pyqtSignal(str, str)
# __________________________________________________________________
def __init__(self, title, icon, inbox, logger):
self._inbox = inbox
self._effects = {}
self._effectButtons = {}
self._sources = {}
super().__init__(title, icon, logger)
if 'output' in self._settings['parameters']:
current_output = self._settings['parameters']['output']
found = False
for info in QAudioDeviceInfo.availableDevices(QAudio.AudioOutput):
if info.deviceName() == current_output:
found = True
break
if not found:
self.warnignDialog()
self.loadEffects()
try:
if UNSTOPPABLE:
self.setWindowFlag(Qt.Tool)
except:
self.setWindowFlags(self.windowFlags() & ~Qt.WindowContextHelpButtonHint)
try:
if ALWAYS_ON_TOP:
self.setAttribute(Qt.WA_AlwaysStackOnTop)
self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint)
except:
pass
# __________________________________________________________________
def _buildUi(self):
self._settings = configparser.ConfigParser()
ini = 'settings.ini'
if os.path.isfile(ini):
self._settings.read_file(codecs.open(ini, 'r', 'utf8'))
if 'parameters' not in self._settings.sections():
self._settings.add_section('parameters')
main_layout = QVBoxLayout()
main_layout.setSpacing(12)
self._led = LedWidget(BROKET_NAME, QSize(40, 20))
self._led.setRedAsBold(True)
self._led.setRedAsRed(True)
self._led.switchOn('gray')
settings_button = QPushButton()
settings_button.setIcon(QIcon("./images/settings.svg"))
settings_button.setFlat(True)
settings_button.setToolTip(self.tr("Configuration"))
settings_button.setIconSize(QSize(16, 16))
settings_button.setFixedSize(QSize(24, 24))
header_layout = QHBoxLayout()
header_layout.addWidget(self._led)
header_layout.addWidget(settings_button, Qt.AlignRight)
main_layout.addLayout(header_layout)
for command, (title, _) in AUDIO_EFFECTS.items():
button = QPushButton(title)
self._effectButtons[button] = command
button.pressed.connect(self.onEffectButton)
main_layout.addWidget(button)
main_layout.addStretch(0)
self.setLayout(main_layout)
settings_button.pressed.connect(self.onSettingsButton)
self.switchLed.connect(self._led.switchOn)
# __________________________________________________________________
def closeEvent(self, e):
try:
if UNSTOPPABLE:
e.ignore()
except:
self.aboutToClose.emit()
# __________________________________________________________________
@pyqtSlot()
def onConnectedToMqttBroker(self):
self._led.switchOn('green')
# __________________________________________________________________
def loadEffects(self):
output_devices = {}
for info in QAudioDeviceInfo.availableDevices(QAudio.AudioOutput):
if info.deviceName() not in output_devices:
output_devices[info.deviceName()] = info
info = QAudioDeviceInfo.defaultOutputDevice()
if 'output' in self._settings['parameters'] and self._settings['parameters']['output'] in output_devices:
print(self._settings['parameters']['output'])
info = output_devices[self._settings['parameters']['output']]
else:
self._settings['parameters']['output'] = info.deviceName()
format = info.preferredFormat()
self._effects.clear()
for command, (_, file) in AUDIO_EFFECTS.items():
output = QAudioOutput(info, format, self)
output.stateChanged.connect(self.onOutputStateChanged)
source = QFile(file)
source.open(QIODevice.ReadOnly)
source.seek(0)
self._effects[command] = (output, source)
self._sources[output] = source
# __________________________________________________________________
@pyqtSlot()
def onDisconnectedToMqttBroker(self):
self._led.switchOn('red')
# __________________________________________________________________
@pyqtSlot()
def onEffectButton(self):
button = self.sender()
if button in self._effectButtons:
print(self._settings['parameters']['output'])
print(button.text(), self._effectButtons[button])
command = self._effectButtons[button]
try:
output, source = self._effects[command]
print(output.state())
if output.state() == QAudio.ActiveState:
output.stop()
source.seek(0)
output.start(source)
except Exception as e:
self._logger.error(self.tr("Kick audio effect failed, command not found: {}".format(command)))
self._logger.debug(e)
# __________________________________________________________________
@pyqtSlot(str, str)
def onMessageReceived(self, topic, message):
if message.startswith("DISCONNECTED"):
self._led.switchOn('yellow')
else:
if self._led.color() != 'green':
self._led.switchOn('green')
if topic == self._inbox and message.startswith("effect:"):
_, _, command = message.partition(':')
try:
output, source = self._effects[command]
print(output.state())
if output.state() == QAudio.ActiveState:
output.stop()
source.seek(0)
output.start(source)
except Exception as e:
self._logger.error(self.tr("Kick audio effect failed, command not found: {}".format(command)))
self._logger.debug(e)
# __________________________________________________________________
@pyqtSlot(QAudio.State)
def onOutputStateChanged(self, state):
output = self.sender()
if state == QAudio.ActiveState:
print(output, 'Active')
elif state == QAudio.SuspendedState:
print(output, 'Suspended')
elif state == QAudio.StoppedState:
print(output, 'Stopped')
elif state == QAudio.IdleState:
print(output, 'Idle')
if output in self._sources:
self._sources[output].seek(0)
elif state == QAudio.InterruptedState:
print(output, 'Interrupted')
else:
print(output, 'Unexpected')
# __________________________________________________________________
@pyqtSlot()
def onSettingsButton(self):
dlg = AudioBoxSettingsDialog(self._settings, self._logger)
dlg.setModal(True)
dlg.move(self.pos() + QPoint(20, 20))
dlg.exec()
with open('settings.ini', 'w') as configfile:
self._settings.write(configfile)
self.loadEffects()
# __________________________________________________________________
def warnignDialog(self):
dlg = AudioOutputNotFoundDialog(self._settings, self._logger)
dlg.setModal(True)
dlg.move(self.pos() + QPoint(20, 20))
dlg.exec()
with open('settings.ini', 'w') as configfile:
self._settings.write(configfile)
self.loadEffects()