diff --git a/CHANGELOG.md b/CHANGELOG.md index a01f91c..557ab93 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # CHANGELOG -## version 8.4.6 (unreleased) +## version 8.4.6 (2019-09-12) - Fix: clicks and time changes in ADPCM compression/decompression - Fix: Range error bug when converting some files to ADPCM +- Fix: The length of the output array when converting to ADPCM - Fix: properly reset chunks in WaveFile objects when using the same object to read multiple files or performing conversions. - Fix: keep metadata when performing bit depth conversions or applying compression diff --git a/dist/wavefile.cjs.js b/dist/wavefile.cjs.js index c6e8131..8925733 100644 --- a/dist/wavefile.cjs.js +++ b/dist/wavefile.cjs.js @@ -334,19 +334,22 @@ function encode$1(samples) { let block = []; /** @type {number} */ let fileIndex = 0; + /** @type {number} */ + let blockCount = 0; for (let i=0; il&&(l=Math.max(l+n,0));lf||56319k||57343l&&(l=Math.max(l+n,0));lg||56319k||57343a||53a?1:Math.ceil(a/8);this.max=Math.pow(2,a)-1;this.min=0;a=8-((a-1|7)+ -1-a);this.j=Math.pow(2,0>4;g[d++]=(f&15)<<4|k>>2;g[d++]= +1-a);this.j=Math.pow(2,0>4;f[d++]=(g&15)<<4|k>>2;f[d++]= (k&3)<<6|l&63}return e}function ha(a,b){return a=0parseInt(a,10)||"53">4;h.push(X(l<<4^k));h.push(X(l))}d=h;c.set(d,e);e+=d.length;d=[]}d.push(a[g])}return c}function na(a){var b=a[0];P(b);var c= -[];c.push(b&255);c.push(b>>8&255);c.push(A);c.push(0);for(b=3;b>3;b>c&&(a|=4,b-=c,d+=c);c>>=1;b>c&&(a|=2,b-=c,d+=c);c>>=1;b>c&&(a|=1,d+=c);b=a;x=b&8?x-d:x+d;-32768>x?x=-32768:32767A?A=0:88>1);a&1&&(b+=B>>2);b+=B>>3;a&8&&(b=-b);v+=b;32767v&&(v=-32767);w+=Y[a];0>w?w=0: -88>8&128;e||(d*=-1);32635>8&127];d=g<<4|d>>g+3&15}else d>>=4;b[c]=d^e^85}return b}function ra(a){for(var b=new Int16Array(a.length),c=0;c>4)+4;d=4!=g?1<>8&128;0!=e&&(d=-d);32635>7&255];b[c]=~(e|g<<4|d>>g+3&15)}return b}function ua(a){for(var b=new Int16Array(a.length),c=0;c>4&7;e=va[e]+((d&15)<g)b[c]=g,c++;else{var h=0,f=0;2047>=g?(h=1,f=192):65535>=g?(h=2,f=224):1114111>=g&&(h=3,f=240,d++);b[c]=(g>>6*h)+f;for(c++;0>6*(h-1)&63,c++,h--}}return c}function Z(a,b,c,d){if(d||Infinity!==b&&-Infinity!==b&&b===b)throw Error(a.message+" at input index "+c+": "+b);throw Error("Argument is not a integer at input index "+c);}function H(a,b,c){c=void 0===c?a.length:c;var d=void 0===b?0:b;c=void 0===c? -a.length:c;b="";for(d=void 0===d?0:d;d=f)b+=String.fromCharCode(f);else{var k=0;194<=f&&223>=f?k=1:224<=f&&239>=f?(k=2,224===a[d]&&(e=160),237===a[d]&&(g=159)):240<=f&&244>=f?(k=3,240===a[d]&&(e=144),244===a[d]&&(g=143)):h=!0;f&=(1<<8-k-1)-1;for(var l=0;lg)h=!0;f=f<<6|a[d]&63;d++}h?b+=String.fromCharCode(65533):65535>=f?b+=String.fromCharCode(f):(f-=65536,b+=String.fromCharCode((f>>10&1023)+55296,(f&1023)+56320))}}return b}function q(a){var b= -[];R(a,b,0);return b}function I(a,b,c,d){d=void 0===d?0:d;b=b||{};var e=new C(b.h,b.A,b.F),g=0,h=d;try{for(var f=a.length;g>4;h.push(X(l<<4^k));h.push(X(l))}d=h;c.set(d,e);e+=d.length;d=[]}d.push(a[f])}return c}function na(a){var b= +a[0];P(b);var c=[];c.push(b&255);c.push(b>>8&255);c.push(A);c.push(0);for(b=3;b>3;b>c&&(a|=4,b-=c,d+=c);c>>=1;b>c&&(a|=2,b-=c,d+=c);c>>=1;b>c&&(a|=1,d+=c);b=a;x=b&8?x-d:x+d;-32768>x?x=-32768:32767A?A=0:88>1);a&1&&(b+=B>>2);b+=B>>3;a&8&&(b=-b);v+=b;32767v&&(v=-32767); +w+=Y[a];0>w?w=0:88>8&128;e||(d*=-1);32635>8&127];d=f<<4|d>>f+3&15}else d>>=4;b[c]=d^e^85}return b}function ra(a){for(var b=new Int16Array(a.length),c=0;c>4)+4;d=4!=f?1<>8&128;0!=e&&(d=-d);32635>7&255];b[c]=~(e|f<<4|d>>f+3&15)}return b}function ua(a){for(var b=new Int16Array(a.length),c=0;c>4&7;e=va[e]+((d&15)<f)b[c]=f,c++;else{var h=0,g=0;2047>=f?(h=1,g=192):65535>=f?(h=2,g=224):1114111>=f&&(h=3,g=240,d++);b[c]=(f>>6*h)+g;for(c++;0>6*(h-1)&63,c++,h--}}return c}function Z(a,b,c,d){if(d||Infinity!==b&&-Infinity!==b&&b===b)throw Error(a.message+" at input index "+c+": "+b);throw Error("Argument is not a integer at input index "+c);}function H(a,b,c){c=void 0===c?a.length:c;var d=void 0===b?0:b;c=void 0===c? +a.length:c;b="";for(d=void 0===d?0:d;d=g)b+=String.fromCharCode(g);else{var k=0;194<=g&&223>=g?k=1:224<=g&&239>=g?(k=2,224===a[d]&&(e=160),237===a[d]&&(f=159)):240<=g&&244>=g?(k=3,240===a[d]&&(e=144),244===a[d]&&(f=143)):h=!0;g&=(1<<8-k-1)-1;for(var l=0;lf)h=!0;g=g<<6|a[d]&63;d++}h?b+=String.fromCharCode(65533):65535>=g?b+=String.fromCharCode(g):(g-=65536,b+=String.fromCharCode((g>>10&1023)+55296,(g&1023)+56320))}}return b}function q(a){var b= +[];R(a,b,0);return b}function I(a,b,c,d){d=void 0===d?0:d;b=b||{};var e=new C(b.h,b.A,b.F),f=0,h=d;try{for(var g=a.length;fK;K++)E["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(K)]=K;var V=new Float32Array(1),Y=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],O=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442, 11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767],x=0,A=0,v=0,w=0,B=7,qa=[1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7],ta=[0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7],va=[0,132,396,924,1980,4092,8316,16764];z.prototype.f=function(a,b,c){Math.abs(b)>this.o-2*this.u&&(b=0>b?-Infinity:Infinity);var d= -0>((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var e=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),g=this.l(b/Math.pow(2,e)*Math.pow(2,this.c));b!==b?(g=Math.pow(2,this.c-1),e=(1<=Math.pow(2,1-this.a)?(2<=g/Math.pow(2,this.c)&&(e+=1,g=1),e>this.a?(e=(1<=b;)a[g]=parseInt(e.substring(0, -8),2),e=e.substring(8),g--,d++;return d};z.prototype.l=function(a){var b=Math.floor(a);a-=b;return.5>a?b:.5b?b+Math.pow(2,this.h):b)&255;c++;for(var d=this.b,e=2;e((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var e=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),f=this.l(b/Math.pow(2,e)*Math.pow(2,this.c));b!==b?(f=Math.pow(2,this.c-1),e=(1<=Math.pow(2,1-this.a)?(2<=f/Math.pow(2,this.c)&&(e+=1,f=1),e>this.a?(e=(1<=b;)a[f]=parseInt(e.substring(0, +8),2),e=e.substring(8),f--,d++;return d};z.prototype.l=function(a){var b=Math.floor(a);a-=b;return.5>a?b:.5b?b+Math.pow(2,this.h):b)&255;c++;for(var d=this.b,e=2;ethis.max||athis.max&&(a-=2*this.max+2);return a};C.prototype.f=function(a,b){return this.b.i(a,void 0===b?0:b)};C.prototype.c=function(a,b,c){return this.b.f(a, b,void 0===c?0:c)};C.prototype.i=function(a){return 16===a?new z(5,11):32===a?new z(8,23):new z(11,52)};t.prototype.Ia=function(a){this.c=0;this.container=this.i(a,4);if(-1===this.W.indexOf(this.container))throw Error("Not a supported format.");this.a.m="RIFX"===this.container;this.chunkSize=this.b(a);this.format=this.i(a,4);this.O={chunkId:this.container,chunkSize:this.chunkSize,format:this.format,subChunks:this.L(a)}};t.prototype.j=function(a,b){b=void 0===b?!1:b;for(var c=this.O.subChunks,d=[], e=0;ethis.data.samples.length)throw Error("Range error"); -return J(this.data.samples.slice(a,a+this.dataType.h/8),this.dataType)};m.prototype.setSample=function(a,b){a*=this.dataType.h/8;if(a+this.dataType.h/8>this.data.samples.length)throw Error("Range error");I([b],this.dataType,this.data.samples,void 0===a?0:a)};m.prototype.ua=function(a,b,c,d,e,g){"4"==a?this.Z(a,b,c,d,e,g):"8a"==a||"8m"==a?this.$(a,b,c,d,e,g):-1==Object.keys(this.D).indexOf(a)||2this.data.samples.length)throw Error("Range error"); +return J(this.data.samples.slice(a,a+this.dataType.h/8),this.dataType)};m.prototype.setSample=function(a,b){a*=this.dataType.h/8;if(a+this.dataType.h/8>this.data.samples.length)throw Error("Range error");I([b],this.dataType,this.data.samples,void 0===a?0:a)};m.prototype.ua=function(a,b,c,d,e,f){"4"==a?this.Z(a,b,c,d,e,f):"8a"==a||"8m"==a?this.$(a,b,c,d,e,f):-1==Object.keys(this.D).indexOf(a)||2parseInt(this.bitDepth,10)))throw Error("Invalid bit depth.");};m.prototype.P=function(){this.dataType={h:(parseInt(this.bitDepth, 10)-1|7)+1,A:"32f"==this.bitDepth||"64"==this.bitDepth,F:"8"!=this.bitDepth,m:"RIFX"==this.container};-1<["4","8a","8m"].indexOf(this.bitDepth)&&(this.dataType.h=8,this.dataType.F=!1)};m.prototype.R=function(){this.Ja();var a=this.fmt.numChannels;if(1>a||65535a||4294967295a.length)for(var c=0,d=4-a.length;ca&&!e?(this.u(a,g+1,b),this.u(c[g].dwPosition,g+2,c[g].label),e=!0):this.u(c[g].dwPosition,g+1,c[g].label);e||this.u(a,this.cue.points.length+1,b)}this.cue.dwCuePoints=this.cue.points.length};l.prototype.deleteCuePoint=function(a){this.cue.chunkId= +1E3;return a};l.prototype.setCuePoint=function(a,b){b=void 0===b?"":b;this.cue.chunkId="cue ";a=a*this.fmt.sampleRate/1E3;var c=this.I();this.G();var d=this.cue.points.length;this.cue.points=[];var e=!1;if(0===d)this.u(a,1,b);else{for(var f=0;fa&&!e?(this.u(a,f+1,b),this.u(c[f].dwPosition,f+2,c[f].label),e=!0):this.u(c[f].dwPosition,f+1,c[f].label);e||this.u(a,this.cue.points.length+1,b)}this.cue.dwCuePoints=this.cue.points.length};l.prototype.deleteCuePoint=function(a){this.cue.chunkId= "cue ";var b=this.I();this.G();var c=this.cue.points.length;this.cue.points=[];for(var d=0;db[g]&&(b[g]=-1);var f=d;d=c;W(f);W(d);g=la(f,d);h={V:Math.pow(2,parseInt(f,10))/2,T:Math.pow(2,parseInt(d,10))/2,U:Math.pow(2,parseInt(f,10))/2-1,S:Math.pow(2,parseInt(d,10))/2-1};c=b.length; -if("8"==f)for(f=0;f> -2],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[d]&3)<<4|a[d+1]>>4],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[d+1]&15)<<2|a[d+2]>>6],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[d+2]&63];2===b%3?c=c.substring(0,c.length-1)+"\x3d":1===b%3&&(c=c.substring(0,c.length-2)+"\x3d\x3d");return c};f.prototype.toDataURI=function(){return"data:audio/wav;base64,"+this.toBase64()};f.prototype.fromDataURI=function(a){this.fromBase64(a.replace("data:audio/wav;base64,", -""))};return f}();window.WaveFile=wa; +b){var c=a,d=this.bitDepth;void 0===b||b||("32f"!=a&&(c=this.dataType.h.toString()),d=this.dataType.h);this.J();var e=this.data.samples.length/(this.dataType.h/8);b=new Float64Array(e);e=new Float64Array(e);F(this.data.samples,this.dataType,b);if("32f"==d||"64"==d)for(var f=0,h=b.length;fb[f]&&(b[f]=-1);var g=d;d=c;W(g);W(d);f=la(g,d);h={V:Math.pow(2,parseInt(g,10))/2,T:Math.pow(2,parseInt(d,10))/2,U:Math.pow(2,parseInt(g,10))/2-1,S:Math.pow(2,parseInt(d,10))/2-1};c=b.length; +if("8"==g)for(g=0;g> +2],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[d]&3)<<4|a[d+1]>>4],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[d+1]&15)<<2|a[d+2]>>6],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[d+2]&63];2===b%3?c=c.substring(0,c.length-1)+"\x3d":1===b%3&&(c=c.substring(0,c.length-2)+"\x3d\x3d");return c};g.prototype.toDataURI=function(){return"data:audio/wav;base64,"+this.toBase64()};g.prototype.fromDataURI=function(a){this.fromBase64(a.replace("data:audio/wav;base64,", +""))};return g}();window.WaveFile=wa; diff --git a/dist/wavefile.umd.js b/dist/wavefile.umd.js index 2f886f6..f5f65e5 100644 --- a/dist/wavefile.umd.js +++ b/dist/wavefile.umd.js @@ -1 +1 @@ -Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(searchElement,fromIndex){if(null==this)throw new TypeError('"this" is null or not defined');var o=Object(this),len=o.length>>>0;if(0===len)return!1;var x,y,n=0|fromIndex,k=Math.max(n>=0?n:len-Math.abs(n),0);for(;k=size)){var second,first=string.charCodeAt(index);return first>=55296&&first<=56319&&size>index+1&&(second=string.charCodeAt(index+1))>=56320&&second<=57343?1024*(first-55296)+second-56320+65536:first}};defineProperty?defineProperty(String.prototype,"codePointAt",{value:codePointAt,configurable:!0,writable:!0}):String.prototype.codePointAt=codePointAt}(),Uint8Array.prototype.slice||Object.defineProperty(Uint8Array.prototype,"slice",{value:Array.prototype.slice}),function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.WaveFile=factory()}(this,function(){"use strict";for(var chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",lookup=new Uint8Array(256),i=0;i0?parseInt(sample/args.oldMax*args.newMax,10):parseInt(sample/args.oldMin*args.newMin,10)}function floatToInt_(sample,args){return parseInt(sample>0?sample*args.newMax:sample*args.newMin,10)}function intToFloat_(sample,args){return sample>0?sample/args.oldMax:sample/args.oldMin}function floatToFloat_(sample){return f64f32_[0]=sample,f64f32_[0]}function validateBitDepth_(bitDepth){if("32f"!=bitDepth&&"64"!=bitDepth&&(parseInt(bitDepth,10)<"8"||parseInt(bitDepth,10)>"53"))throw new Error("Invalid bit depth.")}var INDEX_TABLE=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],STEP_TABLE=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767],encoderPredicted_=0,encoderIndex_=0,decoderPredicted_=0,decoderIndex_=0,decoderStep_=7;function encodeBlock(block){for(var adpcmSamples=function(sample){encodeSample_(sample);var adpcmSamples=[];return adpcmSamples.push(255&sample),adpcmSamples.push(sample>>8&255),adpcmSamples.push(encoderIndex_),adpcmSamples.push(0),adpcmSamples}(block[0]),i=3;i32768?num-65536:num,decoderIndex_=block[2],decoderStep_=STEP_TABLE[decoderIndex_];for(var result=[decoderPredicted_,decoderPredicted_],i=4;i>4,first_sample=second_sample<<4^original_sample;result.push(decodeSample_(first_sample)),result.push(decodeSample_(second_sample))}return result}function encodeSample_(sample){var delta=sample-encoderPredicted_,value=0;delta>=0?value=0:(value=8,delta=-delta);var step=STEP_TABLE[encoderIndex_],diff=step>>3;return delta>step&&(value|=4,delta-=step,diff+=step),delta>(step>>=1)&&(value|=2,delta-=step,diff+=step),delta>(step>>=1)&&(value|=1,diff+=step),function(value,diff){8&value?encoderPredicted_-=diff:encoderPredicted_+=diff;encoderPredicted_<-32768?encoderPredicted_=-32768:encoderPredicted_>32767&&(encoderPredicted_=32767);(encoderIndex_+=INDEX_TABLE[7&value])<0?encoderIndex_=0:encoderIndex_>88&&(encoderIndex_=88)}(value,diff),value}function decodeSample_(nibble){var difference=0;return 4&nibble&&(difference+=decoderStep_),2&nibble&&(difference+=decoderStep_>>1),1&nibble&&(difference+=decoderStep_>>2),difference+=decoderStep_>>3,8&nibble&&(difference=-difference),(decoderPredicted_+=difference)>32767?decoderPredicted_=32767:decoderPredicted_<-32767&&(decoderPredicted_=-32767),function(nibble){(decoderIndex_+=INDEX_TABLE[nibble])<0?decoderIndex_=0:decoderIndex_>88&&(decoderIndex_=88);decoderStep_=STEP_TABLE[decoderIndex_]}(nibble),decoderPredicted_}var LOG_TABLE=[1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7];function encodeSample(sample){var compandedValue=void 0,sign=~(sample=-32768==sample?-32767:sample)>>8&128;if(sign||(sample*=-1),sample>32635&&(sample=32635),sample>=256){var exponent=LOG_TABLE[sample>>8&127];compandedValue=exponent<<4|sample>>exponent+3&15}else compandedValue=sample>>4;return 85^compandedValue^sign}function decodeSample(aLawSample){var sign=0;128&(aLawSample^=85)&&(aLawSample&=-129,sign=-1);var position=4+((240&aLawSample)>>4),decoded=0;return decoded=4!=position?1<2&&void 0!==arguments[2]?arguments[2]:0,end=arguments.length>3&&void 0!==arguments[3]?arguments[3]:bytes.length;if(end%offset)throw new Error("Bad buffer length.");for(var index=start;index2&&void 0!==arguments[2]?arguments[2]:0,i=0,len=str.length;i>6*count)+offset,index++;count>0;)buffer[index]=128|codePoint>>6*(count-1)&63,index++,count--}}return index}var TYPE_ERR="Unsupported type",TYPE_NAN="Argument is not a valid number";function validateIsNumber(value){if(null==value)throw new Error(TYPE_NAN);if(value.constructor!==Number&&value.constructor!==Boolean)throw new Error(TYPE_NAN)}var classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")},createClass=function(){function defineProperties(target,props){for(var i=0;ithis.biasP2-2*this.ebitsFbits&&(num=num<0?-1/0:1/0);var sign=((num=+num)||1/num)<0?1:num<0?1:0;num=Math.abs(num);var exp=Math.min(Math.floor(Math.log(num)/Math.LN2),1023),fraction=this.roundToEven(num/Math.pow(2,exp)*Math.pow(2,this.fbits));return num!=num?(fraction=Math.pow(2,this.fbits-1),exp=(1<=Math.pow(2,1-this.bias)?(fraction/Math.pow(2,this.fbits)>=2&&(exp+=1,fraction=1),exp>this.bias?(exp=(1<=0;i--){var t=buffer[i+index].toString(2);leftBits+="00000000".substring(t.length)+t}var sign="1"==leftBits.charAt(0)?-1:1;leftBits=leftBits.substring(1);var exponent=parseInt(leftBits.substring(0,this.ebits),2);return leftBits=leftBits.substring(this.ebits),exponent==eMax?0!==parseInt(leftBits,2)?NaN:sign*(1/0):(0===exponent?(exponent+=1,significand=parseInt(leftBits,2)):significand=parseInt("1"+leftBits,2),sign*significand*this.fbias*Math.pow(2,exponent-this.bias))}},{key:"packFloatBits_",value:function(buffer,index,sign,exp,fraction){var bits=[];bits.push(sign);for(var i=this.ebits;i>0;i-=1)bits[i]=exp%2?1:0,exp=Math.floor(exp/2);for(var len=bits.length,_i=this.fbits;_i>0;_i-=1)bits[len+_i]=fraction%2?1:0,fraction=Math.floor(fraction/2);for(var str=bits.join(""),numBytes=this.numBytes+index-1,k=index;numBytes>=index;)buffer[numBytes]=parseInt(str.substring(0,8),2),str=str.substring(8),numBytes--,k++;return k}},{key:"roundToEven",value:function(n){var w=Math.floor(n),f=n-w;return f<.5?w:f>.5?w+1:w%2?w+1:w}}]),IEEE754Buffer}(),UintBuffer=function(){function UintBuffer(bits){classCallCheck(this,UintBuffer),this.bits=bits,this.bytes=bits<8?1:Math.ceil(bits/8),this.max=Math.pow(2,bits)-1,this.min=0;var r=8-(1+(bits-1|7)-bits);this.lastByteMask_=Math.pow(2,r>0?r:8)-1}return createClass(UintBuffer,[{key:"pack",value:function(buffer,num){var index=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;if(num!=num)throw new Error("NaN");this.overflow(num),buffer[index]=255&(num<0?num+Math.pow(2,this.bits):num),index++;for(var len=this.bytes,i=2;i8&&(buffer[index]=Math.floor(num/Math.pow(2,8*(this.bytes-1)))&this.lastByteMask_,index++),index}},{key:"unpack",value:function(buffer){var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,num=this.unpackUnsafe(buffer,index);return this.overflow(num),num}},{key:"unpackUnsafe",value:function(buffer,index){for(var num=0,x=0;xthis.max||num2&&void 0!==arguments[2]?arguments[2]:0;return get(TwosComplementBuffer.prototype.__proto__||Object.getPrototypeOf(TwosComplementBuffer.prototype),"pack",this).call(this,buffer,num,index)}},{key:"unpack",value:function(buffer){var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,num=get(TwosComplementBuffer.prototype.__proto__||Object.getPrototypeOf(TwosComplementBuffer.prototype),"unpackUnsafe",this).call(this,buffer,index);return num=this.sign_(num),this.overflow(num),num}},{key:"sign_",value:function(num){return num>this.max&&(num-=2*this.max+2),num}}]),TwosComplementBuffer}(),NumberBuffer=function(){function NumberBuffer(bits,fp,signed){classCallCheck(this,NumberBuffer);var parser=void 0;fp?(!function(bits){if(!bits||16!==bits&&32!==bits&&64!==bits)throw new Error(TYPE_ERR+": float, bits: "+bits)}(bits),parser=this.getFPParser_(bits)):(!function(bits){if(!bits||bits<1||bits>53)throw new Error(TYPE_ERR+": int, bits: "+bits)}(bits),parser=signed?new TwosComplementBuffer(bits):new UintBuffer(bits)),this.parser=parser,this.offset=Math.ceil(bits/8)}return createClass(NumberBuffer,[{key:"unpack",value:function(buffer){var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return this.parser.unpack(buffer,index)}},{key:"pack",value:function(buffer,num){var index=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return this.parser.pack(buffer,num,index)}},{key:"getFPParser_",value:function(bits){return 16===bits?new IEEE754Buffer(5,11):32===bits?new IEEE754Buffer(8,23):new IEEE754Buffer(11,52)}}]),NumberBuffer}();function throwValueError_(e,value,i,fp){throw fp||value!==1/0&&value!==-1/0&&value==value?new Error(e.message+" at input index "+i+": "+value):new Error("Argument is not a integer at input index "+i)}function unpackString(buffer){return function(buffer){for(var start=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,end=arguments.length>2&&void 0!==arguments[2]?arguments[2]:buffer.length,str="",index=start;index=0&&charCode<=127)str+=String.fromCharCode(charCode);else{var count=0;charCode>=194&&charCode<=223?count=1:charCode>=224&&charCode<=239?(count=2,224===buffer[index]&&(lowerBoundary=160),237===buffer[index]&&(upperBoundary=159)):charCode>=240&&charCode<=244?(count=3,240===buffer[index]&&(lowerBoundary=144),244===buffer[index]&&(upperBoundary=143)):replace=!0,charCode&=(1<<8-count-1)-1;for(var i=0;iupperBoundary)&&(replace=!0),charCode=charCode<<6|63&buffer[index],index++;replace?str+=String.fromCharCode(65533):charCode<=65535?str+=String.fromCharCode(charCode):(charCode-=65536,str+=String.fromCharCode(55296+(charCode>>10&1023),56320+(1023&charCode)))}}return str}(buffer,arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,arguments.length>2&&void 0!==arguments[2]?arguments[2]:buffer.length)}function packString(str){var buffer=[];return pack(str,buffer,0),buffer}function packStringTo(str,buffer){return pack(str,buffer,arguments.length>2&&void 0!==arguments[2]?arguments[2]:0)}function packArrayTo(values,theType,buffer){var index=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,packer=new NumberBuffer((theType=theType||{}).bits,theType.fp,theType.signed),i=0,start=index;try{for(var valuesLen=values.length;i3&&void 0!==arguments[3]?arguments[3]:0,end=arguments.length>4&&void 0!==arguments[4]?arguments[4]:buffer.length,safe=arguments.length>5&&void 0!==arguments[5]&&arguments[5],packer=new NumberBuffer((theType=theType||{}).bits,theType.fp,theType.signed),offset=packer.offset;end=function(buffer,start,end,offset,safe){var extra=(end-start)%offset;if(safe&&(extra||buffer.length3&&void 0!==arguments[3]?arguments[3]:0)}function pack$1(value,theType){var output=[];return packTo(value,theType,output),output}function unpackArray(buffer,theType){var output=[];return unpackArrayTo(buffer,theType,output,arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,arguments.length>3&&void 0!==arguments[3]?arguments[3]:buffer.length,arguments.length>4&&void 0!==arguments[4]&&arguments[4]),output}function unpack$1(buffer,theType){var index=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return unpackArray(buffer,theType,index,index+Math.ceil(theType.bits/8),!0)[0]}var RIFFFile=function(){function RIFFFile(){classCallCheck(this,RIFFFile),this.container="",this.chunkSize=0,this.format="",this.signature={},this.head=0,this.uInt32={bits:32,be:!1},this.supported_containers=["RIFF","RIFX"]}return createClass(RIFFFile,[{key:"setSignature",value:function(buffer){if(this.head=0,this.container=this.readString(buffer,4),-1===this.supported_containers.indexOf(this.container))throw Error("Not a supported format.");this.uInt32.be="RIFX"===this.container,this.chunkSize=this.readUInt32(buffer),this.format=this.readString(buffer,4),this.signature={chunkId:this.container,chunkSize:this.chunkSize,format:this.format,subChunks:this.getSubChunksIndex_(buffer)}}},{key:"findChunk",value:function(chunkId){for(var multiple=arguments.length>1&&void 0!==arguments[1]&&arguments[1],chunks=this.signature.subChunks,chunk=[],i=0;i1&&void 0!==arguments[1])||arguments[1];if(this.clearHeaders(),this.setSignature(wavBuffer),this.uInt16.be=this.uInt32.be,"WAVE"!=this.format)throw Error('Could not find the "WAVE" format identifier');this.readDs64Chunk_(wavBuffer),this.readFmtChunk_(wavBuffer),this.readFactChunk_(wavBuffer),this.readBextChunk_(wavBuffer),this.readCueChunk_(wavBuffer),this.readSmplChunk_(wavBuffer),this.readDataChunk_(wavBuffer,samples),this.readJunkChunk_(wavBuffer),this.readLISTChunk_(wavBuffer)}},{key:"clearHeaders",value:function(){var tmpWav=new WaveFileReader;Object.assign(this.fmt,tmpWav.fmt),Object.assign(this.fact,tmpWav.fact),Object.assign(this.cue,tmpWav.cue),Object.assign(this.smpl,tmpWav.smpl),Object.assign(this.bext,tmpWav.bext),Object.assign(this.ds64,tmpWav.ds64),Object.assign(this.data,tmpWav.data),this.LIST=[],Object.assign(this.junk,tmpWav.junk)}},{key:"readFmtChunk_",value:function(buffer){var chunk=this.findChunk("fmt ");if(!chunk)throw Error('Could not find the "fmt " chunk');this.head=chunk.chunkData.start,this.fmt.chunkId=chunk.chunkId,this.fmt.chunkSize=chunk.chunkSize,this.fmt.audioFormat=this.readUInt16_(buffer),this.fmt.numChannels=this.readUInt16_(buffer),this.fmt.sampleRate=this.readUInt32(buffer),this.fmt.byteRate=this.readUInt32(buffer),this.fmt.blockAlign=this.readUInt16_(buffer),this.fmt.bitsPerSample=this.readUInt16_(buffer),this.readFmtExtension_(buffer)}},{key:"readFmtExtension_",value:function(buffer){this.fmt.chunkSize>16&&(this.fmt.cbSize=this.readUInt16_(buffer),this.fmt.chunkSize>18&&(this.fmt.validBitsPerSample=this.readUInt16_(buffer),this.fmt.chunkSize>20&&(this.fmt.dwChannelMask=this.readUInt32(buffer),this.fmt.subformat=[this.readUInt32(buffer),this.readUInt32(buffer),this.readUInt32(buffer),this.readUInt32(buffer)])))}},{key:"readFactChunk_",value:function(buffer){var chunk=this.findChunk("fact");chunk&&(this.head=chunk.chunkData.start,this.fact.chunkId=chunk.chunkId,this.fact.chunkSize=chunk.chunkSize,this.fact.dwSampleLength=this.readUInt32(buffer))}},{key:"readCueChunk_",value:function(buffer){var chunk=this.findChunk("cue ");if(chunk){this.head=chunk.chunkData.start,this.cue.chunkId=chunk.chunkId,this.cue.chunkSize=chunk.chunkSize,this.cue.dwCuePoints=this.readUInt32(buffer);for(var i=0;i-1){this.head=subChunk.chunkData.start;var item={chunkId:subChunk.chunkId,chunkSize:subChunk.chunkSize,dwName:this.readUInt32(buffer)};"ltxt"==subChunk.chunkId&&(item.dwSampleLength=this.readUInt32(buffer),item.dwPurposeID=this.readUInt32(buffer),item.dwCountry=this.readUInt16_(buffer),item.dwLanguage=this.readUInt16_(buffer),item.dwDialect=this.readUInt16_(buffer),item.dwCodePage=this.readUInt16_(buffer)),item.value=this.readZSTR_(buffer,this.head),this.LIST[this.LIST.length-1].subChunks.push(item)}}else"INFO"==format&&(this.head=subChunk.chunkData.start,this.LIST[this.LIST.length-1].subChunks.push({chunkId:subChunk.chunkId,chunkSize:subChunk.chunkSize,value:this.readZSTR_(buffer,this.head)}))}},{key:"readJunkChunk_",value:function(buffer){var chunk=this.findChunk("junk");chunk&&(this.junk={chunkId:chunk.chunkId,chunkSize:chunk.chunkSize,chunkData:[].slice.call(buffer.slice(chunk.chunkData.start,chunk.chunkData.end))})}},{key:"readZSTR_",value:function(bytes){for(var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,i=index;i2&&void 0!==arguments[2])||arguments[2],packedString=packString(str);if(fixedSize)for(var i=packedString.length;i16&&(extension=extension.concat(pack$1(this.fmt.cbSize,this.uInt16))),this.fmt.chunkSize>18&&(extension=extension.concat(pack$1(this.fmt.validBitsPerSample,this.uInt16))),this.fmt.chunkSize>20&&(extension=extension.concat(pack$1(this.fmt.dwChannelMask,this.uInt32))),this.fmt.chunkSize>24&&(extension=extension.concat(pack$1(this.fmt.subformat[0],this.uInt32),pack$1(this.fmt.subformat[1],this.uInt32),pack$1(this.fmt.subformat[2],this.uInt32),pack$1(this.fmt.subformat[3],this.uInt32))),extension}},{key:"getLISTBytes_",value:function(){for(var bytes=[],i=0;i-1?(bytes=bytes.concat(packString(subChunks[i].chunkId),pack$1(subChunks[i].value.length+4+1,this.uInt32),pack$1(subChunks[i].dwName,this.uInt32),writeString(subChunks[i].value,subChunks[i].value.length))).push(0):"ltxt"==subChunks[i].chunkId&&(bytes=bytes.concat(this.getLtxtChunkBytes_(subChunks[i])))),bytes.length%2&&bytes.push(0);return bytes}},{key:"getLtxtChunkBytes_",value:function(ltxt){return[].concat(packString(ltxt.chunkId),pack$1(ltxt.value.length+20,this.uInt32),pack$1(ltxt.dwName,this.uInt32),pack$1(ltxt.dwSampleLength,this.uInt32),pack$1(ltxt.dwPurposeID,this.uInt32),pack$1(ltxt.dwCountry,this.uInt16),pack$1(ltxt.dwLanguage,this.uInt16),pack$1(ltxt.dwDialect,this.uInt16),pack$1(ltxt.dwCodePage,this.uInt16),writeString(ltxt.value,ltxt.value.length))}},{key:"getJunkBytes_",value:function(){var bytes=[];return this.junk.chunkId?bytes.concat(packString(this.junk.chunkId),pack$1(this.junk.chunkData.length,this.uInt32),this.junk.chunkData):bytes}}]),WaveFileParser}();var WaveFileCreator=function(_WaveFileParser){function WaveFileCreator(){classCallCheck(this,WaveFileCreator);var _this=possibleConstructorReturn(this,(WaveFileCreator.__proto__||Object.getPrototypeOf(WaveFileCreator)).call(this));return _this.bitDepth="0",_this.dataType={},_this.WAV_AUDIO_FORMATS={4:17,8:1,"8a":6,"8m":7,16:1,24:1,32:1,"32f":3,64:3},_this}return inherits(WaveFileCreator,WaveFileParser),createClass(WaveFileCreator,[{key:"fromScratch",value:function(numChannels,sampleRate,bitDepthCode,samples){var options=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{};this.clearHeaders(),this.newWavFile_(numChannels,sampleRate,bitDepthCode,samples,options)}},{key:"newWavFile_",value:function(numChannels,sampleRate,bitDepthCode,samples){var options=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{};options.container||(options.container="RIFF"),this.container=options.container,this.bitDepth=bitDepthCode,samples=function(samples){if(samples.length>0&&samples[0].constructor===Array){for(var finalSamples=[],i=0,len=samples[0].length;i1&&void 0!==arguments[1])||arguments[1];get(WaveFileCreator.prototype.__proto__||Object.getPrototypeOf(WaveFileCreator.prototype),"fromBuffer",this).call(this,wavBuffer,samples),this.bitDepthFromFmt_(),this.updateDataType_()}},{key:"toBuffer",value:function(){return this.validateWavHeader_(),get(WaveFileCreator.prototype.__proto__||Object.getPrototypeOf(WaveFileCreator.prototype),"toBuffer",this).call(this)}},{key:"getSample",value:function(index){if((index*=this.dataType.bits/8)+this.dataType.bits/8>this.data.samples.length)throw new Error("Range error");return unpack$1(this.data.samples.slice(index,index+this.dataType.bits/8),this.dataType)}},{key:"setSample",value:function(index,sample){if((index*=this.dataType.bits/8)+this.dataType.bits/8>this.data.samples.length)throw new Error("Range error");packTo(sample,this.dataType,this.data.samples,index)}},{key:"makeWavHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){"4"==bitDepthCode?this.createADPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options):"8a"==bitDepthCode||"8m"==bitDepthCode?this.createALawMulawHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options):-1==Object.keys(this.WAV_AUDIO_FORMATS).indexOf(bitDepthCode)||numChannels>2?this.createExtensibleHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options):this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options)}},{key:"createPCMHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.container=options.container,this.chunkSize=36+samplesLength,this.format="WAVE",this.bitDepth=bitDepthCode,this.fmt={chunkId:"fmt ",chunkSize:16,audioFormat:this.WAV_AUDIO_FORMATS[bitDepthCode]||65534,numChannels:numChannels,sampleRate:sampleRate,byteRate:numChannels*numBytes*sampleRate,blockAlign:numChannels*numBytes,bitsPerSample:parseInt(bitDepthCode,10),cbSize:0,validBitsPerSample:0,dwChannelMask:0,subformat:[]}}},{key:"createADPCMHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options),this.chunkSize=40+samplesLength,this.fmt.chunkSize=20,this.fmt.byteRate=4055,this.fmt.blockAlign=256,this.fmt.bitsPerSample=4,this.fmt.cbSize=2,this.fmt.validBitsPerSample=505,this.fact={chunkId:"fact",chunkSize:4,dwSampleLength:2*samplesLength}}},{key:"createExtensibleHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options),this.chunkSize=60+samplesLength,this.fmt.chunkSize=40,this.fmt.bitsPerSample=1+(parseInt(bitDepthCode,10)-1|7),this.fmt.cbSize=22,this.fmt.validBitsPerSample=parseInt(bitDepthCode,10),this.fmt.dwChannelMask=function(numChannels){var mask=0;return 1===numChannels?mask=4:2===numChannels?mask=3:4===numChannels?mask=51:6===numChannels?mask=63:8===numChannels&&(mask=1599),mask}(numChannels),this.fmt.subformat=[1,1048576,2852126848,1905997824]}},{key:"createALawMulawHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options),this.chunkSize=40+samplesLength,this.fmt.chunkSize=20,this.fmt.cbSize=2,this.fmt.validBitsPerSample=8,this.fact={chunkId:"fact",chunkSize:4,dwSampleLength:samplesLength}}},{key:"bitDepthFromFmt_",value:function(){3===this.fmt.audioFormat&&32===this.fmt.bitsPerSample?this.bitDepth="32f":6===this.fmt.audioFormat?this.bitDepth="8a":7===this.fmt.audioFormat?this.bitDepth="8m":this.bitDepth=this.fmt.bitsPerSample.toString()}},{key:"validateBitDepth_",value:function(){if(!this.WAV_AUDIO_FORMATS[this.bitDepth]){if(parseInt(this.bitDepth,10)>8&&parseInt(this.bitDepth,10)<54)return!0;throw new Error("Invalid bit depth.")}return!0}},{key:"updateDataType_",value:function(){this.dataType={bits:1+(parseInt(this.bitDepth,10)-1|7),fp:"32f"==this.bitDepth||"64"==this.bitDepth,signed:"8"!=this.bitDepth,be:"RIFX"==this.container},["4","8a","8m"].indexOf(this.bitDepth)>-1&&(this.dataType.bits=8,this.dataType.signed=!1)}},{key:"validateWavHeader_",value:function(){if(this.validateBitDepth_(),channels=this.fmt.numChannels,bits=this.fmt.bitsPerSample,channels<1||channels*bits/8>65535)throw new Error("Invalid number of channels.");var channels,bits;if(!function(channels,bits,sampleRate){return!(sampleRate<1||channels*(bits/8)*sampleRate>4294967295)}(this.fmt.numChannels,this.fmt.bitsPerSample,this.fmt.sampleRate))throw new Error("Invalid sample rate.")}}]),WaveFileCreator}();var WaveFileMetaEditor=function(_WaveFileCreator){function WaveFileMetaEditor(){return classCallCheck(this,WaveFileMetaEditor),possibleConstructorReturn(this,(WaveFileMetaEditor.__proto__||Object.getPrototypeOf(WaveFileMetaEditor)).apply(this,arguments))}return inherits(WaveFileMetaEditor,WaveFileCreator),createClass(WaveFileMetaEditor,[{key:"getTag",value:function(tag){var index=this.getTagIndex_(tag);return null!==index.TAG?this.LIST[index.LIST].subChunks[index.TAG].value:null}},{key:"setTag",value:function(tag,value){tag=function(tag){if(tag.constructor!==String)throw new Error("Invalid tag name.");if(tag.length<4)for(var i=0,len=4-tag.length;i1&&void 0!==arguments[1]?arguments[1]:"";this.cue.chunkId="cue ",position=position*this.fmt.sampleRate/1e3;var existingPoints=this.getCuePoints_();this.clearLISTadtl_();var len=this.cue.points.length;this.cue.points=[];var hasSet=!1;if(0===len)this.setCuePoint_(position,1,labl);else{for(var i=0;iposition&&!hasSet?(this.setCuePoint_(position,i+1,labl),this.setCuePoint_(existingPoints[i].dwPosition,i+2,existingPoints[i].label),hasSet=!0):this.setCuePoint_(existingPoints[i].dwPosition,i+1,existingPoints[i].label);hasSet||this.setCuePoint_(position,this.cue.points.length+1,labl)}this.cue.dwCuePoints=this.cue.points.length}},{key:"deleteCuePoint",value:function(index){this.cue.chunkId="cue ";var existingPoints=this.getCuePoints_();this.clearLISTadtl_();var len=this.cue.points.length;this.cue.points=[];for(var i=0;i0&&void 0!==arguments[0]?arguments[0]:"16";this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,"16",function(adpcmSamples){for(var blockAlign=arguments.length>1&&void 0!==arguments[1]?arguments[1]:256,samples=new Int16Array(2*adpcmSamples.length),block=[],fileIndex=0,i=0;i0&&void 0!==arguments[0]?arguments[0]:"16";this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,"16",function(samples){for(var pcmSamples=new Int16Array(samples.length),i=0;i>8&128)&&(sample=-sample),sample>CLIP&&(sample=CLIP),~(sign|(exponent=encodeTable[(sample+=BIAS)>>7&255])<<4|sample>>exponent+3&15));return muLawSamples}(output),{container:this.correctContainer_()})}},{key:"fromMuLaw",value:function(){var bitDepthCode=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"16";this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,"16",function(samples){for(var muLawSample,sign,exponent,mantissa,sample,pcmSamples=new Int16Array(samples.length),i=0;i>4&7]+(mantissa<1&&void 0!==arguments[1])||arguments[1],toBitDepth=newBitDepth,thisBitDepth=this.bitDepth;changeResolution||("32f"!=newBitDepth&&(toBitDepth=this.dataType.bits.toString()),thisBitDepth=this.dataType.bits),this.assureUncompressed_();var sampleCount=this.data.samples.length/(this.dataType.bits/8),typedSamplesInput=new Float64Array(sampleCount),typedSamplesOutput=new Float64Array(sampleCount);unpackArrayTo(this.data.samples,this.dataType,typedSamplesInput),"32f"!=thisBitDepth&&"64"!=thisBitDepth||function(samples){for(var i=0,len=samples.length;i1?samples[i]=1:samples[i]<-1&&(samples[i]=-1)}(typedSamplesInput),bitDepth(typedSamplesInput,thisBitDepth,toBitDepth,typedSamplesOutput),this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,newBitDepth,typedSamplesOutput,{container:this.correctContainer_()})}},{key:"assure16Bit_",value:function(){this.assureUncompressed_(),"16"!=this.bitDepth&&this.toBitDepth("16")}},{key:"assureUncompressed_",value:function(){"8a"==this.bitDepth?this.fromALaw():"8m"==this.bitDepth?this.fromMuLaw():"4"==this.bitDepth&&this.fromIMAADPCM()}},{key:"correctContainer_",value:function(){return"RF64"==this.container?"RIFF":this.container}},{key:"fromExisting_",value:function(numChannels,sampleRate,bitDepthCode,samples){var options=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{},tmpWav=new WaveFileMetaEditor;Object.assign(this.fmt,tmpWav.fmt),Object.assign(this.fact,tmpWav.fact),Object.assign(this.ds64,tmpWav.ds64),Object.assign(this.data,tmpWav.data),this.newWavFile_(numChannels,sampleRate,bitDepthCode,samples,options)}},{key:"outputSize_",value:function(){var outputSize=this.data.samples.length/2;return outputSize%2&&outputSize++,outputSize}}]),WaveFileConverter}();return function(_WaveFileConverter){function WaveFile(){var wavBuffer=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;classCallCheck(this,WaveFile);var _this=possibleConstructorReturn(this,(WaveFile.__proto__||Object.getPrototypeOf(WaveFile)).call(this));return wavBuffer&&_this.fromBuffer(wavBuffer),_this}return inherits(WaveFile,WaveFileConverter),createClass(WaveFile,[{key:"fromBase64",value:function(base64String){this.fromBuffer(new Uint8Array(function(base64){var len=base64.length,bufferLength=.75*base64.length,p=0,encoded1=void 0,encoded2=void 0,encoded3=void 0,encoded4=void 0;"="===base64[base64.length-1]&&(bufferLength--,"="===base64[base64.length-2]&&bufferLength--);for(var arraybuffer=new ArrayBuffer(bufferLength),bytes=new Uint8Array(arraybuffer),_i2=0;_i2>4,bytes[p++]=(15&encoded2)<<4|encoded3>>2,bytes[p++]=(3&encoded3)<<6|63&encoded4;return arraybuffer}(base64String)))}},{key:"toBase64",value:function(){var buffer=this.toBuffer();return function(arraybuffer,byteOffset,length){for(var bytes=new Uint8Array(arraybuffer,byteOffset,length),len=bytes.length,base64="",_i=0;_i>2],base64+=chars[(3&bytes[_i])<<4|bytes[_i+1]>>4],base64+=chars[(15&bytes[_i+1])<<2|bytes[_i+2]>>6],base64+=chars[63&bytes[_i+2]];return len%3==2?base64=base64.substring(0,base64.length-1)+"=":len%3==1&&(base64=base64.substring(0,base64.length-2)+"=="),base64}(buffer,0,buffer.length)}},{key:"toDataURI",value:function(){return"data:audio/wav;base64,"+this.toBase64()}},{key:"fromDataURI",value:function(dataURI){this.fromBase64(dataURI.replace("data:audio/wav;base64,",""))}}]),WaveFile}()}); +Array.prototype.includes||Object.defineProperty(Array.prototype,"includes",{value:function(searchElement,fromIndex){if(null==this)throw new TypeError('"this" is null or not defined');var o=Object(this),len=o.length>>>0;if(0===len)return!1;var x,y,n=0|fromIndex,k=Math.max(n>=0?n:len-Math.abs(n),0);for(;k=size)){var second,first=string.charCodeAt(index);return first>=55296&&first<=56319&&size>index+1&&(second=string.charCodeAt(index+1))>=56320&&second<=57343?1024*(first-55296)+second-56320+65536:first}};defineProperty?defineProperty(String.prototype,"codePointAt",{value:codePointAt,configurable:!0,writable:!0}):String.prototype.codePointAt=codePointAt}(),Uint8Array.prototype.slice||Object.defineProperty(Uint8Array.prototype,"slice",{value:Array.prototype.slice}),function(global,factory){"object"==typeof exports&&"undefined"!=typeof module?module.exports=factory():"function"==typeof define&&define.amd?define(factory):global.WaveFile=factory()}(this,function(){"use strict";for(var chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",lookup=new Uint8Array(256),i=0;i0?parseInt(sample/args.oldMax*args.newMax,10):parseInt(sample/args.oldMin*args.newMin,10)}function floatToInt_(sample,args){return parseInt(sample>0?sample*args.newMax:sample*args.newMin,10)}function intToFloat_(sample,args){return sample>0?sample/args.oldMax:sample/args.oldMin}function floatToFloat_(sample){return f64f32_[0]=sample,f64f32_[0]}function validateBitDepth_(bitDepth){if("32f"!=bitDepth&&"64"!=bitDepth&&(parseInt(bitDepth,10)<"8"||parseInt(bitDepth,10)>"53"))throw new Error("Invalid bit depth.")}var INDEX_TABLE=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],STEP_TABLE=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767],encoderPredicted_=0,encoderIndex_=0,decoderPredicted_=0,decoderIndex_=0,decoderStep_=7;function encodeBlock(block){for(var adpcmSamples=function(sample){encodeSample_(sample);var adpcmSamples=[];return adpcmSamples.push(255&sample),adpcmSamples.push(sample>>8&255),adpcmSamples.push(encoderIndex_),adpcmSamples.push(0),adpcmSamples}(block[0]),i=3;i32768?num-65536:num,decoderIndex_=block[2],decoderStep_=STEP_TABLE[decoderIndex_];for(var result=[decoderPredicted_,decoderPredicted_],i=4;i>4,first_sample=second_sample<<4^original_sample;result.push(decodeSample_(first_sample)),result.push(decodeSample_(second_sample))}return result}function encodeSample_(sample){var delta=sample-encoderPredicted_,value=0;delta>=0?value=0:(value=8,delta=-delta);var step=STEP_TABLE[encoderIndex_],diff=step>>3;return delta>step&&(value|=4,delta-=step,diff+=step),delta>(step>>=1)&&(value|=2,delta-=step,diff+=step),delta>(step>>=1)&&(value|=1,diff+=step),function(value,diff){8&value?encoderPredicted_-=diff:encoderPredicted_+=diff;encoderPredicted_<-32768?encoderPredicted_=-32768:encoderPredicted_>32767&&(encoderPredicted_=32767);(encoderIndex_+=INDEX_TABLE[7&value])<0?encoderIndex_=0:encoderIndex_>88&&(encoderIndex_=88)}(value,diff),value}function decodeSample_(nibble){var difference=0;return 4&nibble&&(difference+=decoderStep_),2&nibble&&(difference+=decoderStep_>>1),1&nibble&&(difference+=decoderStep_>>2),difference+=decoderStep_>>3,8&nibble&&(difference=-difference),(decoderPredicted_+=difference)>32767?decoderPredicted_=32767:decoderPredicted_<-32767&&(decoderPredicted_=-32767),function(nibble){(decoderIndex_+=INDEX_TABLE[nibble])<0?decoderIndex_=0:decoderIndex_>88&&(decoderIndex_=88);decoderStep_=STEP_TABLE[decoderIndex_]}(nibble),decoderPredicted_}var LOG_TABLE=[1,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7];function encodeSample(sample){var compandedValue=void 0,sign=~(sample=-32768==sample?-32767:sample)>>8&128;if(sign||(sample*=-1),sample>32635&&(sample=32635),sample>=256){var exponent=LOG_TABLE[sample>>8&127];compandedValue=exponent<<4|sample>>exponent+3&15}else compandedValue=sample>>4;return 85^compandedValue^sign}function decodeSample(aLawSample){var sign=0;128&(aLawSample^=85)&&(aLawSample&=-129,sign=-1);var position=4+((240&aLawSample)>>4),decoded=0;return decoded=4!=position?1<2&&void 0!==arguments[2]?arguments[2]:0,end=arguments.length>3&&void 0!==arguments[3]?arguments[3]:bytes.length;if(end%offset)throw new Error("Bad buffer length.");for(var index=start;index2&&void 0!==arguments[2]?arguments[2]:0,i=0,len=str.length;i>6*count)+offset,index++;count>0;)buffer[index]=128|codePoint>>6*(count-1)&63,index++,count--}}return index}var TYPE_ERR="Unsupported type",TYPE_NAN="Argument is not a valid number";function validateIsNumber(value){if(null==value)throw new Error(TYPE_NAN);if(value.constructor!==Number&&value.constructor!==Boolean)throw new Error(TYPE_NAN)}var classCallCheck=function(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")},createClass=function(){function defineProperties(target,props){for(var i=0;ithis.biasP2-2*this.ebitsFbits&&(num=num<0?-1/0:1/0);var sign=((num=+num)||1/num)<0?1:num<0?1:0;num=Math.abs(num);var exp=Math.min(Math.floor(Math.log(num)/Math.LN2),1023),fraction=this.roundToEven(num/Math.pow(2,exp)*Math.pow(2,this.fbits));return num!=num?(fraction=Math.pow(2,this.fbits-1),exp=(1<=Math.pow(2,1-this.bias)?(fraction/Math.pow(2,this.fbits)>=2&&(exp+=1,fraction=1),exp>this.bias?(exp=(1<=0;i--){var t=buffer[i+index].toString(2);leftBits+="00000000".substring(t.length)+t}var sign="1"==leftBits.charAt(0)?-1:1;leftBits=leftBits.substring(1);var exponent=parseInt(leftBits.substring(0,this.ebits),2);return leftBits=leftBits.substring(this.ebits),exponent==eMax?0!==parseInt(leftBits,2)?NaN:sign*(1/0):(0===exponent?(exponent+=1,significand=parseInt(leftBits,2)):significand=parseInt("1"+leftBits,2),sign*significand*this.fbias*Math.pow(2,exponent-this.bias))}},{key:"packFloatBits_",value:function(buffer,index,sign,exp,fraction){var bits=[];bits.push(sign);for(var i=this.ebits;i>0;i-=1)bits[i]=exp%2?1:0,exp=Math.floor(exp/2);for(var len=bits.length,_i=this.fbits;_i>0;_i-=1)bits[len+_i]=fraction%2?1:0,fraction=Math.floor(fraction/2);for(var str=bits.join(""),numBytes=this.numBytes+index-1,k=index;numBytes>=index;)buffer[numBytes]=parseInt(str.substring(0,8),2),str=str.substring(8),numBytes--,k++;return k}},{key:"roundToEven",value:function(n){var w=Math.floor(n),f=n-w;return f<.5?w:f>.5?w+1:w%2?w+1:w}}]),IEEE754Buffer}(),UintBuffer=function(){function UintBuffer(bits){classCallCheck(this,UintBuffer),this.bits=bits,this.bytes=bits<8?1:Math.ceil(bits/8),this.max=Math.pow(2,bits)-1,this.min=0;var r=8-(1+(bits-1|7)-bits);this.lastByteMask_=Math.pow(2,r>0?r:8)-1}return createClass(UintBuffer,[{key:"pack",value:function(buffer,num){var index=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;if(num!=num)throw new Error("NaN");this.overflow(num),buffer[index]=255&(num<0?num+Math.pow(2,this.bits):num),index++;for(var len=this.bytes,i=2;i8&&(buffer[index]=Math.floor(num/Math.pow(2,8*(this.bytes-1)))&this.lastByteMask_,index++),index}},{key:"unpack",value:function(buffer){var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,num=this.unpackUnsafe(buffer,index);return this.overflow(num),num}},{key:"unpackUnsafe",value:function(buffer,index){for(var num=0,x=0;xthis.max||num2&&void 0!==arguments[2]?arguments[2]:0;return get(TwosComplementBuffer.prototype.__proto__||Object.getPrototypeOf(TwosComplementBuffer.prototype),"pack",this).call(this,buffer,num,index)}},{key:"unpack",value:function(buffer){var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,num=get(TwosComplementBuffer.prototype.__proto__||Object.getPrototypeOf(TwosComplementBuffer.prototype),"unpackUnsafe",this).call(this,buffer,index);return num=this.sign_(num),this.overflow(num),num}},{key:"sign_",value:function(num){return num>this.max&&(num-=2*this.max+2),num}}]),TwosComplementBuffer}(),NumberBuffer=function(){function NumberBuffer(bits,fp,signed){classCallCheck(this,NumberBuffer);var parser=void 0;fp?(!function(bits){if(!bits||16!==bits&&32!==bits&&64!==bits)throw new Error(TYPE_ERR+": float, bits: "+bits)}(bits),parser=this.getFPParser_(bits)):(!function(bits){if(!bits||bits<1||bits>53)throw new Error(TYPE_ERR+": int, bits: "+bits)}(bits),parser=signed?new TwosComplementBuffer(bits):new UintBuffer(bits)),this.parser=parser,this.offset=Math.ceil(bits/8)}return createClass(NumberBuffer,[{key:"unpack",value:function(buffer){var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;return this.parser.unpack(buffer,index)}},{key:"pack",value:function(buffer,num){var index=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return this.parser.pack(buffer,num,index)}},{key:"getFPParser_",value:function(bits){return 16===bits?new IEEE754Buffer(5,11):32===bits?new IEEE754Buffer(8,23):new IEEE754Buffer(11,52)}}]),NumberBuffer}();function throwValueError_(e,value,i,fp){throw fp||value!==1/0&&value!==-1/0&&value==value?new Error(e.message+" at input index "+i+": "+value):new Error("Argument is not a integer at input index "+i)}function unpackString(buffer){return function(buffer){for(var start=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,end=arguments.length>2&&void 0!==arguments[2]?arguments[2]:buffer.length,str="",index=start;index=0&&charCode<=127)str+=String.fromCharCode(charCode);else{var count=0;charCode>=194&&charCode<=223?count=1:charCode>=224&&charCode<=239?(count=2,224===buffer[index]&&(lowerBoundary=160),237===buffer[index]&&(upperBoundary=159)):charCode>=240&&charCode<=244?(count=3,240===buffer[index]&&(lowerBoundary=144),244===buffer[index]&&(upperBoundary=143)):replace=!0,charCode&=(1<<8-count-1)-1;for(var i=0;iupperBoundary)&&(replace=!0),charCode=charCode<<6|63&buffer[index],index++;replace?str+=String.fromCharCode(65533):charCode<=65535?str+=String.fromCharCode(charCode):(charCode-=65536,str+=String.fromCharCode(55296+(charCode>>10&1023),56320+(1023&charCode)))}}return str}(buffer,arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,arguments.length>2&&void 0!==arguments[2]?arguments[2]:buffer.length)}function packString(str){var buffer=[];return pack(str,buffer,0),buffer}function packStringTo(str,buffer){return pack(str,buffer,arguments.length>2&&void 0!==arguments[2]?arguments[2]:0)}function packArrayTo(values,theType,buffer){var index=arguments.length>3&&void 0!==arguments[3]?arguments[3]:0,packer=new NumberBuffer((theType=theType||{}).bits,theType.fp,theType.signed),i=0,start=index;try{for(var valuesLen=values.length;i3&&void 0!==arguments[3]?arguments[3]:0,end=arguments.length>4&&void 0!==arguments[4]?arguments[4]:buffer.length,safe=arguments.length>5&&void 0!==arguments[5]&&arguments[5],packer=new NumberBuffer((theType=theType||{}).bits,theType.fp,theType.signed),offset=packer.offset;end=function(buffer,start,end,offset,safe){var extra=(end-start)%offset;if(safe&&(extra||buffer.length3&&void 0!==arguments[3]?arguments[3]:0)}function pack$1(value,theType){var output=[];return packTo(value,theType,output),output}function unpackArray(buffer,theType){var output=[];return unpackArrayTo(buffer,theType,output,arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,arguments.length>3&&void 0!==arguments[3]?arguments[3]:buffer.length,arguments.length>4&&void 0!==arguments[4]&&arguments[4]),output}function unpack$1(buffer,theType){var index=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return unpackArray(buffer,theType,index,index+Math.ceil(theType.bits/8),!0)[0]}var RIFFFile=function(){function RIFFFile(){classCallCheck(this,RIFFFile),this.container="",this.chunkSize=0,this.format="",this.signature={},this.head=0,this.uInt32={bits:32,be:!1},this.supported_containers=["RIFF","RIFX"]}return createClass(RIFFFile,[{key:"setSignature",value:function(buffer){if(this.head=0,this.container=this.readString(buffer,4),-1===this.supported_containers.indexOf(this.container))throw Error("Not a supported format.");this.uInt32.be="RIFX"===this.container,this.chunkSize=this.readUInt32(buffer),this.format=this.readString(buffer,4),this.signature={chunkId:this.container,chunkSize:this.chunkSize,format:this.format,subChunks:this.getSubChunksIndex_(buffer)}}},{key:"findChunk",value:function(chunkId){for(var multiple=arguments.length>1&&void 0!==arguments[1]&&arguments[1],chunks=this.signature.subChunks,chunk=[],i=0;i1&&void 0!==arguments[1])||arguments[1];if(this.clearHeaders(),this.setSignature(wavBuffer),this.uInt16.be=this.uInt32.be,"WAVE"!=this.format)throw Error('Could not find the "WAVE" format identifier');this.readDs64Chunk_(wavBuffer),this.readFmtChunk_(wavBuffer),this.readFactChunk_(wavBuffer),this.readBextChunk_(wavBuffer),this.readCueChunk_(wavBuffer),this.readSmplChunk_(wavBuffer),this.readDataChunk_(wavBuffer,samples),this.readJunkChunk_(wavBuffer),this.readLISTChunk_(wavBuffer)}},{key:"clearHeaders",value:function(){var tmpWav=new WaveFileReader;Object.assign(this.fmt,tmpWav.fmt),Object.assign(this.fact,tmpWav.fact),Object.assign(this.cue,tmpWav.cue),Object.assign(this.smpl,tmpWav.smpl),Object.assign(this.bext,tmpWav.bext),Object.assign(this.ds64,tmpWav.ds64),Object.assign(this.data,tmpWav.data),this.LIST=[],Object.assign(this.junk,tmpWav.junk)}},{key:"readFmtChunk_",value:function(buffer){var chunk=this.findChunk("fmt ");if(!chunk)throw Error('Could not find the "fmt " chunk');this.head=chunk.chunkData.start,this.fmt.chunkId=chunk.chunkId,this.fmt.chunkSize=chunk.chunkSize,this.fmt.audioFormat=this.readUInt16_(buffer),this.fmt.numChannels=this.readUInt16_(buffer),this.fmt.sampleRate=this.readUInt32(buffer),this.fmt.byteRate=this.readUInt32(buffer),this.fmt.blockAlign=this.readUInt16_(buffer),this.fmt.bitsPerSample=this.readUInt16_(buffer),this.readFmtExtension_(buffer)}},{key:"readFmtExtension_",value:function(buffer){this.fmt.chunkSize>16&&(this.fmt.cbSize=this.readUInt16_(buffer),this.fmt.chunkSize>18&&(this.fmt.validBitsPerSample=this.readUInt16_(buffer),this.fmt.chunkSize>20&&(this.fmt.dwChannelMask=this.readUInt32(buffer),this.fmt.subformat=[this.readUInt32(buffer),this.readUInt32(buffer),this.readUInt32(buffer),this.readUInt32(buffer)])))}},{key:"readFactChunk_",value:function(buffer){var chunk=this.findChunk("fact");chunk&&(this.head=chunk.chunkData.start,this.fact.chunkId=chunk.chunkId,this.fact.chunkSize=chunk.chunkSize,this.fact.dwSampleLength=this.readUInt32(buffer))}},{key:"readCueChunk_",value:function(buffer){var chunk=this.findChunk("cue ");if(chunk){this.head=chunk.chunkData.start,this.cue.chunkId=chunk.chunkId,this.cue.chunkSize=chunk.chunkSize,this.cue.dwCuePoints=this.readUInt32(buffer);for(var i=0;i-1){this.head=subChunk.chunkData.start;var item={chunkId:subChunk.chunkId,chunkSize:subChunk.chunkSize,dwName:this.readUInt32(buffer)};"ltxt"==subChunk.chunkId&&(item.dwSampleLength=this.readUInt32(buffer),item.dwPurposeID=this.readUInt32(buffer),item.dwCountry=this.readUInt16_(buffer),item.dwLanguage=this.readUInt16_(buffer),item.dwDialect=this.readUInt16_(buffer),item.dwCodePage=this.readUInt16_(buffer)),item.value=this.readZSTR_(buffer,this.head),this.LIST[this.LIST.length-1].subChunks.push(item)}}else"INFO"==format&&(this.head=subChunk.chunkData.start,this.LIST[this.LIST.length-1].subChunks.push({chunkId:subChunk.chunkId,chunkSize:subChunk.chunkSize,value:this.readZSTR_(buffer,this.head)}))}},{key:"readJunkChunk_",value:function(buffer){var chunk=this.findChunk("junk");chunk&&(this.junk={chunkId:chunk.chunkId,chunkSize:chunk.chunkSize,chunkData:[].slice.call(buffer.slice(chunk.chunkData.start,chunk.chunkData.end))})}},{key:"readZSTR_",value:function(bytes){for(var index=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,i=index;i2&&void 0!==arguments[2])||arguments[2],packedString=packString(str);if(fixedSize)for(var i=packedString.length;i16&&(extension=extension.concat(pack$1(this.fmt.cbSize,this.uInt16))),this.fmt.chunkSize>18&&(extension=extension.concat(pack$1(this.fmt.validBitsPerSample,this.uInt16))),this.fmt.chunkSize>20&&(extension=extension.concat(pack$1(this.fmt.dwChannelMask,this.uInt32))),this.fmt.chunkSize>24&&(extension=extension.concat(pack$1(this.fmt.subformat[0],this.uInt32),pack$1(this.fmt.subformat[1],this.uInt32),pack$1(this.fmt.subformat[2],this.uInt32),pack$1(this.fmt.subformat[3],this.uInt32))),extension}},{key:"getLISTBytes_",value:function(){for(var bytes=[],i=0;i-1?(bytes=bytes.concat(packString(subChunks[i].chunkId),pack$1(subChunks[i].value.length+4+1,this.uInt32),pack$1(subChunks[i].dwName,this.uInt32),writeString(subChunks[i].value,subChunks[i].value.length))).push(0):"ltxt"==subChunks[i].chunkId&&(bytes=bytes.concat(this.getLtxtChunkBytes_(subChunks[i])))),bytes.length%2&&bytes.push(0);return bytes}},{key:"getLtxtChunkBytes_",value:function(ltxt){return[].concat(packString(ltxt.chunkId),pack$1(ltxt.value.length+20,this.uInt32),pack$1(ltxt.dwName,this.uInt32),pack$1(ltxt.dwSampleLength,this.uInt32),pack$1(ltxt.dwPurposeID,this.uInt32),pack$1(ltxt.dwCountry,this.uInt16),pack$1(ltxt.dwLanguage,this.uInt16),pack$1(ltxt.dwDialect,this.uInt16),pack$1(ltxt.dwCodePage,this.uInt16),writeString(ltxt.value,ltxt.value.length))}},{key:"getJunkBytes_",value:function(){var bytes=[];return this.junk.chunkId?bytes.concat(packString(this.junk.chunkId),pack$1(this.junk.chunkData.length,this.uInt32),this.junk.chunkData):bytes}}]),WaveFileParser}();var WaveFileCreator=function(_WaveFileParser){function WaveFileCreator(){classCallCheck(this,WaveFileCreator);var _this=possibleConstructorReturn(this,(WaveFileCreator.__proto__||Object.getPrototypeOf(WaveFileCreator)).call(this));return _this.bitDepth="0",_this.dataType={},_this.WAV_AUDIO_FORMATS={4:17,8:1,"8a":6,"8m":7,16:1,24:1,32:1,"32f":3,64:3},_this}return inherits(WaveFileCreator,WaveFileParser),createClass(WaveFileCreator,[{key:"fromScratch",value:function(numChannels,sampleRate,bitDepthCode,samples){var options=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{};this.clearHeaders(),this.newWavFile_(numChannels,sampleRate,bitDepthCode,samples,options)}},{key:"newWavFile_",value:function(numChannels,sampleRate,bitDepthCode,samples){var options=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{};options.container||(options.container="RIFF"),this.container=options.container,this.bitDepth=bitDepthCode,samples=function(samples){if(samples.length>0&&samples[0].constructor===Array){for(var finalSamples=[],i=0,len=samples[0].length;i1&&void 0!==arguments[1])||arguments[1];get(WaveFileCreator.prototype.__proto__||Object.getPrototypeOf(WaveFileCreator.prototype),"fromBuffer",this).call(this,wavBuffer,samples),this.bitDepthFromFmt_(),this.updateDataType_()}},{key:"toBuffer",value:function(){return this.validateWavHeader_(),get(WaveFileCreator.prototype.__proto__||Object.getPrototypeOf(WaveFileCreator.prototype),"toBuffer",this).call(this)}},{key:"getSample",value:function(index){if((index*=this.dataType.bits/8)+this.dataType.bits/8>this.data.samples.length)throw new Error("Range error");return unpack$1(this.data.samples.slice(index,index+this.dataType.bits/8),this.dataType)}},{key:"setSample",value:function(index,sample){if((index*=this.dataType.bits/8)+this.dataType.bits/8>this.data.samples.length)throw new Error("Range error");packTo(sample,this.dataType,this.data.samples,index)}},{key:"makeWavHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){"4"==bitDepthCode?this.createADPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options):"8a"==bitDepthCode||"8m"==bitDepthCode?this.createALawMulawHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options):-1==Object.keys(this.WAV_AUDIO_FORMATS).indexOf(bitDepthCode)||numChannels>2?this.createExtensibleHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options):this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options)}},{key:"createPCMHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.container=options.container,this.chunkSize=36+samplesLength,this.format="WAVE",this.bitDepth=bitDepthCode,this.fmt={chunkId:"fmt ",chunkSize:16,audioFormat:this.WAV_AUDIO_FORMATS[bitDepthCode]||65534,numChannels:numChannels,sampleRate:sampleRate,byteRate:numChannels*numBytes*sampleRate,blockAlign:numChannels*numBytes,bitsPerSample:parseInt(bitDepthCode,10),cbSize:0,validBitsPerSample:0,dwChannelMask:0,subformat:[]}}},{key:"createADPCMHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options),this.chunkSize=40+samplesLength,this.fmt.chunkSize=20,this.fmt.byteRate=4055,this.fmt.blockAlign=256,this.fmt.bitsPerSample=4,this.fmt.cbSize=2,this.fmt.validBitsPerSample=505,this.fact={chunkId:"fact",chunkSize:4,dwSampleLength:2*samplesLength}}},{key:"createExtensibleHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options),this.chunkSize=60+samplesLength,this.fmt.chunkSize=40,this.fmt.bitsPerSample=1+(parseInt(bitDepthCode,10)-1|7),this.fmt.cbSize=22,this.fmt.validBitsPerSample=parseInt(bitDepthCode,10),this.fmt.dwChannelMask=function(numChannels){var mask=0;return 1===numChannels?mask=4:2===numChannels?mask=3:4===numChannels?mask=51:6===numChannels?mask=63:8===numChannels&&(mask=1599),mask}(numChannels),this.fmt.subformat=[1,1048576,2852126848,1905997824]}},{key:"createALawMulawHeader_",value:function(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options){this.createPCMHeader_(bitDepthCode,numChannels,sampleRate,numBytes,samplesLength,options),this.chunkSize=40+samplesLength,this.fmt.chunkSize=20,this.fmt.cbSize=2,this.fmt.validBitsPerSample=8,this.fact={chunkId:"fact",chunkSize:4,dwSampleLength:samplesLength}}},{key:"bitDepthFromFmt_",value:function(){3===this.fmt.audioFormat&&32===this.fmt.bitsPerSample?this.bitDepth="32f":6===this.fmt.audioFormat?this.bitDepth="8a":7===this.fmt.audioFormat?this.bitDepth="8m":this.bitDepth=this.fmt.bitsPerSample.toString()}},{key:"validateBitDepth_",value:function(){if(!this.WAV_AUDIO_FORMATS[this.bitDepth]){if(parseInt(this.bitDepth,10)>8&&parseInt(this.bitDepth,10)<54)return!0;throw new Error("Invalid bit depth.")}return!0}},{key:"updateDataType_",value:function(){this.dataType={bits:1+(parseInt(this.bitDepth,10)-1|7),fp:"32f"==this.bitDepth||"64"==this.bitDepth,signed:"8"!=this.bitDepth,be:"RIFX"==this.container},["4","8a","8m"].indexOf(this.bitDepth)>-1&&(this.dataType.bits=8,this.dataType.signed=!1)}},{key:"validateWavHeader_",value:function(){if(this.validateBitDepth_(),channels=this.fmt.numChannels,bits=this.fmt.bitsPerSample,channels<1||channels*bits/8>65535)throw new Error("Invalid number of channels.");var channels,bits;if(!function(channels,bits,sampleRate){return!(sampleRate<1||channels*(bits/8)*sampleRate>4294967295)}(this.fmt.numChannels,this.fmt.bitsPerSample,this.fmt.sampleRate))throw new Error("Invalid sample rate.")}}]),WaveFileCreator}();var WaveFileMetaEditor=function(_WaveFileCreator){function WaveFileMetaEditor(){return classCallCheck(this,WaveFileMetaEditor),possibleConstructorReturn(this,(WaveFileMetaEditor.__proto__||Object.getPrototypeOf(WaveFileMetaEditor)).apply(this,arguments))}return inherits(WaveFileMetaEditor,WaveFileCreator),createClass(WaveFileMetaEditor,[{key:"getTag",value:function(tag){var index=this.getTagIndex_(tag);return null!==index.TAG?this.LIST[index.LIST].subChunks[index.TAG].value:null}},{key:"setTag",value:function(tag,value){tag=function(tag){if(tag.constructor!==String)throw new Error("Invalid tag name.");if(tag.length<4)for(var i=0,len=4-tag.length;i1&&void 0!==arguments[1]?arguments[1]:"";this.cue.chunkId="cue ",position=position*this.fmt.sampleRate/1e3;var existingPoints=this.getCuePoints_();this.clearLISTadtl_();var len=this.cue.points.length;this.cue.points=[];var hasSet=!1;if(0===len)this.setCuePoint_(position,1,labl);else{for(var i=0;iposition&&!hasSet?(this.setCuePoint_(position,i+1,labl),this.setCuePoint_(existingPoints[i].dwPosition,i+2,existingPoints[i].label),hasSet=!0):this.setCuePoint_(existingPoints[i].dwPosition,i+1,existingPoints[i].label);hasSet||this.setCuePoint_(position,this.cue.points.length+1,labl)}this.cue.dwCuePoints=this.cue.points.length}},{key:"deleteCuePoint",value:function(index){this.cue.chunkId="cue ";var existingPoints=this.getCuePoints_();this.clearLISTadtl_();var len=this.cue.points.length;this.cue.points=[];for(var i=0;i0&&void 0!==arguments[0]?arguments[0]:"16";this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,"16",function(adpcmSamples){for(var blockAlign=arguments.length>1&&void 0!==arguments[1]?arguments[1]:256,samples=new Int16Array(2*adpcmSamples.length),block=[],fileIndex=0,i=0;i0&&void 0!==arguments[0]?arguments[0]:"16";this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,"16",function(samples){for(var pcmSamples=new Int16Array(samples.length),i=0;i>8&128)&&(sample=-sample),sample>CLIP&&(sample=CLIP),~(sign|(exponent=encodeTable[(sample+=BIAS)>>7&255])<<4|sample>>exponent+3&15));return muLawSamples}(output),{container:this.correctContainer_()})}},{key:"fromMuLaw",value:function(){var bitDepthCode=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"16";this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,"16",function(samples){for(var muLawSample,sign,exponent,mantissa,sample,pcmSamples=new Int16Array(samples.length),i=0;i>4&7]+(mantissa<1&&void 0!==arguments[1])||arguments[1],toBitDepth=newBitDepth,thisBitDepth=this.bitDepth;changeResolution||("32f"!=newBitDepth&&(toBitDepth=this.dataType.bits.toString()),thisBitDepth=this.dataType.bits),this.assureUncompressed_();var sampleCount=this.data.samples.length/(this.dataType.bits/8),typedSamplesInput=new Float64Array(sampleCount),typedSamplesOutput=new Float64Array(sampleCount);unpackArrayTo(this.data.samples,this.dataType,typedSamplesInput),"32f"!=thisBitDepth&&"64"!=thisBitDepth||function(samples){for(var i=0,len=samples.length;i1?samples[i]=1:samples[i]<-1&&(samples[i]=-1)}(typedSamplesInput),bitDepth(typedSamplesInput,thisBitDepth,toBitDepth,typedSamplesOutput),this.fromExisting_(this.fmt.numChannels,this.fmt.sampleRate,newBitDepth,typedSamplesOutput,{container:this.correctContainer_()})}},{key:"assure16Bit_",value:function(){this.assureUncompressed_(),"16"!=this.bitDepth&&this.toBitDepth("16")}},{key:"assureUncompressed_",value:function(){"8a"==this.bitDepth?this.fromALaw():"8m"==this.bitDepth?this.fromMuLaw():"4"==this.bitDepth&&this.fromIMAADPCM()}},{key:"correctContainer_",value:function(){return"RF64"==this.container?"RIFF":this.container}},{key:"fromExisting_",value:function(numChannels,sampleRate,bitDepthCode,samples){var options=arguments.length>4&&void 0!==arguments[4]?arguments[4]:{},tmpWav=new WaveFileMetaEditor;Object.assign(this.fmt,tmpWav.fmt),Object.assign(this.fact,tmpWav.fact),Object.assign(this.ds64,tmpWav.ds64),Object.assign(this.data,tmpWav.data),this.newWavFile_(numChannels,sampleRate,bitDepthCode,samples,options)}},{key:"outputSize_",value:function(){var outputSize=this.data.samples.length/2;return outputSize%2&&outputSize++,outputSize}}]),WaveFileConverter}();return function(_WaveFileConverter){function WaveFile(){var wavBuffer=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;classCallCheck(this,WaveFile);var _this=possibleConstructorReturn(this,(WaveFile.__proto__||Object.getPrototypeOf(WaveFile)).call(this));return wavBuffer&&_this.fromBuffer(wavBuffer),_this}return inherits(WaveFile,WaveFileConverter),createClass(WaveFile,[{key:"fromBase64",value:function(base64String){this.fromBuffer(new Uint8Array(function(base64){var len=base64.length,bufferLength=.75*base64.length,p=0,encoded1=void 0,encoded2=void 0,encoded3=void 0,encoded4=void 0;"="===base64[base64.length-1]&&(bufferLength--,"="===base64[base64.length-2]&&bufferLength--);for(var arraybuffer=new ArrayBuffer(bufferLength),bytes=new Uint8Array(arraybuffer),_i2=0;_i2>4,bytes[p++]=(15&encoded2)<<4|encoded3>>2,bytes[p++]=(3&encoded3)<<6|63&encoded4;return arraybuffer}(base64String)))}},{key:"toBase64",value:function(){var buffer=this.toBuffer();return function(arraybuffer,byteOffset,length){for(var bytes=new Uint8Array(arraybuffer,byteOffset,length),len=bytes.length,base64="",_i=0;_i>2],base64+=chars[(3&bytes[_i])<<4|bytes[_i+1]>>4],base64+=chars[(15&bytes[_i+1])<<2|bytes[_i+2]>>6],base64+=chars[63&bytes[_i+2]];return len%3==2?base64=base64.substring(0,base64.length-1)+"=":len%3==1&&(base64=base64.substring(0,base64.length-2)+"=="),base64}(buffer,0,buffer.length)}},{key:"toDataURI",value:function(){return"data:audio/wav;base64,"+this.toBase64()}},{key:"fromDataURI",value:function(dataURI){this.fromBase64(dataURI.replace("data:audio/wav;base64,",""))}}]),WaveFile}()}); diff --git a/docs/index.html b/docs/index.html index ee9fbb2..3c3bf9d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -1423,7 +1423,7 @@


- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/index.js.html b/docs/index.js.html index 97378da..73e7235 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -142,7 +142,7 @@

index.js


- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/lib_wavefile-converter.js.html b/docs/lib_wavefile-converter.js.html index 13505a0..6be3c53 100644 --- a/docs/lib_wavefile-converter.js.html +++ b/docs/lib_wavefile-converter.js.html @@ -344,7 +344,7 @@

lib/wavefile-converter.js


- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/lib_wavefile-creator.js.html b/docs/lib_wavefile-creator.js.html index 558775d..2b1224d 100644 --- a/docs/lib_wavefile-creator.js.html +++ b/docs/lib_wavefile-creator.js.html @@ -451,7 +451,7 @@

lib/wavefile-creator.js


- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/lib_wavefile-parser.js.html b/docs/lib_wavefile-parser.js.html index 5887074..de5fc9f 100644 --- a/docs/lib_wavefile-parser.js.html +++ b/docs/lib_wavefile-parser.js.html @@ -469,7 +469,7 @@

lib/wavefile-parser.js


- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/lib_wavefile-reader.js.html b/docs/lib_wavefile-reader.js.html index b957b27..87e5809 100644 --- a/docs/lib_wavefile-reader.js.html +++ b/docs/lib_wavefile-reader.js.html @@ -668,7 +668,7 @@

lib/wavefile-reader.js


- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/module-wavefile.html b/docs/module-wavefile.html index 3491074..0eab73d 100644 --- a/docs/module-wavefile.html +++ b/docs/module-wavefile.html @@ -21072,7 +21072,7 @@

toRIFX
- Documentation generated by JSDoc 3.6.3 on Wed Sep 11 2019 13:07:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.6.3 on Thu Sep 12 2019 12:46:01 GMT-0300 (Hora oficial do Brasil) using the docdash theme.