如何使用Python的条形图进行实时声音净空?

发布于 2025-02-06 12:28:15 字数 1250 浏览 1 评论 0原文

我正在尝试使用条形图和Pyaudio进行简单的声音空间。经过几次尝试后,我意识到问题与While循环有关。这让我想到..如何从循环中获取价值?主要问题是条形图重叠,使代码通过时间较慢。有可能这样做吗?

这是图表的图像:

并且想要类似的东西:

“在此处输入图像说明”

主要思想是将此图表带入级别。

这是代码:

import numpy as np 
import pyaudio as pa 
import struct 
import matplotlib.pyplot as plt


fps =   516

CHUNK = fps * 2
FORMAT = pa.paInt16
CHANNELS = 1
RATE = 44100 

p = pa.PyAudio()

stream = p.open(
    format = FORMAT,
    channels = CHANNELS,
    rate = RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

fig, ax = plt.subplots(1)
ax.set_ylim(-80,40)
fig.show()

while True:
    data = stream.read(CHUNK)

    dataInt = np.array(struct.unpack(str(CHUNK) + 'h', data))
    
    datadBFs = 20*np.log10(np.abs(np.sum(dataInt)/len(dataInt))/32768)
    ax.bar("bar1", datadBFs, align='center', alpha=0.5)
    
    fig.canvas.draw()
    fig.canvas.flush_events()
    

有什么想法吗? 非常感谢,我希望有人可以帮助我!

I am trying to do a simple sound headroom using a bar chart and Pyaudio. After several attemps I realize that the problem is related with the while loop. This makes me think.. how to get a value from the loop? The main problem is that the bar chart overlaps making the code slower throuh the time. Is it possible to do it like this?

This is the image of the chart now:
enter image description here

and want something similar to this:

enter image description here

The principal idea is to have this chart in movement with the levels.

And this is the code:

import numpy as np 
import pyaudio as pa 
import struct 
import matplotlib.pyplot as plt


fps =   516

CHUNK = fps * 2
FORMAT = pa.paInt16
CHANNELS = 1
RATE = 44100 

p = pa.PyAudio()

stream = p.open(
    format = FORMAT,
    channels = CHANNELS,
    rate = RATE,
    input=True,
    output=True,
    frames_per_buffer=CHUNK
)

fig, ax = plt.subplots(1)
ax.set_ylim(-80,40)
fig.show()

while True:
    data = stream.read(CHUNK)

    dataInt = np.array(struct.unpack(str(CHUNK) + 'h', data))
    
    datadBFs = 20*np.log10(np.abs(np.sum(dataInt)/len(dataInt))/32768)
    ax.bar("bar1", datadBFs, align='center', alpha=0.5)
    
    fig.canvas.draw()
    fig.canvas.flush_events()
    

Any Idea?
Thanks a lot and I hope someone could help me!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文