sus-stringify
Version:
A SUS (Sliding Universal Score) stringifier.
3 lines (2 loc) • 3.83 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self)["sus-stringify"]={})}(this,(function(t){"use strict";"function"==typeof SuppressedError&&SuppressedError;class e extends Error{constructor(t,e){super(`Notes are not aligned correctly, with gcd ${t}. Note ticks: ${e.join(", ")}`),this.name="InvalidAlignmentError",this.gcdValue=t,this.noteTicks=e}}var r;function o(t,e){return t<e?o(e,t):0===e?t:o(e,t%e)}class n extends Map{constructor(t){super(),this.generateDefault=t}get(t){const e=super.get(t);if(void 0===e){const e=this.generateDefault();return super.set(t,e),e}return e}}class s{constructor(){r.set(this,void 0),function(t,e,r,o,n){if("m"===o)throw new TypeError("Private method is not writable");if("a"===o&&!n)throw new TypeError("Private accessor was defined without a setter");if("function"==typeof e?t!==e||!n:!e.has(t))throw new TypeError("Cannot write private member to an object whose class did not declare it");"a"===o?n.call(t,r):n?n.value=r:e.set(t,r)}(this,r,[...Array(36).keys()].map((t=>[t,[0,0]])),"f")}generateChannel(t,e){const o=function(t,e,r,o){if("a"===r&&!o)throw new TypeError("Private accessor was defined without a getter");if("function"==typeof e?t!==e||!o:!e.has(t))throw new TypeError("Cannot read private member from an object whose class did not declare it");return"m"===r?o:"a"===r?o.call(t):o?o.value:e.get(t)}(this,r,"f").find((([,[r,o]])=>0===r&&0===o||e<r||o<t));if(!o)throw new Error("No more channel available.");return o[1]=[t,e],o[0]}}function i(t,r="This file was generated by sus-js v1.1.0"){var i;const{metadata:a,bpms:c,taps:u,directionals:f,slides:l,barLengths:p}=t;let h=480;const d=[];d.push(r);for(const[t,e]of Object.entries(a))if("requests"!==t)d.push(`#${t.toUpperCase()} ${"number"==typeof e?e:(w=e,`"${w}"`)}`);else{d.push("");for(const t of e)d.push(`#REQUEST "${t}"`),t.startsWith("ticks_per_beat")&&(h=+t.split(" ")[1])}var w;d.push("");const g=new n((()=>({raws:[],ticksPerMeasure:0})));for(const[t,e]of p)d.push(`#${t.toString().padStart(3,"0")}02:${e}`);d.push("");let v=0;const m=p.map((([t,e],r,o)=>{var n;const[s]=null!==(n=o[r+1])&&void 0!==n?n:[Number.POSITIVE_INFINITY],i=v;return v+=(s-t)*e*h,{tick:i,measure:t,value:e}})).reverse(),$=(t,e,r)=>{var o;const{tick:n,measure:s,value:i}=null!==(o=m.find((({tick:e})=>t>=e)))&&void 0!==o?o:{tick:0,measure:0,value:4},a=Math.floor(s+(t-n)/h/i),c=g.get(`${a.toString().padStart(3,"0")}${e}`);c.raws.push({tick:t-n,value:r}),c.ticksPerMeasure=i*h};if(c.length>=Math.pow(36,2)-1)throw new Error(`Too much BPMS (${c.length} >= 36^2 -1 = ${Math.pow(36,2)-1})`);const k=new Map;for(const[t,e]of c){const r=(k.size+1).toString(36).padStart(2,"0");k.has(e)||(k.set(e,r),d.push(`#BPM${k.get(e)}:${e}`)),$(t,"08",null!==(i=k.get(e))&&void 0!==i?i:r)}for(const{tick:t,lane:e,width:r,type:o}of u)$(t,`1${e.toString(36)}`,`${o}${r.toString(36)}`);for(const{tick:t,lane:e,width:r,type:o}of f)$(t,`5${e.toString(36)}`,`${o}${r.toString(36)}`);const y=new s;for(const t of l){const e=t[0].tick,r=t[t.length-1].tick,o=y.generateChannel(e,r).toString(36);for(const{tick:e,lane:r,width:n,type:s}of t)$(e,`3${r.toString(36)}${o}`,`${s}${n.toString(36)}`)}return g.forEach((({raws:t,ticksPerMeasure:r},n)=>{var s;const i=t.reduce(((t,e)=>o(e.tick,t)),r);if(!Number.isInteger(i))throw new e(i,t.map((({tick:t})=>t)));const a=new Map(t.sort((({tick:t},{tick:e})=>t-e)).map((({tick:t,value:e})=>[t%r,e]))),c=[];for(let t=0;t*i<r;t++){const e=t*i;c.push(null!==(s=a.get(e))&&void 0!==s?s:"00")}d.push(`#${n}:${c.join("")}`)})),d.join("\n")}r=new WeakMap,t.InvalidAlignmentError=e,t.default=i,t.stringify=i,Object.defineProperty(t,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.js.map