import sounddevice as sd
import wavio

# 录制参数
duration = 5  # 录制 5 秒
samplerate = 22050  # 采样率 (XTTS v2 兼容的)
filename = "example_speaker.wav"

print("开始录音，说话吧...")
audio = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype='int16')
sd.wait()
print("录音完成，保存中...")

# 保存 WAV 文件
wavio.write(filename, audio, samplerate, sampwidth=2)
print(f"录音已保存为 {filename}")