Obtain wave pattern of a audio file in Java

 ̄綄美尐妖づ 提交于 2019-11-30 04:52:38

问题


I'm wondering how I may obtain musical information such as amplitude from a audio file?

Suppose we have a raw audio file, what I want to extract data from the file which allows me to generate a curve such as http://www1.icsi.berkeley.edu/Speech/mr/nearfar.html. Once I have obtained this curve, I will perform FFT, etc.

I have been trying to search for solutions in Java Sound, but what I only know so far is that I can pour the data into byte[] using AudioInputStream. But how do I translate that byte[] into a double[] which contains actual information about the sound? In this case, the values in the double[] represent amplitude.

Could anyone please provide a solution to solve this problem in Java?


回答1:


Typical wav file is 16 bit little endian, so you can take each two consecutive bytes, b1 and b2, and get the amplitude as follows:

(double) (b2 << 8 | b1 & 0xFF) / 32767.0;

if the input is stereo, first two bytes would be left channel, then two bytes for right channel.



来源:https://stackoverflow.com/questions/13024683/obtain-wave-pattern-of-a-audio-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!