-
Notifications
You must be signed in to change notification settings - Fork 0
/
pqcd.py
85 lines (69 loc) · 2.34 KB
/
pqcd.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
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile, QIODevice, Slot, QThreadPool
import threading
import socket
import time
import pyaudio
from worker import Worker
window = None
audio = None
threadpool = None
worker = None
chunk = 64
def freq_monitor():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("127.0.0.1", 7356))
while worker.exiting is False:
time.sleep(1)
sock.send(bytes("f\n", "ascii"))
frequency = int(str(sock.recv(1024), "ascii"))
worker.frequency = frequency
window.frequency.setText(str(frequency) + " Hz")
worker.log.frequency = frequency
@Slot()
def record():
global worker
if window.record.text() == "Stop":
worker.exiting = True
window.record.setText("Record")
else:
window.record.setText("Stop")
if worker and worker.exiting:
worker = Worker(window, audio)
threadpool.start(worker)
freq_monitor_thread = threading.Thread(target=freq_monitor)
freq_monitor_thread.start()
def resizeEvent(self, event):
print("Resize")
def myExitHandler():
worker.exiting = True
if __name__ == "__main__":
app = QApplication(sys.argv)
ui_file_name = "pqcd.ui"
ui_file = QFile(ui_file_name)
if not ui_file.open(QIODevice.ReadOnly):
print(f"Cannot open {ui_file_name}: {ui_file.errorString()}")
sys.exit(-1)
loader = QUiLoader()
window = loader.load(ui_file)
ui_file.close()
if not window:
print(loader.errorString())
sys.exit(-1)
window.show()
resizeEvent.__get__(window)
audio = pyaudio.PyAudio() # Create an interface to PortAudio
for i in range(audio.get_device_count()):
window.device.addItem(audio.get_device_info_by_index(i)["name"])
window.device.setCurrentText("pulse")
threadpool = QThreadPool()
worker = Worker(window, audio)
window.record.clicked.connect(record)
window.chunksize.currentTextChanged.connect(worker.set_chunk_size)
window.break_threshold.valueChanged.connect(worker.set_threshold)
window.dat_threshold.valueChanged.connect(worker.set_threshold)
window.detect_threshold.valueChanged.connect(worker.set_detect_threshold)
app.aboutToQuit.connect(myExitHandler)
sys.exit(app.exec_())