From 332413e8efcc820a71f8c2035c6287f65fcdf126 Mon Sep 17 00:00:00 2001 From: nyanpasu64 Date: Fri, 20 Jul 2018 01:13:15 -0700 Subject: [PATCH] Refactor Wave class and add support for float waves --- ovgenpy/wave.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/ovgenpy/wave.py b/ovgenpy/wave.py index a961746..f20d01c 100644 --- a/ovgenpy/wave.py +++ b/ovgenpy/wave.py @@ -28,15 +28,27 @@ class Wave: # TODO extract function, unit tests... switch to pysoundfile and drop logic dtype = self.data.dtype - max_val = np.iinfo(dtype).max + 1 - assert max_val & (max_val - 1) == 0 # power of 2 + def is_type(parent: type) -> bool: + return np.issubdtype(dtype, parent) - if np.issubdtype(dtype, np.uint): - self.offset = -max_val // 2 - self.max_val = max_val // 2 - else: + if is_type(np.integer): + max_int = np.iinfo(dtype).max + 1 + assert max_int & (max_int - 1) == 0 # power of 2 + + if is_type(np.unsignedinteger): + self.offset = -max_int // 2 + self.max_val = max_int // 2 + + elif is_type(np.signedinteger): + self.offset = 0 + self.max_val = max_int + + elif is_type(np.float): self.offset = 0 - self.max_val = max_val + self.max_val = 1 + + else: + raise ValueError(f'unexpected wavfile dtype {dtype}') def __getitem__(self, index: int) -> 'np.ndarray[FLOAT]': """ Copies self.data[item], converted to a FLOAT within range [-1, 1). """