/*!! flipnote.js v5.11.0 https://flipnote.js.org A JavaScript library for parsing, converting, and in-browser playback of the proprietary animation formats used by Nintendo's Flipnote Studio and Flipnote Studio 3D apps. 2018 - 2022 James Daniel Flipnote Studio is (c) Nintendo Co., Ltd. This project isn't affiliated with or endorsed by them in any way. Keep on Flipnoting! */ !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).flipnote={})}(this,(function(t){"use strict";var e=function(t,r){return(e=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r])})(t,r)};function r(t,r){if("function"!=typeof r&&null!==r)throw new TypeError("Class extends value "+String(r)+" is not a constructor or null");function i(){this.constructor=t}e(t,r),t.prototype=null===r?Object.create(r):(i.prototype=r.prototype,new i)}var i=function(){return(i=Object.assign||function(t){for(var e,r=1,i=arguments.length;r0&&n[n.length-1])||6!==o[0]&&2!==o[0])){s=0;continue}if(3===o[0]&&(!n||o[1]>n[0]&&o[1]=t.length&&(t=void 0),{value:t&&t[i++],done:!t}}};throw new TypeError(e?"Object is not iterable.":"Symbol.iterator is not defined.")}function a(t,e){var r="function"==typeof Symbol&&t[Symbol.iterator];if(!r)return t;var i,n,o=r.call(t),s=[];try{for(;(void 0===e||e-- >0)&&!(i=o.next()).done;)s.push(i.value)}catch(t){n={error:t}}finally{try{i&&!i.done&&(r=o.return)&&r.call(o)}finally{if(n)throw n.error}}return s}function u(){for(var t=[],e=0;e=this.allocSize;)this.newPage();t>this.realSize&&(this.realSize=t),this.pageIdx=Math.floor(t/this.pageSize),this.pagePtr=t%this.pageSize,this.realPtr=t},t.prototype.writeByte=function(t){this.pages[this.pageIdx][this.pagePtr]=t,this.setPointer(this.realPtr+1)},t.prototype.writeBytes=function(t,e,r){for(var i=r||t.length,n=e||0;n>>0&255),this.writeByte(t>>>8&255)},t.prototype.writeU32=function(t){this.writeByte(t>>>0&255),this.writeByte(t>>>8&255),this.writeByte(t>>>16&255),this.writeByte(t>>>24&255)},t.prototype.getBytes=function(){for(var t=new Uint8Array(this.realSize),e=this.numPages,r=0;rr?r:t}var y=function(t,e,r){return t+r*(e-t)};function m(t,e,r){return r<0||r>=e?0:t[r]}function g(t){for(var e=t.length,r=0,i=0;i=32767)&&(r+=1)}return r/e}function v(t){for(var e=t.length,r=0,i=0;i=e&&t<=r,(i||"value")+" "+t+" should be between "+e+" and "+r)}function S(t,e){try{return t.require(e)}catch(t){throw new Error("Could not require("+e+")")}}var F="undefined"!=typeof window&&void 0!==window.document;function E(){return b(F,"This feature is only available in browser environments")}var A="undefined"!=typeof process&&null!=process.versions&&null!=process.versions.node;function x(){return b(A,"This feature is only available in NodeJS environments")}var P="object"==typeof self&&self.constructor&&"DedicatedWorkerGlobalScope"===self.constructor.name,T=function(){if(F||P){var t=A?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{};return(t.crypto||t.msCrypto).subtle}if(A)return S(module,"crypto").webcrypto.subtle}();function C(t,e){return n(this,void 0,void 0,(function(){var r,i,n;return o(this,(function(o){switch(o.label){case 0:return r=t.split("\n").filter((function(t){return!t.startsWith("-----")&&!t.endsWith("-----")})).join(""),i=atob(r),n=new Uint8Array(i.length).map((function(t,e){return i.charCodeAt(e)})),[4,T.importKey("spki",n.buffer,{name:"RSASSA-PKCS1-v1_5",hash:e},!1,["verify"])];case 1:return[2,o.sent()]}}))}))}function k(t,e,r){return n(this,void 0,void 0,(function(){return o(this,(function(i){switch(i.label){case 0:return[4,T.verify("RSASSA-PKCS1-v1_5",t,e,r)];case 1:return[2,i.sent()]}}))}))}var B;function U(t){return new Date(1e3*(t+946684800))}function _(t,e){return 100*t*(1/e)/100}t.FlipnoteRegion=void 0,(B=t.FlipnoteRegion||(t.FlipnoteRegion={})).EUR="EUR",B.USA="USA",B.JPN="JPN",B.UNKNOWN="UNKNOWN";var L=/^[0159]{1}[0-9A-F]{6}0[0-9A-F]{8}$/,M=/^[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{6}$/,I=/^(00|10|12|14)[0-9a-f]{2}-[0-9a-f]{4}-[0-9a-f]{3}0-[0-9a-f]{4}[0159]{1}[0-9a-f]{1}$/,R=["01FACA7A4367FC5F","03D6E959E2F9A42D","03F80445160587FA","04068426E1008915","092A3EC8199FD5D5","0B8D56BA1BD441B8","0E61C75C9B5AD90B","14E494E35A443235"],z=R.map((function(t){return j(t)}));function N(t){return L.test(t)||R.includes(t)}function D(t){return M.test(t)}function O(t){var e,r;if(I.test(t))return!0;try{for(var i=s(z),n=i.next();!n.done;n=i.next()){var o=n.value;if(t.endsWith(o))return!0}}catch(t){e={error:t}}finally{try{n&&!n.done&&(r=i.return)&&r.call(i)}finally{if(e)throw e.error}}return!1}function W(e){switch(e.charAt(0)){case"0":case"1":return t.FlipnoteRegion.JPN;case"5":return t.FlipnoteRegion.USA;case"9":return t.FlipnoteRegion.EUR;default:return t.FlipnoteRegion.UNKNOWN}}function H(e){if(O(e))switch(e.charAt(19)){case"0":case"1":return t.FlipnoteRegion.JPN;case"5":return t.FlipnoteRegion.USA;case"9":return t.FlipnoteRegion.EUR;default:return t.FlipnoteRegion.UNKNOWN}switch(e.slice(0,2)){case"00":return t.FlipnoteRegion.JPN;case"02":return t.FlipnoteRegion.USA;case"04":return t.FlipnoteRegion.EUR;default:return t.FlipnoteRegion.UNKNOWN}}function K(t){return I.test(t)?(t.slice(19,21)+t.slice(17,19)+t.slice(15,17)+t.slice(12,14)+t.slice(10,12)+t.slice(7,9)+t.slice(5,7)+t.slice(2,4)).toUpperCase():null}function j(t){return L.test(t)?(t.slice(14,16)+t.slice(12,14)+"-"+t.slice(10,12)+t.slice(8,10)+"-"+t.slice(6,8)+t.slice(4,6)+"-"+t.slice(2,4)+t.slice(0,2)).toLowerCase():null}var G,V,q,Y,X,Q=Object.freeze({__proto__:null,get FlipnoteRegion(){return t.FlipnoteRegion},isPpmFsid:N,isKwzFsid:D,isKwzDsiLibraryFsid:O,isFsid:function(t){return N(t)||D(t)},getPpmFsidRegion:W,getKwzFsidRegion:H,convertKwzFsidToPpmFsid:K,convertPpmFsidToKwzFsidSuffix:j,convertPpmFsidToPossibleKwzFsids:function(t){var e=j(t);return e?["00"+e,"10"+e,"12"+e,"14"+e]:null},testKwzFsidMatchesPpmFsid:function(t,e){return K(t)==e},getFsidRegion:function(e){return N(e)?W(e):D(e)?H(e):t.FlipnoteRegion.UNKNOWN}});!function(){if(!F)return function(){};var t=document.createElement("a")}(),t.FlipnoteFormat=void 0,(G=t.FlipnoteFormat||(t.FlipnoteFormat={})).PPM="PPM",G.KWZ="KWZ",function(t){t[t.Jpeg=0]="Jpeg",t[t.Rgba=1]="Rgba"}(V||(V={})),function(t){t[t.Left=0]="Left",t[t.Right=1]="Right"}(q||(q={})),t.FlipnoteAudioTrack=void 0,(Y=t.FlipnoteAudioTrack||(t.FlipnoteAudioTrack={}))[Y.BGM=0]="BGM",Y[Y.SE1=1]="SE1",Y[Y.SE2=2]="SE2",Y[Y.SE3=3]="SE3",Y[Y.SE4=4]="SE4",t.FlipnoteSoundEffectTrack=void 0,(X=t.FlipnoteSoundEffectTrack||(t.FlipnoteSoundEffectTrack={}))[X.SE1=1]="SE1",X[X.SE2=2]="SE2",X[X.SE3=3]="SE3",X[X.SE4=4]="SE4";for(var J=function(t){function e(){var e=null!==t&&t.apply(this,arguments)||this;return e[Symbol.toStringTag]="Flipnote",e.titleFormats={COMMENT:"Comment by $USERNAME",FLIPNOTE:"Flipnote by $USERNAME",ICON:"Folder icon"},e.soundMeta=new Map,e.layerVisibility={1:!0,2:!0,3:!0},e.isFolderIcon=!1,e.isComment=!1,e.isDsiLibraryNote=!1,e}return r(e,t),e.prototype.getTitle=function(t){return void 0===t&&(t=this.titleFormats),this.isFolderIcon?t.ICON:(this.isComment?t.COMMENT:t.FLIPNOTE).replace("$USERNAME",this.meta.current.username)},e.prototype.toString=function(){return this.getTitle()},e.prototype[Symbol.iterator]=function(){var t;return o(this,(function(e){switch(e.label){case 0:t=0,e.label=1;case 1:return t0},e}(f),$=[.5,.5,1,2,4,6,12,20,30],Z={WHITE:[255,255,255,255],BLACK:[14,14,14,255],RED:[255,42,42,255],BLUE:[10,57,255,255]},tt=[4294967295,4283585106,4294967295,4288453788,4282665215,4283388360,4289506815,4278255360,4294918216,4290269009,4294945709,4278255360,4290205622,4278255360,4278255360,4278255360],et="-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCPLwTL6oSflv+gjywi/sM0TUB\n90xqOvuCpjduETjPoN2FwMebxNjdKIqHUyDu4AvrQ6BDJc6gKUbZ1E27BGZoCPH4\n9zQRb+zAM6M9EjHwQ6BABr0u2TcF7xGg2uQ9MBWz9AfbVQ91NjfrNWo0f7UPmffv\n1VvixmTk1BCtavZxBwIDAQAB\n-----END PUBLIC KEY-----",rt=function(e){function i(r,n){var o=e.call(this,r)||this;return o.format=t.FlipnoteFormat.PPM,o[Symbol.toStringTag]="Flipnote Studio PPM animation file",o.imageWidth=i.width,o.imageHeight=i.height,o.aspect=i.aspect,o.imageOffsetX=0,o.imageOffsetY=0,o.numLayers=i.numLayers,o.numLayerColors=i.numLayerColors,o.publicKey=i.publicKey,o.srcWidth=i.width,o.audioTracks=i.audioTracks,o.soundEffectTracks=i.soundEffectTracks,o.rawSampleRate=i.rawSampleRate,o.sampleRate=i.sampleRate,o.globalPalette=i.globalPalette,o.prevDecodedFrame=null,o.decodeHeader(),o.decodeAnimationHeader(),o.decodeSoundHeader(),0!=(o.version>>4&15)&&o.decodeMeta(),o.layerBuffers=[new Uint8Array(i.width*i.height),new Uint8Array(i.width*i.height)],o.prevLayerBuffers=[new Uint8Array(i.width*i.height),new Uint8Array(i.width*i.height)],o.lineEncodingBuffers=[new Uint8Array(i.height),new Uint8Array(i.height)],o.prevDecodedFrame=null,o}return r(i,e),i.prototype.decodeHeader=function(){b(16>1&1),isSpinoff:this.isSpinoff,frameCount:this.frameCount,frameSpeed:this.frameSpeed,bgmSpeed:this.bgmSpeed,duration:this.duration,thumbIndex:e,timestamp:f,root:{username:r,fsid:h,region:W(h),filename:null},parent:{username:i,fsid:o,region:W(o),filename:a},current:{username:n,fsid:s,region:W(s),filename:u}}},i.prototype.decodeAnimationHeader=function(){this.seek(1696);var t=this.readUint16(),e=t/4;b(e<=this.frameCount),this.seek(1704);for(var r=new Uint32Array(e),i=0;i>7&1},i.prototype.getThumbnailImage=function(){this.seek(160);for(var t=this.readBytes(1536),e=new Uint32Array(3072),r=0;r<48;r+=8)for(var i=0;i<64;i+=8)for(var n=0;n<8;n+=1)for(var o=0;o<8;o+=2){var s=i+o,a=r+n;e[64*a+s]=tt[15&t[0]],e[64*a+s+1]=tt[t[0]<<4&15]}return{format:V.Rgba,width:64,height:48,data:e.buffer}},i.prototype.decodeFrame=function(t){if(w(t,0,this.frameCount-1,"Frame index"),this.prevDecodedFrame===t)return this.layerBuffers;this.prevDecodedFrame===t-1||this.isKeyFrame(t)||0===t||this.decodeFrame(t-1),this.prevDecodedFrame=t,this.seek(this.frameOffsets[t]);var e=this.readUint8(),r=e>>7&1,n=e>>5&3;this.layerBuffers[0].fill(0),this.layerBuffers[1].fill(0);var o=0,s=0;n&&(o=this.readInt8(),s=this.readInt8());for(var a=0;a<2;a++){(c=this.lineEncodingBuffers[a]).fill(0);for(var u=0;u>2&3,c[u++]=h>>4&3,c[u++]=h>>6&3):u+=4}}for(a=0;a<2;a++)for(var f=this.layerBuffers[a],c=this.lineEncodingBuffers[a],l=0;l>=1)f[d+m]=1&y;break;case 2:f.fill(1,d,d+i.width);for(p=this.readUint32(!1);0!==p;p<<=1,d+=8)if(2147483648&p)for(y=this.readUint8(),m=0;m<8;m++,y>>=1)f[d+m]=1&y;break;case 3:y=0;for(var g=0;g>=1}}var v=this.layerBuffers[0],b=this.layerBuffers[1],S=this.prevLayerBuffers[0],F=this.prevLayerBuffers[1];if(r||0!==o||0!==s){if(!r){var E=i.width,A=i.height,x=Math.max(o,0),P=Math.max(s,0),T=Math.min(E+o,E),C=Math.min(A+s,A),k=s*E+o,B=void 0,U=void 0;for(l=P;l>1&3],i[e>>3&3]]},i.prototype.getFramePalette=function(t){var e=this;return w(t,0,this.frameCount-1,"Frame index"),this.getFramePaletteIndices(t).map((function(t){return e.globalPalette[t]}))},i.prototype.getIsKeyFrame=function(t){var e=1===this.isKeyFrame(t);return[e,e]},i.prototype.getFrameLayerDepths=function(t){return[0,0]},i.prototype.getFrameAuthor=function(t){return this.meta.current.fsid},i.prototype.getFrameCameraFlags=function(t){return[!1,!1]},i.prototype.getFrameLayerOrder=function(t){return[1,0]},i.prototype.decodeSoundFlags=function(){if(void 0!==this.soundFlags)return this.soundFlags;b(1696+this.frameDataLength>4,h=!h;var f=d[a],c=f>>3;1&s&&(c+=f>>2),2&s&&(c+=f>>1),4&s&&(c+=f),8&s&&(c=-c),u=p(u+=c,-32768,32767),a=p(a+=l[s],0,88),i[o++]=u}return i},i.prototype.getAudioTrackPcm=function(e,r){void 0===r&&(r=this.sampleRate);var i=this.decodeAudioTrack(e),n=this.rawSampleRate;if(e===t.FlipnoteAudioTrack.BGM){var o=1/this.bgmrate/(1/this.framerate);n=this.rawSampleRate*o}return n!==r?function(t,e,r){for(var i=t.length,n=i/e*r,o=new Int16Array(n),s=e/r,a=0;an);o++){var s=e[r+o]+t[o]/2;e[r+o]=p(s,-32768,32767)}},i.prototype.getAudioMasterPcm=function(e){void 0===e&&(e=this.sampleRate);var r=Math.ceil(this.duration*e),i=new Int16Array(r),n=this.hasAudioTrack(t.FlipnoteAudioTrack.BGM),o=this.hasAudioTrack(t.FlipnoteAudioTrack.SE1),s=this.hasAudioTrack(t.FlipnoteAudioTrack.SE2),a=this.hasAudioTrack(t.FlipnoteAudioTrack.SE3);if(n){var u=this.getAudioTrackPcm(t.FlipnoteAudioTrack.BGM,e);this.pcmAudioMix(u,i,0)}if(o||s||a)for(var h=e/this.framerate,f=o?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE1,e):null,c=s?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE2,e):null,l=a?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE3,e):null,d=this.decodeSoundFlags(),p=0;p16){var e=this.readUint16();this.bitValue|=e<<16-this.bitIndex,this.bitIndex-=16}var r=this.bitValue&st[t];return this.bitValue>>=t,this.bitIndex+=t,r},s.prototype.readFsid=function(){if(this.settings.dsiLibraryNote)return this.readHex(10,!0).slice(2,18);var t=this.readHex(10);return(t.slice(0,4)+"-"+t.slice(4,8)+"-"+t.slice(8,12)+"-"+t.slice(12,18)).toLowerCase()},s.prototype.readFilename=function(){var t=this.pointer,e=this.readChars(28);if(28===e.length)return e;this.seek(t);var r=this.readHex(3),i=this.readChars(13),n=this.readUint16().toString().padStart(3,"0");return this.seek(t+28),r+"_"+i+"_"+n},s.prototype.decodeMeta=function(){if(this.settings.quickMeta)return this.decodeMetaQuick();b(this.sectionMap.has("KFH")),this.seek(this.sectionMap.get("KFH").ptr+12);var t=U(this.readUint32()),e=U(this.readUint32());this.readUint32();var r=this.readFsid(),i=this.readFsid(),n=this.readFsid(),o=this.readWideChars(11),s=this.readWideChars(11),a=this.readWideChars(11),u=this.readFilename(),h=this.readFilename(),f=this.readFilename(),c=this.readUint16(),l=this.readUint16(),d=this.readUint16(),p=this.readUint8(),y=this.readUint8();this.isSpinoff=n!==i||n!==r,this.frameCount=c,this.frameSpeed=p,this.framerate=it[p],this.duration=_(this.frameCount,this.framerate),this.thumbFrameIndex=l,this.layerVisibility={1:0==(1&y),2:0==(2&y),3:0==(3&y)},this.meta={lock:0!=(1&d),loop:0!=(2&d),isSpinoff:this.isSpinoff,frameCount:c,frameSpeed:p,duration:this.duration,thumbIndex:l,timestamp:e,creationTimestamp:t,root:{username:o,fsid:r,region:H(r),filename:u,isDsiFilename:28!==u.length},parent:{username:s,fsid:i,region:H(i),filename:h,isDsiFilename:28!==h.length},current:{username:a,fsid:n,region:H(n),filename:f,isDsiFilename:28!==f.length}}},s.prototype.decodeMetaQuick=function(){b(this.sectionMap.has("KFH")),this.seek(this.sectionMap.get("KFH").ptr+8+196);var t=this.readUint16(),e=this.readUint16();this.readUint16();var r=this.readUint8(),i=this.readUint8();this.frameCount=t,this.thumbFrameIndex=e,this.frameSpeed=r,this.framerate=it[r],this.duration=_(this.frameCount,this.framerate),this.layerVisibility={1:0==(1&i),2:0==(2&i),3:0==(3&i)}},s.prototype.getFrameOffsets=function(){b(this.sectionMap.has("KMI")&&this.sectionMap.has("KMC"));var t=this.frameCount,e=this.sectionMap.get("KMI"),r=this.sectionMap.get("KMC");b(e.length/28>=t);for(var i=new Uint32Array(t),n=new Uint32Array(t),o=[],s=e.ptr+8,a=r.ptr+12,u=0;u>8&15,e>>12&15,e>>16&15,e>>20&15,e>>24&15,e>>28&15]},s.prototype.getFramePalette=function(t){var e=this;return w(t,0,this.frameCount-1,"Frame index"),this.getFramePaletteIndices(t).map((function(t){return e.globalPalette[t]}))},s.prototype.getFrameDiffingFlag=function(t){return w(t,0,this.frameCount-1,"Frame index"),this.seek(this.frameMetaOffsets[t]),this.readUint32()>>4&7},s.prototype.getIsKeyFrame=function(t){var e=this.getFrameDiffingFlag(t);return[0==(1&e),0==(2&e),0==(4&e)]},s.prototype.getFrameLayerDepths=function(t){return w(t,0,this.frameCount-1,"Frame index"),this.seek(this.frameMetaOffsets[t]+20),[this.readUint8(),this.readUint8(),this.readUint8()]},s.prototype.getFrameAuthor=function(t){return w(t,0,this.frameCount-1,"Frame index"),this.seek(this.frameMetaOffsets[t]+10),this.readFsid()},s.prototype.getFrameCameraFlags=function(t){this.seek(this.frameMetaOffsets[t]+26);var e=this.readUint8();return[0!=(1&e),0!=(2&e),0!=(4&e)]},s.prototype.getFrameLayerOrder=function(t){w(t,0,this.frameCount-1,"Frame index");var e=this.getFrameLayerDepths(t);return[2,1,0].sort((function(t,r){return e[r]-e[t]}))},s.prototype.decodeFrame=function(t,e,r){if(void 0===e&&(e=7),void 0===r&&(r=!1),w(t,0,this.frameCount-1,"Frame index"),this.prevDecodedFrame===t)return this.layerBuffers;this.prevDecodedFrame!==t-1&&0!==t&&(r&&(e&=~this.getFrameDiffingFlag(t+1)),0!==e&&this.decodeFrame(t-1,e,!0));for(var i=this.frameDataOffsets[t],n=this.frameLayerSizes[t],o=0;o<3&&(!this.settings.dsiLibraryNote||3!==o);o++){this.seek(i);var a=n[o];i+=a;var u=this.layerBuffers[o];if(38!==a&&0!=(e>>o&1)){this.bitIndex=16,this.bitValue=0;for(var h=0,f=0;f<240;f+=128)for(var c=0;c<320;c+=128)for(var l=0;l<128;l+=8){var d=f+l;if(d>=240)break;for(var p=0;p<128;p+=8){var y=c+p;if(y>=320)break;if(h>0)h-=1;else{var m=d*s.width+y,g=this.readBits(3);if(0===g){var v=8*this.readBits(5),b=bt.subarray(v,v+8);u.set(b,m),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320)}else if(1===g){v=8*this.readBits(13),b=ut.subarray(v,v+8);u.set(b,m),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320),u.set(b,m+=320)}else if(2===g){v=8*this.readBits(5);var S=bt.subarray(v,v+8),F=wt.subarray(v,v+8);u.set(S,m),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320)}else if(3===g){v=8*this.readBits(13),S=ut.subarray(v,v+8),F=ht.subarray(v,v+8);u.set(S,m),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320)}else if(4===g)for(var E=this.readBits(8),A=1;A<255;A<<=1){if(E&A){v=8*this.readBits(5),b=bt.subarray(v,v+8);u.set(b,m)}else{v=8*this.readBits(13),b=ut.subarray(v,v+8);u.set(b,m)}m+=320}else{if(5===g){h=this.readBits(5);continue}if(7===g){var x=this.readBits(2);S=void 0,F=void 0;if(0!==this.readBits(1)){var P=8*this.readBits(5),T=8*this.readBits(5);S=bt.subarray(P,P+8),F=bt.subarray(T,T+8),x+=1}else{P=8*this.readBits(13),T=8*this.readBits(13);S=ut.subarray(P,P+8),F=ut.subarray(T,T+8)}switch(x%4){case 0:u.set(S,m),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320);break;case 1:u.set(S,m),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(S,m+=320);break;case 2:u.set(S,m),u.set(F,m+=320),u.set(S,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(S,m+=320),u.set(F,m+=320);break;case 3:u.set(S,m),u.set(F,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320),u.set(F,m+=320),u.set(S,m+=320),u.set(F,m+=320)}}}}}}}}return this.prevDecodedFrame=t,this.layerBuffers},s.prototype.decodeFrameSoundFlags=function(t){w(t,0,this.frameCount-1,"Frame index"),this.seek(this.frameMetaOffsets[t]+23);var e=this.readUint8();return[0!=(1&e),0!=(2&e),0!=(4&e),0!=(8&e)]},s.prototype.decodeSoundFlags=function(){var t=this;return void 0!==this.soundFlags||(this.soundFlags=new Array(this.frameCount).fill(!1).map((function(e,r){return t.decodeFrameSoundFlags(r)}))),this.soundFlags},s.prototype.getSoundEffectFlags=function(){return this.decodeSoundFlags().map((function(e){var r;return(r={})[t.FlipnoteSoundEffectTrack.SE1]=e[0],r[t.FlipnoteSoundEffectTrack.SE2]=e[1],r[t.FlipnoteSoundEffectTrack.SE3]=e[2],r[t.FlipnoteSoundEffectTrack.SE4]=e[3],r}))},s.prototype.getFrameSoundEffectFlags=function(e){var r,i=this.decodeFrameSoundFlags(e);return(r={})[t.FlipnoteSoundEffectTrack.SE1]=i[0],r[t.FlipnoteSoundEffectTrack.SE2]=i[1],r[t.FlipnoteSoundEffectTrack.SE3]=i[2],r[t.FlipnoteSoundEffectTrack.SE4]=i[3],r},s.prototype.getAudioTrackRaw=function(t){var e=this.soundMeta.get(t);return b(e.ptr+e.length4?(s=3&f,u=(a=d[i])>>3,1&s&&(u+=a),2&s&&(u=-u),r+=u,i+=c[s],f>>=2,y+=2):(s=15&f,u=(a=d[i])>>3,1&s&&(u+=a>>2),2&s&&(u+=a>>1),4&s&&(u+=a),8&s&&(u=-u),r+=u,i+=l[s],f>>=4,y+=4),i=p(i,0,79),r=p(r,-2048,2047),e[o]=16*r,o+=1;return o},s.prototype.decodeAudioTrack=function(e){var r=this.settings,i=this.getAudioTrackRaw(e),n=60*this.rawSampleRate,o=new Int16Array(n),s=0,a=40;if(this.isDsiLibraryNote)if(e===t.FlipnoteAudioTrack.BGM){var u=!0;if(null!==r.initialBgmPredictor&&(s=r.initialBgmPredictor,u=!1),null!==r.initialBgmStepIndex&&(a=r.initialBgmStepIndex,u=!1),u&&r.guessInitialBgmState){var h=4294967295,f=0;for(a=0;a<=40;a++){var c=this.decodeAdpcm(i,o,s,a),l=v(o.subarray(0,c));ln);o++){var s=e[r+o]+t[o];e[r+o]=p(s,-32768,32767)}},s.prototype.getAudioMasterPcm=function(e){void 0===e&&(e=this.sampleRate);var r=Math.ceil(this.duration*e),i=new Int16Array(r),n=this.hasAudioTrack(t.FlipnoteAudioTrack.BGM),o=this.hasAudioTrack(t.FlipnoteAudioTrack.SE1),s=this.hasAudioTrack(t.FlipnoteAudioTrack.SE2),a=this.hasAudioTrack(t.FlipnoteAudioTrack.SE3),u=this.hasAudioTrack(t.FlipnoteAudioTrack.SE4);if(n){var h=this.getAudioTrackPcm(t.FlipnoteAudioTrack.BGM,e);this.pcmAudioMix(h,i,0)}if(o||s||a||u)for(var f=e/this.framerate,c=o?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE1,e):null,l=s?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE2,e):null,d=a?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE3,e):null,p=u?this.getAudioTrackPcm(t.FlipnoteAudioTrack.SE4,e):null,y=this.decodeSoundFlags(),m=0;m=200&&i.status<300?e(i.response):r({type:"httpError",status:i.status,statusText:i.statusText}))},i.send(null)}},{matches:function(t){return A&&"string"==typeof t},load:function(t,e,r){x(),S(module,"https").get(t,(function(t){var i=[];t.on("data",(function(t){return i.push(t)})),t.on("end",(function(){var t=Buffer.concat(i);e(t.buffer)})),t.on("error",(function(t){return r(t)}))}))}},{matches:function(t){return F&&"undefined"!=typeof File&&"undefined"!=typeof FileReader&&t instanceof File},load:function(t,e,r){var i=new FileReader;i.onload=function(t){e(i.result)},i.onerror=function(t){r({type:"fileReadError"})},i.readAsArrayBuffer(t)}},{matches:function(t){return F&&"undefined"!=typeof Blob&&"undefined"!=typeof Response&&t instanceof Blob},load:function(t,e,r){new Response(t).arrayBuffer().then(e).catch(r)}},{matches:function(t){return A&&t instanceof Buffer},load:function(t,e,r){e(t.buffer)}},{matches:function(t){return t instanceof ArrayBuffer},load:function(t,e,r){e(t)}}];function Et(t,e){return void 0===e&&(e=Ft),new Promise((function(r,i){for(var n=0;n0)throw new Error(`Can not guess numComponents for attribute '${t}'. Tried ${r} but ${e} values is not evenly divisible by ${r}. You should specify it.`);return r}function Ot(t,e){if(_t(t))return t;if(_t(t.data))return t.data;Array.isArray(t)&&(t={data:t});let r=t.type;return r||(r=Rt(e)?Uint16Array:Float32Array),new r(t.data)}function Wt(t,e){const r={};return Object.keys(e).forEach((function(i){if(!Rt(i)){const o=e[i],s=o.attrib||o.name||o.attribName||Mt+i;if(o.value){if(!Array.isArray(o.value)&&!_t(o.value))throw new Error("array.value is not array or typedarray");r[s]={value:o.value}}else{let e,a,u,h;if(o.buffer&&o.buffer instanceof WebGLBuffer)e=o.buffer,h=o.numComponents||o.size,a=o.type,u=o.normalize;else if("number"==typeof o||"number"==typeof o.data){const r=o.data||o,s=o.type||Float32Array,f=r*s.BYTES_PER_ELEMENT;a=function(t){if(t===Int8Array)return 5120;if(t===Uint8Array)return 5121;if(t===Uint8ClampedArray)return 5121;if(t===Int16Array)return 5122;if(t===Uint16Array)return 5123;if(t===Int32Array)return 5124;if(t===Uint32Array)return 5125;if(t===Float32Array)return 5126;throw new Error("unsupported typed array type")}(s),u=void 0!==o.normalize?o.normalize:(n=s)===Int8Array||n===Uint8Array,h=o.numComponents||o.size||Dt(i,r),e=t.createBuffer(),t.bindBuffer(34962,e),t.bufferData(34962,f,o.drawType||35044)}else{const r=Ot(o,i);e=It(t,r,void 0,o.drawType),a=Ut(r),u=void 0!==o.normalize?o.normalize:function(t){return t instanceof Int8Array||t instanceof Uint8Array}(r),h=function(t,e){return t.numComponents||t.size||Dt(e,function(t){return t.length?t:t.data}(t).length)}(o,i)}r[s]={buffer:e,numComponents:h,type:a,normalize:u,stride:o.stride||0,offset:o.offset||0,divisor:void 0===o.divisor?void 0:o.divisor,drawType:o.drawType}}}var n})),t.bindBuffer(34962,null),r}const Ht=["position","positions","a_position"];function Kt(t,e,r){const i=Wt(t,e),n=Object.assign({},r||{});n.attribs=Object.assign({},r?r.attribs:{},i);const o=e.indices;if(o){const e=Ot(o,"indices");n.indices=It(t,e,34963),n.numElements=e.length,n.elementType=Ut(e)}else n.numElements||(n.numElements=function(t,e){let r,i;for(i=0;i""!==t);let o=0,s="";for(;;){const t=n[o++];s+=t;const u=(a=t[0])>="0"&&a<="9",h=u?parseInt(t):t;u&&(s+=n[o++]);if(o===n.length){r[h]=e;break}{const t=n[o++],e="["===t,a=r[h]||(e?[]:{});r[h]=a,r=a,i[s]=i[s]||function(t){return function(e){ue(t,e)}}(a),s+=t}}var a}function ue(t,e){for(const r in e){const i=t[r];"function"==typeof i?i(e[r]):ue(t[r],e[r])}}function he(t,...e){const r=t.uniformSetters||t,i=e.length;for(let t=0;t=this.rendererStack.length)throw new Error("No renderer to fall back to");this.rendererStackIdx+=1,this.setSubRenderer(this.rendererStack[this.rendererStackIdx])},t.prototype.switchToHtml5=function(){this.setSubRenderer(le)},t.prototype.setCanvasSize=function(t,e){var r=this.renderer;r.setCanvasSize(t,e),this.width=t,this.width=e,this.dstWidth=r.dstWidth,this.dstHeight=r.dstHeight},t.prototype.setNote=function(t){this.note=t,this.renderer.setNote(t),this.frameIndex=void 0,this.srcWidth=this.renderer.srcWidth,this.srcHeight=this.renderer.srcHeight},t.prototype.clear=function(t){this.renderer.clear(t)},t.prototype.drawFrame=function(t){this.renderer.drawFrame(t),this.frameIndex=t},t.prototype.forceUpdate=function(){this.renderer.forceUpdate()},t.prototype.requestStereoScopeMode=function(t){this.renderer.requestStereoScopeMode(t),this.stereoscopeMode=this.renderer.stereoscopeMode},t.prototype.getDataUrl=function(t,e){return this.renderer.getDataUrl()},t.prototype.getBlob=function(t,e){return n(this,void 0,void 0,(function(){return o(this,(function(t){return[2,this.renderer.getBlob()]}))}))},t.prototype.destroy=function(){this.renderer.destroy(),this.note=null},t}(),pe=F?window.AudioContext||window.webkitAudioContext:null,ye=function(){function t(){this.useEq=!1,this.useAnalyser=!1,this.eqSettings=[[31.25,4.1],[62.5,1.2],[125,0],[250,-4.1],[500,-2.3],[1e3,.5],[2e3,6.5],[8e3,5.1],[16e3,5.1]],this._volume=1,this._loop=!1,this._startTime=0,this._ctxStartTime=0,this.nodeRefs=[],E()}return Object.defineProperty(t.prototype,"volume",{get:function(){return this._volume},set:function(t){this.setVolume(t)},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"loop",{get:function(){return this._loop},set:function(t){this._loop=t,this.source&&(this.source.loop=t)},enumerable:!1,configurable:!0}),t.prototype.getCtx=function(){return this.ctx||(this.ctx=new pe),this.ctx},t.prototype.setBuffer=function(t,e){var r=this.getCtx(),i=t.length,n=r.createBuffer(1,i,e),o=n.getChannelData(0);if(t instanceof Float32Array)o.set(t,0);else if(t instanceof Int16Array)for(var s=0;s.01&&(s=n),s>=i){if(!o.loop)return o.pause(),o._hasEnded=!0,void o.emit(t.PlayerEvent.Ended);o.playbackStartTime=r,o.emit(t.PlayerEvent.Loop)}o.setCurrentTime(s%i),o.playbackLoopId=requestAnimationFrame(o.playbackLoop)}},E();var s="string"==typeof e?document.querySelector(e):e;this.parserSettings=n,this.renderer=new de(s,r,i,{onlost:function(){return o.emit(t.PlayerEvent.Error)},onrestored:function(){return o.reload()}}),this.audio=new ye,this.el=s}return Object.defineProperty(e.prototype,"src",{get:function(){return this._src},set:function(t){throw new Error("Setting a Player source has been deprecated, please use the load() method instead")},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"paused",{get:function(){return!this.isPlaying},set:function(t){t?this.pause():this.play()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"currentFrame",{get:function(){return this._frame},set:function(t){this.setCurrentFrame(t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"currentTime",{get:function(){return this.isNoteLoaded?this.playbackTime:null},set:function(t){this.setCurrentTime(t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"progress",{get:function(){return this.isNoteLoaded?this.playbackTime/this.duration*100:null},set:function(t){this.setProgress(t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"volume",{get:function(){return this.getVolume()},set:function(t){this.setVolume(t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"muted",{get:function(){return this.getMuted()},set:function(t){this.setMuted(t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"loop",{get:function(){return this.getLoop()},set:function(t){this.setLoop(t)},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"framerate",{get:function(){return this.note.framerate},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"frameCount",{get:function(){return this.note.frameCount},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"frameSpeed",{get:function(){return this.note.frameSpeed},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"buffered",{get:function(){return Tt([[0,this.duration]])},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"seekable",{get:function(){return Tt([[0,this.duration]])},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"currentSrc",{get:function(){return this._src},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"videoWidth",{get:function(){return this.isNoteLoaded?this.note.imageWidth:0},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"videoHeight",{get:function(){return this.isNoteLoaded?this.note.imageHeight:0},enumerable:!1,configurable:!0}),e.prototype.load=function(e,r,i){return n(this,void 0,void 0,(function(){var s,u,h,f=this;return o(this,(function(c){switch(c.label){case 0:return this.isNoteLoaded&&this.closeNote(),this._src=e,e?(this.emit(t.PlayerEvent.LoadStart),[4,(l=function(){return r(e,f.parserSettings,i)},n(void 0,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),[4,l().catch((function(t){throw t}))];case 1:return[2,[null,t.sent()]];case 2:return[2,[t.sent(),null]];case 3:return[2]}}))})))]):[2,this.openNote(this.note)];case 1:if(s=a.apply(void 0,[c.sent(),2]),u=s[0],h=s[1],u)throw this.emit(t.PlayerEvent.Error,u),new Error("Error loading Flipnote: "+u.message);return this.lastParser=r,this.lastLoaders=i,this.openNote(h),[2]}var l}))}))},e.prototype.reload=function(){return n(this,void 0,void 0,(function(){return o(this,(function(t){switch(t.label){case 0:return this.note&&this.lastParser?[4,this.load(this.note.buffer,this.lastParser,this.lastLoaders)]:[3,2];case 1:return[2,t.sent()];case 2:return[2]}}))}))},e.prototype.updateSettings=function(t){return n(this,void 0,void 0,(function(){return o(this,(function(e){switch(e.label){case 0:return this.parserSettings=t,[4,this.reload()];case 1:return[2,e.sent()]}}))}))},e.prototype.closeNote=function(){this.pause(),this.note=null,this.isNoteLoaded=!1,this.meta=null,this._src=null,this._frame=0,this.playbackTime=0,this.duration=0,this.loop=!1,this.isPlaying=!1,this.wasPlaying=!1,this.hasPlaybackStarted=!1,this.showThumbnail=!0,this.renderer.clear()},e.prototype.openNote=function(e){this.isNoteLoaded&&this.closeNote(),this.note=e,this.meta=e.meta,this.emit(t.PlayerEvent.LoadedMeta),this.noteFormat=e.format,this.duration=e.duration,this.playbackTime=0,this._frame=0,this.isNoteLoaded=!0,this.isPlaying=!1,this.wasPlaying=!1,this.hasPlaybackStarted=!1,this.layerVisibility=e.layerVisibility,this.showThumbnail=!0,this.audio.setBuffer(e.getAudioMasterPcm(),e.sampleRate),this.emit(t.PlayerEvent.CanPlay),this.emit(t.PlayerEvent.CanPlayThrough),this.setLoop(e.meta.loop),this.renderer.setNote(e),this.drawFrame(e.thumbFrameIndex),this.emit(t.PlayerEvent.LoadedData),this.emit(t.PlayerEvent.Load),this.emit(t.PlayerEvent.Ready),this.autoplay&&this.play()},e.prototype.setCurrentTime=function(e){this.assertNoteLoaded();var r=Math.floor(e/(1/this.framerate));this.setCurrentFrame(r),this.playbackTime=e,this.emit(t.PlayerEvent.Progress,this.progress)},e.prototype.getCurrentTime=function(){return this.currentTime},e.prototype.getTimeCounter=function(){return kt(Math.max(this.currentTime,0))+" / "+kt(this.duration)},e.prototype.getFrameCounter=function(){return Ct(this.currentFrame+1,3)+" / "+Ct(this.frameCount,3)},e.prototype.setProgress=function(t){this.assertNoteLoaded(),w(t,0,100,"progress"),this.currentTime=this.duration*(t/100)},e.prototype.getProgress=function(){return this.progress},e.prototype.play=function(){return n(this,void 0,void 0,(function(){var e;return o(this,(function(r){return this.assertNoteLoaded(),this.isPlaying||(this._hasEnded&&(this.playbackTime=0,this._hasEnded=!1),e=performance.now(),this.playbackStartTime=e/1e3-this.playbackTime,this.isPlaying=!0,this.hasPlaybackStarted=!0,this.playAudio(),this.playbackLoop(e),this.emit(t.PlayerEvent.Play)),[2]}))}))},e.prototype.pause=function(){this.isPlaying&&(this.isPlaying=!1,null!==this.playbackLoopId&&cancelAnimationFrame(this.playbackLoopId),this.stopAudio(),this.emit(t.PlayerEvent.Pause))},e.prototype.togglePlay=function(){this.isPlaying?this.pause():this.play()},e.prototype.getPaused=function(){return!this.isPlaying},e.prototype.getDuration=function(){return this.duration},e.prototype.getLoop=function(){return this._loop},e.prototype.setLoop=function(t){this._loop=t,this.audio.loop=t},e.prototype.toggleLoop=function(){this.setLoop(!this._loop)},e.prototype.setCurrentFrame=function(e){this.assertNoteLoaded();var r=Math.max(0,Math.min(Math.floor(e),this.frameCount-1));(r!==this.currentFrame||this.showThumbnail)&&(this._frame=r,this.drawFrame(r),this.showThumbnail=!1,this.isPlaying||(this.playbackTime=r*(1/this.framerate),this.emit(t.PlayerEvent.SeekEnd)),this.emit(t.PlayerEvent.FrameUpdate,this.currentFrame),this.emit(t.PlayerEvent.Progress,this.progress),this.emit(t.PlayerEvent.TimeUpdate,this.currentFrame))},e.prototype.nextFrame=function(){this.loop&&this.currentFrame===this.frameCount-1?this.currentFrame=0:this.currentFrame+=1,this.emit(t.PlayerEvent.FrameNext)},e.prototype.prevFrame=function(){this.loop&&0===this.currentFrame?this.currentFrame=this.frameCount-1:this.currentFrame-=1,this.emit(t.PlayerEvent.FramePrev)},e.prototype.lastFrame=function(){this.currentFrame=this.frameCount-1,this.emit(t.PlayerEvent.FrameLast)},e.prototype.firstFrame=function(){this.currentFrame=0,this.emit(t.PlayerEvent.FrameFirst)},e.prototype.thumbnailFrame=function(){this.currentFrame=this.note.thumbFrameIndex},e.prototype.startSeek=function(){this.isSeeking||(this.emit(t.PlayerEvent.SeekStart),this.wasPlaying=this.isPlaying,this.pause(),this.isSeeking=!0)},e.prototype.seek=function(t){this.isSeeking&&(this.progress=100*t)},e.prototype.endSeek=function(){this.isSeeking&&!0===this.wasPlaying&&this.play(),this.wasPlaying=!1,this.isSeeking=!1},e.prototype.drawFrame=function(t){this.renderer.drawFrame(t)},e.prototype.forceUpdate=function(){this.renderer.forceUpdate()},e.prototype.resize=function(t,e){e!==.75*t&&console.warn("Canvas width to height ratio should be 3:4 for best results (got "+t+"x"+e+")"),this.renderer.setCanvasSize(t,e),this.forceUpdate()},e.prototype.setLayerVisibility=function(t,e){this.note.layerVisibility[t]=e,this.layerVisibility[t]=e,this.forceUpdate()},e.prototype.getLayerVisibility=function(t){return this.layerVisibility[t]},e.prototype.toggleLayerVisibility=function(t){this.setLayerVisibility(t,!this.layerVisibility[t])},e.prototype.playAudio=function(){this.audio.playFrom(this.currentTime)},e.prototype.stopAudio=function(){this.audio.stop()},e.prototype.toggleAudioEq=function(){this.setAudioEq(!this.audio.useEq)},e.prototype.setAudioEq=function(t){this.isPlaying&&(this.wasPlaying=!0,this.stopAudio()),this.audio.useEq=t,this.wasPlaying&&(this.wasPlaying=!1,this.playAudio())},e.prototype.mute=function(){this.setMuted(!0)},e.prototype.unmute=function(){this.setMuted(!1)},e.prototype.setMuted=function(e){this.audio.volume=e?0:this._volume,this._muted=e,this.emit(t.PlayerEvent.VolumeChange,this.audio.volume)},e.prototype.getMuted=function(){return 0===this.volume||this._muted},e.prototype.toggleMuted=function(){this.setMuted(!this._muted)},e.prototype.setVolume=function(e){w(e,0,1,"volume"),this._volume=e,this.audio.volume=e,this.emit(t.PlayerEvent.VolumeChange,this.audio.volume)},e.prototype.getVolume=function(){return this._muted?0:this._volume},e.prototype.seekToNextFrame=function(){this.nextFrame()},e.prototype.fastSeek=function(t){this.currentTime=t},e.prototype.canPlayType=function(t){switch(t){case"application/x-ppm":case"application/x-kwz":case"video/x-ppm":case"video/x-kwz":case"video/vnd.nintendo.ugomemo.ppm":case"video/vnd.nintendo.ugomemo.kwz":return"probably";case"application/octet-stream":return"maybe";case"video/vnd.nintendo.ugomemo.fykt":default:return""}},e.prototype.getVideoPlaybackQuality=function(){return{creationTime:0,droppedVideoFrames:0,corruptedVideoFrames:0,totalVideoFrames:this.frameCount}},e.prototype.requestPictureInPicture=function(){throw new Error("Not implemented")},e.prototype.captureStream=function(){throw new Error("Not implemented")},e.prototype.on=function(t,e){var r=this.events;(Array.isArray(t)?t:[t]).forEach((function(t){r.has(t)?r.get(t).push(e):r.set(t,[e])}))},e.prototype.off=function(t,e){var r=this.events;(Array.isArray(t)?t:[t]).forEach((function(t){if(r.has(t)){var i=r.get(t);r.set(t,i.splice(i.indexOf(e),1))}}))},e.prototype.emit=function(e){for(var r=[],i=1;i=254&&this.flush_char(e)},t.prototype.cl_block=function(t){this.cl_hash(5003),this.free_ent=this.ClearCode+2,this.clear_flg=!0,this.output(this.ClearCode,t)},t.prototype.cl_hash=function(t){for(var e=0;e=0){s=5003-n,0===n&&(s=1);do{if((n-=s)<0&&(n+=5003),this.htab[n]===r){o=this.codetab[n];continue t}}while(this.htab[n]>=0)}this.output(o,e),o=i,this.free_ent<4096?(this.codetab[n]=this.free_ent++,this.htab[n]=r):this.cl_block(e)}else o=this.codetab[n];this.output(o,e),this.output(this.EOFCode,e)},t.prototype.encode=function(t,e){this.pixels=t,e.writeByte(this.initCodeSize),this.remaining=this.width*this.height,this.curPixel=0,this.compress(this.initCodeSize+1,e),e.writeByte(0)},t.prototype.flush_char=function(t){this.a_count>0&&(t.writeByte(this.a_count),t.writeBytes(this.accum,0,this.a_count),this.a_count=0)},t.prototype.get_maxcode=function(t){return(1<0?this.cur_accum|=t<=8;)this.char_out(255&this.cur_accum,e),this.cur_accum>>=8,this.cur_bits-=8;if((this.free_ent>this.maxcode||this.clear_flg)&&(this.clear_flg?(this.maxcode=this.get_maxcode(this.n_bits=this.g_init_bits),this.clear_flg=!1):(++this.n_bits,12==this.n_bits?this.maxcode=4096:this.maxcode=this.get_maxcode(this.n_bits))),t==this.EOFCode){for(;this.cur_bits>0;)this.char_out(255&this.cur_accum,e),this.cur_accum>>=8,this.cur_bits-=8;this.flush_char(e)}},t}(),we=function(t){function e(r,n,o){void 0===o&&(o={});var s=t.call(this)||this;return s.mimeType="gif/image",s.frameCount=0,s.width=r,s.height=n,s.data=new h,s.settings=i(i({},e.defaultSettings),o),s.compressor=new be(r,n,o.colorDepth),s}return r(e,t),e.fromFlipnote=function(t,r){var n;void 0===r&&(r={});var o=new e(t.imageWidth,t.imageHeight,i({delay:100/t.framerate,repeat:(null===(n=t.meta)||void 0===n?void 0:n.loop)?-1:0},r));o.palette=t.globalPalette;for(var s=0;s