Files
mcde/tests/tone-check.py
T
lotherk 1a6572a499 dtchat: add the headless voice test (T6.7)
dtchat-headless gains --call, --audio-in FILE and --audio-out FILE: it exchanges media-key on the ready link, calls the peer, answers an incoming ring (it is a test peer, so the answer is automatic), and pumps the call from its loop: a 20 ms frame from the raw s16le 48 kHz file is encoded and sent while in the call, received frames are decoded into the output file, and call/audio events go to stdout. tests/p2p-audio.sh starts two instances (A listens and records, B connects, calls and plays tests/tone.py's 2 s 440 Hz tone), waits for both in-call, for 100 received frames, prints the CPU time of both, quits them and has tests/tone-check.py verify the recording (length within 25 percent, RMS > 500, zero crossings within 15 percent). Standalone run: A 45475, both in-call, recording 2.00 s / RMS 6339 / 441 Hz, all ok. Wired into the harness as DtChat/p2p-audio (42 entries). In-tree build pending: a foreign make userland holds the lock.
2026-11-17 08:54:05 +00:00

51 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
"""tests/tone-check.py -- check that FILE holds a sine of about HZ.
usage: tone-check.py FILE SECONDS HZ
Checks the length (within 25 percent: jitter buffer and Opus), the RMS
(> 500, so it is not silence) and the zero crossings (within 15 percent).
"""
import math
import struct
import sys
def main():
path = sys.argv[1]
seconds = float(sys.argv[2])
hz = float(sys.argv[3])
with open(path, "rb") as f:
data = f.read()
n = len(data) // 2
if n == 0:
print("tone-check: no samples", file=sys.stderr)
return 1
samples = struct.unpack("<%dh" % n, data[:n * 2])
rate = 48000
duration = n / rate
if abs(duration - seconds) > seconds * 0.25:
print("tone-check: %.2f s instead of %.2f" % (duration, seconds),
file=sys.stderr)
return 1
rms = math.sqrt(sum(float(s) * s for s in samples) / n)
if rms < 500:
print("tone-check: rms %.0f is silence" % rms, file=sys.stderr)
return 1
crossings = 0
prev = samples[0]
for s in samples[1:]:
if (prev < 0 and s >= 0) or (prev >= 0 and s < 0):
crossings += 1
prev = s
measured = crossings / 2.0 / duration
if abs(measured - hz) > hz * 0.15:
print("tone-check: %.0f Hz instead of %.0f" % (measured, hz),
file=sys.stderr)
return 1
print("tone-check: %.2f s, rms %.0f, %.0f Hz" % (duration, rms, measured))
return 0
if __name__ == "__main__":
sys.exit(main())