docs: publish bridge with restructured markdown docs
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
"""Make a named capture device the Windows default (all three roles).
|
||||
|
||||
WeChat records from the default microphone, so after creating the VB-CABLE
|
||||
virtual cable we point the default capture at "CABLE Output" — then whatever
|
||||
we play into "CABLE Input" is what WeChat hears.
|
||||
|
||||
Only the registry is used to resolve the endpoint id; the actual switch goes
|
||||
through IPolicyConfig (the same COM object the Sound control panel uses).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import ctypes
|
||||
import winreg
|
||||
from ctypes import POINTER, byref, c_int, c_ulong, c_ushort, c_void_p, c_wchar_p
|
||||
|
||||
GUID_DEVINTERFACE = "{0.0.1.00000000}"
|
||||
FRIENDLY = "{a45c254e-df1c-4efd-8020-67d146a850e0},2"
|
||||
MMDEV = r"SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Capture"
|
||||
|
||||
|
||||
class GUID(ctypes.Structure):
|
||||
_fields_ = [
|
||||
("Data1", c_ulong),
|
||||
("Data2", c_ushort),
|
||||
("Data3", c_ushort),
|
||||
("Data4", ctypes.c_ubyte * 8),
|
||||
]
|
||||
|
||||
|
||||
def _guid(text: str) -> GUID:
|
||||
clsid = GUID()
|
||||
ctypes.oledll.ole32.CLSIDFromString(text, byref(clsid))
|
||||
return clsid
|
||||
|
||||
|
||||
CLSID_POLICY = "{870AF99C-171D-4F9E-AF0D-E63DF40C2BC9}"
|
||||
IID_POLICY = "{F8679F50-850A-41CF-9C72-430F290290C8}"
|
||||
|
||||
# IPolicyConfig vtable: IUnknown(3) + GetMixFormat(3) ... SetDefaultEndpoint at 13
|
||||
# IUnknown 0..2, GetMixFormat 3, GetDeviceFormat 4, ResetDeviceFormat 5,
|
||||
# SetDeviceFormat 6, GetProcessingPeriod 7, SetProcessingPeriod 8,
|
||||
# GetShareMode 9, SetShareMode 10, GetPropertyValue 11, SetPropertyValue 12,
|
||||
# SetDefaultEndpoint 13, SetEndpointVisibility 14
|
||||
SET_DEFAULT_INDEX = 13
|
||||
|
||||
|
||||
def capture_devices() -> list[tuple[str, str]]:
|
||||
"""[(endpoint_id, friendly_name)] for every registered capture endpoint."""
|
||||
out: list[tuple[str, str]] = []
|
||||
try:
|
||||
root = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, MMDEV)
|
||||
except OSError:
|
||||
return out
|
||||
with root:
|
||||
i = 0
|
||||
while True:
|
||||
try:
|
||||
sub = winreg.EnumKey(root, i)
|
||||
except OSError:
|
||||
break
|
||||
i += 1
|
||||
name = ""
|
||||
try:
|
||||
with winreg.OpenKey(root, sub + r"\Properties") as props:
|
||||
name, _ = winreg.QueryValueEx(props, FRIENDLY)
|
||||
except OSError:
|
||||
pass
|
||||
out.append((f"{GUID_DEVINTERFACE}.{sub}", name))
|
||||
return out
|
||||
|
||||
|
||||
def find_capture(name_part: str) -> str | None:
|
||||
want = name_part.lower()
|
||||
for device_id, name in capture_devices():
|
||||
if want in (name or "").lower():
|
||||
return device_id
|
||||
return None
|
||||
|
||||
|
||||
def set_default(device_id: str, roles: tuple[int, ...] = (0, 1, 2)) -> None:
|
||||
ole32 = ctypes.windll.ole32
|
||||
ole32.CoInitialize(None)
|
||||
policy = c_void_p()
|
||||
clsid = _guid(CLSID_POLICY)
|
||||
iid = _guid(IID_POLICY)
|
||||
hr = ole32.CoCreateInstance(byref(clsid), None, 1, byref(iid), byref(policy))
|
||||
if hr != 0:
|
||||
raise RuntimeError(f"CoCreateInstance(IPolicyConfig) failed: 0x{hr & 0xFFFFFFFF:08X}")
|
||||
|
||||
vtbl = ctypes.cast(policy, POINTER(POINTER(c_void_p)))[0]
|
||||
fn = ctypes.WINFUNCTYPE(c_int, c_void_p, c_wchar_p, c_int)(vtbl[SET_DEFAULT_INDEX])
|
||||
try:
|
||||
for role in roles:
|
||||
rc = fn(policy, device_id, role)
|
||||
if rc != 0:
|
||||
raise RuntimeError(f"SetDefaultEndpoint(role={role}) failed: 0x{rc & 0xFFFFFFFF:08X}")
|
||||
finally:
|
||||
# Release
|
||||
rel = ctypes.WINFUNCTYPE(c_ulong, c_void_p)(vtbl[2])
|
||||
rel(policy)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for device_id, name in capture_devices():
|
||||
print(device_id, "|", name)
|
||||
target = find_capture("CABLE Output")
|
||||
print("target:", target)
|
||||
if target:
|
||||
set_default(target)
|
||||
print("default capture updated")
|
||||
Reference in New Issue
Block a user