"""Play a WAV file into a named MME output device (used to feed VB-CABLE). We need this because WeChat records from whatever capture device is default, and the only way to get our TTS audio into that stream is to play it into "CABLE Input" (the playback side of the virtual cable) while WeChat records from "CABLE Output". """ from __future__ import annotations import ctypes import time import wave from ctypes import wintypes winmm = ctypes.WinDLL("winmm") WAVE_MAPPER = wintypes.UINT(-1) CALLBACK_NULL = 0x00000000 WHDR_DONE = 0x00000001 class WAVEFORMATEX(ctypes.Structure): _fields_ = [ ("wFormatTag", wintypes.WORD), ("nChannels", wintypes.WORD), ("nSamplesPerSec", wintypes.DWORD), ("nAvgBytesPerSec", wintypes.DWORD), ("nBlockAlign", wintypes.WORD), ("wBitsPerSample", wintypes.WORD), ("cbSize", wintypes.WORD), ] class WAVEHDR(ctypes.Structure): _fields_ = [ ("lpData", ctypes.c_void_p), ("dwBufferLength", wintypes.DWORD), ("dwBytesRecorded", wintypes.DWORD), ("dwUser", ctypes.c_void_p), ("dwFlags", wintypes.DWORD), ("dwLoops", wintypes.DWORD), ("lpNext", ctypes.c_void_p), ("reserved", ctypes.c_void_p), ] class WAVEOUTCAPS(ctypes.Structure): _fields_ = [ ("wMid", wintypes.WORD), ("wPid", wintypes.WORD), ("vDriverVersion", wintypes.UINT), ("szPname", wintypes.WCHAR * 32), ("dwFormats", wintypes.DWORD), ("wChannels", wintypes.WORD), ("wReserved1", wintypes.WORD), ("dwSupport", wintypes.DWORD), ] def output_devices() -> list[tuple[int, str]]: n = winmm.waveOutGetNumDevs() out = [] for i in range(n): caps = WAVEOUTCAPS() if winmm.waveOutGetDevCapsW(i, ctypes.byref(caps), ctypes.sizeof(caps)) == 0: out.append((i, caps.szPname.strip())) return out def find_device(name_part: str) -> int | None: want = name_part.lower() for idx, name in output_devices(): if want in name.lower(): return idx return None def play_wav(path: str, device_name: str | None = None, blocking: bool = True) -> float: """Play `path` into the MME output device whose name contains `device_name`. `device_name=None` (or a name that matches nothing) plays on the default output device (WAVE_MAPPER). Returns the audio duration in seconds (0.0 when nothing was played). """ with wave.open(path, "rb") as f: channels = f.getnchannels() rate = f.getframerate() width = f.getsampwidth() frames = f.readframes(f.getnframes()) if not frames: return 0.0 fmt = WAVEFORMATEX() fmt.wFormatTag = 1 # PCM fmt.nChannels = channels fmt.nSamplesPerSec = rate fmt.wBitsPerSample = width * 8 fmt.nBlockAlign = channels * width fmt.nAvgBytesPerSec = rate * fmt.nBlockAlign fmt.cbSize = 0 idx = find_device(device_name) if device_name else None dev = WAVE_MAPPER if idx is None else idx handle = wintypes.HANDLE() rc = winmm.waveOutOpen(ctypes.byref(handle), dev, ctypes.byref(fmt), 0, 0, CALLBACK_NULL) if rc != 0: raise RuntimeError(f"waveOutOpen rc={rc} (device={device_name!r}, idx={idx})") buf = ctypes.create_string_buffer(frames, len(frames)) hdr = WAVEHDR() hdr.lpData = ctypes.cast(buf, ctypes.c_void_p) hdr.dwBufferLength = len(frames) try: if winmm.waveOutPrepareHeader(handle, ctypes.byref(hdr), ctypes.sizeof(hdr)) != 0: raise RuntimeError("waveOutPrepareHeader failed") if winmm.waveOutWrite(handle, ctypes.byref(hdr), ctypes.sizeof(hdr)) != 0: raise RuntimeError("waveOutWrite failed") if blocking: deadline = time.time() + len(frames) / max(1, fmt.nAvgBytesPerSec) + 10 while not (hdr.dwFlags & WHDR_DONE): if time.time() > deadline: break time.sleep(0.02) duration = len(frames) / max(1, fmt.nAvgBytesPerSec) return duration finally: winmm.waveOutUnprepareHeader(handle, ctypes.byref(hdr), ctypes.sizeof(hdr)) winmm.waveOutClose(handle) def capture_devices() -> list[str]: n = winmm.waveInGetNumDevs() return [str(i) for i in range(n)] if __name__ == "__main__": print("outputs:", output_devices()) print("inputs:", capture_devices())