Java pcm文件与wav文件互转

代码所需要的jar包,百度云盘链接:https://pan.baidu.com/s/1eX-zxMaaG1zZNL8AhLNs1A 密码:uogp

pcm转wav:

package test.voice;

/**
 * pcm格式的文件转wav
 * @author 梁艳松
 * 
 */

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PcmToWav {

    public void aaaa() throws IOException{
        String src = "C://Users//dell-pc//Desktop//16k.pcm";
       FileInputStream fis = new FileInputStream(src);
       FileOutputStream fos = new FileOutputStream("C://Users//dell-pc//Desktop//test_DDDDD.wav");

       //计算长度
       int PCMSize = 0;
       byte[] buf = new byte[1024 * 4];
       int size = fis.read(buf);

       while (size != -1) {
          PCMSize += size;
          size = fis.read(buf);
        }
       fis.close();

       //填入参数,比特率等等。这里用的是16位单声道 8000 hz
       WaveHeader header = new WaveHeader();
       //长度字段 = 内容的大小(PCMSize) + 头部字段的大小(不包括前面4字节的标识符RIFF以及fileLength本身的4字节)
       header.fileLength = PCMSize + (44 - 8);
       header.FmtHdrLeth = 16;
       header.BitsPerSample = 16;
       header.Channels = 1;
       header.FormatTag = 0x0001;
       header.SamplesPerSec = 8000;
       header.BlockAlign = (short)(header.Channels * header.BitsPerSample / 8);
       header.AvgBytesPerSec = header.BlockAlign * header.SamplesPerSec;
       header.DataHdrLeth = PCMSize;

       byte[] h = header.getHeader();

       assert h.length == 44; //WAV标准,头部应该是44字节
       //write header
       fos.write(h, 0, h.length);
       //write data stream
       fis = new FileInputStream(src);
       size = fis.read(buf);
       while (size != -1) {
          fos.write(buf, 0, size);
          size = fis.read(buf);
       }
       fis.close();
       fos.close();
       System.out.println("Convert OK!");
}
}

wav转pcm:

package test.voice;

import java.io.ByteArrayOutputStream;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.util.Arrays;  

import org.apache.commons.io.IOUtils;  

public class WavToPcm {
    /** 
     * WAV转PCM文件 
     * @param wavfilepath wav文件路径 
     * @param pcmfilepath pcm要保存的文件路径及文件名 
     * @return 
     */  
    public String convertAudioFiles(String wavfilepath,String pcmfilepath){  
        FileInputStream fileInputStream;  
        FileOutputStream fileOutputStream;  
        try {  
            fileInputStream = new FileInputStream(wavfilepath);  
            fileOutputStream = new FileOutputStream(pcmfilepath);  
            byte[] wavbyte = InputStreamToByte(fileInputStream);  
            byte[] pcmbyte = Arrays.copyOfRange(wavbyte, 44, wavbyte.length);  
            fileOutputStream.write(pcmbyte);  
            IOUtils.closeQuietly(fileInputStream);  
            IOUtils.closeQuietly(fileOutputStream);  
        } catch (Exception e) {  
            System.out.println(e.getMessage());  
        }  
        return pcmfilepath;  
    }  
    /** 
     * 输入流转byte二进制数据 
     * @param fis 
     * @return 
     * @throws IOException 
     */  
    private static byte[] InputStreamToByte(FileInputStream fis) throws IOException {  
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();  
        long size = fis.getChannel().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;  
    }  
}
阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lys1220/article/details/80322562
个人分类: java
上一篇JAVA mp3文件转pcm文件
下一篇jsp实现时间倒计时功能
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭