forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli7 hours ago399 commits

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