JSFiddle - React, Tailwind, and code Playground
HTML
<textarea id=input placeholder="zzfxM pattern: [[[,1,,,...],[...]],[...]]"></textarea><br>
<button id=compress>Compress</button><br>
<textarea id=output placeholder="Self-compressing zzfxM pattern code"></textarea>
CSS
textarea { width: 100%; height: 15em; word-break: break-all; }
JavaScript
const jsonButEmpty = o => JSON.stringify(o).replace(/null/g, '');
const jsonButZero = o => JSON.stringify(o).replace(/null/g, '0');
compress.onclick = () => {
const patterns = (0, eval)(input.value);
const compressed = patterns.map(pattern => {
return pattern.map(([ins, pan, ...seq]) => {
return [ins, pan, seq.map(v => {
v = v || 0;
const val = v | 0;
const att = Math.round(v * 100) % 100;
const att10 = att / 10 | 0;
const att1 = att % 10;
// note value: 37(-1) ,, 73(36)
// attenuation (tens): 75(.1) 77(.2) .. 83(.9)
// attenuation (ones): 74(.01) 76(.02) .. 82(.09)
return String.fromCharCode(val + 38) +
(att10 ? String.fromCharCode(att10 * 2 + 73) : '') +
(att1 ? String.fromCharCode(att1 * 2 + 72) : '');
}).join('')];
});
});
const code = jsonButEmpty(compressed) + `.map(p=>p.map(p=>[...p.pop()].map(b=>p.push((b=b.charCodeAt())<74?b-38:p.pop()+(b-72>>1)/(b&1?10:100)))&&p))`;
const codeResult = jsonButZero((0, eval)(code));
const origResult = jsonButZero(patterns);
if (codeResult !== origResult) throw `verification failed:\n${origResult}\n${codeResult}`;
output.value = code;
};