| 1 |
"""Partial implementation of the deprecated audioop module. |
| 2 |
|
| 3 |
Only supports: |
| 4 |
- widths 1, 2, and 4 |
| 5 |
- signed samples |
| 6 |
- tomono, tostereo, lin2lin, ratecv |
| 7 |
""" |
| 8 |
|
| 9 |
import math |
| 10 |
import struct |
| 11 |
from typing import Final, List, Optional, Tuple, Union |
| 12 |
|
| 13 |
BufferType = Union[bytes, bytearray] |
| 14 |
State = Tuple[int, Tuple[Tuple[int, ...], ...]] |
| 15 |
|
| 16 |
# width = (_, 1, 2, _, 4) |
| 17 |
_MAX_VALS: Final = [0, 0x7F, 0x7FFF, 0, 0x7FFFFFFF] |
| 18 |
_MIN_VALS: Final = [0, -0x80, -0x8000, 0, -0x80000000] |
| 19 |
_SIGNED_FORMATS: Final = ["", "b", "h", "", "i"] |
| 20 |
_UNSIGNED_FORMATS: Final = ["", "B", "H", "", "I"] |
| 21 |
|
| 22 |
|
| 23 |
def check_size(size: int) -> None: |
| 24 |
if size not in (1, 2, 4): |
| 25 |
raise ValueError(f"Size should be 1, 2, 4. Got {size}") |
| 26 |
|
| 27 |
|
| 28 |
def check_parameters(fragment_length: int, size: int) -> None: |
| 29 |
check_size(size) |
| 30 |
if (fragment_length % size) != 0: |
| 31 |
raise ValueError( |
| 32 |
"Not a whole number of frames: " |
| 33 |
f"fragment_length={fragment_length}, size={size}" |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
def fbound(val: float, min_val: float, max_val: float) -> int: |
| 38 |
if val > max_val: |
| 39 |
val = max_val |
| 40 |
elif val < (min_val + 1): |
| 41 |
val = min_val |
| 42 |
|
| 43 |
val = math.floor(val) |
| 44 |
|
| 45 |
return int(val) |
| 46 |
|
| 47 |
|
| 48 |
def tomono( |
| 49 |
fragment: BufferType, width: int, lfactor: float, rfactor: float |
| 50 |
) -> BufferType: |
| 51 |
fragment_length = len(fragment) |
| 52 |
check_parameters(fragment_length, width) |
| 53 |
|
| 54 |
max_val = _MAX_VALS[width] |
| 55 |
min_val = _MIN_VALS[width] |
| 56 |
struct_format = _SIGNED_FORMATS[width] |
| 57 |
result = bytearray(fragment_length // 2) |
| 58 |
|
| 59 |
for i in range(0, fragment_length, width * 2): |
| 60 |
val_left = struct.unpack_from(struct_format, fragment, i)[0] |
| 61 |
val_right = struct.unpack_from(struct_format, fragment, i + width)[0] |
| 62 |
val_mono = (val_left * lfactor) + (val_right * rfactor) |
| 63 |
sample_mono = fbound(val_mono, min_val, max_val) |
| 64 |
struct.pack_into(struct_format, result, i // 2, sample_mono) |
| 65 |
|
| 66 |
return result |
| 67 |
|
| 68 |
|
| 69 |
def tostereo( |
| 70 |
fragment: BufferType, width: int, lfactor: float, rfactor: float |
| 71 |
) -> BufferType: |
| 72 |
fragment_length = len(fragment) |
| 73 |
check_parameters(fragment_length, width) |
| 74 |
|
| 75 |
max_val = _MAX_VALS[width] |
| 76 |
min_val = _MIN_VALS[width] |
| 77 |
struct_format = _SIGNED_FORMATS[width] |
| 78 |
result = bytearray(fragment_length * 2) |
| 79 |
|
| 80 |
for i in range(0, fragment_length, width): |
| 81 |
val_mono = struct.unpack_from(struct_format, fragment, i)[0] |
| 82 |
sample_left = fbound(val_mono * lfactor, min_val, max_val) |
| 83 |
sample_right = fbound(val_mono * rfactor, min_val, max_val) |
| 84 |
struct.pack_into(struct_format, result, i * 2, sample_left) |
| 85 |
struct.pack_into(struct_format, result, (i * 2) + width, sample_right) |
| 86 |
|
| 87 |
return result |
| 88 |
|
| 89 |
|
| 90 |
def _get_sample32(fragment: BufferType, width: int, index: int) -> int: |
| 91 |
if width == 1: |
| 92 |
return fragment[index] |
| 93 |
|
| 94 |
if width == 2: |
| 95 |
return (fragment[index] << 8) + (fragment[index + 1]) |
| 96 |
|
| 97 |
if width == 4: |
| 98 |
return ( |
| 99 |
(fragment[index] << 24) |
| 100 |
+ (fragment[index + 1] << 16) |
| 101 |
+ (fragment[index + 2] << 8) |
| 102 |
+ fragment[index + 3] |
| 103 |
) |
| 104 |
|
| 105 |
raise ValueError(f"Invalid width: {width}") |
| 106 |
|
| 107 |
|
| 108 |
def _set_sample32(fragment: bytearray, width: int, index: int, sample: int) -> None: |
| 109 |
if width == 1: |
| 110 |
fragment[index] = sample & 0x000000FF |
| 111 |
elif width == 2: |
| 112 |
fragment[index] = (sample >> 8) & 0x000000FF |
| 113 |
fragment[index + 1] = sample & 0x000000FF |
| 114 |
elif width == 4: |
| 115 |
fragment[index] = sample >> 24 |
| 116 |
fragment[index + 1] = (sample >> 16) & 0x000000FF |
| 117 |
fragment[index + 2] = (sample >> 8) & 0x000000FF |
| 118 |
fragment[index + 3] = sample & 0x000000FF |
| 119 |
else: |
| 120 |
raise ValueError(f"Invalid width: {width}") |
| 121 |
|
| 122 |
|
| 123 |
def lin2lin(fragment: BufferType, width: int, new_width: int) -> BufferType: |
| 124 |
if width == new_width: |
| 125 |
return fragment |
| 126 |
|
| 127 |
fragment_length = len(fragment) |
| 128 |
check_parameters(fragment_length, width) |
| 129 |
check_size(new_width) |
| 130 |
|
| 131 |
result = bytearray(int((fragment_length / width) * new_width)) |
| 132 |
|
| 133 |
j = 0 |
| 134 |
for i in range(0, fragment_length, width): |
| 135 |
sample = _get_sample32(fragment, width, i) |
| 136 |
_set_sample32(result, new_width, j, sample) |
| 137 |
j += new_width |
| 138 |
|
| 139 |
return result |
| 140 |
|
| 141 |
|
| 142 |
def ratecv( |
| 143 |
fragment: BufferType, |
| 144 |
width: int, |
| 145 |
nchannels: int, |
| 146 |
inrate: int, |
| 147 |
outrate: int, |
| 148 |
state: Optional[State], |
| 149 |
weightA: int = 1, |
| 150 |
weightB: int = 0, |
| 151 |
) -> Tuple[bytearray, Optional[State]]: |
| 152 |
fragment_length = len(fragment) |
| 153 |
check_size(width) |
| 154 |
if nchannels < 1: |
| 155 |
raise ValueError(f"Number of channels should be >= 1, got {nchannels}") |
| 156 |
bytes_per_frame = width * nchannels |
| 157 |
if (weightA < 1) or (weightB) < 0: |
| 158 |
raise ValueError( |
| 159 |
"weightA should be >= 1, weightB should be >= 0, " |
| 160 |
f"got weightA={weightA}, weightB={weightB}" |
| 161 |
) |
| 162 |
|
| 163 |
if (fragment_length % bytes_per_frame) != 0: |
| 164 |
raise ValueError("Not a whole number of frames") |
| 165 |
|
| 166 |
if (inrate <= 0) or (outrate <= 0): |
| 167 |
raise ValueError("Sampling rate not > 0") |
| 168 |
|
| 169 |
d = math.gcd(inrate, outrate) |
| 170 |
inrate //= d |
| 171 |
outrate //= d |
| 172 |
|
| 173 |
d = math.gcd(weightA, weightB) |
| 174 |
weightA //= d |
| 175 |
weightB //= d |
| 176 |
|
| 177 |
prev_i: List[int] = [0] * nchannels |
| 178 |
cur_i: List[int] = [0] * nchannels |
| 179 |
|
| 180 |
if state is None: |
| 181 |
d = -outrate |
| 182 |
# prev_i and cur_i are already zeroed |
| 183 |
else: |
| 184 |
d, samps = state |
| 185 |
if len(samps) != nchannels: |
| 186 |
raise ValueError("Illegal state argument") |
| 187 |
|
| 188 |
for chan_index, channel in enumerate(samps): |
| 189 |
prev_i[chan_index], cur_i[chan_index] = channel |
| 190 |
|
| 191 |
input_frames = fragment_length // bytes_per_frame |
| 192 |
output_frames = int(math.ceil(input_frames * (outrate / inrate))) |
| 193 |
|
| 194 |
# Approximate version used in C code to avoid overflow: |
| 195 |
# q = 1 + ((input_frames - 1) // inrate) |
| 196 |
# output_frames = q * outrate * bytes_per_frame |
| 197 |
|
| 198 |
result = bytearray(output_frames * bytes_per_frame) |
| 199 |
struct_format = _SIGNED_FORMATS[width] |
| 200 |
|
| 201 |
input_index = 0 |
| 202 |
output_index = 0 |
| 203 |
while True: |
| 204 |
while d < 0: |
| 205 |
if input_frames == 0: |
| 206 |
samps = tuple( |
| 207 |
(prev_i[chan], cur_i[chan]) for chan in range(0, nchannels) |
| 208 |
) |
| 209 |
|
| 210 |
# NOTE: It's critical that result is clipped here |
| 211 |
return result[:output_index], (d, samps) |
| 212 |
|
| 213 |
for chan in range(0, nchannels): |
| 214 |
prev_i[chan] = cur_i[chan] |
| 215 |
cur_i[chan] = struct.unpack_from(struct_format, fragment, input_index)[ |
| 216 |
0 |
| 217 |
] |
| 218 |
input_index += width |
| 219 |
cur_i[chan] = ((weightA * cur_i[chan]) + (weightB * prev_i[chan])) // ( |
| 220 |
weightA + weightB |
| 221 |
) |
| 222 |
|
| 223 |
input_frames -= 1 |
| 224 |
d += outrate |
| 225 |
while d >= 0: |
| 226 |
for chan in range(0, nchannels): |
| 227 |
sample = int( |
| 228 |
( |
| 229 |
(float(prev_i[chan]) * float(d)) |
| 230 |
+ (float(cur_i[chan]) * (float(outrate) - float(d))) |
| 231 |
) |
| 232 |
/ float(outrate) |
| 233 |
) |
| 234 |
struct.pack_into(struct_format, result, output_index, sample) |
| 235 |
output_index += width |
| 236 |
d -= inrate |
| 237 |
|
| 238 |
return result, None |