From 34050fe2a1ffbc8d4333944bcc16d3c76706f1d4 Mon Sep 17 00:00:00 2001 From: Ewald de Wit Date: Sat, 24 Sep 2022 09:54:58 +0200 Subject: [PATCH] Simplify/optimize read_wav --- hifiscan/io_.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/hifiscan/io_.py b/hifiscan/io_.py index 037842a..69e25a2 100644 --- a/hifiscan/io_.py +++ b/hifiscan/io_.py @@ -58,20 +58,22 @@ def read_wav(path: str) -> Sound: frames = wav.readframes(n) if width == 4: buff = np.frombuffer(frames, np.int32) - data = buff.astype('f') / np.float32(2 ** 31 - 1) + norm = 2 ** 31 - 1 elif width == 3: buff = np.frombuffer(frames, np.uint8) uints = buff[0::3].astype(np.uint32) << 8 \ | buff[1::3].astype(np.uint32) << 16 \ | buff[2::3].astype(np.uint32) << 24 - data = uints.view(np.int32).astype('f') / np.float32( - 2 ** 31 - 2 ** 8) + buff = uints.view(np.int32) + norm = 2 ** 31 - 2 ** 8 elif width == 2: buff = np.frombuffer(frames, np.int16) - data = buff.astype('f') / np.float32(2 ** 15 - 1) + norm = 2 ** 15 - 1 else: buff = np.frombuffer(frames, np.int8) - data = buff.astype('f') / np.float32(2 ** 7 - 1) + norm = 2 ** 7 - 1 + data = buff.astype('f') + data /= np.float32(norm) data = data.reshape((-1, ch)).T return Sound(data, rate, width)