This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_noise.py
150 lines (117 loc) · 4.06 KB
/
make_noise.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
import os
import sys
import argparse
import subprocess
import atexit
from PySide2 import QtCore, QtWidgets, QtGui
if sys.platform == "linux":
SOX = "sox"
AUDIODEVICE = "alsa"
elif sys.platform == "win32":
SOX = "bin\\sox.exe"
AUDIODEVICE = "waveaudio"
PID = None
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
class TrayIcon(QtWidgets.QSystemTrayIcon):
def __init__(self, icon, parent):
global SOX
super().__init__()
self.parent = parent
self.play_button = None
self.sox = SOX
self.subp = None
self.is_playing = False
self.setIcon(icon)
self.init_widgets()
def init_widgets(self):
menu = QtWidgets.QMenu()
self.menu_toggle = menu.addAction("Play")
self.menu_toggle.triggered.connect(self.toggle_noise)
self.menu_toggle.setIcon(QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
exit_ = menu.addAction("Exit")
exit_.triggered.connect(self.parent.quit)
self.setContextMenu(menu)
self.activated.connect(self.on_activated)
def on_activated(self, reason):
if reason == self.DoubleClick:
self.toggle_noise()
def toggle_noise(self):
global AUDIODEVICE
if self.is_playing:
try:
clean_up(pid=self.subp.pid)
self.is_playing = False
self.menu_toggle.setIcon(QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
self.menu_toggle.setText("Play")
icon = QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_DialogYesButton)
self.setIcon(icon)
except:
pass
else:
sox_path = self.sox
# print(sox_path, flush=True)
audio_device = '-t ' + AUDIODEVICE
args = [
sox_path,
'--no-show-progress',
'-c 2',
'--null',
audio_device,
'synth 3600 brownnoise',
'band -n 1500 499',
'tremolo 0.05 43',
'reverb 19',
'bass -11 ',
'treble -1',
'vol 14dB ',
'fade q .01',
'repeat 9999',
]
the_call = ' '.join(args)
# print('HERE: ', the_call, flush=True)
self.subp = subprocess.Popen(the_call, shell=True)
global PID
PID = self.subp.pid
self.is_playing = True
self.menu_toggle.setIcon(QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_MediaStop))
self.menu_toggle.setText("Stop")
icon = QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_DialogNoButton)
self.setIcon(icon)
def clean_up(pid=None):
if pid:
try:
if sys.platform == "linux":
os.system("kill %i" % pid)
elif sys.platform == "win32":
os.system("taskkill /F /T /PID %i" % pid)
except:
pass
else:
try:
global PID
if sys.platform == "linux":
os.system("kill %i" % PID)
elif sys.platform == "win32":
os.system("taskkill /F /T /PID %i" % PID)
except:
pass
try:
global SOX
if sys.platform == "linux":
os.system("pkill %s" % SOX)
elif sys.platform == "win32":
os.system("taskkill /F /T /IM %s" % SOX)
except:
pass
atexit.register(clean_up)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
icon = QtWidgets.QApplication.style().standardIcon(QtWidgets.QStyle.SP_DialogYesButton)
tray_icon = TrayIcon(icon, app)
tray_icon.setToolTip('Make some noise!')
tray_icon.show()
app.aboutToQuit.connect(clean_up)
sys.exit(app.exec_())