简体   繁体   中英

Pyglet - Getting sound frequency

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

Anyone who lands here, including you 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) .

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 .

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 . 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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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