拙网论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 218|回复: 0

Python Real-Time Audio Frequency Monitor

[复制链接]

949

主题

1001

帖子

3736

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3736
发表于 2018-7-26 09:57:14 | 显示全部楼层 |阅读模式
https://www.swharden.com/wp/2016-07-31-real-time-audio-monitor-with-pyqt/
A new project I’m working on requires real-time analysis of soundcard input data, and I made a minimal case example of how to do this in a cross-platform way using python 3, numpy, and PyQt. Previous posts compared performance of the matplotlib widget vs PyQtGraph plotwidget and I’ve been working with PyQtGraph ever since. For static figures matplotlib is wonderful, but for fast responsive applications I’m leaning toward PyQtGraph. The full source for this project is on a github page, but here’s a summary of the project.

I made the UI with QT Designer. The graphs are QGraphicsView widgets promoted to a pyqtgraph PlotWidget. I describe how to do this in my previous post. Here’s the content of the primary program:

from PyQt4 import QtGui,QtCoreimport sysimport ui_mainimport numpy as npimport pyqtgraphimport SWHearclass ExampleApp(QtGui.QMainWindow, ui_main.Ui_MainWindow):    def __init__(self, parent=None):        pyqtgraph.setConfigOption('background', 'w') #before loading widget        super(ExampleApp, self).__init__(parent)        self.setupUi(self)        self.grFFT.plotItem.showGrid(True, True, 0.7)        self.grPCM.plotItem.showGrid(True, True, 0.7)        self.maxFFT=0        self.maxPCM=0        self.ear = SWHear.SWHear()        self.ear.stream_start()    def update(self):        if not self.ear.data is None and not self.ear.fft is None:            pcmMax=np.max(np.abs(self.ear.data))            if pcmMax>self.maxPCM:                self.maxPCM=pcmMax                self.grPCM.plotItem.setRange(yRange=[-pcmMax,pcmMax])            if np.max(self.ear.fft)>self.maxFFT:                self.maxFFT=np.max(np.abs(self.ear.fft))                self.grFFT.plotItem.setRange(yRange=[0,self.maxFFT])            self.pbLevel.setValue(1000*pcmMax/self.maxPCM)            pen=pyqtgraph.mkPen(color='b')            self.grPCM.plot(self.ear.datax,self.ear.data,                            pen=pen,clear=True)            pen=pyqtgraph.mkPen(color='r')            self.grFFT.plot(self.ear.fftx[:500],self.ear.fft[:500],                            pen=pen,clear=True)        QtCore.QTimer.singleShot(1, self.update) # QUICKLY repeatif __name__=="__main__":    app = QtGui.QApplication(sys.argv)    form = ExampleApp()    form.show()    form.update() #start with something    app.exec_()    print("DONE")
note: this project uses a gutted version of the SWHEar class which I still haven’t released on githib yet. It will likely have its own project folder. For now, take this project with a grain of salt. The primary advantage of this class is that it makes it easy to use PyAudio to automatically detect input sound cards, channels, and sample rates which are likely to succeed without requiring the user to enter any information.
All files used for this project are [size=1.4]in a GitHub folder
2016-09-05: Okko adapted this project into a screenlet (cross platform) which also includes an installer for Windows. That Githib page is here: https://github.com/ninlith/audio-visualizer-screenlet Below is a screenshot of me running it on my Windows 10 machine

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|抱朴守拙BBS

GMT+8, 2025-5-26 07:08 , Processed in 0.176483 second(s), 19 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表