-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxApp.py
111 lines (85 loc) · 3.61 KB
/
xApp.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
import sys
import time
import asyncio
from quamash import QEventLoop
import pyaudio
from PyQt5.QtWidgets import QDialog, QApplication
from GUI import *
from logic import *
from xAudioControl import xAudio
class GuitarTuner(QDialog):
t = TunerLogic("Ukulele")
# Initialize audio
xAudio
def __init__(self):
super().__init__()
self.ui = Ui_Dialog()
self.ui.setupUi(self)
# self.ui.startBtn_3.clicked.connect(lambda: self.setFrequency("1000 Hz"))
# self.ui.startBtn_3.clicked.connect(lambda: self.setLeftPercentageBar(100))
self.ui.startBtn_3.clicked.connect(self.invokeAsync)
self.ui.stopBtn_3.clicked.connect(self.pauseAsync)
self.ui.instrumentChooserComboxBox_3.currentIndexChanged.connect(
lambda: self.setFrequency(self.ui.instrumentChooserComboxBox_3.currentText()))
self.show()
def invokeAsync(self):
asyncio.ensure_future(
self.updt(0.0001, self.ui.levelLeft_Progress_3, self.ui.levelRight_Progress_3, self.ui.curentKeyLabel_3,
self.ui.frequencyLabel_3))
xAudio.stopStreamStatus = True
def pauseAsync(self):
# GuitarTuner.syncStatus = False
xAudio.streamStatus = False
@staticmethod
async def updt(delay, ProgressBar1, ProgressBar2, label1, label2):
# xAudio.startStream()
while True:
await asyncio.sleep(delay)
if (xAudio.streamStatus):
xAudio.count += 1
xAudio.buf[:-xAudio.FRAME_SIZE] = xAudio.buf[xAudio.FRAME_SIZE:]
xAudio.buf[-xAudio.FRAME_SIZE:] = np.fromstring(
xAudio.stream.read(xAudio.FRAME_SIZE), np.int16)
xAudio.fft = np.fft.rfft(xAudio.buf * xAudio.window)
xAudio.freq = (np.abs(xAudio.fft[xAudio.imin:xAudio.imax]).argmax(
) + xAudio.imin) * xAudio.FREQ_STEP
xAudio.n = xAudio.freq_to_number(xAudio.freq)
xAudio.n0 = int(round(xAudio.n))
xAudio.num_frames += 1
if xAudio.num_frames >= xAudio.FRAMES_PER_FFT:
print(str(xAudio.count)+' freq: {:7.2f} Hz note: {:>3s} {:+.2f}'.format(
xAudio.freq, xAudio.note_name(xAudio.n0), xAudio.n - xAudio.n0))
windowx.setKey('{:>3s}'.format(
xAudio.note_name(xAudio.n0)))
windowx.setFrequency('({:7.2f} Hz)'.format(xAudio.freq))
else:
await asyncio.sleep(delay)
def stopper(loop):
loop.stop()
def setKey(self, key):
self.ui.curentKeyLabel_3.setText(str(key))
def setFrequency(self, freq):
self.ui.frequencyLabel_3.setText(str(freq))
def setLeftPercentageBar(self, val):
self.ui.levelLeft_Progress_3.setProperty("value", val)
def setRightPercentageBar(self, val):
self.ui.levelRight_Progress_3.setProperty("value", val)
def setPercentageBars(self, leftValue, rightValue):
self.ui.levelLeft_Progress_3.setProperty("value", leftValue)
self.ui.levelRight_Progress_3.setProperty("value", rightValue)
def getInstrument(self):
instrument = str(self.ui.instrumentChooserComboxBox_3.currentText())
return instrument
t = TunerLogic(getInstrument)
if __name__ == "__main__":
app = QApplication(sys.argv)
# hijack event loop and have control over it
loop = QEventLoop(app)
asyncio.set_event_loop(loop)
# class instance created
windowx = GuitarTuner()
windowx.exec()
with loop:
loop.run_forever()
loop.close()
sys.exit(app.exec_())