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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1、Java实现wav音频文件换为pcm音频文件(AudioUtils.java) 2、Java实现播放pcm音频文件(PCMPlay.java) WAVwav是一种无损的音频文件格式WAV符合 PIFF(Resource Interchange File Format)规范。所有的WAV都有一个文件,这个文件音频流的编码参数。WAV音频流的编码没有硬性规定,除了PCM之外,还有几乎所有支持ACM规范的编码都可以为WAV音频流进行编码。 PCM:PCM(Pulse Code Modulation----脉码调制录音)。所谓PCM录音就是将声音等模拟信号变成符号化的脉冲列,再予以记录。PCM信号是由[1]、[0]等符号构成的数字信号,而未经过任何编码和压缩处理。与模拟信号比,它不易受传送系统的杂波及失真的影响。动态范围宽,可得到音质相当好的影响效果。 简单来说:wav是一种无损的音频文件格式pcm是没有压缩的编码方式。 WAVPCM的关系 WAV可以使用多种音频编码来压缩其音频流,不过我们常见的都是音频流被PCM编码处理的WAV,但这不表示WAV只能使用PCM编码,MP3编码同样也可以运用在WAV中,和AVI一样,只要安装好了相应的Decode,就可以欣赏这些WAV了。在Windows平台下,基于PCM编码的WAV是被支持得最好的音频格式,所有音频软件都能完美支持,由于本身可以达到较高的音质的要求,因此,WAV也是音乐编辑创作的首选格式,适合保存音乐素材。因此,基于PCM编码的WAV被作为了一种中介的格式,常常使用在其他编码的相互换之中,例如MP3换成WMA。 简单来说:pcm是无损wav文件中音频数据的一种编码方式,但wav还可以用其它方式编码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值