Java File Read via ByteBuffer readWaveFile44100_16Bit_Mono(String dir, String name)

Here you can find the source of readWaveFile44100_16Bit_Mono(String dir, String name)

Description

read Wave Fil Bi Mono

License

Apache License

Declaration

public static short[] readWaveFile44100_16Bit_Mono(String dir, String name) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 See AUTHORS file./*from   w  ww. j a  v  a  2  s .c  o  m*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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

public class Main {
    public static short[] readWaveFile44100_16Bit_Mono(String dir, String name) {
        try {
            return readWaveFile44100_16Bit_Mono(new BufferedInputStream(new FileInputStream(new File(dir, name))));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static short[] readWaveFile44100_16Bit_Mono(InputStream is) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final byte[] buffer = new byte[4096];

        try {
            try {
                is.skip(44); // Skip over the header.

                int read;
                while ((read = is.read(buffer)) > 0) {
                    baos.write(buffer, 0, read);
                }
            } finally {
                is.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        final short[] audioData = new short[baos.size() / 2];
        final ByteBuffer byteBuffer = ByteBuffer.wrap(baos.toByteArray()).order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.asShortBuffer().get(audioData);
        return audioData;
    }
}

Related

  1. readUintLE(byte[] bytes, int pointer, int size)
  2. readUnsignedInt(InputStream is)
  3. readUnsignedInt24(InputStream is)
  4. readUtf8(DataInput in)
  5. readVarLong(byte[] arr)
  6. readWavHeader(DataInputStream inStrm, FileChannel fc)
  7. readWholeFile(File file, StringBuilder stringBuilder)
  8. readZipComment(File file)
  9. skip(final ReadableByteChannel input, final long toSkip)