byte[] 加倍表示
byte[] to double representation
我在读取 wav 时遇到了一些问题。
我必须读取每个样本的 wav 文件样本。
为此,我首先尝试打印我的第 128 个样本,看看我是否有好的值(我知道它们是什么)。为此:
private int bytesToInt(byte[] fenetre, int debut, int nbByte) {
int res = 0;
for (int i = 0; i < nbByte; i++) {
res <<= 8;
res += fenetre[debut + i];
}
return res;
}
public void afficherPremiereFenetre() throws IOException {
int tailleFenetre = 128;
int nbBytePerSample = au.getFormat().getFrameSize();
int nbBytePerFrame = tailleFenetre * nbBytePerSample;
byte[] fenetre = new byte[nbBytePerFrame];
au.read(fenetre, 0, nbBytePerFrame);
for (int i = 0; i < nbBytePerFrame; i += nbBytePerSample) {
System.out
.println((double) bytesToInt(fenetre, i, nbBytePerSample));
}
}
所以我的问题是:如何真正将我的 byte[] 帧转换为 double?
您可以通过 java.nio.ByteBuffer
实现
ByteBuffer.wrap(fenetre).getDouble();
Given a direct byte buffer, the Java virtual machine will make a best
effort to perform native I/O operations directly upon it. That is, it
will attempt to avoid copying the buffer's content to (or from) an
intermediate buffer before (or after) each invocation of one of the
underlying operating system's native I/O operations.
我在读取 wav 时遇到了一些问题。 我必须读取每个样本的 wav 文件样本。
为此,我首先尝试打印我的第 128 个样本,看看我是否有好的值(我知道它们是什么)。为此:
private int bytesToInt(byte[] fenetre, int debut, int nbByte) {
int res = 0;
for (int i = 0; i < nbByte; i++) {
res <<= 8;
res += fenetre[debut + i];
}
return res;
}
public void afficherPremiereFenetre() throws IOException {
int tailleFenetre = 128;
int nbBytePerSample = au.getFormat().getFrameSize();
int nbBytePerFrame = tailleFenetre * nbBytePerSample;
byte[] fenetre = new byte[nbBytePerFrame];
au.read(fenetre, 0, nbBytePerFrame);
for (int i = 0; i < nbBytePerFrame; i += nbBytePerSample) {
System.out
.println((double) bytesToInt(fenetre, i, nbBytePerSample));
}
}
所以我的问题是:如何真正将我的 byte[] 帧转换为 double?
您可以通过 java.nio.ByteBuffer
ByteBuffer.wrap(fenetre).getDouble();
Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer's content to (or from) an intermediate buffer before (or after) each invocation of one of the underlying operating system's native I/O operations.