| 1 |
import sys |
| 2 |
import builtins |
| 3 |
import math |
| 4 |
import struct |
| 5 |
from math import gcd |
| 6 |
from _audioop_cffi import ffi, lib |
| 7 |
|
| 8 |
BIG_ENDIAN = sys.byteorder != 'little' |
| 9 |
_buffer = memoryview |
| 10 |
|
| 11 |
|
| 12 |
class error(Exception): |
| 13 |
pass |
| 14 |
|
| 15 |
|
| 16 |
def _check_size(size): |
| 17 |
if size < 1 or size > 4: |
| 18 |
raise error("Size should be 1, 2, 3 or 4") |
| 19 |
|
| 20 |
def _check_params(length, size): |
| 21 |
_check_size(size) |
| 22 |
if length % size != 0: |
| 23 |
raise error("not a whole number of frames") |
| 24 |
|
| 25 |
|
| 26 |
def _check_state(state): |
| 27 |
if state is None: |
| 28 |
valpred = 0 |
| 29 |
index = 0 |
| 30 |
else: |
| 31 |
valpred, index = state |
| 32 |
if not (-0x8000 <= valpred < 0x8000 and 0 <= index < 89): |
| 33 |
raise ValueError("bad state") |
| 34 |
return (valpred, index) |
| 35 |
|
| 36 |
|
| 37 |
def _sample_count(cp, size): |
| 38 |
return len(cp) // size |
| 39 |
|
| 40 |
|
| 41 |
def _get_samples(cp, size, signed=True): |
| 42 |
for i in range(_sample_count(cp, size)): |
| 43 |
yield _get_sample(cp, size, i, signed) |
| 44 |
|
| 45 |
|
| 46 |
def _struct_format(size, signed): |
| 47 |
if size == 1: |
| 48 |
return "b" if signed else "B" |
| 49 |
elif size == 2: |
| 50 |
return "h" if signed else "H" |
| 51 |
elif size == 3: |
| 52 |
raise NotImplementedError |
| 53 |
elif size == 4: |
| 54 |
return "i" if signed else "I" |
| 55 |
|
| 56 |
def _unpack_int24(buf): |
| 57 |
if BIG_ENDIAN: |
| 58 |
val = (buf[2] & 0xff) | \ |
| 59 |
((buf[1] & 0xff) << 8) | \ |
| 60 |
((buf[0] & 0xff) << 16) |
| 61 |
else: |
| 62 |
val = (buf[0] & 0xff) | \ |
| 63 |
((buf[1] & 0xff) << 8) | \ |
| 64 |
((buf[2] & 0xff) << 16) |
| 65 |
if val & 0x800000: |
| 66 |
val = val - 0x1000000 |
| 67 |
return val |
| 68 |
|
| 69 |
def _pack_int24(into, off, val): |
| 70 |
buf = _buffer(into) |
| 71 |
if BIG_ENDIAN: |
| 72 |
buf[off+2] = val & 0xff |
| 73 |
buf[off+1] = (val >> 8) & 0xff |
| 74 |
buf[off+0] = (val >> 16) & 0xff |
| 75 |
else: |
| 76 |
buf[off+0] = val & 0xff |
| 77 |
buf[off+1] = (val >> 8) & 0xff |
| 78 |
buf[off+2] = (val >> 16) & 0xff |
| 79 |
|
| 80 |
def _get_sample(cp, size, i, signed=True): |
| 81 |
start = i * size |
| 82 |
end = start + size |
| 83 |
chars = _buffer(cp)[start:end] |
| 84 |
if size == 3: |
| 85 |
return _unpack_int24(chars) |
| 86 |
fmt = _struct_format(size, signed) |
| 87 |
return struct.unpack_from(fmt, chars)[0] |
| 88 |
|
| 89 |
|
| 90 |
def _put_sample(cp, size, i, val, signed=True): |
| 91 |
if size == 3: |
| 92 |
_pack_int24(cp, i*size, val) |
| 93 |
return |
| 94 |
fmt = _struct_format(size, signed) |
| 95 |
struct.pack_into(fmt, cp, i * size, val) |
| 96 |
|
| 97 |
|
| 98 |
def _get_maxval(size, signed=True): |
| 99 |
if signed and size == 1: |
| 100 |
return 0x7f |
| 101 |
elif size == 1: |
| 102 |
return 0xff |
| 103 |
elif signed and size == 2: |
| 104 |
return 0x7fff |
| 105 |
elif size == 2: |
| 106 |
return 0xffff |
| 107 |
elif signed and size == 3: |
| 108 |
return 0x7fffff |
| 109 |
elif size == 3: |
| 110 |
return 0xffffff |
| 111 |
elif signed and size == 4: |
| 112 |
return 0x7fffffff |
| 113 |
elif size == 4: |
| 114 |
return 0xffffffff |
| 115 |
|
| 116 |
|
| 117 |
def _get_minval(size, signed=True): |
| 118 |
if not signed: |
| 119 |
return 0 |
| 120 |
elif size == 1: |
| 121 |
return -0x80 |
| 122 |
elif size == 2: |
| 123 |
return -0x8000 |
| 124 |
elif size == 3: |
| 125 |
return -0x800000 |
| 126 |
elif size == 4: |
| 127 |
return -0x80000000 |
| 128 |
|
| 129 |
|
| 130 |
def _get_clipfn(size, signed=True): |
| 131 |
maxval = _get_maxval(size, signed) |
| 132 |
minval = _get_minval(size, signed) |
| 133 |
return lambda val: builtins.max(min(val, maxval), minval) |
| 134 |
|
| 135 |
|
| 136 |
def _overflow(val, size, signed=True): |
| 137 |
minval = _get_minval(size, signed) |
| 138 |
maxval = _get_maxval(size, signed) |
| 139 |
if minval <= val <= maxval: |
| 140 |
return val |
| 141 |
|
| 142 |
bits = size * 8 |
| 143 |
if signed: |
| 144 |
offset = 2**(bits-1) |
| 145 |
return ((val + offset) % (2**bits)) - offset |
| 146 |
else: |
| 147 |
return val % (2**bits) |
| 148 |
|
| 149 |
def _check_bytes(cp): |
| 150 |
|
| 151 |
try: |
| 152 |
memoryview(cp) |
| 153 |
except TypeError: |
| 154 |
raise TypeError("a bytes-like object is required, not '%s'" % \ |
| 155 |
str(type(cp))) |
| 156 |
|
| 157 |
|
| 158 |
def getsample(cp, size, i): |
| 159 |
|
| 160 |
_check_params(len(cp), size) |
| 161 |
if not (0 <= i < len(cp) // size): |
| 162 |
raise error("Index out of range") |
| 163 |
return _get_sample(cp, size, i) |
| 164 |
|
| 165 |
|
| 166 |
def max(cp, size): |
| 167 |
_check_params(len(cp), size) |
| 168 |
|
| 169 |
if len(cp) == 0: |
| 170 |
return 0 |
| 171 |
|
| 172 |
return builtins.max(abs(sample) for sample in _get_samples(cp, size)) |
| 173 |
|
| 174 |
|
| 175 |
def minmax(cp, size): |
| 176 |
_check_params(len(cp), size) |
| 177 |
|
| 178 |
min_sample, max_sample = 0x7fffffff, -0x80000000 |
| 179 |
for sample in _get_samples(cp, size): |
| 180 |
max_sample = builtins.max(sample, max_sample) |
| 181 |
min_sample = builtins.min(sample, min_sample) |
| 182 |
|
| 183 |
return min_sample, max_sample |
| 184 |
|
| 185 |
|
| 186 |
def avg(cp, size): |
| 187 |
_check_params(len(cp), size) |
| 188 |
sample_count = _sample_count(cp, size) |
| 189 |
if sample_count == 0: |
| 190 |
return 0 |
| 191 |
return sum(_get_samples(cp, size)) // sample_count |
| 192 |
|
| 193 |
|
| 194 |
def rms(cp, size): |
| 195 |
_check_params(len(cp), size) |
| 196 |
|
| 197 |
sample_count = _sample_count(cp, size) |
| 198 |
if sample_count == 0: |
| 199 |
return 0 |
| 200 |
|
| 201 |
sum_squares = sum(sample**2 for sample in _get_samples(cp, size)) |
| 202 |
return int(math.sqrt(sum_squares // sample_count)) |
| 203 |
|
| 204 |
|
| 205 |
def _sum2(cp1, cp2, length): |
| 206 |
size = 2 |
| 207 |
return sum(getsample(cp1, size, i) * getsample(cp2, size, i) |
| 208 |
for i in range(length)) + 0.0 |
| 209 |
|
| 210 |
|
| 211 |
def findfit(cp1, cp2): |
| 212 |
size = 2 |
| 213 |
|
| 214 |
if len(cp1) % 2 != 0 or len(cp2) % 2 != 0: |
| 215 |
raise error("Strings should be even-sized") |
| 216 |
|
| 217 |
if len(cp1) < len(cp2): |
| 218 |
raise error("First sample should be longer") |
| 219 |
|
| 220 |
len1 = _sample_count(cp1, size) |
| 221 |
len2 = _sample_count(cp2, size) |
| 222 |
|
| 223 |
sum_ri_2 = _sum2(cp2, cp2, len2) |
| 224 |
sum_aij_2 = _sum2(cp1, cp1, len2) |
| 225 |
sum_aij_ri = _sum2(cp1, cp2, len2) |
| 226 |
|
| 227 |
result = (sum_ri_2 * sum_aij_2 - sum_aij_ri * sum_aij_ri) / sum_aij_2 |
| 228 |
|
| 229 |
best_result = result |
| 230 |
best_i = 0 |
| 231 |
|
| 232 |
for i in range(1, len1 - len2 + 1): |
| 233 |
aj_m1 = _get_sample(cp1, size, i - 1) |
| 234 |
aj_lm1 = _get_sample(cp1, size, i + len2 - 1) |
| 235 |
|
| 236 |
sum_aij_2 += aj_lm1**2 - aj_m1**2 |
| 237 |
sum_aij_ri = _sum2(_buffer(cp1)[i*size:], cp2, len2) |
| 238 |
|
| 239 |
result = (sum_ri_2 * sum_aij_2 - sum_aij_ri * sum_aij_ri) / sum_aij_2 |
| 240 |
|
| 241 |
if result < best_result: |
| 242 |
best_result = result |
| 243 |
best_i = i |
| 244 |
|
| 245 |
factor = _sum2(_buffer(cp1)[best_i*size:], cp2, len2) / sum_ri_2 |
| 246 |
|
| 247 |
return best_i, factor |
| 248 |
|
| 249 |
|
| 250 |
def findfactor(cp1, cp2): |
| 251 |
size = 2 |
| 252 |
|
| 253 |
if len(cp1) % 2 != 0: |
| 254 |
raise error("Strings should be even-sized") |
| 255 |
|
| 256 |
if len(cp1) != len(cp2): |
| 257 |
raise error("Samples should be same size") |
| 258 |
|
| 259 |
sample_count = _sample_count(cp1, size) |
| 260 |
|
| 261 |
sum_ri_2 = _sum2(cp2, cp2, sample_count) |
| 262 |
sum_aij_ri = _sum2(cp1, cp2, sample_count) |
| 263 |
|
| 264 |
return sum_aij_ri / sum_ri_2 |
| 265 |
|
| 266 |
|
| 267 |
def findmax(cp, len2): |
| 268 |
size = 2 |
| 269 |
sample_count = _sample_count(cp, size) |
| 270 |
|
| 271 |
if len(cp) % 2 != 0: |
| 272 |
raise error("Strings should be even-sized") |
| 273 |
|
| 274 |
if len2 < 0 or sample_count < len2: |
| 275 |
raise error("Input sample should be longer") |
| 276 |
|
| 277 |
if sample_count == 0: |
| 278 |
return 0 |
| 279 |
|
| 280 |
result = _sum2(cp, cp, len2) |
| 281 |
best_result = result |
| 282 |
best_i = 0 |
| 283 |
|
| 284 |
for i in range(1, sample_count - len2 + 1): |
| 285 |
sample_leaving_window = getsample(cp, size, i - 1) |
| 286 |
sample_entering_window = getsample(cp, size, i + len2 - 1) |
| 287 |
|
| 288 |
result -= sample_leaving_window**2 |
| 289 |
result += sample_entering_window**2 |
| 290 |
|
| 291 |
if result > best_result: |
| 292 |
best_result = result |
| 293 |
best_i = i |
| 294 |
|
| 295 |
return best_i |
| 296 |
|
| 297 |
|
| 298 |
def avgpp(cp, size): |
| 299 |
_check_bytes(cp) |
| 300 |
_check_params(len(cp), size) |
| 301 |
sample_count = _sample_count(cp, size) |
| 302 |
if sample_count <= 2: |
| 303 |
return 0 |
| 304 |
|
| 305 |
prevextremevalid = False |
| 306 |
prevextreme = None |
| 307 |
avg = 0 |
| 308 |
nextreme = 0 |
| 309 |
|
| 310 |
prevval = getsample(cp, size, 0) |
| 311 |
val = getsample(cp, size, 1) |
| 312 |
|
| 313 |
prevdiff = val - prevval |
| 314 |
|
| 315 |
for i in range(1, sample_count): |
| 316 |
val = getsample(cp, size, i) |
| 317 |
diff = val - prevval |
| 318 |
|
| 319 |
if diff * prevdiff < 0: |
| 320 |
if prevextremevalid: |
| 321 |
avg += abs(prevval - prevextreme) |
| 322 |
nextreme += 1 |
| 323 |
|
| 324 |
prevextremevalid = True |
| 325 |
prevextreme = prevval |
| 326 |
|
| 327 |
prevval = val |
| 328 |
if diff != 0: |
| 329 |
prevdiff = diff |
| 330 |
|
| 331 |
if nextreme == 0: |
| 332 |
return 0 |
| 333 |
|
| 334 |
return avg // nextreme |
| 335 |
|
| 336 |
|
| 337 |
def maxpp(cp, size): |
| 338 |
_check_params(len(cp), size) |
| 339 |
sample_count = _sample_count(cp, size) |
| 340 |
if sample_count <= 1: |
| 341 |
return 0 |
| 342 |
|
| 343 |
prevextremevalid = False |
| 344 |
prevextreme = None |
| 345 |
max = 0 |
| 346 |
|
| 347 |
prevval = getsample(cp, size, 0) |
| 348 |
val = getsample(cp, size, 1) |
| 349 |
|
| 350 |
prevdiff = val - prevval |
| 351 |
|
| 352 |
for i in range(1, sample_count): |
| 353 |
val = getsample(cp, size, i) |
| 354 |
diff = val - prevval |
| 355 |
|
| 356 |
if diff * prevdiff < 0: |
| 357 |
if prevextremevalid: |
| 358 |
extremediff = abs(prevval - prevextreme) |
| 359 |
if extremediff > max: |
| 360 |
max = extremediff |
| 361 |
prevextremevalid = True |
| 362 |
prevextreme = prevval |
| 363 |
|
| 364 |
prevval = val |
| 365 |
if diff != 0: |
| 366 |
prevdiff = diff |
| 367 |
|
| 368 |
return max |
| 369 |
|
| 370 |
|
| 371 |
def cross(cp, size): |
| 372 |
_check_params(len(cp), size) |
| 373 |
|
| 374 |
crossings = -1 |
| 375 |
last_sample = 17 |
| 376 |
for sample in _get_samples(cp, size): |
| 377 |
sample = sample < 0 |
| 378 |
if sample != last_sample: |
| 379 |
crossings += 1 |
| 380 |
last_sample = sample |
| 381 |
return crossings |
| 382 |
|
| 383 |
|
| 384 |
def mul(cp, size, factor): |
| 385 |
_check_params(len(cp), size) |
| 386 |
clip = _get_clipfn(size) |
| 387 |
|
| 388 |
rv = ffi.new("unsigned char[]", len(cp)) |
| 389 |
result = ffi.buffer(rv) |
| 390 |
|
| 391 |
for i, sample in enumerate(_get_samples(cp, size)): |
| 392 |
sample = clip(int(sample * factor)) |
| 393 |
_put_sample(result, size, i, sample) |
| 394 |
|
| 395 |
return result[:] |
| 396 |
|
| 397 |
|
| 398 |
def tomono(cp, size, fac1, fac2): |
| 399 |
_check_params(len(cp), size) |
| 400 |
clip = _get_clipfn(size) |
| 401 |
|
| 402 |
sample_count = _sample_count(cp, size) |
| 403 |
|
| 404 |
rv = ffi.new("unsigned char[]", len(cp) // 2) |
| 405 |
result = ffi.buffer(rv) |
| 406 |
|
| 407 |
for i in range(0, sample_count, 2): |
| 408 |
l_sample = getsample(cp, size, i) |
| 409 |
r_sample = getsample(cp, size, i + 1) |
| 410 |
|
| 411 |
sample = (l_sample * fac1) + (r_sample * fac2) |
| 412 |
sample = int(clip(sample)) |
| 413 |
|
| 414 |
_put_sample(result, size, i // 2, sample) |
| 415 |
|
| 416 |
return result[:] |
| 417 |
|
| 418 |
|
| 419 |
def tostereo(cp, size, fac1, fac2): |
| 420 |
_check_params(len(cp), size) |
| 421 |
|
| 422 |
sample_count = _sample_count(cp, size) |
| 423 |
|
| 424 |
rv = ffi.new("char[]", len(cp) * 2) |
| 425 |
lib.tostereo(rv, ffi.from_buffer(cp), len(cp), size, fac1, fac2) |
| 426 |
return ffi.buffer(rv)[:] |
| 427 |
|
| 428 |
|
| 429 |
def add(cp1, cp2, size): |
| 430 |
_check_params(len(cp1), size) |
| 431 |
|
| 432 |
if len(cp1) != len(cp2): |
| 433 |
raise error("Lengths should be the same") |
| 434 |
|
| 435 |
rv = ffi.new("char[]", len(cp1)) |
| 436 |
lib.add(rv, ffi.from_buffer(cp1), ffi.from_buffer(cp2), len(cp1), size) |
| 437 |
return ffi.buffer(rv)[:] |
| 438 |
|
| 439 |
|
| 440 |
def bias(cp, size, bias): |
| 441 |
_check_params(len(cp), size) |
| 442 |
|
| 443 |
rv = ffi.new("unsigned char[]", len(cp)) |
| 444 |
result = ffi.buffer(rv) |
| 445 |
|
| 446 |
for i, sample in enumerate(_get_samples(cp, size)): |
| 447 |
sample = _overflow(sample + bias, size) |
| 448 |
_put_sample(result, size, i, sample) |
| 449 |
|
| 450 |
return result[:] |
| 451 |
|
| 452 |
|
| 453 |
def reverse(cp, size): |
| 454 |
_check_params(len(cp), size) |
| 455 |
sample_count = _sample_count(cp, size) |
| 456 |
|
| 457 |
rv = ffi.new("unsigned char[]", len(cp)) |
| 458 |
result = ffi.buffer(rv) |
| 459 |
for i, sample in enumerate(_get_samples(cp, size)): |
| 460 |
_put_sample(result, size, sample_count - i - 1, sample) |
| 461 |
|
| 462 |
return result[:] |
| 463 |
|
| 464 |
|
| 465 |
def lin2lin(cp, size, size2): |
| 466 |
_check_bytes(cp) |
| 467 |
_check_params(len(cp), size) |
| 468 |
_check_size(size2) |
| 469 |
|
| 470 |
if size == size2: |
| 471 |
return cp |
| 472 |
|
| 473 |
new_len = (len(cp) // size) * size2 |
| 474 |
rv = ffi.new("unsigned char[]", new_len) |
| 475 |
result = ffi.buffer(rv) |
| 476 |
|
| 477 |
for i in range(_sample_count(cp, size)): |
| 478 |
sample = _get_sample(cp, size, i) |
| 479 |
if size == 1: |
| 480 |
sample <<= 24 |
| 481 |
elif size == 2: |
| 482 |
sample <<= 16 |
| 483 |
elif size == 3: |
| 484 |
sample <<= 8 |
| 485 |
if size2 == 1: |
| 486 |
sample >>= 24 |
| 487 |
elif size2 == 2: |
| 488 |
sample >>= 16 |
| 489 |
elif size2 == 3: |
| 490 |
sample >>= 8 |
| 491 |
sample = _overflow(sample, size2) |
| 492 |
_put_sample(result, size2, i, sample) |
| 493 |
|
| 494 |
return result[:] |
| 495 |
|
| 496 |
|
| 497 |
def ratecv(cp, size, nchannels, inrate, outrate, state, weightA=1, weightB=0): |
| 498 |
_check_params(len(cp), size) |
| 499 |
if nchannels < 1: |
| 500 |
raise error("# of channels should be >= 1") |
| 501 |
|
| 502 |
bytes_per_frame = size * nchannels |
| 503 |
frame_count = len(cp) // bytes_per_frame |
| 504 |
|
| 505 |
if bytes_per_frame // nchannels != size: |
| 506 |
raise OverflowError("width * nchannels too big for a C int") |
| 507 |
|
| 508 |
if weightA < 1 or weightB < 0: |
| 509 |
raise error("weightA should be >= 1, weightB should be >= 0") |
| 510 |
|
| 511 |
if len(cp) % bytes_per_frame != 0: |
| 512 |
raise error("not a whole number of frames") |
| 513 |
|
| 514 |
if inrate <= 0 or outrate <= 0: |
| 515 |
raise error("sampling rate not > 0") |
| 516 |
|
| 517 |
d = gcd(inrate, outrate) |
| 518 |
inrate //= d |
| 519 |
outrate //= d |
| 520 |
d = gcd(weightA, weightB) |
| 521 |
weightA //= d |
| 522 |
weightB //= d |
| 523 |
|
| 524 |
if state is None: |
| 525 |
d = -outrate |
| 526 |
prev_i = ffi.new('int[]', nchannels) |
| 527 |
cur_i = ffi.new('int[]', nchannels) |
| 528 |
else: |
| 529 |
d, samps = state |
| 530 |
|
| 531 |
if len(samps) != nchannels: |
| 532 |
raise error("illegal state argument") |
| 533 |
|
| 534 |
prev_i, cur_i = zip(*samps) |
| 535 |
prev_i = ffi.new('int[]', prev_i) |
| 536 |
cur_i = ffi.new('int[]', cur_i) |
| 537 |
state_d = ffi.new('int[]', (d,)) |
| 538 |
|
| 539 |
q = frame_count // inrate |
| 540 |
ceiling = (q + 1) * outrate |
| 541 |
nbytes = ceiling * bytes_per_frame |
| 542 |
|
| 543 |
rv = ffi.new("char[]", nbytes) |
| 544 |
cpbuf = ffi.from_buffer(cp) |
| 545 |
trim_index = lib.ratecv(rv, cpbuf, frame_count, size, |
| 546 |
nchannels, inrate, outrate, |
| 547 |
state_d, prev_i, cur_i, |
| 548 |
weightA, weightB) |
| 549 |
result = ffi.buffer(rv)[:trim_index] |
| 550 |
d = state_d[0] |
| 551 |
samps = zip(prev_i, cur_i) |
| 552 |
return (result, (d, tuple(samps))) |
| 553 |
|
| 554 |
|
| 555 |
def _get_lin_samples(cp, size): |
| 556 |
for sample in _get_samples(cp, size): |
| 557 |
if size == 1: |
| 558 |
yield sample << 8 |
| 559 |
elif size == 2: |
| 560 |
yield sample |
| 561 |
elif size == 3: |
| 562 |
yield sample >> 8 |
| 563 |
elif size == 4: |
| 564 |
yield sample >> 16 |
| 565 |
|
| 566 |
|
| 567 |
def _put_lin_sample(result, size, i, sample): |
| 568 |
if size == 1: |
| 569 |
sample >>= 8 |
| 570 |
elif size == 2: |
| 571 |
pass |
| 572 |
elif size == 3: |
| 573 |
sample <<= 8 |
| 574 |
elif size == 4: |
| 575 |
sample <<= 16 |
| 576 |
_put_sample(result, size, i, sample) |
| 577 |
|
| 578 |
|
| 579 |
def lin2ulaw(cp, size): |
| 580 |
_check_params(len(cp), size) |
| 581 |
rv = ffi.new("unsigned char[]", _sample_count(cp, size)) |
| 582 |
for i, sample in enumerate(_get_lin_samples(cp, size)): |
| 583 |
rv[i] = lib.st_14linear2ulaw(sample) |
| 584 |
return ffi.buffer(rv)[:] |
| 585 |
|
| 586 |
|
| 587 |
def ulaw2lin(cp, size): |
| 588 |
_check_size(size) |
| 589 |
rv = ffi.new("unsigned char[]", len(cp) * size) |
| 590 |
result = ffi.buffer(rv) |
| 591 |
for i, value in enumerate(cp): |
| 592 |
sample = lib.st_ulaw2linear16(value) |
| 593 |
_put_lin_sample(result, size, i, sample) |
| 594 |
return result[:] |
| 595 |
|
| 596 |
|
| 597 |
def lin2alaw(cp, size): |
| 598 |
_check_params(len(cp), size) |
| 599 |
rv = ffi.new("unsigned char[]", _sample_count(cp, size)) |
| 600 |
for i, sample in enumerate(_get_lin_samples(cp, size)): |
| 601 |
rv[i] = lib.st_linear2alaw(sample) |
| 602 |
return ffi.buffer(rv)[:] |
| 603 |
|
| 604 |
|
| 605 |
def alaw2lin(cp, size): |
| 606 |
_check_size(size) |
| 607 |
rv = ffi.new("unsigned char[]", len(cp) * size) |
| 608 |
result = ffi.buffer(rv) |
| 609 |
for i, value in enumerate(cp): |
| 610 |
sample = lib.st_alaw2linear16(value) |
| 611 |
_put_lin_sample(result, size, i, sample) |
| 612 |
return result[:] |
| 613 |
|
| 614 |
|
| 615 |
def lin2adpcm(cp, size, state): |
| 616 |
_check_params(len(cp), size) |
| 617 |
state = _check_state(state) |
| 618 |
rv = ffi.new("unsigned char[]", len(cp) // size // 2) |
| 619 |
state_ptr = ffi.new("int[]", state) |
| 620 |
cpbuf = ffi.cast("unsigned char*", ffi.from_buffer(cp)) |
| 621 |
lib.lin2adcpm(rv, cpbuf, len(cp), size, state_ptr) |
| 622 |
return ffi.buffer(rv)[:], tuple(state_ptr) |
| 623 |
|
| 624 |
|
| 625 |
def adpcm2lin(cp, size, state): |
| 626 |
_check_size(size) |
| 627 |
state = _check_state(state) |
| 628 |
rv = ffi.new("unsigned char[]", len(cp) * size * 2) |
| 629 |
state_ptr = ffi.new("int[]", state) |
| 630 |
cpbuf = ffi.cast("unsigned char*", ffi.from_buffer(cp)) |
| 631 |
lib.adcpm2lin(rv, cpbuf, len(cp), size, state_ptr) |
| 632 |
return ffi.buffer(rv)[:], tuple(state_ptr) |
| 633 |
|
| 634 |
def byteswap(cp, size): |
| 635 |
if len(cp) % size != 0: |
| 636 |
raise error("not a whole number of frames") |
| 637 |
sample_count = _sample_count(cp, size) |
| 638 |
rv = ffi.new("unsigned char[]", len(cp)) |
| 639 |
base = size |
| 640 |
next_bump = 0 |
| 641 |
bump = 2 * size |
| 642 |
for i in range(len(cp)): |
| 643 |
base -= 1 |
| 644 |
rv[i] = cp[base] |
| 645 |
if base == next_bump: |
| 646 |
base += bump |
| 647 |
next_bump += size |
| 648 |
return ffi.buffer(rv)[:] |