Run neighbor loopback through console WASAPI
This commit is contained in:
parent
bd28491b42
commit
3f4651a488
7 changed files with 103 additions and 26 deletions
|
|
@ -31,9 +31,10 @@ def main() -> int:
|
|||
parser.add_argument("--ffmpeg", default=r"C:\Users\Madman's Lullaby\AppData\Local\Microsoft\WinGet\Links\ffmpeg.exe")
|
||||
parser.add_argument("--remote-dir", default=r"C:\Meta\LocalCastBridge\calibration\remote-captures")
|
||||
parser.add_argument("--focusrite-device", default="Analogue 1 + 2 (Focusrite USB Audio)")
|
||||
parser.add_argument("--loopback-device", default="Voicemeeter Out B3 (VB-Audio Voicemeeter VAIO)")
|
||||
parser.add_argument("--loopback-device", default="")
|
||||
parser.add_argument("--loopback-capture", choices=("wasapi", "dshow"), default="wasapi")
|
||||
parser.add_argument("--remote-wasapi-script", default=r"C:\Meta\LocalCastBridge\scripts\wasapi-loopback-capture.ps1")
|
||||
parser.add_argument("--remote-wasapi-role", choices=("Console", "Multimedia", "Communications"), default="Console")
|
||||
parser.add_argument("--local-loopback-query", default="Scarlett")
|
||||
parser.add_argument("--max-lag-ms", type=float, default=3000.0)
|
||||
parser.add_argument("--min-loopback-rms", type=float, default=1e-4)
|
||||
|
|
@ -47,9 +48,10 @@ def main() -> int:
|
|||
stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%S")
|
||||
|
||||
remote_focusrite = f"{args.remote_dir.rstrip('\\\\/')}\\co_streamer_focusrite-{stamp}.wav"
|
||||
remote_loopback = f"{args.remote_dir.rstrip('\\\\/')}\\co_streamer_loopback-{stamp}.wav"
|
||||
remote_loopback_ext = "f32" if args.loopback_capture == "wasapi" else "wav"
|
||||
remote_loopback = f"{args.remote_dir.rstrip('\\\\/')}\\co_streamer_loopback-{stamp}.{remote_loopback_ext}"
|
||||
local_focusrite = surface_dir / "raw_focusrite.wav"
|
||||
local_remote_loopback = surface_dir / "raw_loopback.wav"
|
||||
local_remote_loopback = surface_dir / f"raw_loopback.{remote_loopback_ext}"
|
||||
local_reference = surface_dir / "local_loopback_reference.wav"
|
||||
|
||||
commands = [
|
||||
|
|
@ -86,7 +88,7 @@ def main() -> int:
|
|||
sample_rate = int(args.sample_rate)
|
||||
_, reference = read_wav_float(local_reference, sample_rate)
|
||||
_, focusrite = read_wav_float(local_focusrite, sample_rate)
|
||||
_, remote_loop = read_wav_float(local_remote_loopback, sample_rate)
|
||||
remote_loop = read_loopback_capture(local_remote_loopback, sample_rate, args.loopback_capture)
|
||||
reference_mono = mono(reference)
|
||||
remote_loop_mono = mono(remote_loop)
|
||||
max_lag = int(float(args.max_lag_ms) * sample_rate / 1000.0)
|
||||
|
|
@ -159,14 +161,47 @@ def remote_ffmpeg_command(args: argparse.Namespace, device: str, output: str, *,
|
|||
|
||||
def remote_loopback_command(args: argparse.Namespace, output: str) -> str:
|
||||
if args.loopback_capture == "dshow":
|
||||
if not args.loopback_device:
|
||||
raise SystemExit("--loopback-device is required when --loopback-capture dshow")
|
||||
return remote_ffmpeg_command(args, args.loopback_device, output, channels=2)
|
||||
log = output.rsplit(".", 1)[0] + ".log"
|
||||
task = "LocalCastWasapiLoopbackCapture"
|
||||
return (
|
||||
f'powershell -NoProfile -ExecutionPolicy Bypass -File "{args.remote_wasapi_script}" '
|
||||
f'-Output "{output}" -Seconds {float(args.seconds):.3f} '
|
||||
f'-SampleRate {int(args.sample_rate)} -Channels 2'
|
||||
"powershell -NoProfile -ExecutionPolicy Bypass -Command "
|
||||
+ quote_for_cmd(
|
||||
"$ErrorActionPreference='Stop'; "
|
||||
f"$out='{escape_ps(output)}'; $log='{escape_ps(log)}'; "
|
||||
"Remove-Item -LiteralPath $out,$log -ErrorAction SilentlyContinue; "
|
||||
f"$cmd='powershell.exe -NoProfile -ExecutionPolicy Bypass -File \"{escape_ps(args.remote_wasapi_script)}\" "
|
||||
f"-Output \"{escape_ps(output)}\" -Seconds {float(args.seconds):.3f} "
|
||||
f"-SampleRate {int(args.sample_rate)} -Channels 2 -Role {args.remote_wasapi_role} *> \"{escape_ps(log)}\"'; "
|
||||
"$bat=[IO.Path]::ChangeExtension($out,'.cmd'); "
|
||||
"Set-Content -LiteralPath $bat -Encoding ASCII -Value ('@echo off' + [Environment]::NewLine + $cmd + [Environment]::NewLine); "
|
||||
"$action=New-ScheduledTaskAction -Execute $bat; "
|
||||
"$trigger=New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(10); "
|
||||
"$principal=New-ScheduledTaskPrincipal -UserId ($env:COMPUTERNAME + '\\' + $env:USERNAME) -LogonType Interactive -RunLevel Limited; "
|
||||
f"Register-ScheduledTask -TaskName '{task}' -Action $action -Trigger $trigger -Principal $principal -Force | Out-Null; "
|
||||
f"Start-ScheduledTask -TaskName '{task}'; "
|
||||
f"$deadline=(Get-Date).AddSeconds({float(args.seconds) + 12.0:.3f}); "
|
||||
"while((Get-Date) -lt $deadline){ "
|
||||
" $info=Get-ScheduledTaskInfo -TaskName '" + task + "'; "
|
||||
" if((Test-Path -LiteralPath $out) -and ((Get-Item -LiteralPath $out).Length -gt 0) -and $info.LastTaskResult -eq 0){ break } "
|
||||
" Start-Sleep -Milliseconds 250 "
|
||||
"}; "
|
||||
"if(-not (Test-Path -LiteralPath $out)){ if(Test-Path -LiteralPath $log){Get-Content $log -Tail 40}; throw 'WASAPI loopback capture did not create output' }; "
|
||||
"Write-Host ('wasapiLoopbackBytes=' + (Get-Item -LiteralPath $out).Length)"
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def quote_for_cmd(command: str) -> str:
|
||||
return '"' + command.replace('"', '\\"') + '"'
|
||||
|
||||
|
||||
def escape_ps(value: str) -> str:
|
||||
return value.replace("'", "''")
|
||||
|
||||
|
||||
def sftp_get(ssh_target: str, remote_file: str, local_file: Path) -> None:
|
||||
remote_sftp_path = "/C:/" + remote_file.replace("\\", "/").split("C:/", 1)[-1]
|
||||
batch = f'get "{remote_sftp_path}" "{local_file}"\n'
|
||||
|
|
@ -207,6 +242,16 @@ def read_wav_float(path: Path, target_rate: int) -> tuple[int, np.ndarray]:
|
|||
return int(rate), ensure_2d(samples)
|
||||
|
||||
|
||||
def read_loopback_capture(path: Path, target_rate: int, capture_kind: str) -> np.ndarray:
|
||||
if capture_kind == "wasapi":
|
||||
samples = np.fromfile(path, dtype=np.float32)
|
||||
if samples.size % 2:
|
||||
samples = samples[:-1]
|
||||
return samples.reshape(-1, 2).astype(np.float32)
|
||||
_, samples = read_wav_float(path, target_rate)
|
||||
return samples
|
||||
|
||||
|
||||
def pcm_to_float(samples: np.ndarray) -> np.ndarray:
|
||||
array = np.asarray(samples)
|
||||
if array.ndim == 1:
|
||||
|
|
@ -230,8 +275,12 @@ def mono(samples: np.ndarray) -> np.ndarray:
|
|||
|
||||
def normalized_delay(signal_a: np.ndarray, signal_b: np.ndarray, max_lag: int) -> tuple[int, float]:
|
||||
count = min(len(signal_a), len(signal_b))
|
||||
if count <= 0:
|
||||
return 0, 0.0
|
||||
a = signal_a[:count].astype(np.float32)
|
||||
b = signal_b[:count].astype(np.float32)
|
||||
if float(np.std(a)) <= 1e-12 or float(np.std(b)) <= 1e-12:
|
||||
return 0, 0.0
|
||||
a = (a - float(np.mean(a))) / (float(np.std(a)) + 1e-9)
|
||||
b = (b - float(np.mean(b))) / (float(np.std(b)) + 1e-9)
|
||||
corr = signal.correlate(a, b, mode="full", method="fft")
|
||||
|
|
|
|||
|
|
@ -2,7 +2,9 @@ param(
|
|||
[string]$Output = "-",
|
||||
[double]$Seconds = 0,
|
||||
[int]$SampleRate = 48000,
|
||||
[int]$Channels = 2
|
||||
[int]$Channels = 2,
|
||||
[ValidateSet("Console", "Multimedia", "Communications")]
|
||||
[string]$Role = "Console"
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
|
@ -17,6 +19,8 @@ namespace LocalCastBridge {
|
|||
public static class WasapiLoopbackCapture {
|
||||
const int eRender = 0;
|
||||
const int eConsole = 0;
|
||||
const int eMultimedia = 1;
|
||||
const int eCommunications = 2;
|
||||
const int CLSCTX_ALL = 23;
|
||||
const uint AUDCLNT_SHAREMODE_SHARED = 0;
|
||||
const uint AUDCLNT_STREAMFLAGS_LOOPBACK = 0x00020000;
|
||||
|
|
@ -107,12 +111,13 @@ namespace LocalCastBridge {
|
|||
int GetNextPacketSize(out uint frames);
|
||||
}
|
||||
|
||||
public static void Capture(string outputPath, double seconds, int targetRate, int targetChannels) {
|
||||
public static void Capture(string outputPath, double seconds, int targetRate, int targetChannels, string roleName) {
|
||||
Stream output = outputPath == "-" ? Console.OpenStandardOutput() : new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read);
|
||||
try {
|
||||
IMMDeviceEnumerator enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
|
||||
IMMDevice device;
|
||||
Check(enumerator.GetDefaultAudioEndpoint(eRender, eConsole, out device), "GetDefaultAudioEndpoint");
|
||||
int role = RoleFromName(roleName);
|
||||
Check(enumerator.GetDefaultAudioEndpoint(eRender, role, out device), "GetDefaultAudioEndpoint");
|
||||
IAudioClient audioClient;
|
||||
Guid audioClientId = IID_IAudioClient;
|
||||
Check(device.Activate(ref audioClientId, CLSCTX_ALL, IntPtr.Zero, out audioClient), "Activate IAudioClient");
|
||||
|
|
@ -122,12 +127,30 @@ namespace LocalCastBridge {
|
|||
Console.Error.WriteLine("MixFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize);
|
||||
IntPtr activeFormatPtr = mixFormatPtr;
|
||||
IntPtr fallbackFormatPtr = IntPtr.Zero;
|
||||
IntPtr closest = IntPtr.Zero;
|
||||
int support = audioClient.IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, activeFormatPtr, out closest);
|
||||
Console.Error.WriteLine("MixFormatSupport hr=0x{0:X8} closest={1}", support, closest != IntPtr.Zero);
|
||||
if (support != 0 && closest != IntPtr.Zero) {
|
||||
activeFormatPtr = closest;
|
||||
fmt = Marshal.PtrToStructure<WAVEFORMATEX>(activeFormatPtr);
|
||||
Console.Error.WriteLine("ClosestFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize);
|
||||
}
|
||||
int init = audioClient.Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 0, 0, activeFormatPtr, IntPtr.Zero);
|
||||
if (init < 0) {
|
||||
fallbackFormatPtr = MakeWaveFormatPcm((ushort)targetChannels, (uint)targetRate, 16);
|
||||
activeFormatPtr = fallbackFormatPtr;
|
||||
fmt = Marshal.PtrToStructure<WAVEFORMATEX>(activeFormatPtr);
|
||||
Console.Error.WriteLine("RetryFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize);
|
||||
IntPtr retryClosest = IntPtr.Zero;
|
||||
int retrySupport = audioClient.IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, activeFormatPtr, out retryClosest);
|
||||
Console.Error.WriteLine("RetryFormatSupport hr=0x{0:X8} closest={1}", retrySupport, retryClosest != IntPtr.Zero);
|
||||
if (retrySupport != 0 && retryClosest != IntPtr.Zero) {
|
||||
Marshal.FreeHGlobal(fallbackFormatPtr);
|
||||
fallbackFormatPtr = IntPtr.Zero;
|
||||
activeFormatPtr = retryClosest;
|
||||
fmt = Marshal.PtrToStructure<WAVEFORMATEX>(activeFormatPtr);
|
||||
Console.Error.WriteLine("RetryClosestFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize);
|
||||
}
|
||||
init = audioClient.Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, 0, 0, activeFormatPtr, IntPtr.Zero);
|
||||
}
|
||||
Check(init, "Initialize loopback");
|
||||
|
|
@ -185,6 +208,7 @@ namespace LocalCastBridge {
|
|||
audioClient.Stop();
|
||||
Marshal.FreeCoTaskMem(mixFormatPtr);
|
||||
if (fallbackFormatPtr != IntPtr.Zero) Marshal.FreeHGlobal(fallbackFormatPtr);
|
||||
if (closest != IntPtr.Zero) Marshal.FreeCoTaskMem(closest);
|
||||
Marshal.ReleaseComObject(captureClient);
|
||||
Marshal.ReleaseComObject(audioClient);
|
||||
Marshal.ReleaseComObject(device);
|
||||
|
|
@ -203,6 +227,12 @@ namespace LocalCastBridge {
|
|||
}
|
||||
}
|
||||
|
||||
static int RoleFromName(string roleName) {
|
||||
if (String.Equals(roleName, "Multimedia", StringComparison.OrdinalIgnoreCase)) return eMultimedia;
|
||||
if (String.Equals(roleName, "Communications", StringComparison.OrdinalIgnoreCase)) return eCommunications;
|
||||
return eConsole;
|
||||
}
|
||||
|
||||
static IntPtr MakeWaveFormatPcm(ushort channels, uint sampleRate, ushort bits) {
|
||||
WAVEFORMATEX fmt = new WAVEFORMATEX();
|
||||
fmt.wFormatTag = 1;
|
||||
|
|
@ -247,4 +277,4 @@ namespace LocalCastBridge {
|
|||
"@
|
||||
|
||||
Add-Type -TypeDefinition $source
|
||||
[LocalCastBridge.WasapiLoopbackCapture]::Capture($Output, $Seconds, $SampleRate, $Channels)
|
||||
[LocalCastBridge.WasapiLoopbackCapture]::Capture($Output, $Seconds, $SampleRate, $Channels, $Role)
|
||||
|
|
|
|||
Loading…
Reference in a new issue