Files
soothe2-re/rtone.py
T

100 lines
3.1 KiB
Python

#!/usr/bin/env python3
import subprocess, os, glob, sys, time, struct
RPP = "/home/m/soothe-bt/render_short.rpp"
OUT = "/home/m/soothe-bt/out_rt.wav"
proc = subprocess.Popen(['reaper', '-nosplash', '-renderproject', RPP],
stdout=open('/tmp/rtone.log', 'w'), stderr=subprocess.STDOUT)
print('reaper', proc.pid, flush=True)
def find_hosts():
out = []
for p in glob.glob('/proc/[0-9]*'):
try:
pid = int(os.path.basename(p))
m = open(f'/proc/{pid}/maps').read()
c = open(f'/proc/{pid}/cmdline', 'rb').read().decode('utf8', 'replace')
if 'soothe2' in m and 'yabridge-host.exe' in c:
out.append(pid)
except Exception:
pass
return out
def out_growth():
try:
return os.path.getsize(OUT)
except Exception:
return 0
# Phase 1: wait for render to actually produce audio
sz0 = out_growth()
base_sz = sz0
t0 = time.time()
hosts = None
grew = 0
# wait for host
while time.time() - t0 < 60:
hosts = find_hosts()
if hosts:
break
time.sleep(0.2)
print('hosts:', hosts, flush=True)
# wait for out file to grow beyond initial (render active)
while time.time() - t0 < 120:
s = out_growth()
if s > base_sz:
grew = s
break
# if reaper exited, stop trying
if proc.poll() is not None:
print('reaper exited, last out size', s, flush=True)
break
time.sleep(0.3)
print('out grew to', grew, flush=True)
if not hosts or grew == 0:
proc.kill()
sys.exit('no live render')
time.sleep(0.5)
N_POW2 = {2,4,8,16,32,64,128,256,512,1024,2048,4096,8192,16384,32768,65536}
UPS = {2,4,8,16}
def scan(pid):
mem = os.open(f'/proc/{pid}/mem', os.O_RDONLY)
found = []
maps = open(f'/proc/{pid}/maps').read()
for line in maps.splitlines():
p = line.split()
lo, hi = (int(x, 16) for x in p[0].split('-'))
if 'r' not in p[1]:
continue
if lo < 0x180000000 or lo >= 0x183000000:
try:
d = os.pread(mem, hi - lo, lo)
except Exception:
continue
if len(d) < 0x1ac + 8:
continue
for o in range(0, len(d) - (0x1ac + 8), 8):
n0 = struct.unpack_from('<I', d, o + 0x19c)[0]
n1 = struct.unpack_from('<I', d, o + 0x1a0)[0]
u = struct.unpack_from('<I', d, o + 0x1ac)[0]
if n0 in N_POW2 and n1 == n0 and u in UPS:
ws = struct.unpack_from('<d', d, o + 0x1a4)[0]
v0 = struct.unpack_from('<Q', d, o)[0]
found.append((lo + o, n0, u, ws, v0))
os.close(mem)
return found
for pid in hosts:
f = scan(pid)
print('PID', pid, 'sig hits:', len(f), flush=True)
for x in f[:30]:
print(' obj', hex(x[0]), 'N=', x[1], 'ups=', x[2],
'win=%.6g' % x[3], 'vptr=%#x' % x[4], flush=True)
with open('/tmp/nsig.txt', 'w') as fo:
for x in f:
fo.write('%d %#x %d %d %.17g %#x\n' % (pid, x[0], x[1], x[2], x[3], x[4]))
proc.kill()
print('done', flush=True)