Files
SonicForgeStudio/native_bridge/debug/probe_app3.py
T

122 lines
4.3 KiB
Python

# Drive REAL app bridge SHM: cycle several VST3 plugins on ch=0, screenshot each, brightness.
# Usage: python probe_app3.py
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 = b"SonicForge_DAW_IPC"
SIZE = 32768
HERE = os.path.dirname(os.path.abspath(__file__))
APP_LOG = os.path.join(os.environ.get('APPDATA', '.'), 'SonicForgeDAW', 'logs', 'bridge.log')
VST = r"C:\Program Files\Common Files\VST3"
PLUGINS = [
("nexus", os.path.join(VST, "Nexus.vst3")),
("kontakt", os.path.join(VST, "Kontakt.vst3")),
("scaler2", os.path.join(VST, "Scaler2.vst3")),
("ample", os.path.join(VST, "Ample Sound", "ACDD.vst3")),
]
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
def shot(hwnd, tag):
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 None
png = os.path.join(HERE, f"probe_app3_{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}"
for tag, path in PLUGINS:
since = len(log_lines())
print(f"--- {tag} ---")
push_controls([(2, 0, 0, 0, path)])
r = wait_log("instrument loaded ch=0", 90, since)
print(" load:", (r or "TIMEOUT").split(" type=")[0])
since = len(log_lines())
push_controls([(4, 0, 0, 0, tag)])
r = wait_log("GUI attached", 40, since)
print(" open:", (r or "TIMEOUT")[:100])
m = re.search(r'hwnd=([0-9A-Fa-f]+)', r or "")
if not m:
print(" NO HWND"); continue
hwnd = int(m.group(1), 16)
time.sleep(8)
res = shot(hwnd, tag)
print(" shot:", res)
# close GUI via WM_CLOSE (VstWindowProc hides + detaches view)
user32.SendMessageW(hwnd, 0x0010, 0, 0) # WM_CLOSE
time.sleep(3)
print("done")