diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b79e89..a66f28b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## version 8.4.4 (2018-08-09) +- Fix: browser directive in package.json + ## version 8.4.3 (2018-08-07) - Fix: Stop adding a extra sample when changing the bit depth - Fix: Add padding byte if sample buffer lenght is odd diff --git a/dist/wavefile.cjs.js b/dist/wavefile.cjs.js index 8b73529..c9ccefa 100644 --- a/dist/wavefile.cjs.js +++ b/dist/wavefile.cjs.js @@ -1112,9 +1112,9 @@ const TYPE_NAN = 'Argument is not a valid number'; * @throws {Error} If the value is not Number or Boolean. */ function validateIsNumber(value) { - if (value === undefined || value === null) { + if (typeof value === 'undefined' || value === null) { throw new Error(TYPE_NAN); - } else if (value.constructor != Number && value.constructor != Boolean) { + } else if (value.constructor !== Number && value.constructor !== Boolean) { throw new Error(TYPE_NAN); } } @@ -1632,6 +1632,8 @@ class NumberBuffer { } /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */ this.parser = parser; + /** @type {number} */ + this.offset = Math.ceil(bits / 8); } /** @@ -1699,6 +1701,38 @@ class NumberBuffer { * */ +/** + * Throw a value error. + * @throws {Error} A Error with a message based on the input params. + */ +function throwValueError_(e, value, i, fp) { + if (!fp && ( + value === Infinity || value === -Infinity || value !== value)) { + throw new Error('Argument is not a integer at input index ' + i); + } else { + throw new Error(e.message + ' at input index ' + i + ': ' + value); + } +} + +/** + * Unpack a array of numbers to a typed array. + * All other unpacking functions are interfaces to this function. + * @param {!Uint8Array|!Array} buffer The byte buffer. + * @param {number=} start The buffer index to start reading. + * @param {number=} end The buffer index to stop reading. + * @param {number=} offset The number of bytes used by the type. + * @param {boolean=} safe True for size-safe buffer reading. + * @throws {Error} On bad buffer length, if safe. + */ +function getUnpackLen_(buffer, start, end, offset, safe) { + /** @type {number} */ + let extra = (end - start) % offset; + if (safe && (extra || buffer.length < offset)) { + throw new Error('Bad buffer length'); + } + return end - extra; +} + /** * Read a string of UTF-8 characters from a byte buffer. * @param {!Uint8Array|!Array} buffer A byte buffer. @@ -1736,37 +1770,9 @@ function packStringTo(str, buffer, index=0) { } // Numbers -/** - * Pack a number as a byte buffer. - * @param {number} value The number. - * @param {!Object} theType The type definition. - * @return {!Array} The packed value. - * @throws {Error} If the type definition is not valid. - * @throws {Error} If the value is not valid. - */ -function pack$1(value, theType) { - /** @type {!Array} */ - let output = []; - packTo(value, theType, output); - return output; -} - -/** - * Pack a number to a byte buffer. - * @param {number} value The value. - * @param {!Object} theType The type definition. - * @param {!Uint8Array|!Array} buffer The output buffer. - * @param {number=} index The buffer index to write. Assumes 0 if undefined. - * @return {number} The next index to write. - * @throws {Error} If the type definition is not valid. - * @throws {Error} If the value is not valid. - */ -function packTo(value, theType, buffer, index=0) { - return packArrayTo([value], theType, buffer, index); -} - /** * Pack a array of numbers to a byte buffer. + * All other packing functions are interfaces to this function. * @param {!Array|!TypedArray} values The value. * @param {!Object} theType The type definition. * @param {!Uint8Array|!Array} buffer The output buffer. @@ -1779,48 +1785,98 @@ function packTo(value, theType, buffer, index=0) { function packArrayTo(values, theType, buffer, index=0) { theType = theType || {}; /** @type {NumberBuffer} */ - let packer = new NumberBuffer( - theType.bits, theType.fp, theType.signed); - /** @type {number} */ - let offset = Math.ceil(theType.bits / 8); + let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed); /** @type {number} */ let i = 0; + /** @type {number} */ + let start = index; try { for (let valuesLen = values.length; i < valuesLen; i++) { validateIsNumber(values[i]); - /** @type {number} */ - let len = index + offset; - while (index < len) { - index = packer.pack(buffer, values[i], index); - } - swap_(theType.be, buffer, offset, index - offset, index); + index = packer.pack(buffer, values[i], index); } - } catch (e) { - /** @type {*} */ - let value = values[i]; - if (!theType.fp && ( - value === Infinity || value === -Infinity || value !== value)) { - throw new Error('Argument is not a integer at input index ' + i); - } else { - throw new Error(e.message + ' at input index ' + i); + if (theType.be) { + endianness(buffer, packer.offset, start, index); } + } catch (e) { + throwValueError_(e, values[i], i, theType.fp); } return index; } /** - * Unpack a number from a byte buffer. + * Unpack a array of numbers to a typed array. + * All other unpacking functions are interfaces to this function. * @param {!Uint8Array|!Array} buffer The byte buffer. * @param {!Object} theType The type definition. - * @param {number=} index The buffer index to read. Assumes zero if undefined. - * @return {number} + * @param {!TypedArray|!Array} output The output array. + * @param {number=} start The buffer index to start reading. + * Assumes zero if undefined. + * @param {number=} end The buffer index to stop reading. + * Assumes the buffer length if undefined. + * @param {boolean=} safe If set to false, extra bytes in the end of + * the array are ignored and input buffers with insufficient bytes will + * write nothing to the output array. If safe is set to true the function + * will throw a 'Bad buffer length' error. Defaults to false. * @throws {Error} If the type definition is not valid - * @throws {Error} On bad buffer length. * @throws {Error} On overflow */ -function unpack$1(buffer, theType, index=0) { - return unpackArray( - buffer, theType, index, index + Math.ceil(theType.bits / 8), true)[0]; +function unpackArrayTo( + buffer, theType, output, start=0, end=buffer.length, safe=false) { + theType = theType || {}; + /** @type {NumberBuffer} */ + let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed); + /** @type {number} */ + let offset = packer.offset; + // getUnpackLen_ will either fix the length of the input buffer + // according to the byte offset of the type (on unsafe mode) or + // throw a Error if the input buffer has a bad length (on safe mode) + end = getUnpackLen_(buffer, start, end, offset, safe); + /** @type {number} */ + let index = 0; + let j = start; + try { + if (theType.be) { + endianness(buffer, offset, start, end); + } + for (; j < end; j += offset, index++) { + output[index] = packer.unpack(buffer, j); + } + if (theType.be) { + endianness(buffer, offset, start, end); + } + } catch (e) { + throwValueError_(e, buffer.slice(j, j + offset), j, theType.fp); + } +} + +/** + * Pack a number to a byte buffer. + * @param {number} value The value. + * @param {!Object} theType The type definition. + * @param {!Uint8Array|!Array} buffer The output buffer. + * @param {number=} index The buffer index to write. Assumes 0 if undefined. + * @return {number} The next index to write. + * @throws {Error} If the type definition is not valid. + * @throws {Error} If the value is not valid. + */ +function packTo(value, theType, buffer, index=0) { + return packArrayTo([value], theType, buffer, index); +} + +/** + * Pack a number as a byte buffer. + * @param {number} value The number. + * @param {!Object} theType The type definition. + * @return {!Array} The packed value. + * @throws {Error} If the type definition is not valid. + * @throws {Error} If the value is not valid. + */ +function pack$1(value, theType) { + /** @type {!Array} */ + let output = []; + packTo(value, theType, output); + return output; } /** @@ -1848,62 +1904,18 @@ function unpackArray( } /** - * Unpack a array of numbers to a typed array. + * Unpack a number from a byte buffer. * @param {!Uint8Array|!Array} buffer The byte buffer. * @param {!Object} theType The type definition. - * @param {!TypedArray|!Array} output The output array. - * @param {number=} start The buffer index to start reading. - * Assumes zero if undefined. - * @param {number=} end The buffer index to stop reading. - * Assumes the buffer length if undefined. - * @param {boolean=} safe If set to false, extra bytes in the end of - * the array are ignored and input buffers with insufficient bytes will - * write nothing to the output array. If safe is set to true the function - * will throw a 'Bad buffer length' error. Defaults to false. + * @param {number=} index The buffer index to read. Assumes zero if undefined. + * @return {number} * @throws {Error} If the type definition is not valid + * @throws {Error} On bad buffer length. * @throws {Error} On overflow */ -function unpackArrayTo( - buffer, theType, output, start=0, end=buffer.length, safe=false) { - theType = theType || {}; - /** @type {NumberBuffer} */ - let packer = new NumberBuffer( - theType.bits, theType.fp, theType.signed); - /** @type {number} */ - let offset = Math.ceil(theType.bits / 8); - /** @type {number} */ - let extra = (end - start) % offset; - if (safe && (extra || buffer.length < offset)) { - throw new Error('Bad buffer length'); - } - end -= extra; - /** @type {number} */ - let i = 0; - try { - swap_(theType.be, buffer, offset, start, end); - for (let j = start; j < end; j += offset, i++) { - output[i] = packer.unpack(buffer, j); - } - swap_(theType.be, buffer, offset, start, end); - } catch (e) { - throw new Error(e.message + ' at output index ' + i); - } -} - -/** - * Swap endianness in a slice of an array when flip == true. - * @param {boolean} flip True if should swap endianness. - * @param {!Uint8Array|!Array} buffer The buffer. - * @param {number} offset The number of bytes each value use. - * @param {number} start The buffer index to start the swap. - * @param {number} end The buffer index to end the swap. - * @throws {Error} On bad buffer length for the swap. - * @private - */ -function swap_(flip, buffer, offset, start, end) { - if (flip) { - endianness(buffer, offset, start, end); - } +function unpack$1(buffer, theType, index=0) { + return unpackArray( + buffer, theType, index, index + Math.ceil(theType.bits / 8), true)[0]; } /* diff --git a/dist/wavefile.js b/dist/wavefile.js index 1953a65..14dca59 100644 --- a/dist/wavefile.js +++ b/dist/wavefile.js @@ -1110,9 +1110,9 @@ const TYPE_NAN = 'Argument is not a valid number'; * @throws {Error} If the value is not Number or Boolean. */ function validateIsNumber(value) { - if (value === undefined || value === null) { + if (typeof value === 'undefined' || value === null) { throw new Error(TYPE_NAN); - } else if (value.constructor != Number && value.constructor != Boolean) { + } else if (value.constructor !== Number && value.constructor !== Boolean) { throw new Error(TYPE_NAN); } } @@ -1630,6 +1630,8 @@ class NumberBuffer { } /** @type {TwosComplementBuffer|UintBuffer|IEEE754Buffer} */ this.parser = parser; + /** @type {number} */ + this.offset = Math.ceil(bits / 8); } /** @@ -1697,6 +1699,38 @@ class NumberBuffer { * */ +/** + * Throw a value error. + * @throws {Error} A Error with a message based on the input params. + */ +function throwValueError_(e, value, i, fp) { + if (!fp && ( + value === Infinity || value === -Infinity || value !== value)) { + throw new Error('Argument is not a integer at input index ' + i); + } else { + throw new Error(e.message + ' at input index ' + i + ': ' + value); + } +} + +/** + * Unpack a array of numbers to a typed array. + * All other unpacking functions are interfaces to this function. + * @param {!Uint8Array|!Array} buffer The byte buffer. + * @param {number=} start The buffer index to start reading. + * @param {number=} end The buffer index to stop reading. + * @param {number=} offset The number of bytes used by the type. + * @param {boolean=} safe True for size-safe buffer reading. + * @throws {Error} On bad buffer length, if safe. + */ +function getUnpackLen_(buffer, start, end, offset, safe) { + /** @type {number} */ + let extra = (end - start) % offset; + if (safe && (extra || buffer.length < offset)) { + throw new Error('Bad buffer length'); + } + return end - extra; +} + /** * Read a string of UTF-8 characters from a byte buffer. * @param {!Uint8Array|!Array} buffer A byte buffer. @@ -1734,37 +1768,9 @@ function packStringTo(str, buffer, index=0) { } // Numbers -/** - * Pack a number as a byte buffer. - * @param {number} value The number. - * @param {!Object} theType The type definition. - * @return {!Array} The packed value. - * @throws {Error} If the type definition is not valid. - * @throws {Error} If the value is not valid. - */ -function pack$1(value, theType) { - /** @type {!Array} */ - let output = []; - packTo(value, theType, output); - return output; -} - -/** - * Pack a number to a byte buffer. - * @param {number} value The value. - * @param {!Object} theType The type definition. - * @param {!Uint8Array|!Array} buffer The output buffer. - * @param {number=} index The buffer index to write. Assumes 0 if undefined. - * @return {number} The next index to write. - * @throws {Error} If the type definition is not valid. - * @throws {Error} If the value is not valid. - */ -function packTo(value, theType, buffer, index=0) { - return packArrayTo([value], theType, buffer, index); -} - /** * Pack a array of numbers to a byte buffer. + * All other packing functions are interfaces to this function. * @param {!Array|!TypedArray} values The value. * @param {!Object} theType The type definition. * @param {!Uint8Array|!Array} buffer The output buffer. @@ -1777,48 +1783,98 @@ function packTo(value, theType, buffer, index=0) { function packArrayTo(values, theType, buffer, index=0) { theType = theType || {}; /** @type {NumberBuffer} */ - let packer = new NumberBuffer( - theType.bits, theType.fp, theType.signed); - /** @type {number} */ - let offset = Math.ceil(theType.bits / 8); + let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed); /** @type {number} */ let i = 0; + /** @type {number} */ + let start = index; try { for (let valuesLen = values.length; i < valuesLen; i++) { validateIsNumber(values[i]); - /** @type {number} */ - let len = index + offset; - while (index < len) { - index = packer.pack(buffer, values[i], index); - } - swap_(theType.be, buffer, offset, index - offset, index); + index = packer.pack(buffer, values[i], index); } - } catch (e) { - /** @type {*} */ - let value = values[i]; - if (!theType.fp && ( - value === Infinity || value === -Infinity || value !== value)) { - throw new Error('Argument is not a integer at input index ' + i); - } else { - throw new Error(e.message + ' at input index ' + i); + if (theType.be) { + endianness(buffer, packer.offset, start, index); } + } catch (e) { + throwValueError_(e, values[i], i, theType.fp); } return index; } /** - * Unpack a number from a byte buffer. + * Unpack a array of numbers to a typed array. + * All other unpacking functions are interfaces to this function. * @param {!Uint8Array|!Array} buffer The byte buffer. * @param {!Object} theType The type definition. - * @param {number=} index The buffer index to read. Assumes zero if undefined. - * @return {number} + * @param {!TypedArray|!Array} output The output array. + * @param {number=} start The buffer index to start reading. + * Assumes zero if undefined. + * @param {number=} end The buffer index to stop reading. + * Assumes the buffer length if undefined. + * @param {boolean=} safe If set to false, extra bytes in the end of + * the array are ignored and input buffers with insufficient bytes will + * write nothing to the output array. If safe is set to true the function + * will throw a 'Bad buffer length' error. Defaults to false. * @throws {Error} If the type definition is not valid - * @throws {Error} On bad buffer length. * @throws {Error} On overflow */ -function unpack$1(buffer, theType, index=0) { - return unpackArray( - buffer, theType, index, index + Math.ceil(theType.bits / 8), true)[0]; +function unpackArrayTo( + buffer, theType, output, start=0, end=buffer.length, safe=false) { + theType = theType || {}; + /** @type {NumberBuffer} */ + let packer = new NumberBuffer(theType.bits, theType.fp, theType.signed); + /** @type {number} */ + let offset = packer.offset; + // getUnpackLen_ will either fix the length of the input buffer + // according to the byte offset of the type (on unsafe mode) or + // throw a Error if the input buffer has a bad length (on safe mode) + end = getUnpackLen_(buffer, start, end, offset, safe); + /** @type {number} */ + let index = 0; + let j = start; + try { + if (theType.be) { + endianness(buffer, offset, start, end); + } + for (; j < end; j += offset, index++) { + output[index] = packer.unpack(buffer, j); + } + if (theType.be) { + endianness(buffer, offset, start, end); + } + } catch (e) { + throwValueError_(e, buffer.slice(j, j + offset), j, theType.fp); + } +} + +/** + * Pack a number to a byte buffer. + * @param {number} value The value. + * @param {!Object} theType The type definition. + * @param {!Uint8Array|!Array} buffer The output buffer. + * @param {number=} index The buffer index to write. Assumes 0 if undefined. + * @return {number} The next index to write. + * @throws {Error} If the type definition is not valid. + * @throws {Error} If the value is not valid. + */ +function packTo(value, theType, buffer, index=0) { + return packArrayTo([value], theType, buffer, index); +} + +/** + * Pack a number as a byte buffer. + * @param {number} value The number. + * @param {!Object} theType The type definition. + * @return {!Array} The packed value. + * @throws {Error} If the type definition is not valid. + * @throws {Error} If the value is not valid. + */ +function pack$1(value, theType) { + /** @type {!Array} */ + let output = []; + packTo(value, theType, output); + return output; } /** @@ -1846,62 +1902,18 @@ function unpackArray( } /** - * Unpack a array of numbers to a typed array. + * Unpack a number from a byte buffer. * @param {!Uint8Array|!Array} buffer The byte buffer. * @param {!Object} theType The type definition. - * @param {!TypedArray|!Array} output The output array. - * @param {number=} start The buffer index to start reading. - * Assumes zero if undefined. - * @param {number=} end The buffer index to stop reading. - * Assumes the buffer length if undefined. - * @param {boolean=} safe If set to false, extra bytes in the end of - * the array are ignored and input buffers with insufficient bytes will - * write nothing to the output array. If safe is set to true the function - * will throw a 'Bad buffer length' error. Defaults to false. + * @param {number=} index The buffer index to read. Assumes zero if undefined. + * @return {number} * @throws {Error} If the type definition is not valid + * @throws {Error} On bad buffer length. * @throws {Error} On overflow */ -function unpackArrayTo( - buffer, theType, output, start=0, end=buffer.length, safe=false) { - theType = theType || {}; - /** @type {NumberBuffer} */ - let packer = new NumberBuffer( - theType.bits, theType.fp, theType.signed); - /** @type {number} */ - let offset = Math.ceil(theType.bits / 8); - /** @type {number} */ - let extra = (end - start) % offset; - if (safe && (extra || buffer.length < offset)) { - throw new Error('Bad buffer length'); - } - end -= extra; - /** @type {number} */ - let i = 0; - try { - swap_(theType.be, buffer, offset, start, end); - for (let j = start; j < end; j += offset, i++) { - output[i] = packer.unpack(buffer, j); - } - swap_(theType.be, buffer, offset, start, end); - } catch (e) { - throw new Error(e.message + ' at output index ' + i); - } -} - -/** - * Swap endianness in a slice of an array when flip == true. - * @param {boolean} flip True if should swap endianness. - * @param {!Uint8Array|!Array} buffer The buffer. - * @param {number} offset The number of bytes each value use. - * @param {number} start The buffer index to start the swap. - * @param {number} end The buffer index to end the swap. - * @throws {Error} On bad buffer length for the swap. - * @private - */ -function swap_(flip, buffer, offset, start, end) { - if (flip) { - endianness(buffer, offset, start, end); - } +function unpack$1(buffer, theType, index=0) { + return unpackArray( + buffer, theType, index, index + Math.ceil(theType.bits / 8), true)[0]; } /* diff --git a/dist/wavefile.min.js b/dist/wavefile.min.js index 8f3524b..7167074 100644 --- a/dist/wavefile.min.js +++ b/dist/wavefile.min.js @@ -1,58 +1,58 @@ -var C="function"==typeof Object.create?Object.create:function(c){function h(){}h.prototype=c;return new h},D;if("function"==typeof Object.setPrototypeOf)D=Object.setPrototypeOf;else{var E;a:{var L={pa:!0},M={};try{M.__proto__=L;E=M.pa;break a}catch(c){}E=!1}D=E?function(c,h){c.__proto__=h;if(c.__proto__!==h)throw new TypeError(c+" is not extensible");return c}:null}var T=D; -function U(c,h){c.prototype=C(h.prototype);c.prototype.constructor=c;if(T)T(c,h);else for(var k in h)if("prototype"!=k)if(Object.defineProperties){var l=Object.getOwnPropertyDescriptor(h,k);l&&Object.defineProperty(c,k,l)}else c[k]=h[k];c.Qa=h.prototype}var V="function"==typeof Object.defineProperties?Object.defineProperty:function(c,h,k){c!=Array.prototype&&c!=Object.prototype&&(c[h]=k.value)},W="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this; -function X(c,h){if(h){var k=W;c=c.split(".");for(var l=0;lk&&(k=Math.max(k+n,0));kh||56319c||57343k&&(k=Math.max(k+n,0));kh||56319c||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.i=Math.pow(2,0>4;g[e++]=(N&15)<<4|h>>2;g[e++]=(h&3)<<6|k&63}return c}function Z(a,b){return a=0parseInt(a,10)||"53">8&255);d.push(t);d.push(0);for(b=3;bd.length;)d.push(0);return d}function ia(a){p=Q(a[1]<<8|a[0]);q=a[2];u=G[q];for(var b=[p,Q(a[3]<<8|a[2])],d=4;d>4;b.push(R(c<<4^e));b.push(R(c))}return b}function Q(a){return 32768>3;b>d&&(a|=4,b-=d,e+=d);d>>=1;b>d&&(a|=2,b-=d,e+=d);d>>=1;b>d&&(a|=1,e+=d); -b=a;r=b&8?r-e:r+e;-32768>r?r=-32768:32767t?t=0:88>1);a&1&&(b+=u>>2);b+=u>>3;a&8&&(b=-b);p+=b;32767p&&(p=-32767);q+=S[a];0>q?q=0:88>8&128;c||(e*=-1);32635>8&127];e=g<<4|e>>g+3&15}else e>>=4;b[d]=e^c^85}return b}function la(a){for(var b= -new Int16Array(a.length),d=0;d>4)+4;e=4!=g?1<>8&128;0!=c&&(e=-e);32635>7&255];b[d]=~(c|g<<4|e>>g+3&15)}return b}function oa(a){for(var b=new Int16Array(a.length),d=0;d>4&7;c=pa[c]+((e&15)<g)b[d]=g,d++;else{var f=0,h=0;2047>=g?(f=1,h=192):65535>=g?(f=2,h=224):1114111>=g&&(f=3,h=240,e++);b[d]=(g>>6*f)+h;for(d++;0>6*(f-1)&63,d++,f--}}return d}function x(a,b,d){d= -void 0===d?a.length:d;var e=void 0===b?0:b;d=void 0===d?a.length:d;b="";for(e=void 0===e?0:e;e=h)b+=String.fromCharCode(h);else{var k=0;194<=h&&223>=h?k=1:224<=h&&239>=h?(k=2,224===a[e]&&(c=160),237===a[e]&&(g=159)):240<=h&&244>=h?(k=3,240===a[e]&&(c=144),244===a[e]&&(g=143)):f=!0;h&=(1<<8-k-1)-1;for(var l=0;lg)f=!0;h=h<<6|a[e]&63;e++}f?b+=String.fromCharCode(65533):65535>=h?b+=String.fromCharCode(h):(h-=65536,b+=String.fromCharCode((h>> -10&1023)+55296,(h&1023)+56320))}}return b}function m(a){var b=[];I(a,b,0);return b}function f(a,b){var d=[];A([a],b,d,0);return d}function A(a,b,d,e){e=void 0===e?0:e;b=b||{};var c=new h(b.h,b.C,b.D),g=Math.ceil(b.h/8),f=0;try{for(var k=a.length;fB;B++)w["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(B)]= -B;n.prototype.f=function(a,b,d){Math.abs(b)>this.m-2*this.o&&(b=0>b?-Infinity:Infinity);var e=0>((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var c=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),g=this.j(b/Math.pow(2,c)*Math.pow(2,this.c));b!==b?(g=Math.pow(2,this.c-1),c=(1<=Math.pow(2,1-this.a)?(2<=g/Math.pow(2,this.c)&&(c+=1,g=1),c>this.a?(c=(1<=b;)a[g]=parseInt(c.substring(0,8),2),c=c.substring(8),g--,e++;return e};n.prototype.j=function(a){var b=Math.floor(a);a-=b;return.5>a?b:.5b?b+Math.pow(2,this.h):b)&255;d++;for(var e=this.b,c=2;cthis.max||athis.max&&(a-=2*this.max+2);return a}; -h.prototype.c=function(a,b){return this.a.g(a,void 0===b?0:b)};h.prototype.b=function(a,b,d){return this.a.f(a,b,void 0===d?0:d)};h.prototype.f=function(a){return 16===a?new n(5,11):32===a?new n(8,23):new n(11,52)};c.prototype.getSample=function(a){a*=this.g.h/8;if(a+this.g.h/8>this.data.samples.length)throw Error("Range error");return J(this.data.samples.slice(a,a+this.g.h/8),this.g)};c.prototype.setSample=function(a,b){a*=this.g.h/8;if(a+this.g.h/8>this.data.samples.length)throw Error("Range error"); -A([b],this.g,this.data.samples,void 0===a?0:a)};c.prototype.fromScratch=function(a,b,d,e,c){c=void 0===c?{}:c;c.container||(c.container="RIFF");this.container=c.container;this.bitDepth=d;e=this.Ba(e);this.P();var g=this.g.h/8;this.data.samples=new Uint8Array(e.length*g);A(e,this.g,this.data.samples);this.J();this.Ca(d,a,b,g,this.data.samples.length,c);this.data.chunkId="data";this.data.chunkSize=this.data.samples.length;this.R()};c.prototype.fromBuffer=function(a,b){b=void 0===b?!0:b;this.J();this.Pa(a, -b);this.da();this.P()};c.prototype.toBuffer=function(){this.R();return this.ca()};c.prototype.fromBase64=function(a){this.fromBuffer(new Uint8Array(z(a)))};c.prototype.toBase64=function(){var a=this.toBuffer();a=new Uint8Array(a,0,a.length);for(var b=a.length,d="",c=0;c>2],d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[c]&3)<<4|a[c+1]>>4],d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[c+ -1]&15)<<2|a[c+2]>>6],d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[c+2]&63];2===b%3?d=d.substring(0,d.length-1)+"\x3d":1===b%3&&(d=d.substring(0,d.length-2)+"\x3d\x3d");return d};c.prototype.toDataURI=function(){return"data:audio/wav;base64,"+this.toBase64()};c.prototype.fromDataURI=function(a){this.fromBase64(a.replace("data:audio/wav;base64,",""))};c.prototype.toRIFF=function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,K(this.data.samples, -this.g))};c.prototype.toRIFX=function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,K(this.data.samples,this.g),{container:"RIFX"})};c.prototype.toBitDepth=function(a,b){var d=a,c=this.bitDepth;void 0===b||b||("32f"!=a&&(d=this.g.h.toString()),c=this.g.h);this.I();var f=this.data.samples.length/(this.g.h/8);b=new Float64Array(f);f=new Float64Array(f);y(this.data.samples,this.g,b);"32f"!=c&&"64"!=c||this.Z(b);var g=c;c=d;P(g);P(c);var h=da(g,c),k={V:Math.pow(2,parseInt(g, -10))/2,T:Math.pow(2,parseInt(c,10))/2,U:Math.pow(2,parseInt(g,10))/2-1,S:Math.pow(2,parseInt(c,10))/2-1};d=b.length;if("8"==g)for(g=0;ga&&!f?(this.u(a,g+1,b),this.u(d[g].dwPosition,g+2,d[g].label),f=!0):this.u(d[g].dwPosition,g+1,d[g].label);f||this.u(a,this.cue.points.length+1,b)}this.cue.dwCuePoints=this.cue.points.length};c.prototype.deleteCuePoint=function(a){this.cue.chunkId= -"cue ";var b=this.A();this.H();var d=this.cue.points.length;this.cue.points=[];for(var c=0;ca.length)for(var b=0,d=4-a.length;ba[b]&&(a[b]=-1)};c.prototype.ca=function(){this.c.l="RIFX"===this.container;this.a.l=this.c.l;for(var a=[this.ra(),this.ma(),this.ja(),this.W(),this.oa(),m(this.data.chunkId),f(this.data.samples.length,this.a),this.data.samples,this.ka(), -this.ya(),this.sa()],b=0,d=0;da||53a?1:Math.ceil(a/8);this.max=Math.pow(2,a)-1;this.min=0;a=8-((a-1|7)+1-a);this.i=Math.pow(2,0>4;g[e++]=(O&15)<<4|h>>2;g[e++]=(h&3)<<6|k&63}return c}function aa(a,b){return a=0parseInt(a,10)||"53">8&255);d.push(t);d.push(0);for(b=3;bd.length;)d.push(0);return d}function ja(a){p=R(a[1]<<8|a[0]);q=a[2];v=H[q];for(var b=[p,R(a[3]<<8|a[2])],d=4;d>4;b.push(S(c<<4^e));b.push(S(c))}return b}function R(a){return 32768>3;b>d&&(a|=4,b-=d,e+=d);d>>=1;b>d&&(a|=2,b-=d,e+=d); +d>>=1;b>d&&(a|=1,e+=d);b=a;r=b&8?r-e:r+e;-32768>r?r=-32768:32767t?t=0:88>1);a&1&&(b+=v>>2);b+=v>>3;a&8&&(b=-b);p+=b;32767p&&(p=-32767);q+=T[a];0>q?q=0:88>8&128;c||(e*=-1);32635>8&127];e=g<<4|e>>g+3&15}else e>>=4;b[d]=e^ +c^85}return b}function ma(a){for(var b=new Int16Array(a.length),d=0;d>4)+4;e=4!=g?1<>8&128;0!=c&&(e=-e);32635>7&255];b[d]=~(c|g<<4|e>>g+3&15)}return b}function pa(a){for(var b=new Int16Array(a.length),d=0;d>4& +7;c=qa[c]+((e&15)<g)b[d]=g,d++;else{var f=0,h=0;2047>=g?(f=1,h=192):65535>=g?(f=2,h=224):1114111>=g&&(f=3,h=240,e++);b[d]=(g>>6*f)+h;for(d++;0>6*(f-1)& +63,d++,f--}}return d}function U(a,b,d,e){if(e||Infinity!==b&&-Infinity!==b&&b===b)throw Error(a.message+" at input index "+d+": "+b);throw Error("Argument is not a integer at input index "+d);}function x(a,b,d){d=void 0===d?a.length:d;var e=void 0===b?0:b;d=void 0===d?a.length:d;b="";for(e=void 0===e?0:e;e=h)b+=String.fromCharCode(h);else{var k=0;194<=h&&223>=h?k=1:224<=h&&239>=h?(k=2,224===a[e]&&(c=160),237===a[e]&&(g=159)):240<=h&&244>=h?(k=3,240=== +a[e]&&(c=144),244===a[e]&&(g=143)):f=!0;h&=(1<<8-k-1)-1;for(var l=0;lg)f=!0;h=h<<6|a[e]&63;e++}f?b+=String.fromCharCode(65533):65535>=h?b+=String.fromCharCode(h):(h-=65536,b+=String.fromCharCode((h>>10&1023)+55296,(h&1023)+56320))}}return b}function m(a){var b=[];J(a,b,0);return b}function A(a,b,d,e){e=void 0===e?0:e;b=b||{};var c=new h(b.h,b.w,b.D),g=0,f=e;try{for(var k=a.length;gB;B++)w["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(B)]=B;n.prototype.f=function(a,b,d){Math.abs(b)>this.m-2*this.o&&(b=0>b?-Infinity:Infinity);var e=0>((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var c=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),g=this.j(b/Math.pow(2,c)*Math.pow(2,this.c));b!==b?(g=Math.pow(2,this.c-1),c=(1<=Math.pow(2,1-this.a)?(2<=g/Math.pow(2,this.c)&&(c+=1,g=1),c>this.a?(c=(1<=b;)a[g]=parseInt(c.substring(0,8),2),c=c.substring(8),g--,e++;return e};n.prototype.j=function(a){var b=Math.floor(a);a-=b;return.5>a?b:.5b?b+Math.pow(2,this.h):b)&255;d++;for(var e=this.b,c=2;cthis.max||athis.max&&(a-=2*this.max+2);return a};h.prototype.f=function(a,b){return this.b.g(a,void 0===b?0:b)};h.prototype.c=function(a,b,d){return this.b.f(a,b,void 0===d?0:d)};h.prototype.g=function(a){return 16===a?new n(5,11):32===a?new n(8,23):new n(11,52)};c.prototype.getSample=function(a){a*=this.g.h/8;if(a+this.g.h/8>this.data.samples.length)throw Error("Range error");return L(this.data.samples.slice(a,a+this.g.h/8),this.g)};c.prototype.setSample= +function(a,b){a*=this.g.h/8;if(a+this.g.h/8>this.data.samples.length)throw Error("Range error");A([b],this.g,this.data.samples,void 0===a?0:a)};c.prototype.fromScratch=function(a,b,d,e,c){c=void 0===c?{}:c;c.container||(c.container="RIFF");this.container=c.container;this.bitDepth=d;e=this.Ba(e);this.P();var g=this.g.h/8;this.data.samples=new Uint8Array(e.length*g);A(e,this.g,this.data.samples);this.J();this.Ca(d,a,b,g,this.data.samples.length,c);this.data.chunkId="data";this.data.chunkSize=this.data.samples.length; +this.R()};c.prototype.fromBuffer=function(a,b){b=void 0===b?!0:b;this.J();this.Pa(a,b);this.da();this.P()};c.prototype.toBuffer=function(){this.R();return this.ca()};c.prototype.fromBase64=function(a){this.fromBuffer(new Uint8Array(z(a)))};c.prototype.toBase64=function(){var a=this.toBuffer();a=new Uint8Array(a,0,a.length);for(var b=a.length,d="",c=0;c>2],d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[c]& +3)<<4|a[c+1]>>4],d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[c+1]&15)<<2|a[c+2]>>6],d+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[c+2]&63];2===b%3?d=d.substring(0,d.length-1)+"\x3d":1===b%3&&(d=d.substring(0,d.length-2)+"\x3d\x3d");return d};c.prototype.toDataURI=function(){return"data:audio/wav;base64,"+this.toBase64()};c.prototype.fromDataURI=function(a){this.fromBase64(a.replace("data:audio/wav;base64,",""))};c.prototype.toRIFF=function(){this.fromScratch(this.fmt.numChannels, +this.fmt.sampleRate,this.bitDepth,K(this.data.samples,this.g))};c.prototype.toRIFX=function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,K(this.data.samples,this.g),{container:"RIFX"})};c.prototype.toBitDepth=function(a,b){var d=a,c=this.bitDepth;void 0===b||b||("32f"!=a&&(d=this.g.h.toString()),c=this.g.h);this.I();var f=this.data.samples.length/(this.g.h/8);b=new Float64Array(f);f=new Float64Array(f);y(this.data.samples,this.g,b);"32f"!=c&&"64"!=c||this.Z(b);var g=c; +c=d;Q(g);Q(c);var h=ea(g,c),k={V:Math.pow(2,parseInt(g,10))/2,T:Math.pow(2,parseInt(c,10))/2,U:Math.pow(2,parseInt(g,10))/2-1,S:Math.pow(2,parseInt(c,10))/2-1};d=b.length;if("8"==g)for(g=0;ga&&!f?(this.u(a,g+1,b),this.u(d[g].dwPosition,g+2,d[g].label),f=!0):this.u(d[g].dwPosition,g+1,d[g].label);f||this.u(a,this.cue.points.length+1,b)}this.cue.dwCuePoints=this.cue.points.length}; +c.prototype.deleteCuePoint=function(a){this.cue.chunkId="cue ";var b=this.B();this.H();var d=this.cue.points.length;this.cue.points=[];for(var c=0;ca.length)for(var b=0,d=4-a.length;ba[b]&&(a[b]=-1)};c.prototype.ca=function(){this.c.l="RIFX"===this.container;this.a.l=this.c.l;for(var a=[this.ra(),this.ma(),this.ja(),this.W(),this.oa(),m(this.data.chunkId),f(this.data.samples.length, +this.a),this.data.samples,this.ka(),this.ya(),this.sa()],b=0,d=0;dparseInt(this.bitDepth,10)))throw Error("Invalid bit depth.");};c.prototype.aa=function(){var a=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(1>this.fmt.numChannels||65535this.fmt.sampleRate||4294967295>>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";var f64f32_=new Float32Array(1);function bitDepth(input,original,target,output){validateBitDepth_(original),validateBitDepth_(target);var toFunction=function(original,target){var func=function(x){return x};original!=target&&(func=["32f","64"].includes(original)?["32f","64"].includes(target)?floatToFloat_:floatToInt_:["32f","64"].includes(target)?intToFloat_:intToInt_);return func}(original,target),options={oldMin:Math.pow(2,parseInt(original,10))/2,newMin:Math.pow(2,parseInt(target,10))/2,oldMax:Math.pow(2,parseInt(original,10))/2-1,newMax:Math.pow(2,parseInt(target,10))/2-1},len=input.length;if("8"==original)for(var 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;i>4,first_sample=second_sample<<4^original_sample;result.push(decodeSample_(first_sample)),result.push(decodeSample_(second_sample))}return result}function sign_(num){return num>32768?num-65536:num}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,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}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 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 pack$1(value,theType){var output=[];return packTo(value,theType,output),output}function packTo(value,theType,buffer){return packArrayTo([value],theType,buffer,arguments.length>3&&void 0!==arguments[3]?arguments[3]: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),offset=Math.ceil(theType.bits/8),i=0;try{for(var valuesLen=values.length;i2&&void 0!==arguments[2]?arguments[2]:0;return unpackArray(buffer,theType,index,index+Math.ceil(theType.bits/8),!0)[0]}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 unpackArrayTo(buffer,theType,output){var start=arguments.length>3&&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=Math.ceil(theType.bits/8),extra=(end-start)%offset;if(safe&&(extra||buffer.length2&&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;index0&&void 0!==arguments[0]?arguments[0]:null;classCallCheck(this,WaveFile),this.WAV_AUDIO_FORMATS={4:17,8:1,"8a":6,"8m":7,16:1,24:1,32:1,"32f":3,64:3},this.head_=0,this.uInt32_={bits:32,be:!1},this.uInt16_={bits:16,be:!1},this.container="",this.chunkSize=0,this.format="",this.fmt={chunkId:"",chunkSize:0,audioFormat:0,numChannels:0,sampleRate:0,byteRate:0,blockAlign:0,bitsPerSample:0,cbSize:0,validBitsPerSample:0,dwChannelMask:0,subformat:[]},this.fact={chunkId:"",chunkSize:0,dwSampleLength:0},this.cue={chunkId:"",chunkSize:0,dwCuePoints:0,points:[]},this.smpl={chunkId:"",chunkSize:0,dwManufacturer:0,dwProduct:0,dwSamplePeriod:0,dwMIDIUnityNote:0,dwMIDIPitchFraction:0,dwSMPTEFormat:0,dwSMPTEOffset:0,dwNumSampleLoops:0,dwSamplerData:0,loops:[]},this.bext={chunkId:"",chunkSize:0,description:"",originator:"",originatorReference:"",originationDate:"",originationTime:"",timeReference:[0,0],version:0,UMID:"",loudnessValue:0,loudnessRange:0,maxTruePeakLevel:0,maxMomentaryLoudness:0,maxShortTermLoudness:0,reserved:"",codingHistory:""},this.ds64={chunkId:"",chunkSize:0,riffSizeHigh:0,riffSizeLow:0,dataSizeHigh:0,dataSizeLow:0,originationTime:0,sampleCountHigh:0,sampleCountLow:0},this.data={chunkId:"",chunkSize:0,samples:new Uint8Array(0)},this.LIST=[],this.junk={chunkId:"",chunkSize:0,chunkData:[]},this.bitDepth="0",this.signature={},this.dataType={},bytes&&this.fromBuffer(bytes)}return createClass(WaveFile,[{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:"fromScratch",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=this.interleave_(samples),this.updateDataType_();var numBytes=this.dataType.bits/8;this.data.samples=new Uint8Array(samples.length*numBytes),packArrayTo(samples,this.dataType,this.data.samples),this.clearHeader_(),this.makeWavHeader(bitDepthCode,numChannels,sampleRate,numBytes,this.data.samples.length,options),this.data.chunkId="data",this.data.chunkSize=this.data.samples.length,this.validateWavHeader_()}},{key:"fromBuffer",value:function(bytes){var samples=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];this.clearHeader_(),this.readWavBuffer(bytes,samples),this.bitDepthFromFmt_(),this.updateDataType_()}},{key:"toBuffer",value:function(){return this.validateWavHeader_(),this.writeWavBuffer()}},{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,",""))}},{key:"toRIFF",value:function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,unpackArray(this.data.samples,this.dataType))}},{key:"toRIFX",value:function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,unpackArray(this.data.samples,this.dataType),{container:"RIFX"})}},{key:"toBitDepth",value:function(newBitDepth){var changeResolution=!(arguments.length>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||this.truncateSamples_(typedSamplesInput),bitDepth(typedSamplesInput,thisBitDepth,toBitDepth,typedSamplesOutput),this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,newBitDepth,typedSamplesOutput,{container:this.correctContainer_()})}},{key:"toIMAADPCM",value:function(){if(8e3!==this.fmt.sampleRate)throw new Error("Only 8000 Hz files can be compressed as IMA-ADPCM.");if(1!==this.fmt.numChannels)throw new Error("Only mono files can be compressed as IMA-ADPCM.");this.assure16Bit_();var output=new Int16Array(this.data.samples.length/2);unpackArrayTo(this.data.samples,this.dataType,output),this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,"4",function(samples){for(var adpcmSamples=new Uint8Array(samples.length/2+512),block=[],fileIndex=0,i=0;i0&&void 0!==arguments[0]?arguments[0]:"16";this.fromScratch(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.fromScratch(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.fromScratch(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]:"";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&&samples[0].constructor===Array){for(var finalSamples=[],i=0,len=samples[0].length;i-1&&(this.dataType.bits=8,this.dataType.signed=!1)}},{key:"correctContainer_",value:function(){return"RF64"==this.container?"RIFF":this.container}},{key:"truncateSamples_",value:function(samples){for(var i=0,len=samples.length;i1?samples[i]=1:samples[i]<-1&&(samples[i]=-1)}},{key:"writeWavBuffer",value:function(){this.uInt16_.be="RIFX"===this.container,this.uInt32_.be=this.uInt16_.be;for(var fileBody=[this.getJunkBytes_(),this.getDs64Bytes_(),this.getBextBytes_(),this.getFmtBytes_(),this.getFactBytes_(),packString(this.data.chunkId),pack$1(this.data.samples.length,this.uInt32_),this.data.samples,this.getCueBytes_(),this.getSmplBytes_(),this.getLISTBytes_()],fileBodyLength=0,i=0;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_),this.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_),this.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}},{key:"readWavBuffer",value:function(buffer,samples){this.head_=0,this.readRIFFChunk_(buffer),this.getSignature_(buffer),this.readDs64Chunk_(buffer),this.readFmtChunk_(buffer),this.readFactChunk_(buffer),this.readBextChunk_(buffer),this.readCueChunk_(buffer),this.readSmplChunk_(buffer),this.readDataChunk_(buffer,samples),this.readJunkChunk_(buffer),this.readLISTChunk_(buffer)}},{key:"getSignature_",value:function(buffer){this.head_=0;var chunkId=this.getChunkId_(buffer,0);this.uInt32_.be="RIFX"==chunkId;var format=unpackString(buffer,8,12);this.head_+=4,this.signature={chunkId:chunkId,chunkSize:this.getChunkSize_(buffer,0),format: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;i16&&(this.fmt.cbSize=this.read_(buffer,this.uInt16_),this.fmt.chunkSize>18&&(this.fmt.validBitsPerSample=this.read_(buffer,this.uInt16_),this.fmt.chunkSize>20&&(this.fmt.dwChannelMask=this.read_(buffer,this.uInt32_),this.fmt.subformat=[this.read_(buffer,this.uInt32_),this.read_(buffer,this.uInt32_),this.read_(buffer,this.uInt32_),this.read_(buffer,this.uInt32_)])))}},{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.read_(buffer,this.uInt32_))}},{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.read_(buffer,this.uInt32_);for(var i=0;i-1){this.head_=subChunk.chunkData.start;var item={chunkId:subChunk.chunkId,chunkSize:subChunk.chunkSize,dwName:this.read_(buffer,this.uInt32_)};"ltxt"==subChunk.chunkId&&(item.dwSampleLength=this.read_(buffer,this.uInt32_),item.dwPurposeID=this.read_(buffer,this.uInt32_),item.dwCountry=this.read_(buffer,this.uInt16_),item.dwLanguage=this.read_(buffer,this.uInt16_),item.dwDialect=this.read_(buffer,this.uInt16_),item.dwCodePage=this.read_(buffer,this.uInt16_)),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:"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=this.getDwChannelMask_(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:"getDwChannelMask_",value:function(numChannels){var dwChannelMask=0;return 1===numChannels?dwChannelMask=4:2===numChannels?dwChannelMask=3:4===numChannels?dwChannelMask=51:6===numChannels?dwChannelMask=63:8===numChannels&&(dwChannelMask=1599),dwChannelMask}},{key:"validateWavHeader_",value:function(){this.validateBitDepth_(),this.validateNumChannels_(),this.validateSampleRate_()}},{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:"validateNumChannels_",value:function(){var blockAlign=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(this.fmt.numChannels<1||blockAlign>65535)throw new Error("Invalid number of channels.");return!0}},{key:"validateSampleRate_",value:function(){var byteRate=this.fmt.numChannels*(this.fmt.bitsPerSample/8)*this.fmt.sampleRate;if(this.fmt.sampleRate<1||byteRate>4294967295)throw new Error("Invalid sample rate.");return!0}},{key:"writeString_",value:function(str,maxSize){var push=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],bytes=packString(str);if(push)for(var i=bytes.length;i1&&void 0!==arguments[1]?arguments[1]:0;i>>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";var f64f32_=new Float32Array(1);function bitDepth(input,original,target,output){validateBitDepth_(original),validateBitDepth_(target);var toFunction=function(original,target){var func=function(x){return x};original!=target&&(func=["32f","64"].includes(original)?["32f","64"].includes(target)?floatToFloat_:floatToInt_:["32f","64"].includes(target)?intToFloat_:intToInt_);return func}(original,target),options={oldMin:Math.pow(2,parseInt(original,10))/2,newMin:Math.pow(2,parseInt(target,10))/2,oldMax:Math.pow(2,parseInt(original,10))/2-1,newMax:Math.pow(2,parseInt(target,10))/2-1},len=input.length;if("8"==original)for(var 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;i>4,first_sample=second_sample<<4^original_sample;result.push(decodeSample_(first_sample)),result.push(decodeSample_(second_sample))}return result}function sign_(num){return num>32768?num-65536:num}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]}return function(){function WaveFile(){var bytes=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;classCallCheck(this,WaveFile),this.WAV_AUDIO_FORMATS={4:17,8:1,"8a":6,"8m":7,16:1,24:1,32:1,"32f":3,64:3},this.head_=0,this.uInt32_={bits:32,be:!1},this.uInt16_={bits:16,be:!1},this.container="",this.chunkSize=0,this.format="",this.fmt={chunkId:"",chunkSize:0,audioFormat:0,numChannels:0,sampleRate:0,byteRate:0,blockAlign:0,bitsPerSample:0,cbSize:0,validBitsPerSample:0,dwChannelMask:0,subformat:[]},this.fact={chunkId:"",chunkSize:0,dwSampleLength:0},this.cue={chunkId:"",chunkSize:0,dwCuePoints:0,points:[]},this.smpl={chunkId:"",chunkSize:0,dwManufacturer:0,dwProduct:0,dwSamplePeriod:0,dwMIDIUnityNote:0,dwMIDIPitchFraction:0,dwSMPTEFormat:0,dwSMPTEOffset:0,dwNumSampleLoops:0,dwSamplerData:0,loops:[]},this.bext={chunkId:"",chunkSize:0,description:"",originator:"",originatorReference:"",originationDate:"",originationTime:"",timeReference:[0,0],version:0,UMID:"",loudnessValue:0,loudnessRange:0,maxTruePeakLevel:0,maxMomentaryLoudness:0,maxShortTermLoudness:0,reserved:"",codingHistory:""},this.ds64={chunkId:"",chunkSize:0,riffSizeHigh:0,riffSizeLow:0,dataSizeHigh:0,dataSizeLow:0,originationTime:0,sampleCountHigh:0,sampleCountLow:0},this.data={chunkId:"",chunkSize:0,samples:new Uint8Array(0)},this.LIST=[],this.junk={chunkId:"",chunkSize:0,chunkData:[]},this.bitDepth="0",this.signature={},this.dataType={},bytes&&this.fromBuffer(bytes)}return createClass(WaveFile,[{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:"fromScratch",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=this.interleave_(samples),this.updateDataType_();var numBytes=this.dataType.bits/8;this.data.samples=new Uint8Array(samples.length*numBytes),packArrayTo(samples,this.dataType,this.data.samples),this.clearHeader_(),this.makeWavHeader(bitDepthCode,numChannels,sampleRate,numBytes,this.data.samples.length,options),this.data.chunkId="data",this.data.chunkSize=this.data.samples.length,this.validateWavHeader_()}},{key:"fromBuffer",value:function(bytes){var samples=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];this.clearHeader_(),this.readWavBuffer(bytes,samples),this.bitDepthFromFmt_(),this.updateDataType_()}},{key:"toBuffer",value:function(){return this.validateWavHeader_(),this.writeWavBuffer()}},{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,",""))}},{key:"toRIFF",value:function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,unpackArray(this.data.samples,this.dataType))}},{key:"toRIFX",value:function(){this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,this.bitDepth,unpackArray(this.data.samples,this.dataType),{container:"RIFX"})}},{key:"toBitDepth",value:function(newBitDepth){var changeResolution=!(arguments.length>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||this.truncateSamples_(typedSamplesInput),bitDepth(typedSamplesInput,thisBitDepth,toBitDepth,typedSamplesOutput),this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,newBitDepth,typedSamplesOutput,{container:this.correctContainer_()})}},{key:"toIMAADPCM",value:function(){if(8e3!==this.fmt.sampleRate)throw new Error("Only 8000 Hz files can be compressed as IMA-ADPCM.");if(1!==this.fmt.numChannels)throw new Error("Only mono files can be compressed as IMA-ADPCM.");this.assure16Bit_();var output=new Int16Array(this.data.samples.length/2);unpackArrayTo(this.data.samples,this.dataType,output),this.fromScratch(this.fmt.numChannels,this.fmt.sampleRate,"4",function(samples){for(var adpcmSamples=new Uint8Array(samples.length/2+512),block=[],fileIndex=0,i=0;i0&&void 0!==arguments[0]?arguments[0]:"16";this.fromScratch(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.fromScratch(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.fromScratch(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]:"";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&&samples[0].constructor===Array){for(var finalSamples=[],i=0,len=samples[0].length;i-1&&(this.dataType.bits=8,this.dataType.signed=!1)}},{key:"correctContainer_",value:function(){return"RF64"==this.container?"RIFF":this.container}},{key:"truncateSamples_",value:function(samples){for(var i=0,len=samples.length;i1?samples[i]=1:samples[i]<-1&&(samples[i]=-1)}},{key:"writeWavBuffer",value:function(){this.uInt16_.be="RIFX"===this.container,this.uInt32_.be=this.uInt16_.be;for(var fileBody=[this.getJunkBytes_(),this.getDs64Bytes_(),this.getBextBytes_(),this.getFmtBytes_(),this.getFactBytes_(),packString(this.data.chunkId),pack$1(this.data.samples.length,this.uInt32_),this.data.samples,this.getCueBytes_(),this.getSmplBytes_(),this.getLISTBytes_()],fileBodyLength=0,i=0;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_),this.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_),this.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}},{key:"readWavBuffer",value:function(buffer,samples){this.head_=0,this.readRIFFChunk_(buffer),this.getSignature_(buffer),this.readDs64Chunk_(buffer),this.readFmtChunk_(buffer),this.readFactChunk_(buffer),this.readBextChunk_(buffer),this.readCueChunk_(buffer),this.readSmplChunk_(buffer),this.readDataChunk_(buffer,samples),this.readJunkChunk_(buffer),this.readLISTChunk_(buffer)}},{key:"getSignature_",value:function(buffer){this.head_=0;var chunkId=this.getChunkId_(buffer,0);this.uInt32_.be="RIFX"==chunkId;var format=unpackString(buffer,8,12);this.head_+=4,this.signature={chunkId:chunkId,chunkSize:this.getChunkSize_(buffer,0),format: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;i16&&(this.fmt.cbSize=this.read_(buffer,this.uInt16_),this.fmt.chunkSize>18&&(this.fmt.validBitsPerSample=this.read_(buffer,this.uInt16_),this.fmt.chunkSize>20&&(this.fmt.dwChannelMask=this.read_(buffer,this.uInt32_),this.fmt.subformat=[this.read_(buffer,this.uInt32_),this.read_(buffer,this.uInt32_),this.read_(buffer,this.uInt32_),this.read_(buffer,this.uInt32_)])))}},{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.read_(buffer,this.uInt32_))}},{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.read_(buffer,this.uInt32_);for(var i=0;i-1){this.head_=subChunk.chunkData.start;var item={chunkId:subChunk.chunkId,chunkSize:subChunk.chunkSize,dwName:this.read_(buffer,this.uInt32_)};"ltxt"==subChunk.chunkId&&(item.dwSampleLength=this.read_(buffer,this.uInt32_),item.dwPurposeID=this.read_(buffer,this.uInt32_),item.dwCountry=this.read_(buffer,this.uInt16_),item.dwLanguage=this.read_(buffer,this.uInt16_),item.dwDialect=this.read_(buffer,this.uInt16_),item.dwCodePage=this.read_(buffer,this.uInt16_)),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:"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=this.getDwChannelMask_(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:"getDwChannelMask_",value:function(numChannels){var dwChannelMask=0;return 1===numChannels?dwChannelMask=4:2===numChannels?dwChannelMask=3:4===numChannels?dwChannelMask=51:6===numChannels?dwChannelMask=63:8===numChannels&&(dwChannelMask=1599),dwChannelMask}},{key:"validateWavHeader_",value:function(){this.validateBitDepth_(),this.validateNumChannels_(),this.validateSampleRate_()}},{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:"validateNumChannels_",value:function(){var blockAlign=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(this.fmt.numChannels<1||blockAlign>65535)throw new Error("Invalid number of channels.");return!0}},{key:"validateSampleRate_",value:function(){var byteRate=this.fmt.numChannels*(this.fmt.bitsPerSample/8)*this.fmt.sampleRate;if(this.fmt.sampleRate<1||byteRate>4294967295)throw new Error("Invalid sample rate.");return!0}},{key:"writeString_",value:function(str,maxSize){var push=!(arguments.length>2&&void 0!==arguments[2])||arguments[2],bytes=packString(str);if(push)for(var i=bytes.length;i1&&void 0!==arguments[1]?arguments[1]:0;i
- Documentation generated by JSDoc 3.5.5 on Tue Aug 07 2018 19:15:56 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Thu Aug 09 2018 16:27:19 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/index.js.html b/docs/index.js.html index 103311e..2b82457 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -2282,7 +2282,7 @@

index.js


- Documentation generated by JSDoc 3.5.5 on Tue Aug 07 2018 19:15:56 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Thu Aug 09 2018 16:27:19 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/module-wavefile.html b/docs/module-wavefile.html index f1dda57..307d391 100644 --- a/docs/module-wavefile.html +++ b/docs/module-wavefile.html @@ -6049,7 +6049,7 @@
Parameters:

- Documentation generated by JSDoc 3.5.5 on Tue Aug 07 2018 19:15:56 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Thu Aug 09 2018 16:27:19 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/package-lock.json b/package-lock.json index 1f0f366..220157e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "wavefile", - "version": "9.0.0", + "version": "8.4.4", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1149,9 +1149,9 @@ "dev": true }, "byte-data": { - "version": "16.0.2", - "resolved": "https://registry.npmjs.org/byte-data/-/byte-data-16.0.2.tgz", - "integrity": "sha512-KaPsHGlGs+wjnhP4oDQoortUbmIbkynA9I5yGl7mKOosRHBIRrnVpkiPOjbZF58BPglm8dqDfZesvCrtr0u9nw==", + "version": "16.0.3", + "resolved": "https://registry.npmjs.org/byte-data/-/byte-data-16.0.3.tgz", + "integrity": "sha512-IzV3mzv8OnnzPdb9CoESQr2ikPX/gkHUesRu+vff9XB7KwMxyflPDewtPFWXPvF+Xukl52ceor2IRLbnQZf3PQ==", "requires": { "endianness": "8.0.2", "ieee754-buffer": "0.2.1", diff --git a/package.json b/package.json index b915772..d9b06d8 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,13 @@ { "name": "wavefile", - "version": "8.4.3", + "version": "8.4.4", "description": "Create, read and write wav files according to the specs.", "homepage": "https://github.com/rochars/wavefile", "author": "Rafael da Silva Rocha ", "license": "MIT", "module": "./index.js", "main": "./dist/wavefile.cjs.js", + "browser": "./dist/wavefile.min.js", "bin": "./bin/wavefile.js", "engines": { "node": ">=8" @@ -100,7 +101,7 @@ "alawmulaw": "^5.0.2", "base64-arraybuffer-es6": "^0.3.1", "bitdepth": "^7.0.2", - "byte-data": "^16.0.2", + "byte-data": "^16.0.3", "imaadpcm": "^4.0.1" } }