Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#!/usr/bin/env python3"""Generate a reference stimulus for the mic test: silence, 1 kHz tone,log sweep 100 Hz -> 8 kHz, then a 1 kHz tone again."""
import sysimport wavefrom pathlib import Path
import numpy as np
SR = 48000OUT = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("captures/stimulus.wav")
def tone(f, dur, amp): t = np.arange(int(SR * dur)) / SR return amp * np.sin(2 * np.pi * f * t)
def sweep(f0, f1, dur, amp): n = int(SR * dur) t = np.arange(n) / SR k = (f1 / f0) ** (1 / dur) phase = 2 * np.pi * f0 * (k ** t - 1) / np.log(k) return amp * np.sin(phase)
amp = 0.25 # ~-12 dBFSparts = [ np.zeros(int(SR * 0.5)), tone(1000, 1.0, amp), np.zeros(int(SR * 0.2)), sweep(100, 8000, 3.0, amp), np.zeros(int(SR * 0.2)), tone(1000, 1.0, amp), np.zeros(int(SR * 0.5)),]x = np.concatenate(parts)pcm = (np.clip(x, -1, 1) * 32767).astype("<i2")
OUT.parent.mkdir(parents=True, exist_ok=True)with wave.open(str(OUT), "wb") as w: w.setnchannels(1) w.setsampwidth(2) w.setframerate(SR) w.writeframes(pcm.tobytes())print(f"wrote {OUT} ({len(x)/SR:.2f}s)")