42 lines
1.6 KiB
Python
42 lines
1.6 KiB
Python
# The deterministic integer test signal shared by tools/fixtures/m4a.py, tools/fixtures/caf.py and the Luau side
|
|
# (tests/audio/_signal.luau computes the same numbers): triangle + square + MINSTD noise (exact in doubles) + clicks, per channel.
|
|
import struct
|
|
|
|
|
|
def signal(frames, channels, bits, seed):
|
|
s = seed
|
|
peak = 2 ** (bits - 1) - 1
|
|
out = []
|
|
for i in range(frames):
|
|
for c in range(channels):
|
|
s = (s * 48271) % 2147483647
|
|
p1 = 97 + 31 * c
|
|
tri = abs((i % p1) * 2 - p1) * 2 - p1
|
|
v = tri * (peak // (4 * p1))
|
|
p2 = 1500 + 200 * c
|
|
v += (peak // 6) if (i // p2) % 2 == 0 else -(peak // 6)
|
|
v += ((s // 65536) % 4096 - 2048) * (peak // 65536 + 1)
|
|
if i % 7001 < 3:
|
|
v += peak // 2
|
|
out.append(max(-peak - 1, min(peak, v)))
|
|
return out
|
|
|
|
|
|
def pack(samples, bits, big=False):
|
|
e = ">" if big else "<"
|
|
if bits == 16:
|
|
return struct.pack(e + "%dh" % len(samples), *samples)
|
|
if bits == 24:
|
|
b = bytearray()
|
|
for v in samples:
|
|
x = struct.pack("<i", v)[:3]
|
|
b += x[::-1] if big else x
|
|
return bytes(b)
|
|
return struct.pack(e + "%di" % len(samples), *samples)
|
|
|
|
|
|
def wav(samples, channels, rate, bits):
|
|
data = pack(samples, bits)
|
|
fmt = struct.pack("<HHIIHH", 1, channels, rate, rate * channels * bits // 8, channels * bits // 8, bits)
|
|
return b"RIFF" + struct.pack("<I", 36 + len(data)) + b"WAVEfmt " + struct.pack("<I", 16) + fmt + b"data" + struct.pack("<I", len(data)) + data
|