android wav 去头信息转化成pcm 格式数据

/**
 * WAV转PCM文件
 *
 * @param wavfilepath wav文件路径
 * @return
 */
public static byte[] convertAudioFiles(String wavfilepath) {
    FileInputStream fileInputStream;

    byte[] pcmbyte = new byte[0];
    try {
        fileInputStream = new FileInputStream(wavfilepath);
     
        byte[] sWavbyte = InputStreamToByte(fileInputStream);

        LUtils.d(TAG, "sWavbyte==" + Arrays.toString(sWavbyte));

        pcmbyte = Arrays.copyOfRange(sWavbyte, 0, sWavbyte.length);
        LUtils.d(TAG, "pcmbyte==" + Arrays.toString(pcmbyte));
  
        IOUtils.closeQuietly(fileInputStream);

    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
    return pcmbyte;
}

 

/**
 * 输入流转byte二进制数据
 *
 * @param fis
 * @return
 * @throws IOException
 */
private static byte[] InputStreamToByte(FileInputStream fis) throws IOException {
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    long size = fis.getChannel().size();
    int conutlenget = conutlenget(fis);
    //        、、=====================================
    Log.d("MainActivity", "  long size===" + size);
    byte[] buffer = null;
    if (size <= Integer.MAX_VALUE) {
        buffer = new byte[(int) size];
    } else {
        buffer = new byte[8];
        for (int ix = 0; ix < 8; ++ix) {
            int offset = 64 - (ix + 1) * 8;
            buffer[ix] = (byte) ((size >> offset) & 0xff);
        }
    }
    int len;
    while ((len = fis.read(buffer)) != -1) {
        byteStream.write(buffer, 0, len);
    }
    byte[] data = byteStream.toByteArray();

    IOUtils.closeQuietly(byteStream);
    return data;
}

 

/**
 * 打印包头
 * @param fis
 * @return
 * @throws IOException
 */
private static int conutlenget(FileInputStream fis) throws IOException {

    byte[] intValue = new byte[4];
    byte[] shortValue = new byte[2];
    //4
    String mChunkID = "" + (char) fis.read() + (char) fis.read() + (char) fis.read() + (char) fis.read();
    Log.d(TAG, "Read file chunkID:" + mChunkID);//chunkID

    //4
    fis.read(intValue);
    int mChunkSize = byteArrayToInt(intValue);
    Log.d(TAG, "Read file chunkSize:" + mChunkSize);//20646

    //4
    String mFormat = "" + (char) fis.read() + (char) fis.read() + (char) fis.read() + (char) fis.read();
    Log.d(TAG, "Read file format:" + mFormat);//WAVE

    //4
    String mSubChunk1ID = "" + (char) fis.read() + (char) fis.read() + (char) fis.read() + (char) fis.read();
    Log.d(TAG, "Read fmt chunkID:" + mSubChunk1ID);//fmt

    //4
    fis.read(intValue);
    int mSubChunk1Size = byteArrayToInt(intValue);
    Log.d(TAG, "Read fmt chunkSize:" + mSubChunk1Size);//16

    //2
    fis.read(shortValue);
    int mAudioFormat = byteArrayToShort(shortValue);
    Log.d(TAG, "Read audioFormat:" + mAudioFormat);//1

    //2
    fis.read(shortValue);
    int mNumChannel = byteArrayToShort(shortValue);
    Log.d(TAG, "Read channel number:" + mNumChannel);//1

    //4
    fis.read(intValue);
    int mSampleRate = byteArrayToInt(intValue);
    Log.d(TAG, "Read samplerate:" + mSampleRate);//8000

    //4
    fis.read(intValue);
    int mByteRate = byteArrayToInt(intValue);
    Log.d(TAG, "Read byterate:" + mByteRate); //16000

    //2
    fis.read(shortValue);
    int mBlockAlign = byteArrayToShort(shortValue);
    Log.d(TAG, "Read blockalign:" + mBlockAlign);//2

    //2
    fis.read(shortValue);
    int mBitsPerSample = byteArrayToShort(shortValue);
    Log.d(TAG, "Read bitspersample:" + mBitsPerSample); //16

    //4
    String mSubChunk2ID = "" + (char) fis.read() + (char) fis.read() + (char) fis.read() + (char) fis.read();
    Log.d(TAG, "Read data chunkID:" + mSubChunk2ID);//LIST

    //4
    fis.read(intValue);
    int mSubChunk2Size = byteArrayToInt(intValue);
    Log.d(TAG, "Read data chunkSize:" + mSubChunk2Size);//chunkSize122

    Log.d(TAG, "Read wav file success !");


    return mSubChunk2Size;
}

private static short byteArrayToShort(byte[] b) {
    return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getShort();
}

private static int byteArrayToInt(byte[] b) {
    return ByteBuffer.wrap(b).order(ByteOrder.LITTLE_ENDIAN).getInt();
}

你可能感兴趣的:(android,音频转化,android,音频转化)