Last active
June 9, 2026 12:18
-
-
Save kartben/ad18c15f64dc045a96fdeb5a84460598 to your computer and use it in GitHub Desktop.
Generate PCM test files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| python3 - <<'PY' | |
| import math | |
| import struct | |
| sample_rates = [8000, 16000, 32000, 44100, 48000] | |
| widths = [8, 16, 24, 32] | |
| freq = 1000 | |
| duration_s = 1 | |
| steps = 61 | |
| def pcm_format_name(width): | |
| return { | |
| 8: "s8", | |
| 16: "s16le", | |
| 24: "s24le", | |
| 32: "s32le", | |
| }[width] | |
| def pack_sample(value, width): | |
| if width == 8: | |
| return struct.pack("<b", value) | |
| if width == 16: | |
| return struct.pack("<h", value) | |
| if width == 24: | |
| # Signed 24-bit little-endian PCM | |
| return int(value).to_bytes(3, byteorder="little", signed=True) | |
| if width == 32: | |
| return struct.pack("<i", value) | |
| raise ValueError(f"Unsupported width: {width}") | |
| for width in widths: | |
| max_amp = (1 << (width - 1)) - 1 | |
| for sample_rate in sample_rates: | |
| fmt = pcm_format_name(width) | |
| out = f"stereo_opposite_levels_{fmt}_{sample_rate}hz.pcm" | |
| with open(out, "wb") as f: | |
| print(f"\nGenerating {out}") | |
| for step in range(steps): | |
| left_db = -60 + step | |
| right_db = -step | |
| left_amp = max_amp * (10 ** (left_db / 20)) | |
| right_amp = max_amp * (10 ** (right_db / 20)) | |
| print(f"{step:02d}: L={left_db:4} dBFS, R={right_db:4} dBFS") | |
| for n in range(sample_rate * duration_s): | |
| s = math.sin(2 * math.pi * freq * n / sample_rate) | |
| left = int(round(left_amp * s)) | |
| right = int(round(right_amp * s)) | |
| f.write(pack_sample(left, width)) | |
| f.write(pack_sample(right, width)) | |
| print(f"Wrote {out}") | |
| PY |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment