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:
and if I read the same file using librosa.load
and I plot it I get something like this:
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: