|
| 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() |
0 commit comments