-2

I have some troubles with a wav reading. I have to read a wav file sample per sample.

To do this, I first try to print my 128 first samples to see if I have good values (I know what they are). To do that :

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));
    }
}

So my question is : how to really translate my byte[] frame to double ?

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76
onda47
  • 615
  • 2
  • 6
  • 21
  • 2
    Converting byte[] to double[]: http://stackoverflow.com/questions/2905556/how-can-i-convert-a-byte-array-into-a-double-and-back – Chris C Mar 30 '15 at 16:52

1 Answers1

1

You could achieve this with 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.

Jean-François Savard
  • 20,626
  • 7
  • 49
  • 76