简体   繁体   English

Pyglet-获取声音频率

[英]Pyglet - Getting sound frequency

我想获得使用media.load()加载的声音的频率,以便以后可以将其可视化,有没有办法使用pyglet做到这一点?

Anyone who lands here, including you OP. 登陆这里的任何人,包括您OP。
To navigate this information (and helpful in many other cases), try poking around on the variables you're using. 要浏览此信息(在许多其他情况下很有用),请尝试浏览正在使用的变量。

mport pyglet

music = pyglet.media.load('./test.wav')
music.play()

pyglet.app.run()

Here's a dead simple music player, and it works (avbin7 is required if you want to play .mp3 etc) . 这是一个简陋的简单音乐播放器,它可以正常工作(如果要播放.mp3等,则需要avbin7)

Now, you'd like to get the frequency? 现在,您想获得频率吗?
If you don't know how, always begin by exploring the library and/or variables by doing: 如果您不知道如何做,请务必首先通过以下步骤探索和/或变量:

print(dir(music))

This will instantly give you a dead give away of: 这将立即给您致命的赠品:

['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_audio_buffer', '_audio_stream', '_audio_stream_index', '_buffered_audio_data', '_decode_audio_packet', '_decode_video_packet', '_duration', '_ensure_video_packets', '_events', '_file', '_get_duration', '_get_packet', '_get_queue_source', '_is_queued', '_packet', '_process_packet', '_video_stream', '_video_stream_index', '_video_timestamp', 'audio_format', 'delete', 'duration', 'get_animation', 'get_audio_data', 'get_next_video_frame', 'get_next_video_timestamp', 'info', 'is_queued', 'play', 'seek', 'video_format']

Here, video_format stuck out to me, looking through the rest of the pile we find audio_format . 在这里, video_format向我伸出来,看着其余的部分,我们找到了audio_format

This should be poking you in the eye like a needle. 这应该像针一样戳您的眼睛。
Next logical step is to print that variable, either of these alternatives is a good choice: 下一步的逻辑步骤是打印该变量,以下两种选择都是不错的选择:

print(music.audio_format)
print(music.audio_format())
print(dir(audio_format))

But the first one will give you: 但是第一个会给你:

AudioFormat(channels=2, sample_size=16, sample_rate=44100)

And there it is, 44100Hz . 就是44100Hz THe frequency you're looking for. 您正在寻找的频率。

import pyglet

music = pyglet.media.load('./test.wav')
print(dir(music)) # find music.audio_format / music.video_format
print(music.audio_format) # try it out
# >>> AudioFormat(channels=2, sample_size=16, sample_rate=44100)
music.play()
pyglet.app.run()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM