UNPKG

json-midi-encoder

Version:

This module encodes a JSON representation of MIDI data into a binary MIDI file.

2 lines 8.83 kB
export declare const worker = "(()=>{var e={455:function(e,t){!function(e){\"use strict\";var t=function(e){return function(t){var r=e(t);return t.add(r),r}},r=function(e){return function(t,r){return e.set(t,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,s=536870912,a=2*s,i=function(e,t){return function(r){var i=t.get(r),o=void 0===i?r.size:i<a?i+1:0;if(!r.has(o))return e(r,o);if(r.size<s){for(;r.has(o);)o=Math.floor(Math.random()*a);return e(r,o)}if(r.size>n)throw new Error(\"Congratulations, you created a collection of unique numbers which uses all available integers!\");for(;r.has(o);)o=Math.floor(Math.random()*n);return e(r,o)}},o=new WeakMap,f=r(o),c=i(f,o),u=t(c);e.addUniqueNumber=u,e.generateUniqueNumber=c}(t)}},t={};function r(n){var s=t[n];if(void 0!==s)return s.exports;var a=t[n]={exports:{}};return e[n].call(a.exports,a,a.exports,r),a.exports}(()=>{\"use strict\";const e=-32603,t=-32602,n=-32601,s=(e,t)=>Object.assign(new Error(e),{status:t}),a=t=>s('The handler of the method called \"'.concat(t,'\" returned an unexpected result.'),e),i=(t,r)=>async({data:{id:i,method:o,params:f}})=>{const c=r[o];try{if(void 0===c)throw(e=>s('The requested method called \"'.concat(e,'\" is not supported.'),n))(o);const r=void 0===f?c():c(f);if(void 0===r)throw(t=>s('The handler of the method called \"'.concat(t,'\" returned no required result.'),e))(o);const u=r instanceof Promise?await r:r;if(null===i){if(void 0!==u.result)throw a(o)}else{if(void 0===u.result)throw a(o);const{result:e,transferables:r=[]}=u;t.postMessage({id:i,result:e},r)}}catch(e){const{message:r,status:n=-32603}=e;t.postMessage({error:{code:n,message:r},id:i})}};var o=r(455);const f=new Map,c=(e,r,n)=>({...r,connect:({port:t})=>{t.start();const n=e(t,r),s=(0,o.generateUniqueNumber)(f);return f.set(s,(()=>{n(),t.close(),f.delete(s)})),{result:s}},disconnect:({portId:e})=>{const r=f.get(e);if(void 0===r)throw(e=>s('The specified parameter called \"portId\" with the given value \"'.concat(e,'\" does not identify a port connected to this worker.'),t))(e);return r(),{result:null}},isSupported:async()=>{if(await new Promise((e=>{const t=new ArrayBuffer(0),{port1:r,port2:n}=new MessageChannel;r.onmessage=({data:t})=>e(null!==t),n.postMessage(t,[t])}))){const e=n();return{result:e instanceof Promise?await e:e}}return{result:!1}}}),u=(e,t,r=()=>!0)=>{const n=c(u,t,r),s=i(e,n);return e.addEventListener(\"message\",s),()=>e.removeEventListener(\"message\",s)},d=e=>{const t=new ArrayBuffer(e);return{arrayBuffer:t,dataView:new DataView(t)}},l=e=>{const t=new ArrayBuffer(e);return{arrayBuffer:t,dataView:new DataView(t)}},m=e=>{const t=e.reduce(((e,t)=>e+t.byteLength),0),[,r]=e.reduce((([e,t],r)=>(t.set(new Uint8Array(r),e),[e+r.byteLength,t])),[0,new Uint8Array(t)]);return r.buffer},h=(e=>t=>{const r=Math.max(1,Math.floor(Math.log(t)/Math.log(2)/7)+1),{arrayBuffer:n,dataView:s}=e(r);for(let e=1;e<r;e+=1)s.setUint8(r-1-e,t>>7*e&127|128);return s.setUint8(r-1,127&t),n})(l),U=((e,t,r,n)=>s=>{if((e=>void 0!==e.channelPrefix)(s)){const{arrayBuffer:t,dataView:r}=e(4);return r.setUint8(0,255),r.setUint8(1,32),r.setUint8(2,1),r.setUint8(3,s.channelPrefix),t}if((e=>void 0!==e.channelPressure)(s)){const{arrayBuffer:t,dataView:r}=e(2);return r.setUint8(0,13|15&s.channel),r.setUint8(1,s.channelPressure.pressure),t}if((e=>void 0!==e.controlChange)(s)){const{arrayBuffer:t,dataView:r}=e(3);return r.setUint8(0,176|15&s.channel),r.setUint8(1,s.controlChange.type),r.setUint8(2,s.controlChange.value),t}if((e=>void 0!==e.copyrightNotice)(s))return t(s,2,\"copyrightNotice\");if((e=>void 0!==e.cuePoint&&void 0===e.metaTypeByte)(s))return t(s,7,\"cuePoint\");if((e=>void 0!==e.deviceName)(s))return t(s,9,\"deviceName\");if((e=>void 0!==e.endOfTrack)(s)){const{arrayBuffer:t,dataView:r}=e(3);return r.setUint8(0,255),r.setUint8(1,47),r.setUint8(2,0),t}if((e=>void 0!==e.instrumentName)(s))return t(s,4,\"instrumentName\");if((e=>void 0!==e.keyPressure)(s)){const{arrayBuffer:t,dataView:r}=e(3);return r.setUint8(0,10|15&s.channel),r.setUint8(1,s.keyPressure.noteNumber),r.setUint8(2,s.keyPressure.pressure),t}if((e=>void 0!==e.keySignature)(s)){const{arrayBuffer:t,dataView:r}=e(5);return r.setUint8(0,255),r.setUint8(1,89),r.setUint8(2,2),r.setUint8(3,s.keySignature.key),r.setUint8(4,s.keySignature.scale),t}if((e=>void 0!==e.lyric)(s))return t(s,5,\"lyric\");if((e=>void 0!==e.marker)(s))return t(s,6,\"marker\");if((e=>void 0!==e.midiPort)(s)){const{arrayBuffer:t,dataView:r}=e(4);return r.setUint8(0,255),r.setUint8(1,33),r.setUint8(2,1),r.setUint8(3,s.midiPort),t}if((e=>void 0!==e.noteOff)(s)){const{arrayBuffer:t,dataView:r}=e(3);return r.setUint8(0,128|15&s.channel),r.setUint8(1,s.noteOff.noteNumber),r.setUint8(2,s.noteOff.velocity),t}if((e=>void 0!==e.noteOn)(s)){const{arrayBuffer:t,dataView:r}=e(3);return r.setUint8(0,144|15&s.channel),r.setUint8(1,s.noteOn.noteNumber),r.setUint8(2,s.noteOn.velocity),t}if((e=>void 0!==e.pitchBend)(s)){const{arrayBuffer:t,dataView:r}=e(3);return r.setUint8(0,224|15&s.channel),r.setUint8(1,127&s.pitchBend),r.setUint8(2,s.pitchBend>>7),t}if((e=>void 0!==e.programChange)(s)){const{arrayBuffer:t,dataView:r}=e(2);return r.setUint8(0,192|15&s.channel),r.setUint8(1,s.programChange.programNumber),t}if((e=>void 0!==e.programName)(s))return t(s,8,\"programName\");if((e=>void 0!==e.sequencerSpecificData)(s)){const{arrayBuffer:t,dataView:a}=e(2);a.setUint8(0,255),a.setUint8(1,127);const i=s.sequencerSpecificData.length/2,o=n(i),{arrayBuffer:f,dataView:c}=e(i);for(let e=0;e<s.sequencerSpecificData.length;e+=2)c.setUint8(e/2,parseInt(s.sequencerSpecificData.slice(e,e+2),16));return r([t,o,f])}if((e=>void 0!==e.setTempo)(s)){const{arrayBuffer:t,dataView:r}=e(6);return r.setUint8(0,255),r.setUint8(1,81),r.setUint8(2,3),r.setUint8(3,s.setTempo.microsecondsPerQuarter>>16),r.setUint8(4,s.setTempo.microsecondsPerQuarter>>8),r.setUint8(5,s.setTempo.microsecondsPerQuarter),t}if((e=>void 0!==e.smpteOffset)(s)){const{arrayBuffer:t,dataView:r}=e(8);let n;if(24===s.smpteOffset.frameRate)n=0;else if(25===s.smpteOffset.frameRate)n=32;else if(29===s.smpteOffset.frameRate)n=64;else{if(30!==s.smpteOffset.frameRate)throw new Error;n=96}return r.setUint8(0,255),r.setUint8(1,84),r.setUint8(2,5),r.setUint8(3,s.smpteOffset.hour|n),r.setUint8(4,s.smpteOffset.minutes),r.setUint8(5,s.smpteOffset.seconds),r.setUint8(6,s.smpteOffset.frame),r.setUint8(7,s.smpteOffset.subFrame),t}if((e=>void 0!==e.sysex)(s)){const{arrayBuffer:t,dataView:a}=e(1);a.setUint8(0,240);const i=s.sysex.length/2,o=n(i),{arrayBuffer:f,dataView:c}=e(i);for(let e=0;e<s.sysex.length;e+=2)c.setUint8(e/2,parseInt(s.sysex.slice(e,e+2),16));return r([t,o,f])}if((e=>void 0!==e.timeSignature)(s)){const{arrayBuffer:t,dataView:r}=e(7);let n=s.timeSignature.denominator,a=0;for(;n>1;)n/=2,a+=1;return r.setUint8(0,255),r.setUint8(1,88),r.setUint8(2,4),r.setUint8(3,s.timeSignature.numerator),r.setUint8(4,a),r.setUint8(5,s.timeSignature.metronome),r.setUint8(6,s.timeSignature.thirtyseconds),t}if((e=>void 0!==e.metaTypeByte)(s))return t(s,parseInt(s.metaTypeByte,16),\"text\");if((e=>void 0!==e.text&&void 0===e.metaTypeByte)(s))return t(s,1,\"text\");if((e=>void 0!==e.trackName)(s))return t(s,3,\"trackName\");throw new Error('Unencodable event with a delta of \"'.concat(s.delta,'\".'))})(l,((e,t,r,n)=>(s,a,i)=>{const{arrayBuffer:o,dataView:f}=e(2);f.setUint8(0,255),f.setUint8(1,a);const c=r.encode(s[i]).buffer,u=n(c.byteLength);return t([o,u,c])})(l,m,new TextEncoder,h),m,h),y=e=>{const t=e.reduce(((e,t)=>e+t.byteLength),0),{uint8Array:r}=e.reduce((({offset:e,uint8Array:t},r)=>(t.set(new Uint8Array(r),e),{offset:e+r.byteLength,uint8Array:t})),{offset:0,uint8Array:new Uint8Array(t)});return r.buffer},w=e=>{const{arrayBuffer:t,dataView:r}=d(8),n=[t];let s=0;r.setUint32(0,1297379947);for(const t of e){const r=h(t.delta);try{const e=U(t);s+=r.byteLength+e.byteLength,n.push(r,e)}catch(r){if(r.message.match(/Unencodable\\smessage\\swith\\sa\\sdelta\\sof\\s[0-9]+\\./)){const r=e.indexOf(t);throw new Error(\"Unencodable message at index \".concat(r,\".\"))}throw r}}return r.setUint32(4,s),y(n)},p=({division:e,format:t,tracks:r})=>{const n=[];try{n.push(((e,t,r)=>{const{arrayBuffer:n,dataView:s}=d(14);return s.setUint32(0,1297377380),s.setUint32(4,6),s.setUint16(8,t),s.setUint16(10,r.length),s.setUint16(12,e),n})(e,t,r))}catch(e){throw new Error(\"The given JSON object seems to be invalid.\")}for(const e of r)try{n.push(w(e))}catch(t){if(t.message.match(/Unencodable\\sevent\\sat\\sposition\\s[0-9]+\\./)){const n=r.indexOf(e);throw new Error(\"\".concat(t.message.slice(0,-1),\" of the track at index \").concat(n,\".\"))}throw t}return y(n)};u(self,{encode:({midiFile:e})=>{const t=p(e);return{result:t,transferables:[t]}}})})()})();"; //# sourceMappingURL=worker.d.ts.map