Files
SonicForgeStudio/native_bridge/debug/probe_fix_flow.py
T

181 lines
6.6 KiB
Python

# Verify fix flow user: create SHM plain -> spawn bridge (no debugger) -> load Nexus ch=0
# -> open GUI -> screenshot -> close -> reopen -> verify unmute + other plugin loads.
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time, re
from PIL import Image
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
SHM_NAME = "SonicForge_DAW_IPC"
SIZE = 32768
HERE = os.path.dirname(os.path.abspath(__file__))
BRIDGE = r"C:\Users\locpham\SonicForgeStudio\native_bridge\build\Release\daw_vst_bridge.exe"
LOG = os.path.join(HERE, "probe_fix_flow.log")
NEXUS = r"C:\Program Files\Common Files\VST3\Nexus.vst3"
AMPLE = r"C:\Program Files\Common Files\VST3\Ample Sound\ACDD.vst3"
kernel32 = ctypes.windll.kernel32
kernel32.CreateFileMappingA.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.c_char_p]
kernel32.CreateFileMappingA.restype = ctypes.c_void_p
kernel32.MapViewOfFile.argtypes = [ctypes.c_void_p, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD, ctypes.c_size_t]
kernel32.MapViewOfFile.restype = ctypes.c_void_p
INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
PAGE_READWRITE = 0x04
FILE_MAP_ALL_ACCESS = 0xF001F
hMap = kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READWRITE, 0, SIZE, SHM_NAME.encode())
if not hMap:
print("CreateFileMapping failed", ctypes.get_last_error()); sys.exit(1)
ptr = kernel32.MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0)
if not ptr:
print("MapViewOfFile failed", ctypes.get_last_error()); sys.exit(1)
def w32(off, fmt, *args):
ctypes.memmove(ptr + off, struct.pack(fmt, *args), struct.calcsize(fmt))
def write_control(type_, arg0, arg1, ch, path):
for i in range(8):
off = 2836 + i * 1040
t = ctypes.c_uint32.from_address(ptr + off).value
if t == 0:
w32(off, 'IIII', type_, arg0, arg1, ch)
if path:
b = path.encode('utf-8')[:1023] + b'\x00'
ctypes.memmove(ptr + off + 16, b, len(b))
return i
raise RuntimeError("control queue full")
def push_controls(events):
for i in range(8):
w32(2836 + i * 1040, 'I', 0)
for (type_, arg0, arg1, ch, path) in events:
write_control(type_, arg0, arg1, ch, path)
w32(11156, 'I', len(events))
def write_index():
return ctypes.c_uint32.from_address(ptr + 4).value
def wait_log(pattern, timeout, since=0):
end = time.time() + timeout
while time.time() < end:
try:
with open(LOG, 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
except FileNotFoundError:
lines = []
for i in range(max(0, len(lines) - 1 - since), len(lines)):
if pattern in lines[i]:
return lines[i].strip()
time.sleep(0.2)
return None
open(LOG, 'w', encoding='utf-8').close()
proc = subprocess.Popen(
[BRIDGE, "--shm", SHM_NAME],
env={**os.environ, "SF_SHM_NAME": SHM_NAME, "SF_PARENT_PID": str(os.getpid()),
"SF_SAMPLE_RATE": "48000", "SF_BLOCK_SIZE": "256"},
stdout=open(LOG, 'ab'), stderr=subprocess.STDOUT,
)
time.sleep(1.5)
print("bridge pid", proc.pid, "poll", proc.poll())
if proc.poll() is not None:
print("!!! BRIDGE EXITED EARLY"); sys.exit(1)
def shot(hwnd, tag):
user32 = ctypes.windll.user32
rw = w.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rw))
wdt, hgt = rw.right - rw.left, rw.bottom - rw.top
if wdt <= 0 or hgt <= 0:
return f"bad rect {rw.left},{rw.top},{rw.right},{rw.bottom}"
png = os.path.join(HERE, f"probe_fix_{tag}.png")
ps = f"""
Add-Type -AssemblyName System.Drawing
$bmp = New-Object System.Drawing.Bitmap({wdt}, {hgt})
$g = [System.Drawing.Graphics]::FromImage($bmp)
$g.CopyFromScreen({rw.left}, {rw.top}, 0, 0, $bmp.Size)
$bmp.Save('{png}')
$g.Dispose(); $bmp.Dispose()
"""
subprocess.run(["powershell", "-NoProfile", "-Command", ps], check=False)
img = Image.open(png).convert('L')
px = list(img.getdata())
n = len(px)
avg = sum(px) / n
buckets = [0, 0, 0, 0, 0, 0, 0, 0]
for v in px:
buckets[v // 32] += 1
pct = [round(100 * b / n) for b in buckets]
return f"avgLum {round(avg,1)} buckets[0-31..224-255] {pct} size {img.size}"
def muted_now():
# bridge log "channel muted while editor open" appears while editor open;
# after close, next load/open should NOT show mute. We instead check writeIndex.
return None
# [1] load Nexus ch=0
since = len(open(LOG, 'rb').read().splitlines())
print("[1] load Nexus ch=0...")
push_controls([(2, 0, 0, 0, NEXUS)])
r = wait_log("instrument loaded ch=0", 90)
print(" ", r)
# [2] open GUI
since = len(open(LOG, 'rb').read().splitlines())
print("[2] open GUI ch=0...")
push_controls([(4, 0, 0, 0, "nexus")])
r = wait_log("GUI attached", 40)
print(" ", r)
m = re.search(r'hwnd=([0-9A-Fa-f]+)', r or "")
hwnd = int(m.group(1), 16) if m else None
time.sleep(8)
if hwnd:
print(" shot:", shot(hwnd, "nexus_open"))
alive = proc.poll() is None
print(" alive:", alive)
wi = write_index(); time.sleep(2); wi2 = write_index()
print(" writeIndex", wi, "->", wi2, "advancing:", wi2 > wi)
# [3] close GUI via WM_CLOSE
if hwnd:
ctypes.windll.user32.SendMessageW(hwnd, 0x0010, 0, 0)
time.sleep(4)
alive = proc.poll() is None
print("[3] after close alive:", alive)
# [4] reopen GUI (user symptom: reopen -> mute)
since = len(open(LOG, 'rb').read().splitlines())
print("[4] reopen GUI ch=0...")
push_controls([(4, 0, 0, 0, "nexus")])
r = wait_log("GUI attached", 40)
print(" ", r)
m = re.search(r'hwnd=([0-9A-Fa-f]+)', r or "")
hwnd = int(m.group(1), 16) if m else None
time.sleep(8)
if hwnd:
print(" shot:", shot(hwnd, "nexus_reopen"))
alive = proc.poll() is None
wi = write_index(); time.sleep(2); wi2 = write_index()
print(" alive:", alive, "writeIndex", wi, "->", wi2, "advancing:", wi2 > wi)
# [5] load OTHER plugin while GUI open (user symptom: not load)
since = len(open(LOG, 'rb').read().splitlines())
print("[5] load Ample while Nexus GUI open...")
push_controls([(2, 0, 0, 0, AMPLE)])
r = wait_log("instrument loaded ch=0", 60)
print(" ", r)
alive = proc.poll() is None
wi = write_index(); time.sleep(2); wi2 = write_index()
print(" alive:", alive, "writeIndex", wi, "->", wi2, "advancing:", wi2 > wi)
print("FINAL alive:", proc.poll() is None)
print("--- last 25 log lines ---")
for l in open(LOG, 'r', encoding='utf-8', errors='replace').readlines()[-25:]:
print(l.rstrip())
kernel32.UnmapViewOfFile.argtypes = [ctypes.c_void_p]
kernel32.CloseHandle.argtypes = [ctypes.c_void_p]
kernel32.UnmapViewOfFile(ptr)
kernel32.CloseHandle(hMap)
proc.terminate()
print("done")