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

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

Description

read Aiff Header

License

Open Source License

Declaration

private static boolean readAiffHeader(DataInputStream inStrm, FileChannel fc) 

Method Source Code

//package com.java2s;

import java.io.DataInputStream;
import java.io.EOFException;

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 readAiffHeader(DataInputStream inStrm, FileChannel fc) {
        byte[] buf = new byte[10];
        boolean isAIFC = false;
        try {// w  w  w  .j  a  va  2s  .  c  om
            inStrm.readInt();

            inStrm.readFully(buf, 0, 4);
            if ("AIFC".equals(new String(buf, 0, 4)))
                isAIFC = true;
            else if (!"AIFF".equals(new String(buf, 0, 4)))
                return false;

            String ckID;
            int ckSize;
            int offset;
            long dataPos = 0;
            int numSampleFrames = 0;
            long vTimestamp;
            boolean foundOne = false;
            double freq;
            while (true) {
                inStrm.readFully(buf, 0, 4);
                ckSize = inStrm.readInt();

                ckID = new String(buf, 0, 4);
                if ("COMM".equals(ckID)) {
                    srcChannels = inStrm.readShort();
                    numSampleFrames = inStrm.readInt();
                    srcBPS = inStrm.readShort();
                    inStrm.readFully(buf, 0, 10);
                    freq = extendedToDouble(buf);
                    srcSamplingRate = (int) freq;
                    if (isAIFC) {
                        ckID = new String(buf, 0, 4);
                        if (!"NONE".equals(ckID)) {
                            System.err.println("Compressed AIFF files are not supported");
                            return false;
                        }
                        if (ckSize - 22 > 0)
                            inStrm.skip(ckSize - 22);
                    }
                    if (foundOne)
                        break;
                    foundOne = true;
                } else if ("SSND".equals(ckID)) {
                    offset = inStrm.readInt();
                    inStrm.readInt();
                    dataPos = fc.position() + offset;
                    if (foundOne)
                        break;
                    foundOne = true;
                } else if ("FVER".equals(ckID)) {
                    vTimestamp = inStrm.readInt();
                    if (vTimestamp != 0xA2805140l) {
                        System.err.println("AIFF format version not recognized");
                        return false;
                    }
                }
            }
            if (srcBPS != 8 && srcBPS != 16 && srcBPS != 24 && srcBPS != 32) {
                System.err.println(String.format("Error : Only 8bit, 16bit, 24bit and 32bit PCM are supported."));
                return false;
            }
            srcBPS /= 8;
            length = numSampleFrames * srcChannels * srcBPS;
            if (length == 0) {
                System.err.println(String.format("Couldn't find data chank"));
                return false;
            }
            fc.position(dataPos);

        } catch (EOFException eof) {
            System.err.println(String.format("Couldn't find data chank"));
            return false;
        } catch (NumberFormatException e1) {
            System.err.println("Error reading file header");
            return false;
        } catch (IOException e1) {
            System.err.println("Error reading file header");
            return false;
        }
        return true;
    }

    public static double extendedToDouble(byte[] buf) throws NumberFormatException {
        ByteBuffer b = ByteBuffer.wrap(buf, 0, 10).order(ByteOrder.BIG_ENDIAN);
        int e = b.getShort();
        double sign = ((e & 0x80) != 0) ? -1 : 1;
        e &= 0x7fff;
        long m = b.getLong();

        double normal = (m & 0x8000000000000000L) != 0 ? 1 : 0;
        m &= 0x7fffffffffffffffL;

        if (m == 0)
            return 0;
        else {
            if (e == 0)
                e = -16382;
            else if ((e & 0x7fff) == 0x7fff)
                throw new NumberFormatException("NAN or Infinity");
            else
                e -= 16383;
        }
        // Shifting the divisor by 62 bits and then dividing the result by 2 deals
        // with the fact that java does not having unsign longs
        return sign * (normal + (double) m / (1L << 62) / 2) * Math.pow(2.0, (float) e);
    }
}

Related

  1. read(Path file)
  2. read(Path file, int pos, int length)
  3. read(Reader reader)
  4. read24BitInteger(byte[] threeBytes, ByteOrder order)
  5. read_file(File input)
  6. readAndTransfer(final String dir)
  7. readAsByteArray(final InputStream source)
  8. readBigEndianWord(byte[] buf)
  9. readBinaryFile(File file)