我正在研究处理音频数据的应用程序。我正在使用Java(我添加了MP3SPI,Jlayer和Tritonus)。我将音频数据从.WAV文件中提取到字节数组。我正在使用的音频数据样本为16位立体声。

根据我阅读的一个样本格式是:

aabbccdd

其中AABB表示左通道和CCDD Rigth通道(每个通道的2个字节)。我需要将此示例转换为双值类型。我已经阅读了有关数据格式的信息。 Java使用Big Endian,WAV文件使用Little Endian。我有点困惑。您能帮我转换过程吗?谢谢大家

有帮助吗?

解决方案

警告:签署整数和字节。也许您需要在将它们包装在一起时掩盖低字节:

for (int i =0; i < length; i += 4) {
    double left = (double)((bytes [i] & 0xff) | (bytes[i + 1] << 8));
    double right = (double)((bytes [i + 2] & 0xff) | (bytes[i + 3] << 8));

    ... your code here ...

}

其他提示

When you use the ByteBuffer (java.nio.ByteBuffer) you can use the method order;

[order]

public final ByteBuffer order(ByteOrder bo)

Modifies this buffer's byte order.

Parameters:
    bo - The new byte order, either BIG_ENDIAN or LITTLE_ENDIAN
Returns:
    This buffer

After this you can get the above mentioned values with;

getChar() getShort() getInt() getFloat() getDouble()

What a great language is Java ;-)

Little Endian means that the data is in the form BBAA and DDCC. You would just swap it around.

From the beginning of the frame:

int left = (bytes[i+1] << 8) + bytes[i];
int right = (bytes[i+3] << 8) + bytes[i+2];

where i is your the index of your sample.

I would personally look for a library that does the endian swapping for you. Each audio file format has assumptions about the endianness for you and getting this right is tricky for all the bit depths/datatypes wave files support:

  • 8bit - uint8
  • 16bit - int16
  • 24bit - int32
  • 32bit - int32 as float
  • 32bit - float
  • 64bit - double

If you want to support most common types of wave files you'll need endian conversions for all of these datatypes.

I would look at ByteSwapper, which will give you byteswapping for most of the types listed above.

Its too bad Java doesn't have an endianness field in their File IO classes. Being able to simply open a file whos edianness is big or little is a much easier solution to this issue.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top