音频-pcm -> wav 编码

object AudioEncodeUtil {

    private const val TAG = "AudioEncodeUtil"

    /**
     * PCM 文件转 WAV 文件
     * @param inPcmFilePath 输入 PCM 文件路径
     * @param outWavFilePath 输出 WAV 文件路径
     * @param sampleRate 采样率,例如 44100
     * @param channels 声道数 单声道 1 或 双声道 2
     * @param bitNum 采样位数,8 或 16
     */
    fun convertPcm2Wav(
        inPcmFilePath: String?,
        outWavFilePath: String?,
        sampleRate: Int ,
        channels: Int ,
        bitNum: Int
    ) {
        if (inPcmFilePath.isNullOrEmpty() || outWavFilePath.isNullOrEmpty()) {
            MLog.d(
                TAG,
                "convertPcm2Wav: 文件路径为空 inPcmFilePath = $inPcmFilePath, outWavFilePath = $outWavFilePath"
            )
            return
        }
        MLog.d(TAG, "convertPcm2Wav: 开始转 WAV")
        var `in`: FileInputStream? = null
        var out: FileOutputStream? = null
        val data = ByteArray(1024)
        try {
            `in` = FileInputStream(inPcmFilePath)
            out = FileOutputStream(outWavFilePath)
            // PCM 文件大小
            val totalAudioLen = `in`.channel.size()
            writeWaveFileHeader(out, totalAudioLen, sampleRate, channels, bitNum)
            var length = 0
            while (`in`.read(data).also { length = it } > 0) {
                out.write(data, 0, length)
            }
        } catch (e: Exception) {
            MLog.d(TAG, "convertPcm2Wav: error = $e")
            e.printStackTrace()
        } finally {
            // 编码结束后删除文件 .temp
            MLog.d(TAG, "convertPcm2Wav: deleteFile = $inPcmFilePath")
            File(inPcmFilePath).delete()
            MLog.d(TAG, "convertPcm2Wav: 关闭流")
            if (`in` != null) {
                try {
                    `in`.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
            if (out != null) {
                try {
                    out.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
        }
    }

    /**
     * 输出 WAV 文件
     * @param out WAV输出文件流
     * @param totalAudioLen 整个音频 PCM 数据大小
     * @param sampleRate 采样率
     * @param channels 声道数
     * @param bitNum 采样位数
     */

    private fun writeWaveFileHeader(
        out: FileOutputStream, totalAudioLen: Long,
        sampleRate: Int, channels: Int, bitNum: Int
    ) {
        val header = getWaveHeader(totalAudioLen, sampleRate, channels, bitNum)
        out.write(header, 0, AudioConstant.WAVE_HEAD_SIZE)
    }

    /**
     * 获取Wav header 字节数据
     * @param totalAudioLen 整个音频PCM数据大小
     * @param sampleRate 采样率
     * @param channels 声道数
     * @param bitNum 采样位数
     */
    fun getWaveHeader(totalAudioLen: Long, sampleRate: Int, channels: Int, bitNum: Int): ByteArray {

        // 总大小,由于不包括 RIFF 和 WAV,所以是 44 - 8 = 36,在加上 PCM 文件大小
        val totalDataLen = totalAudioLen + 36
        // 采样字节 byte 率
        val byteRate = (sampleRate * channels * bitNum / 8).toLong()
        val header = ByteArray(AudioConstant.WAVE_HEAD_SIZE)
        header[0] = 'R'.code.toByte() // RIFF
        header[1] = 'I'.code.toByte()
        header[2] = 'F'.code.toByte()
        header[3] = 'F'.code.toByte()
        header[4] = (totalDataLen and 0xffL).toByte() // 数据大小
        header[5] = (totalDataLen shr 8 and 0xffL).toByte()
        header[6] = (totalDataLen shr 16 and 0xffL).toByte()
        header[7] = (totalDataLen shr 24 and 0xffL).toByte()
        header[8] = 'W'.code.toByte() // WAVE
        header[9] = 'A'.code.toByte()
        header[10] = 'V'.code.toByte()
        header[11] = 'E'.code.toByte()
        // FMT Chunk
        header[12] = 'f'.code.toByte() // 'fmt '
        header[13] = 'm'.code.toByte()
        header[14] = 't'.code.toByte()
        header[15] = ' '.code.toByte() // 过渡字节
        // 数据大小
        header[16] = 16 // 4 bytes: size of 'fmt ' chunk
        header[17] = 0
        header[18] = 0
        header[19] = 0
        // 编码方式 10H 为 PCM 编码格式
        header[20] = 1 // format = 1
        header[21] = 0
        // 通道数
        header[22] = channels.toByte()
        header[23] = 0
        // 采样率,每个通道的播放速度
        header[24] = (sampleRate and 0xff).toByte()
        header[25] = (sampleRate shr 8 and 0xff).toByte()
        header[26] = (sampleRate shr 16 and 0xff).toByte()
        header[27] = (sampleRate shr 24 and 0xff).toByte()
        // 音频数据传送速率,采样率*通道数*采样深度/8
        header[28] = (byteRate and 0xffL).toByte()
        header[29] = (byteRate shr 8 and 0xffL).toByte()
        header[30] = (byteRate shr 16 and 0xffL).toByte()
        header[31] = (byteRate shr 24 and 0xffL).toByte()
        // 确定系统一次要处理多少个这样字节的数据,确定缓冲区,通道数*采样位数
        header[32] = (channels * 16 / 8).toByte()
        header[33] = 0
        // 每个样本的数据位数
        header[34] = 16
        header[35] = 0
        // Data chunk
        header[36] = 'd'.code.toByte() // data
        header[37] = 'a'.code.toByte()
        header[38] = 't'.code.toByte()
        header[39] = 'a'.code.toByte()
        header[40] = (totalAudioLen and 0xffL).toByte()
        header[41] = (totalAudioLen shr 8 and 0xffL).toByte()
        header[42] = (totalAudioLen shr 16 and 0xffL).toByte()
        header[43] = (totalAudioLen shr 24 and 0xffL).toByte()
        return header
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值