File size: 1,112 Bytes
3713433
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""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("<i2").tobytes())
print("Audio capture saved", flush=True)