Skip to content

Commit 0cd5f93

Browse files
committed
still ongoin project
1 parent 8a9a457 commit 0cd5f93

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

buffered stereo pyqtgraph.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
from multiprocessing import Process, Queue
2+
import pyqtgraph.Qt as qt
3+
import pyqtgraph as pg
4+
import soundcard as sc
5+
import numpy as np
6+
import time
7+
import sys
8+
9+
colorArray = ['#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3',
10+
'#808000', '#ffd8b1', '#000075', '#808080', '#ffffff',]
11+
12+
mics = sc.all_microphones(include_loopback=True)
13+
print(mics)
14+
# audioIn=mics[int(input("Choose input device: "))]
15+
audioIn = mics[1]
16+
RATE = 48000
17+
18+
CHUNK = 1024
19+
BUFFER = 20000
20+
LEN = BUFFER + CHUNK
21+
22+
channelsCNT = audioIn.channels
23+
channels = range(0, audioIn.channels)
24+
25+
app = qt.QtWidgets.QApplication(sys.argv)
26+
27+
win = pg.GraphicsLayoutWidget(title="Buffered plot") # creates a window
28+
win.setGeometry(100, 100, 900, 300)
29+
30+
row = 0
31+
p = win.addPlot(row, 0, colspan=channelsCNT, title="All Channels: ") # creates empty space for the plot in the window
32+
p.setYRange(-1, 1, padding=0)
33+
p.setXRange(0, LEN, padding=0)
34+
curves = []
35+
for channel in channels:
36+
curve = p.plot(pen=colorArray[channel]) # create an empty "plot"
37+
curve.setPos(0, 0) # set x position in the graph to 0
38+
curves.append([curve])
39+
40+
if channelsCNT > 1:
41+
row += 1
42+
for channel in channels:
43+
p = win.addPlot(row, channel, title="Channel " + str(channel)) # creates empty space for the plot in the window
44+
curve = p.plot(pen=colorArray[channel]) # create an empty "plot"
45+
p.setYRange(-1, 1, padding=0)
46+
p.setXRange(0, LEN, padding=0)
47+
curve.setPos(0, 0) # set x position in the graph to 0
48+
curves[channel].append(curve)
49+
50+
51+
def stream(q):
52+
with audioIn.recorder(samplerate=RATE) as mic:
53+
while 1:
54+
# start = time.time()
55+
data = mic.record(numframes=CHUNK)
56+
q.put(data)
57+
time.sleep(0.001) # avoid overuse of resources
58+
# print("---->", time.time() - start)
59+
60+
61+
def update(q: Queue):
62+
win.show()
63+
plotData = np.zeros((LEN, channelsCNT))
64+
while True:
65+
while True:
66+
try:
67+
data = q.get_nowait()
68+
# print(q.qsize())
69+
except:
70+
app.processEvents()
71+
break
72+
shift = len(data)
73+
# print(shift)
74+
plotData = np.roll(plotData, -shift, axis=0)
75+
plotData[-shift:, :] = data
76+
for channel in channels:
77+
for c in curves[channel]:
78+
c.setData(plotData[:,channel])
79+
app.processEvents()
80+
time.sleep(0.001)
81+
82+
83+
if __name__ == "__main__":
84+
q = Queue()
85+
p1 = Process(target=update, args=(q,))
86+
p3 = Process(target=stream, args=(q,))
87+
p3.start()
88+
p1.start()
89+
p1.join()
90+
p3.join()

fft pyqtgraph.py

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from multiprocessing import Process, Queue
2+
import pyqtgraph.Qt as qt
3+
import pyqtgraph as pg
4+
import soundcard as sc
5+
import numpy as np
6+
import time
7+
import sys
8+
import scipy.signal as scipySignal
9+
10+
colorArray = ['#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3',
11+
'#808000', '#ffd8b1', '#000075', '#808080', '#ffffff',]
12+
13+
mics = sc.all_microphones(include_loopback=True)
14+
print(mics)
15+
# audioIn=mics[int(input("Choose input device: "))]
16+
audioIn = mics[1]
17+
RATE = 48000
18+
19+
CHUNK = 2048
20+
BUFFER = 20000
21+
LEN = BUFFER + CHUNK
22+
23+
channelsCNT = audioIn.channels
24+
channels = range(0, audioIn.channels)
25+
26+
app = qt.QtWidgets.QApplication(sys.argv)
27+
28+
win = pg.GraphicsLayoutWidget(title="Buffered plot") # creates a window
29+
win.setGeometry(100, 100, 900, 300)
30+
31+
row = 0
32+
33+
curves=[]
34+
for channel in channels:
35+
p = win.addPlot(row, channel, title="Channel " + str(channel)) # creates empty space for the plot in the window
36+
curve = p.plot(pen=colorArray[channel]) # create an empty "plot"
37+
p.setYRange(0, 1, padding=0)
38+
p.setXRange(0, CHUNK//2, padding=0)
39+
curve.setPos(0, 0) # set x position in the graph to 0
40+
curves.append(curve)
41+
42+
if channelsCNT > 1:
43+
row += 1
44+
45+
46+
47+
def stream(q):
48+
with audioIn.recorder(samplerate=RATE) as mic:
49+
while 1:
50+
# start = time.time()
51+
data = mic.record(numframes=CHUNK)
52+
q.put(data)
53+
time.sleep(0.001) # avoid overuse of resources
54+
# print("---->", time.time() - start)
55+
56+
57+
def update(q: Queue):
58+
win.show()
59+
while True:
60+
while True:
61+
try:
62+
data = q.get_nowait()
63+
# print(q.qsize())
64+
except:
65+
app.processEvents()
66+
break
67+
# signal = np.sum(data, axis=1).real
68+
69+
for channel in channels:
70+
signal = np.sum(data, axis=1).real
71+
if channel==0:
72+
signal = scipySignal.detrend(signal)
73+
fft = np.abs(np.fft.fft(signal)) * 2 / (CHUNK // 2)
74+
fft = fft[:int(len(fft) / 2)]
75+
curves[channel].setData(fft)
76+
app.processEvents()
77+
time.sleep(0.001)
78+
79+
80+
if __name__ == "__main__":
81+
q = Queue()
82+
p1 = Process(target=update, args=(q,))
83+
p3 = Process(target=stream, args=(q,))
84+
p3.start()
85+
p1.start()
86+
p1.join()
87+
p3.join()

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
numpy==1.19.4
2+
pyqtgraph==0.12.4
3+
scipy==1.8.1
4+
SoundCard==0.4.2

0 commit comments

Comments
 (0)