原始wav字节到uint数组或其他一些格式

时间:2018-05-08 10:18:31

标签: python numpy wav uint8t bytestream

我需要将文件作为字节读取以使用webrtcvad库。 我在github

的例子中就像这样做了
    def read_wave(path):
    """Reads a .wav file.
    Takes the path, and returns (PCM audio data, sample rate).
    """
    with contextlib.closing(wave.open(path, 'rb')) as wf:
        num_channels = wf.getnchannels()
        assert num_channels == 1
        sample_width = wf.getsampwidth()
        assert sample_width == 2
        sample_rate = wf.getframerate()
        assert sample_rate in (8000, 16000, 32000)
        pcm_data = wf.readframes(wf.getnframes())
        return pcm_data, sample_rate

但是现在我需要将这个字节数组转换成我可以使用的字节,我这样做(就像在here上看到的那样):

wav_r = np.fromstring(wav_bytes, dtype=np.uint8)

其中wav_bytes来自read_wav的pcm_data。

但是当我绘制这个wav_r时,我得到这样的东西: wav_r

如果我使用librosa.load读取相同的文件并绘制它,我会得到这样的结果: correct wav

这就是wav_r的样子。

所以关于如何正确地将字节转换为可以使用的数组的任何想法?

谢谢!

PD:我也尝试过使用int8而不是uint8,它给了我这个,但仍然不正确:

enter image description here

0 个答案:

没有答案