"""Record the Windows speaker output, including the simulator's legacy TTS. Optional demo dependencies: pip install soundcard numpy imageio-ffmpeg The caller creates STOP in the output directory to finish capture. """ import json import pathlib import sys import time import wave import numpy as np import soundcard as sc out = pathlib.Path(sys.argv[1]).resolve() out.mkdir(parents=True, exist_ok=True) speaker = sc.default_speaker() microphone = sc.get_microphone(speaker.id, include_loopback=True) with microphone.recorder(samplerate=48000, channels=2, blocksize=48000) as capture: with wave.open(str(out / "speaker.wav"), "wb") as wav: wav.setnchannels(2) wav.setsampwidth(2) wav.setframerate(48000) (out / "audio-ready.json").write_text(json.dumps({"started": time.time(), "speaker": speaker.name})) print("Audio capture ready", flush=True) while not (out / "STOP").exists(): block = capture.record(numframes=4800) wav.writeframes((np.clip(block, -1, 1) * 32767).astype("