Files
SonicForgeStudio/native_bridge/debug/driver_narrow.py
T

108 lines
3.7 KiB
Python

# Narrow test: load ONLY ch=5 Nexus -> open GUI ch=5. Is multi-instance the trigger?
import ctypes, ctypes.wintypes as w, struct, subprocess, sys, os, time
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
SHM_NAME = "SonicForge_DAW_IPC_NARROW"
SIZE = 32768
BRIDGE = r"C:\Users\locpham\SonicForgeStudio\native_bridge\build\Release\daw_vst_bridge.exe"
LOG = os.path.join(os.path.dirname(os.path.abspath(__file__)), "narrow_bridge.log")
NEXUS = r"C:\Program Files\Common Files\VST3\Nexus.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)
w32(11156, 'I', 0)
n = 0
for (type_, arg0, arg1, ch, path) in events:
write_control(type_, arg0, arg1, ch, path)
n += 1
w32(11156, 'I', n)
def wait_log(pattern, timeout, since=0):
end = time.time() + timeout
lines = []
try:
with open(LOG, 'r', encoding='utf-8', errors='replace') as f:
lines = f.readlines()
except FileNotFoundError:
pass
last = max(0, len(lines) - since)
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(last, len(lines)):
if pattern in lines[i]:
return lines[i].strip()
last = max(0, len(lines))
time.sleep(0.2)
return None
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, 'wb'), stderr=subprocess.STDOUT,
)
time.sleep(1.5)
print("bridge pid", proc.pid, "poll", proc.poll())
print("[1] load ch=5 Nexus only...")
push_controls([(2, 0, 0, 5, NEXUS)])
r = wait_log("instrument loaded ch=5", 60)
print(" ", r)
print("[2] open GUI ch=5...")
push_controls([(4, 0, 0, 5, "nexus")])
r = wait_log("GUI attached", 30)
print(" ", r)
time.sleep(2)
print("alive:", proc.poll() is None)
print("--- last 20 log lines ---")
with open(LOG, 'r', encoding='utf-8', errors='replace') as f:
for l in f.readlines()[-20:]:
print(l.rstrip())
kernel32.UnmapViewOfFile(ptr)
kernel32.CloseHandle(hMap)
try:
proc.terminate()
except Exception:
pass
print("done")