Files
SonicForgeStudio/native_bridge/debug/probe_app2.py
T

131 lines
4.6 KiB
Python

# Drive REAL app bridge SHM (SonicForge_DAW_IPC_<daw_pid>): load Nexus ch=0, open GUI, screenshot, brightness.
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time, re
from PIL import Image
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
DAW_PID = sys.argv[1] if len(sys.argv) > 1 else "21148"
SHM_NAME = b"SonicForge_DAW_IPC"
SIZE = 32768
HERE = os.path.dirname(os.path.abspath(__file__))
LOG = os.path.join(HERE, "probe_app2.log")
APP_LOG = os.path.join(os.environ.get('APPDATA', '.'), 'SonicForgeDAW', 'logs', 'bridge.log')
NEXUS = r"C:\Program Files\Common Files\VST3\Nexus.vst3"
PNG = os.path.join(HERE, "probe_app2.png")
user32 = ctypes.windll.user32
kernel32 = ctypes.windll.kernel32
kernel32.OpenFileMappingA.argtypes = [ctypes.wintypes.DWORD, w.BOOL, ctypes.c_char_p]
kernel32.OpenFileMappingA.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
FILE_MAP_ALL_ACCESS = 0xF001F
hMap = kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCESS, False, SHM_NAME)
if not hMap:
print("OpenFileMapping 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 log_lines():
try:
with open(APP_LOG, 'r', encoding='utf-8', errors='replace') as f:
return f.readlines()
except FileNotFoundError:
return []
def wait_log(pattern, timeout, since):
end = time.time() + timeout
while time.time() < end:
lines = log_lines()
for i in range(since, len(lines)):
if pattern in lines[i]:
return lines[i].strip()
time.sleep(0.2)
return None
since = len(log_lines())
print("shm", SHM_NAME, "log lines", since)
print("[1] load Nexus ch=0...")
push_controls([(2, 0, 0, 0, NEXUS)])
r = wait_log("instrument loaded ch=0", 60, since)
print(" ", r)
since = len(log_lines())
print("[2] open GUI ch=0...")
push_controls([(4, 0, 0, 0, "nexus")])
r = wait_log("GUI attached", 40, since)
print("[open]", r)
hwnd = None
if r:
m = re.search(r'hwnd=([0-9A-Fa-f]+)', r)
if m:
hwnd = int(m.group(1), 16)
print("hwnd =", hex(hwnd) if hwnd else None)
time.sleep(8)
if hwnd:
rw = w.RECT()
user32.GetWindowRect(hwnd, ctypes.byref(rw))
print("window rect", rw.left, rw.top, rw.right, rw.bottom, "vis", user32.IsWindowVisible(hwnd))
wdt, hgt = rw.right - rw.left, rw.bottom - rw.top
if wdt <= 0 or hgt <= 0:
print("bad rect"); sys.exit(1)
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]
print("screenshot", PNG, img.size, "avgLum", round(avg, 1), "buckets[0-31..224-255]", pct)
else:
found = []
@ctypes.WINFUNCTYPE(w.BOOL, w.HWND, w.LPARAM)
def cb(h, lp):
buf = ctypes.create_unicode_buffer(64)
user32.GetClassNameW(h, buf, 64)
if 'SonicForge' in buf.value:
found.append((h, buf.value))
return True
user32.EnumWindows(cb, 0)
print("native vst windows:", [(hex(h), c) for h, c in found])
kernel32.UnmapViewOfFile.argtypes = [ctypes.c_void_p]
kernel32.CloseHandle.argtypes = [ctypes.c_void_p]
kernel32.UnmapViewOfFile(ptr)
kernel32.CloseHandle(hMap)
print("done")