import pyaudio
import numpy as np
import pyqtgraph as pg
# Initialize PyAudio
pa = pyaudio.PyAudio()
# Set up audio stream
stream = pa.open(
format=pyaudio.paInt16,
channels=1,
rate=44100,
input=True,
frames_per_buffer=1024
)
# Set up PyQTGraph
app = pg.mkQApp()
win = pg.GraphicsLayoutWidget()
win.show()
plot = win.addPlot(title='Real-time Audio Waveform')
curve = plot.plot()
# Function to update the plot
def update():
wf_data = np.frombuffer(stream.read(1024), dtype=np.int16)
curve.setData(wf_data)
# Start the audio stream
timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)
# Start Qt event loop
pg.mkQApp().exec()