Java File Read via ByteBuffer readWavHeader(DataInputStream inStrm, FileChannel fc)

Here you can find the source of readWavHeader(DataInputStream inStrm, FileChannel fc)

Description

read Wav Header

License

Open Source License

Declaration

private static boolean readWavHeader(DataInputStream inStrm, FileChannel fc) 

Method Source Code

//package com.java2s;

import java.io.DataInputStream;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import java.nio.channels.FileChannel;

public class Main {
    public static int srcChannels, dstChannels, srcBPS;
    public static int srcSamplingRate, dstSamplingRate, dstBPS;
    public static long length = 0;

    private static boolean readWavHeader(DataInputStream inStrm, FileChannel fc) {
        byte[] buf = new byte[16];
        int size;
        ByteBuffer bb = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN);
        bb.clear();/*w  w  w  .  j  a v  a 2s  . co m*/
        try {
            inStrm.readInt();

            inStrm.readFully(buf, 0, 8);
            if (!"WAVEfmt ".equals(new String(buf, 0, 8)))
                return false;

            inStrm.readFully(buf, 0, 4);
            size = bb.getInt();
            if (size > buf.length) {
                buf = new byte[size];
                bb = ByteBuffer.wrap(buf).order(ByteOrder.LITTLE_ENDIAN);
            }
            inStrm.readFully(buf, 0, size);
            bb.clear();

            if (bb.getShort() != 1) {
                System.err.println(String.format("Error: Only PCM is supported."));
                return false;
            }
            srcChannels = bb.getShort();
            srcSamplingRate = bb.getInt();
            srcBPS = bb.getInt();
            if ((int) srcBPS % srcSamplingRate * srcChannels != 0)
                return false;

            srcBPS /= srcSamplingRate * srcChannels;

            byte[] cbuf = new byte[4];
            for (;;) {
                inStrm.readFully(cbuf, 0, 4);
                try {
                    bb.clear();
                    inStrm.readFully(buf, 0, 4);
                    length = (long) bb.getInt();
                } catch (IOException ex) {
                    break;
                }
                if ("data".equals(new String(cbuf, 0, 4)))
                    break;
                fc.position(fc.position() + length);
            }
            if (fc.position() > fc.size()) {
                System.err.println(String.format("Couldn't find data chank"));
                return false;
            }

            if (srcBPS != 1 && srcBPS != 2 && srcBPS != 3 && srcBPS != 4) {
                System.err.println(String.format("Error : Only 8bit, 16bit, 24bit and 32bit PCM are supported."));
                return false;
            }

        } catch (IOException e1) {
            System.err.println("Error reading file header");
            return false;
        }
        return true;
    }
}

Related

  1. readUnsignedInt(InputStream is)
  2. readUnsignedInt24(InputStream is)
  3. readUtf8(DataInput in)
  4. readVarLong(byte[] arr)
  5. readWaveFile44100_16Bit_Mono(String dir, String name)
  6. readWholeFile(File file, StringBuilder stringBuilder)
  7. readZipComment(File file)
  8. skip(final ReadableByteChannel input, final long toSkip)
  9. streamRead(SocketChannel in)