Open gmabey opened 2 years ago
While I don't have a code snippet lying around, a good place to start would be numpy's built-in frombuffer
method:
https://numpy.org/doc/stable/reference/generated/numpy.frombuffer.html
As you likely already know, audioread produces buffers containing 16-bit little-endian signed integers. So you'll want to invoke frombuffer
with that number format.
The following function will read a stereo file into numpy
arrays:
import numpy as np
import audioread
def audioread_load(path):
result_audioread = []
with audioread.audio_open(filename) as f:
for i, buf in enumerate(f):
result_audioread.append(np.frombuffer(buf, dtype=np.short))
result_audioread = np.concatenate(result_audioread)
left_audioread = result_audioread[0::2]
right_audioread = result_audioread[1::2]
return left_audioread, right_audioread, f.samplerate
The docs would be much more useful to me if this introductory sample
included some notion of how to convert
buf
to a numpy array. If you have a minute. (and, if that's easy to do)