java pcm转wav

public class WavHeader {
    /**
     * RIFF数据块
     */
    final String riffChunkId = "RIFF";
    int riffChunkSize;
    final String riffType = "WAVE";

    /**
     * FORMAT 数据块
     */
    final String formatChunkId = "fmt ";
    final int formatChunkSize = 16;
    final short audioFormat = 1;
    short mChannels;
    int mSampleRate;
    int mByteRate;
    short mBlockAlign;
    short mSampleBits;

    /**
     * FORMAT 数据块
     */
    final String mDataChunkId = "data";
    int dataChunkSize;

    WavHeader(int totalAudioLen, int sampleRate, short channels, short sampleBits) {
        this.riffChunkSize = totalAudioLen + 44 - 8;
        this.mChannels = channels;
        this.mSampleRate = sampleRate;
        this.mByteRate = sampleRate * sampleBits * channels / 8 ;
        this.mBlockAlign = (short) (channels * sampleBits / 8);
        this.mSampleBits = sampleBits;
        this.dataChunkSize = totalAudioLen;
    }

    public byte[] getHeader() {
        ByteBuffer buffer = ByteBuffer.allocate(44);
        buffer = buffer.order(ByteOrder.LITTLE_ENDIAN);
        buffer.put(riffChunkId.getBytes(StandardCharsets.UTF_8)); // The "RIFF" chunk descriptor
        buffer.putInt(riffChunkSize);  // ChunkSize 下个地址开始到文件尾的总字节数(此Chunk的数据大小)
        buffer.put(riffType.getBytes(StandardCharsets.UTF_8)); // "WAVE"
        buffer.put(formatChunkId.getBytes(StandardCharsets.UTF_8)); // "fmt + 空格"
        buffer.putInt(formatChunkSize); // SubChunkSize1 一般为16
        buffer.putShort(audioFormat); // 1:表示是PCM 编码
        buffer.putShort(mChannels); // 通道
        buffer.putInt(mSampleRate); // 采样率
        buffer.putInt(mByteRate); // 码率:采样率 * 采样位数 * 声道个数,bytePerSecond = sampleRate * (bitsPerSample / 8) * channels
        buffer.putShort(mBlockAlign); // 每次采样的大小:采样位数*声道数/8
        buffer.putShort(mSampleBits); // 采样位数
        buffer.put(mDataChunkId.getBytes(StandardCharsets.UTF_8)); // "data"
        buffer.putInt(dataChunkSize); // 音频数据长度
        buffer.flip();
        return buffer.array();
    }
}

/*
@param sampleRate 采样率
@param channels 通道个数,1 单通道 2 双通道...
@param sampleBits 每次采样的数据位数 一般为16位
@param pcmPath  pcm 文件路径
@param  destinationPath 输出的wav文件路径
 */

public static void convertPCMFileToWAVFile(int sampleRate, short channels, short sampleBits, String pcmPath, String destinationPath) throws IOException {
    File pcmFile = new File(pcmPath);
    if (!pcmFile.exists()) {
        throw new FileNotFoundException();
    }
    // 先删除目标文件
    File wavFile = new File(destinationPath);
    if (wavFile.exists()) {
        wavFile.delete();
    }
    int nTotalAudioSize = (int) pcmFile.length();
    WavHeader header = new WavHeader(nTotalAudioSize, sampleRate, channels, sampleBits);
    byte[] buffer = new byte[1024 * 4]; // Length of All Files, Total Size

    InputStream inStream = new BufferedInputStream(new FileInputStream(pcmFile));
    OutputStream outStream = new BufferedOutputStream(new FileOutputStream(wavFile));

    // 写入WAVFile 头部
    try {
        outStream.write(header.getHeader());
        // 循环读入数据
        do {
            int size = inStream.read(buffer);
            if (size != -1)
            {
                outStream.write(buffer, 0, size);
            }else
            {
                break;
            }
        }while(true);
    } catch (IOException e) { // 写入头部失败
        throw e;
    }finally {
        try {
            inStream.close();
            outStream.close();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
    WTLog.i("PcmToWav", "makePCMFileToWAVFile  success!" + new SimpleDateFormat("yyyy-MM-dd hh:mm").format(new Date()));
}
————————————————
版权声明:本文为优快云博主「skyf1y」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/skyf1y/article/details/110260126

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值