将 wav/wave 文件读入 short[] 数组 [英] reading wav/wave file into short[] array
问题描述
是否有将 wav 文件读入 short[] 数组的 api?我正在尝试将 wav 文件读入短数组.
is there an api to read wav files into an array of short[]? i'm trying to read a wav file into short array.
我找不到任何内置的 API
i couldn't find any built API for that
推荐答案
通过 AudioInputStream 将其引入,就像通常处理声音一样.
Bring it in via an AudioInputStream as one normally does for sound.
随着您从流中获取的每个缓冲区负载,一次迭代两个字节(如果是 16 位编码)并使用您最喜欢的算法将两个字节转换为单个短整数.(取决于文件是 Big-Endian 还是 Little-Endian.)
With each buffer load you grab from the stream, iterate through it, two bytes at a time (if it is 16-bit encoding) and use your favorite algorithm to convert the two bytes to a single short int. (Depends if file is Big-Endian or Little-Endian.)
然后,通过附加到您喜欢的短数组形式来保存结果,而不是像在更常见的播放情况中那样发布到 SourceDataLine.
Then, save the results by appending to your preferred form of short array, instead of posting to a SourceDataLine like is done in the more usual playback situations.
引用自 Java 教程:
Quote from Java Tutorials:
因为 Java Sound API 允许您访问音频数据作为字节数组,您可以按照您选择的任何方式更改这些字节.
Because the Java Sound API gives you access to the audio data as an array of bytes, you can alter these bytes in any way you choose.
您可以查看此 Java 声音教程中的代码:使用文件和格式转换器,读取声音文件"部分,并注意注释:在这里,对现在位于 audioBytes 数组中的音频数据做一些有用的事情..."
You might check out the code in this Java sound tutorial: Using Files and Format Converters, the section "Reading Sound Files" and note the comment: "Here, do something useful with the audio data that's now in the audioBytes array..."
例如,一些有用的东西可能是:
For example, that something useful might be:
myShortArray[i] = (short)(( buffer[i*2] & 0xff )|( buffer[i*2 + 1] << 8 ));
buffer = 接收 AudioInputStream 的字节缓冲区.
buffer = byte buffer receiving the AudioInputStream.
i = 数组的索引.
这篇关于将 wav/wave 文件读入 short[] 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!