开发者

Java - read WAV file as a float array [duplicate]

开发者 https://www.devze.com 2023-01-14 08:51 出处:网络
This question already has answers here: How do I use audio sample data from Java Sound? (2 answers) Closed 7 years ago.
This question already has answers here: How do I use audio sample data from Java Sound? (2 answers) Closed 7 years ago.

I'm trying to read a WAV file (and in the future also MP3 and Ogg) file as an array of floats in Java, much like libsndfile in C.

I'm looking at the various documentati开发者_如何学编程on in the javax.sampled packages, but I can't find a clear explanation on how to do that; I'd like to avoid recurring to external libraries if possible.

Any suggestion?


JavaSound will only give you "raw" access to the sound data embedded in the WAV file, e.g. 8 bit unsigned PCM or 16 bit signed PCM data (if your WAV files are PCM encoded). In either case, the value of each sample is in the range 0 to 255 or -32768 to 32767 and you simply have to shift the range to fit in -1f to 1f.

You should start with AudioSystem.getAudioInputStream(...) using an appropriate source (File, InputStream or URL), examine the format to check the sample size and the number of channels and then you can read and convert the PCM encoded data from the AudioInputStream accordingly.


found this page:

http://www.jsresources.org/faq_audio.html

see in particular 9.4: How can I reconstruct sample values from a byte array?

It works as expected, by comparing java's output with c++ (libsndfile) and matlab (wavread). I'm going to put the code on m github account when I have time

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号