我希望我能找到一种方法从 python 中的 mp3 获取幅度数据。与 audacity 类似,但我不需要视觉效果,一个简单的值数组就可以了。我希望我的代码在声音变大时在某些点对声音做出反应。我正在使用 pygame 播放音频并尝试将其转换为 sndarray 但它只给了我前 0.00018 秒。无论如何,我可以得到整个mp3吗?它不一定是实时的,因为无论如何我都希望能够提前做出反应,并且会使用 pygame 跟踪我的位置。
我正在使用树莓派来构建这个云,而不是其他功能。我已经完成了照明工作,需要它对闪电做出反应,遗憾的是 lightshowpi 不是一个选择。任何帮助将不胜感激
编辑:这就是我到目前为止所拥有的,感谢 coder-don。它有效,但我挂在 while 循环上。我不知道为什么。我使用的 mp3 相当长,这可能是问题所在吗?
import os, sys
from gi.repository import Gst, GObject
Gst.init()
GObject.threads_init()
def get_peaks(filename):
global do_run
pipeline_txt = (
'filesrc location="%s" ! decodebin ! audioconvert ! '
'audio/x-raw,channels=1,rate=22050,endianness=1234,'
'width=32,depth=32,signed=(bool)TRUE !'
'level name=level interval=1000000000 !'
'fakesink' % filename)
pipeline = Gst.parse_launch(pipeline_txt)
level = pipeline.get_by_name('level')
bus = pipeline.get_bus()
bus.add_signal_watch()
peaks = []
do_run = True
def show_peak(bus, message):
global do_run
if message.type == Gst.MESSAGE_EOS:
pipeline.set_state(Gst.State.NULL)
do_run = False
return
# filter only on level messages
if message.src is not level or \
not message.structure.has_key('peak'):
return
peaks.append(message.structure['peak'][0])
# connect the callback
bus.connect('message', show_peak)
# run the pipeline until we got eos
pipeline.set_state(Gst.State.PLAYING)
ctx = GObject.MainContext()
while ctx and do_run:
ctx.iteration()
return peaks
def normalize(peaks):
_min = min(peaks)
print(_min)
_max = max(peaks)
print(_max)
d = _max - _min
return [(x - _min) / d for x in peaks]
if __name__ == '__main__':
filename = os.path.realpath(sys.argv[1])
peaks = get_peaks(filename)
print('Sample is %d seconds' % len(peaks))
print('Minimum is', min(peaks))
print('Maximum is', max(peaks))
peaks = normalize(peaks)
print(peaks)
Run Code Online (Sandbox Code Playgroud)
使用pydub,您可以非常轻松地loudness 获取 highest amplitude文件。mp3然后,您可以使用这些参数之一来使代码/灯光对此做出反应。
从pydub网站,
\n\nAudioSegment(\xe2\x80\xa6).max
\n\nAudioSegment 中任何样本的最高振幅。对于标准化(在 pydub.effects.normalize 中提供)之类的事情很有用。
\n\nfrom pydub import AudioSegment\nsound = AudioSegment.from_file("/path/to/sound.mp3", format="mp3") \npeak_amplitude = sound.max\nRun Code Online (Sandbox Code Playgroud)\n\nAudioSegment(\xe2\x80\xa6).dBFS
\n\n返回 AudioSegment 的响度(以 dBFS 为单位)(相对于最大可能响度的 db)。最大幅度的方波约为 0 dBFS(最大响度),而最大幅度的正弦波约为 -3 dBFS。
\n\nfrom pydub import AudioSegment\nsound = AudioSegment.from_file("/path/to/sound.mp3", format="mp3")\nloudness = sound.dBFS\nRun Code Online (Sandbox Code Playgroud)\n