platypush/platypush/plugins/sound/_utils/_convert.py
Fabio Manganiello 23e53f13f0 Removed/commented references to numpy.typing.
That module isn't available yet on the numpy version installed on Debian
`oldstable`.
2023-10-23 20:23:57 +00:00

25 lines
771 B
Python

import numpy as np
def convert_nd_array(wave, dtype): # pylint: disable=too-many-return-statements
"""
Given a wave as a series of floating point numbers, convert them to the
appropriate data type.
"""
t = np.dtype(dtype)
if t in {np.float16, np.float32, np.float64}:
return wave.astype(t)
if t == np.int8:
return (wave * 2**8).astype(t)
if t == np.uint8:
return ((wave + 1) * 2**8).astype(t)
if t == np.int16:
return (wave * 2**15).astype(t)
if t == np.uint16:
return ((wave + 1) * 2**16).astype(t)
if t == np.int32:
return (wave * 2**31).astype(t)
if t == np.uint32:
return ((wave + 1) * 2**32).astype(t)
raise AssertionError(f'Unsupported dtype: {dtype}')