Convert audio bytes to numpy array. Args: input (bytes): Raw audio bytes. Returns: numpy.ndarray: Decoded audio samples.
(input)
| 205 | |
| 206 | |
| 207 | def load_bytes(input): |
| 208 | """Convert audio bytes to numpy array. |
| 209 | |
| 210 | Args: |
| 211 | input (bytes): Raw audio bytes. |
| 212 | |
| 213 | Returns: |
| 214 | numpy.ndarray: Decoded audio samples. |
| 215 | """ |
| 216 | # Only run the (expensive) frame-rate validation when the payload is an |
| 217 | # actual audio container (WAV, MP3, OGG, …). Raw PCM buffers have no |
| 218 | # recognisable header and would cause pydub to spend ~200 ms before |
| 219 | # raising an exception that is then silently swallowed anyway. |
| 220 | if _is_audio_container(input): |
| 221 | try: |
| 222 | input = validate_frame_rate(input) |
| 223 | except Exception: |
| 224 | pass |
| 225 | middle_data = np.frombuffer(input, dtype=np.int16) |
| 226 | middle_data = np.asarray(middle_data) |
| 227 | if middle_data.dtype.kind not in "iu": |
| 228 | raise TypeError("'middle_data' must be an array of integers") |
| 229 | dtype = np.dtype("float32") |
| 230 | if dtype.kind != "f": |
| 231 | raise TypeError("'dtype' must be a floating point type") |
| 232 | |
| 233 | i = np.iinfo(middle_data.dtype) |
| 234 | abs_max = 2 ** (i.bits - 1) |
| 235 | offset = i.min + abs_max |
| 236 | array = np.frombuffer((middle_data.astype(dtype) - offset) / abs_max, dtype=np.float32) |
| 237 | return array |
| 238 | |
| 239 | |
| 240 | def validate_frame_rate( |
no test coverage detected
searching dependent graphs…