实时读取音频字节数据
目标是获得一个对音乐有一定响应的简单 2D 音频可视化器。
我已经设置了基础知识,其中我有图形可以响应输入的某些数据。给定一个文件,我加载一个用于播放的音频输入流(这很好用),并让它在线程中运行。在另一个线程中,我想以接近播放的速率提取字节数据(或者可能更快,以允许延迟处理该数据)。然后,我想将其提供给 FFT 进程,并将结果数据提供给我的图形对象,该对象将使用它作为任何可视化的参数。
对于这个过程,我有两个问题:
1)如何获取字节数据并以与文件正常播放相匹配的速率处理它?使用audioInputStream 是这里的方法吗?
2)一旦我进行FFT,获得可用数据的好方法是什么(即:功率谱?以某种方式过滤掉某些频率?等等..)
The goal is to get a simple 2d audio visualizer that is somewhat responsive to the music.
I've got the basics set up, where I have graphics that will respond to some data being fed in. Given a file, I load up an audioInputStream for playback (this works fine), and have that running in a thread. In another thread, I would like to extract byte data at a rate close to the playback (or perhaps faster, to allow for delay in processing that data). I then want to feed that to an FFT process, and feed the resulting data to my graphics object that will use it as a parameter for whatever the visualization is.
I have two questions for this process:
1) How can I get the byte data and process it at a rate that will match the normal playback of a file? Is using an audioInputStream the way to go here?
2) Once I do FFT, what's a good way to get usable data (ie: power spectrum? Somehow filtering out certain frequencies? etc..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于(2)使用FFT提取“特征”的一些考虑。
只要有足够的空闲 CPU 周期,您就应该计算短期 FFT,例如 512 个点。对于可视化,没有必要保留所有信息(即使用重叠窗口),而是可以每秒计算 5 次 100ms FFT。
然后您应该计算以 dB(分贝)为单位的对数功率谱。
这会让您对声音的详细频率内容有一个很好的印象。
根据您想要可视化的内容,您可以组合一些低频 FFT 线(计算 RMS)来获取声音的“低音”内容等。
有关详细信息,请参阅这篇文章。
Some considerations about (2) using the FFT to extract "features".
You should calculate short-term FFTs for example 512 points, whenever there is enough CPU cycles free to do so. For a visualisation it is not necessary to preserve all information (i.e. work with overlapping windows), instead you could calculate a 100ms FFT 5 times per second.
Then you should calculate the logarithmic power spectrum in dB (decibel).
This gives you a pretty good impression about the detailed frequency content of your sound.
Depending on what you like to visualize you could for example combine some low frequency FFT lines (calculate the RMS) to get the "Bass" content of your sound and so on.
See this post for details.