我有两个解决方案来进行块读/流式wav文件。在
这是第一位。我自己写的,所以不要转述。在def stream_gen(path: str):
WINDOW_s = 10
HEADER = 44
bytes_per_sampling: int
samplerate: int
CHUNk: int
first_block = True
run = True
with open(path, 'rb') as stream:
data = stream.read(HEADER)
samplerate = int.from_bytes(data[24:28], byteorder='little')
bits_per_sampling = int.from_bytes(data[34:36], byteorder='little')
if bits_per_sampling == 16:
dtype = 'int16'
elif bits_per_sampling == 32:
dtype = 'int32'
else:
raise IOError()
CHUNK = WINDOW_s * samplerate * (bits_per_sampling // 8)
while run:
data = stream.read(CHUNK)
if data == b'':
break
yield(np.frombuffer(data, dtype=dtype))
第二个是显而易见的选择。这是专业人士写的。在
^{pr2}$