0

I need to read a file as bytes to use the webrtcvad library. I have done this like in the example from his 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

But now I need to convert this array of bytes to something I can work with, and I am doing this like so (like seen on here):

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

where wav_bytes it's the pcm_data from read_wav.

But when I plot this wav_r I get something like this: wav_r

and if I read the same file using librosa.load and I plot it I get something like this: correct wav

which it is how wav_r should look like.

So any idea on how can I correctly convert bytes to some array that I can work with?

Thanks!

PD: I have also tried using int8 instead of uint8 and it gives me this, which still incorrect:

enter image description here

Isaac
  • 1,436
  • 2
  • 15
  • 29
  • Try `np.int16` (given that `sample_width == 2` probably means a sample has two bytes). – MB-F May 08 '18 at 11:05
  • Thanks, it worked! Add it as answer if you want and I'll mark it as correct – Isaac May 08 '18 at 11:32
  • I just have to divide it by 32768 to have it in the same fomat as librosa.load. So the converion is like this `wav_r = np.fromstring(wav_bytes, dtype=np.int16)/32768` – Isaac May 08 '18 at 11:44
  • There are other libraries available, such as [`scipy.io.wavfile.read`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html) or the one that I wrote called [`wavio`](https://pypi.org/project/wavio/) (also [available on github](https://github.com/WarrenWeckesser/wavio), where you can see the code where I convert the file to a numpy array). – Warren Weckesser May 08 '18 at 15:11

0 Answers0