wav to pcm 和 pcm to wav

wav2pcm

import numpy as np


def wav2pcm(wav_file, pcm_file, data_type=np.int16):
    f = open(wav_file, "rb")
    f.seek(0)
    f.read(44)
    data = np.fromfile(f, dtype=data_type)
    data.tofile(pcm_file)


if __name__ == '__main__':
    wav2pcm(r"C:\Users\pc\Desktop\vin_2022-09-09-17-00-55_oms_merge.wav",
            r"C:\Users\pc\Desktop\vin_2022-09-09-17-00-55_oms_merge.pcm")

pcm2wav

import wave


def pcm2wav(pcm_file, wav_file, channels=1, bits=16, sample_rate=16000):
    pcmf = open(pcm_file, 'rb')
    pcmdata = pcmf.read()
    pcmf.close()

    if bits % 8 != 0:
        raise ValueError("bits % 8 must == 0. now bits:" + str(bits))

    wavfile = wave.open(wav_file, 'wb')
    wavfile.setnchannels(channels)
    wavfile.setsampwidth(bits // 8)
    wavfile.setframerate(sample_rate)
    wavfile.writeframes(pcmdata)
    wavfile.close()


if __name__ == '__main__':
    pcm2wav(r"C:\Users\pc\Desktop\vin_2022-09-09-17-00-55_oms_merge_rnnoise.pcm",
            r"C:\Users\pc\Desktop\test.wav")
posted @ 2022-12-23 10:55  不能说的秘密  阅读(53)  评论(0编辑  收藏  举报