| 1 | |
| 2 | set -euo pipefail |
| 3 | STATE="${HARNESS_STATE:-/tmp/claude-1000/yumush-harness}" |
| 4 | source "$STATE/env" |
| 5 | out="$STATE/audio" |
| 6 | mkdir -p "$out" |
| 7 | python3 - "$out/tone.wav" "${1:-8}" <<'PY' |
| 8 | import sys, wave, struct, math |
| 9 | w = wave.open(sys.argv[1], "wb"); w.setnchannels(2); w.setsampwidth(2); w.setframerate(48000) |
| 10 | v = [int(12000 * math.sin(2 * math.pi * 440 * i / 48000)) for i in range(48000 * int(sys.argv[2]))] |
| 11 | w.writeframes(b"".join(struct.pack("<hh", a, a) for a in v)) |
| 12 | PY |
| 13 | pw-play --target harness-app "$out/tone.wav" & |
| 14 | player=$! |
| 15 | sleep 2 |
| 16 | timeout 4 pw-record --target harness-speaker -P '{ stream.capture.sink = true }' "$out/speaker.wav" || true |
| 17 | wait "$player" || true |
| 18 | python3 - "$out/speaker.wav" <<'PY' |
| 19 | import sys, wave, struct, math |
| 20 | w = wave.open(sys.argv[1]) |
| 21 | n = w.getnframes(); ch = w.getnchannels(); width = w.getsampwidth() |
| 22 | raw = w.readframes(n) |
| 23 | fmt = {2: "h", 4: "i"}[width] |
| 24 | s = struct.unpack("<" + fmt * (len(raw) // width), raw)[::ch] |
| 25 | scale = float(1 << (8 * width - 1)) |
| 26 | x = [v / scale for v in s[: 48000 * 2]] |
| 27 | def power(f): |
| 28 | re = sum(v * math.cos(2 * math.pi * f * i / 48000) for i, v in enumerate(x)) |
| 29 | im = sum(v * math.sin(2 * math.pi * f * i / 48000) for i, v in enumerate(x)) |
| 30 | return math.hypot(re, im) / max(len(x), 1) |
| 31 | rms = math.sqrt(sum(v * v for v in x) / max(len(x), 1)) |
| 32 | print(f"rms {rms:.4f} 440Hz {power(440):.4f} 1000Hz {power(1000):.4f}") |
| 33 | PY |