# Part of the Fifteen-Thieves Project
# Chris Xiong 2020
# License: Expat (MIT)
import time
import math
import sys
import xv.model
import xv.amap
import xv.util
inst_sets=[(96,"classical"),(97,"contemporary"),(98,"solo"),(99,"enhanced"),(80,"special_1"),(81,"special_2")]
rhyt_sets=[(104,"classical"),(105,"contemporary"),(106,"solo"),(107,"enhanced")]
if __name__ == "__main__":
print("loading address mapping...", flush=True)
m = xv.amap.AddrMapTemplate("amaps/sd-80.amap")
t = xv.amap.create_addr_map_tree(m)
print("creating device...", flush=True)
x = xv.model.Model("SD-80 Part A",[0x00, 0x48],0x10,t)
print("initializing device...", flush=True)
x.write_locations("sd_root.setup.native_on", 0)
time.sleep(0.05)
gm2pcb=[]
gm2rhy=[]
with open("data/gm2pcbanks","r") as f:
gm2pcb = [int(x.strip(), 16) for x in f.readline().split(',')]
gm2rhy = [int(x.strip()) for x in f.readline().split(',')]
for iset in inst_sets:
for pc in range(0, 128):
for bk in range(0, 1 if iset[0] < 96 else gm2pcb[pc]):
x.midio.send_message([0xb0,0x00,iset[0]])
x.midio.send_message([0xb0,0x20,bk])
x.midio.send_message([0xc0,pc])
mr = x.read_locations("sd_root.part[0]", 1)
pcn = x.aroot.value("sd_root.part[0].patch.pc_common.name")
pcn = pcn.decode().strip().replace('/','∕')
print(f"{bk:02d}:{pc:03d} {pcn}")
fn = f"data/patchparam/{iset[1]}/{bk:02d}-{pc:03d}-{pcn}.memoryregion"
xv.util.write_memoryregion(fn, mr)
for rset in rhyt_sets:
for pc in gm2rhy:
x.midio.send_message([0xb0,0x00,rset[0]])
x.midio.send_message([0xb0,0x20,0])
x.midio.send_message([0xc0,pc])
mr = x.read_locations("sd_root.part[0]", 2)
pcn = x.aroot.value("sd_root.part[0].rhythm.ry_common.name")
pcn = pcn.decode().strip().replace('/','∕')
print(f"{pc:02d} {pcn}")
fn = f"data/rhythmparam/{rset[1]}/{pc:02d}-{pcn}.memoryregion"
xv.util.write_memoryregion(fn, mr)