UNPKG

wavefile-creator

Version:
61 lines (60 loc) 28.8 kB
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.objectCreate=$jscomp.ASSUME_ES5||"function"==typeof Object.create?Object.create:function(d){var h=function(){};h.prototype=d;return new h};$jscomp.underscoreProtoCanBeSet=function(){var d={a:!0},h={};try{return h.__proto__=d,h.a}catch(f){}return!1}; $jscomp.setPrototypeOf="function"==typeof Object.setPrototypeOf?Object.setPrototypeOf:$jscomp.underscoreProtoCanBeSet()?function(d,h){d.__proto__=h;if(d.__proto__!==h)throw new TypeError(d+" is not extensible");return d}:null; $jscomp.inherits=function(d,h){d.prototype=$jscomp.objectCreate(h.prototype);d.prototype.constructor=d;if($jscomp.setPrototypeOf){var f=$jscomp.setPrototypeOf;f(d,h)}else for(f in h)if("prototype"!=f)if(Object.defineProperties){var k=Object.getOwnPropertyDescriptor(h,f);k&&Object.defineProperty(d,f,k)}else d[f]=h[f];d.superClass_=h.prototype}; $jscomp.checkStringArgs=function(d,h,f){if(null==d)throw new TypeError("The 'this' value for String.prototype."+f+" must not be null or undefined");if(h instanceof RegExp)throw new TypeError("First argument to String.prototype."+f+" must not be a regular expression");return d+""};$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(d,h,f){d!=Array.prototype&&d!=Object.prototype&&(d[h]=f.value)}; $jscomp.getGlobal=function(d){return"undefined"!=typeof window&&window===d?d:"undefined"!=typeof global&&null!=global?global:d};$jscomp.global=$jscomp.getGlobal(this);$jscomp.polyfill=function(d,h,f,k){if(h){f=$jscomp.global;d=d.split(".");for(k=0;k<d.length-1;k++){var l=d[k];l in f||(f[l]={});f=f[l]}d=d[d.length-1];k=f[d];h=h(k);h!=k&&null!=h&&$jscomp.defineProperty(f,d,{configurable:!0,writable:!0,value:h})}}; $jscomp.polyfill("String.prototype.codePointAt",function(d){return d?d:function(d){var h=$jscomp.checkStringArgs(this,null,"codePointAt"),k=h.length;d=Number(d)||0;if(0<=d&&d<k){d|=0;var l=h.charCodeAt(d);if(55296>l||56319<l||d+1===k)return l;d=h.charCodeAt(d+1);return 56320>d||57343<d?l:1024*(l-55296)+d+9216}}},"es6","es3");$jscomp.owns=function(d,h){return Object.prototype.hasOwnProperty.call(d,h)}; $jscomp.assign="function"==typeof Object.assign?Object.assign:function(d,h){for(var f=1;f<arguments.length;f++){var k=arguments[f];if(k)for(var l in k)$jscomp.owns(k,l)&&(d[l]=k[l])}return d};$jscomp.polyfill("Object.assign",function(d){return d||$jscomp.assign},"es6","es3");$jscomp.arrayIteratorImpl=function(d){var h=0;return function(){return h<d.length?{done:!1,value:d[h++]}:{done:!0}}};$jscomp.arrayIterator=function(d){return{next:$jscomp.arrayIteratorImpl(d)}};$jscomp.SYMBOL_PREFIX="jscomp_symbol_"; $jscomp.initSymbol=function(){$jscomp.initSymbol=function(){};$jscomp.global.Symbol||($jscomp.global.Symbol=$jscomp.Symbol)};$jscomp.SymbolClass=function(d,h){this.$jscomp$symbol$id_=d;$jscomp.defineProperty(this,"description",{configurable:!0,writable:!0,value:h})};$jscomp.SymbolClass.prototype.toString=function(){return this.$jscomp$symbol$id_}; $jscomp.Symbol=function(){function d(f){if(this instanceof d)throw new TypeError("Symbol is not a constructor");return new $jscomp.SymbolClass($jscomp.SYMBOL_PREFIX+(f||"")+"_"+h++,f)}var h=0;return d}(); $jscomp.initSymbolIterator=function(){$jscomp.initSymbol();var d=$jscomp.global.Symbol.iterator;d||(d=$jscomp.global.Symbol.iterator=$jscomp.global.Symbol("Symbol.iterator"));"function"!=typeof Array.prototype[d]&&$jscomp.defineProperty(Array.prototype,d,{configurable:!0,writable:!0,value:function(){return $jscomp.iteratorPrototype($jscomp.arrayIteratorImpl(this))}});$jscomp.initSymbolIterator=function(){}}; $jscomp.initSymbolAsyncIterator=function(){$jscomp.initSymbol();var d=$jscomp.global.Symbol.asyncIterator;d||(d=$jscomp.global.Symbol.asyncIterator=$jscomp.global.Symbol("Symbol.asyncIterator"));$jscomp.initSymbolAsyncIterator=function(){}};$jscomp.iteratorPrototype=function(d){$jscomp.initSymbolIterator();d={next:d};d[$jscomp.global.Symbol.iterator]=function(){return this};return d}; $jscomp.iteratorFromArray=function(d,h){$jscomp.initSymbolIterator();d instanceof String&&(d+="");var f=0,k={next:function(){if(f<d.length){var l=f++;return{value:h(l,d[l]),done:!1}}k.next=function(){return{done:!0,value:void 0}};return k.next()}};k[Symbol.iterator]=function(){return k};return k}; (function(d,h){"object"===typeof exports&&"undefined"!==typeof module?h(exports):"function"===typeof define&&define.amd?define(["exports"],h):(d=d||self,h(d.wavefileCreator={}))})(this,function(d){function h(a,b,c,g){g=void 0===g?a.length:g;if(g%b)throw Error("Bad buffer length.");for(c=void 0===c?0:c;c<g;c+=b){var d=a,w=b,e=c;w--;for(var h=0;h<w;h++){var f=d[e+h];d[e+h]=d[e+w];d[e+w]=f;w--}}}function f(a,b,c){c=void 0===c?0:c;for(var g=0,d=a.length;g<d;g++){var e=a.codePointAt(g);if(128>e)b[c]=e, c++;else{var h=0,f=0;2047>=e?(h=1,f=192):65535>=e?(h=2,f=224):1114111>=e&&(h=3,f=240,g++);b[c]=(e>>6*h)+f;for(c++;0<h;)b[c]=128|e>>6*(h-1)&63,c++,h--}}return c}function k(a){var b=Math.floor(a);a-=b;return.5>a?b:.5<a?b+1:b%2?b+1:b}function l(a,b,c){c=void 0===c?a.length:c;var g=void 0===b?0:b;c=void 0===c?a.length:c;b="";for(g=void 0===g?0:g;g<c;){var d=128,e=191,h=!1,f=a[g++];if(0<=f&&127>=f)b+=String.fromCharCode(f);else{var k=0;194<=f&&223>=f?k=1:224<=f&&239>=f?(k=2,224===a[g]&&(d=160),237===a[g]&& (e=159)):240<=f&&244>=f?(k=3,240===a[g]&&(d=144),244===a[g]&&(e=143)):h=!0;f&=(1<<8-k-1)-1;for(var l=0;l<k;l++){if(a[g]<d||a[g]>e)h=!0;f=f<<6|a[g]&63;g++}h?b+=String.fromCharCode(65533):65535>=f?b+=String.fromCharCode(f):(f-=65536,b+=String.fromCharCode((f>>10&1023)+55296,(f&1023)+56320))}}return b}function p(a){var b=[];f(a,b,0);return b}function A(a,b,c){return f(a,b,void 0===c?0:c)}function B(a,b,c,g){g=void 0===g?0:g;b=b||{};var d=new x(b.bits,b.fp,b.signed),e=0,f=g;try{for(var k=a.length;e<k;e++)g= d.pack(c,a[e],g);b.be&&h(c,d.offset,f,g)}catch(E){C(E,a[e],e)}return g}function D(a,b,c,g){return B([a],b,c,void 0===g?0:g)}function e(a,b){var c=[];D(a,b,c);return c}function z(a,b,c){var g=c=void 0===c?0:c,d=c+Math.ceil(b.bits/8),e=!0;d=void 0===d?a.length:d;c=[];g=void 0===g?0:g;var f=void 0===e?!1:e;g=void 0===g?0:g;d=void 0===d?a.length:d;f=void 0===f?!1:f;b=b||{};e=new x(b.bits,b.fp,b.signed);var k=e.offset,l=(d-g)%k;if(f&&(l||a.length<k))throw Error("Bad buffer length");d-=l;f=0;l=g;try{for(b.be&& h(a,k,g,d);l<d;l+=k,f++)c[f]=e.unpack(a,l);b.be&&h(a,k,g,d)}catch(F){C(F,a.slice(l,l+k),l)}return c[0]}function C(a,b,c){a.message=a.constructor.name+" at index "+c+": "+b;throw a;}function r(a,b,c){c=void 0===c?!0:c;a=p(a);if(c)for(c=a.length;c<b;c++)a.push(0);return a}var v=function(a,b){this.ebits=a;this.fbits=b;this.bias=(1<<a-1)-1;this.numBytes=Math.ceil((a+b)/8);this.biasP2=Math.pow(2,this.bias+1);this.ebitsFbits=a+b;this.fbias=Math.pow(2,-(8*this.numBytes-1-a))};v.prototype.pack=function(a, b,c){if("number"!=typeof b)throw new TypeError;Math.abs(b)>this.biasP2-2*this.ebitsFbits&&(b=0>b?-Infinity:Infinity);var g=0>((b=+b)||1/b)?1:0>b?1:0;b=Math.abs(b);var d=Math.min(Math.floor(Math.log(b)/Math.LN2),1023),e=k(b/Math.pow(2,d)*Math.pow(2,this.fbits));b!==b?(e=Math.pow(2,this.fbits-1),d=(1<<this.ebits)-1):0!==b&&(b>=Math.pow(2,1-this.bias)?(2<=e/Math.pow(2,this.fbits)&&(d+=1,e=1),d>this.bias?(d=(1<<this.ebits)-1,e=0):(d+=this.bias,e=k(e)-Math.pow(2,this.fbits))):(e=k(b/Math.pow(2,1-this.bias- this.fbits)),d=0));return this.packFloatBits_(a,c,g,d,e)};v.prototype.unpack=function(a,b){for(var c=(1<<this.ebits)-1,g="",d=this.numBytes-1;0<=d;d--){var e=a[d+b].toString(2);g+="00000000".substring(e.length)+e}d="1"==g.charAt(0)?-1:1;g=g.substring(1);e=parseInt(g.substring(0,this.ebits),2);g=g.substring(this.ebits);if(e==c)return 0!==parseInt(g,2)?NaN:Infinity*d;0===e?(e+=1,c=parseInt(g,2)):c=parseInt("1"+g,2);return d*c*this.fbias*Math.pow(2,e-this.bias)};v.prototype.packFloatBits_=function(a, b,c,d,e){var g=[];g.push(c);for(c=this.ebits;0<c;--c)g[c]=d%2?1:0,d=Math.floor(d/2);d=g.length;for(c=this.fbits;0<c;--c)g[d+c]=e%2?1:0,e=Math.floor(e/2);e=g.join("");g=this.numBytes+b-1;for(d=b;g>=b;)a[g]=parseInt(e.substring(0,8),2),e=e.substring(8),g--,d++;return d};var t=function(a){this.bits=a;this.bytes=8>a?1:Math.ceil(a/8);this.max=Math.pow(2,a)-1;this.min=0;a=8-((a-1|7)+1-a);this.lastByteMask_=Math.pow(2,0<a?a:8)-1};t.prototype.pack=function(a,b,c){c=void 0===c?0:c;if(b!==b||"number"!=typeof b)throw new TypeError; this.overflow(b);return this.pack_(a,b,c)};t.prototype.packUnsafe=function(a,b,c){return this.pack_(a,b,void 0===c?0:c)};t.prototype.unpack=function(a,b){var c=this.unpackUnsafe(a,void 0===b?0:b);this.overflow(c);return c};t.prototype.unpackUnsafe=function(a,b){b=void 0===b?0:b;for(var c=0,d=0;d<this.bytes;d++)c+=a[b+d]*Math.pow(256,d);return c};t.prototype.overflow=function(a){if(a>this.max||a<this.min)throw new RangeError;};t.prototype.pack_=function(a,b,c){c=void 0===c?0:c;a[c]=(0>b?b+Math.pow(2, this.bits):b)&255;c++;for(var d=this.bytes,e=2;e<d;e++)a[c]=Math.floor(b/Math.pow(2,8*(e-1)))&255,c++;8<this.bits&&(a[c]=Math.floor(b/Math.pow(2,8*(this.bytes-1)))&this.lastByteMask_,c++);return c};var y=function(a){a=t.call(this,a)||this;a.max=Math.pow(2,a.bits)/2-1;a.min=-a.max-1;return a};$jscomp.inherits(y,t);y.prototype.pack=function(a,b,c){return t.prototype.pack.call(this,a,b,void 0===c?0:c)};y.prototype.unpack=function(a,b){var c=t.prototype.unpackUnsafe.call(this,a,void 0===b?0:b);c=this.sign_(c); this.overflow(c);return c};y.prototype.sign_=function(a){a>this.max&&(a-=2*this.max+2);return a};var x=function(a,b,c){if(b){if(!a||16!==a&&32!==a&&64!==a)throw Error("Unsupported type: float, bits: "+a);b=this.getFPParser_(a)}else{if(!a||1>a||53<a)throw Error("Unsupported type: int, bits: "+a);b=c?new y(a):new t(a)}this.parser=b;this.offset=Math.ceil(a/8)};x.prototype.unpack=function(a,b){return this.parser.unpack(a,void 0===b?0:b)};x.prototype.pack=function(a,b,c){return this.parser.pack(a,b,void 0=== c?0:c)};x.prototype.getFPParser_=function(a){return 16===a?new v(5,11):32===a?new v(8,23):new v(11,52)};var u=function(){this.container="";this.chunkSize=0;this.format="";this.signature=null;this.head=0;this.uInt32={bits:32,be:!1,signed:!1,fp:!1};this.supported_containers=["RIFF","RIFX"]};u.prototype.setSignature=function(a){this.head=0;this.container=this.readString(a,4);if(-1===this.supported_containers.indexOf(this.container))throw Error("Not a supported format.");this.uInt32.be="RIFX"===this.container; this.chunkSize=this.readUInt32(a);this.format=this.readString(a,4);this.signature={chunkId:this.container,chunkSize:this.chunkSize,format:this.format,subChunks:this.getSubChunksIndex_(a),chunkData:{start:0,end:this.chunkSize}}};u.prototype.findChunk=function(a,b){b=void 0===b?!1:b;for(var c=this.signature.subChunks,d=[],e=0;e<c.length;e++)if(c[e].chunkId==a)if(b)d.push(c[e]);else return c[e];return"LIST"==a?d.length?d:null:null};u.prototype.readString=function(a,b){var c=l(a,this.head,this.head+b); this.head+=b;return c};u.prototype.readUInt32=function(a){a=z(a,this.uInt32,this.head);this.head+=4;return a};u.prototype.getSubChunksIndex_=function(a){for(var b=[],c=this.head;c<=a.length-8;)b.push(this.getSubChunkIndex_(a,c)),c+=8+b[b.length-1].chunkSize,c=c%2?c+1:c;return b};u.prototype.getSubChunkIndex_=function(a,b){var c={chunkId:this.getChunkId_(a,b),chunkSize:this.getChunkSize_(a,b)};"LIST"==c.chunkId?(c.format=l(a,b+8,b+12),this.head+=4,c.subChunks=this.getSubChunksIndex_(a)):(this.head= b+8+(c.chunkSize%2?c.chunkSize+1:c.chunkSize),c.chunkData={start:b+8,end:this.head});return c};u.prototype.getChunkId_=function(a,b){this.head+=4;return l(a,b,b+4)};u.prototype.getChunkSize_=function(a,b){this.head+=4;return z(a,this.uInt32,b+4)};var n=function(a,b){a=void 0===a?null:a;b=void 0===b?!0:b;var c=u.call(this)||this;c.supported_containers.push("RF64");c.fmt={chunkId:"",chunkSize:0,audioFormat:0,numChannels:0,sampleRate:0,byteRate:0,blockAlign:0,bitsPerSample:0,cbSize:0,validBitsPerSample:0, dwChannelMask:0,subformat:[]};c.fact={chunkId:"",chunkSize:0,dwSampleLength:0};c.cue={chunkId:"",chunkSize:0,dwCuePoints:0,points:[]};c.smpl={chunkId:"",chunkSize:0,dwManufacturer:0,dwProduct:0,dwSamplePeriod:0,dwMIDIUnityNote:0,dwMIDIPitchFraction:0,dwSMPTEFormat:0,dwSMPTEOffset:0,dwNumSampleLoops:0,dwSamplerData:0,loops:[]};c.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:""};c.ds64={chunkId:"",chunkSize:0,riffSizeHigh:0,riffSizeLow:0,dataSizeHigh:0,dataSizeLow:0,originationTime:0,sampleCountHigh:0,sampleCountLow:0};c.data={chunkId:"",chunkSize:0,samples:new Uint8Array(0)};c.LIST=[];c.junk={chunkId:"",chunkSize:0,chunkData:[]};c.uInt16={bits:16,be:!1,signed:!1,fp:!1};a&&c.fromBuffer(a,b);return c};$jscomp.inherits(n,u);n.prototype.fromBuffer=function(a,b){b=void 0=== b?!0:b;this.clearHeaders();this.setSignature(a);this.uInt16.be=this.uInt32.be;if("WAVE"!=this.format)throw Error('Could not find the "WAVE" format identifier');this.readDs64Chunk_(a);this.readFmtChunk_(a);this.readFactChunk_(a);this.readBextChunk_(a);this.readCueChunk_(a);this.readSmplChunk_(a);this.readDataChunk_(a,b);this.readJunkChunk_(a);this.readLISTChunk_(a)};n.prototype.clearHeaders=function(){var a=new n;Object.assign(this.fmt,a.fmt);Object.assign(this.fact,a.fact);Object.assign(this.cue, a.cue);Object.assign(this.smpl,a.smpl);Object.assign(this.bext,a.bext);Object.assign(this.ds64,a.ds64);Object.assign(this.data,a.data);this.LIST=[];Object.assign(this.junk,a.junk)};n.prototype.readFmtChunk_=function(a){var b=this.findChunk("fmt ");if(b)this.head=b.chunkData.start,this.fmt.chunkId=b.chunkId,this.fmt.chunkSize=b.chunkSize,this.fmt.audioFormat=this.readUInt16_(a),this.fmt.numChannels=this.readUInt16_(a),this.fmt.sampleRate=this.readUInt32(a),this.fmt.byteRate=this.readUInt32(a),this.fmt.blockAlign= this.readUInt16_(a),this.fmt.bitsPerSample=this.readUInt16_(a),this.readFmtExtension_(a);else throw Error('Could not find the "fmt " chunk');};n.prototype.readFmtExtension_=function(a){16<this.fmt.chunkSize&&(this.fmt.cbSize=this.readUInt16_(a),18<this.fmt.chunkSize&&(this.fmt.validBitsPerSample=this.readUInt16_(a),20<this.fmt.chunkSize&&(this.fmt.dwChannelMask=this.readUInt32(a),this.fmt.subformat=[this.readUInt32(a),this.readUInt32(a),this.readUInt32(a),this.readUInt32(a)])))};n.prototype.readFactChunk_= function(a){var b=this.findChunk("fact");b&&(this.head=b.chunkData.start,this.fact.chunkId=b.chunkId,this.fact.chunkSize=b.chunkSize,this.fact.dwSampleLength=this.readUInt32(a))};n.prototype.readCueChunk_=function(a){var b=this.findChunk("cue ");if(b)for(this.head=b.chunkData.start,this.cue.chunkId=b.chunkId,this.cue.chunkSize=b.chunkSize,this.cue.dwCuePoints=this.readUInt32(a),b=0;b<this.cue.dwCuePoints;b++)this.cue.points.push({dwName:this.readUInt32(a),dwPosition:this.readUInt32(a),fccChunk:this.readString(a, 4),dwChunkStart:this.readUInt32(a),dwBlockStart:this.readUInt32(a),dwSampleOffset:this.readUInt32(a)})};n.prototype.readSmplChunk_=function(a){var b=this.findChunk("smpl");if(b)for(this.head=b.chunkData.start,this.smpl.chunkId=b.chunkId,this.smpl.chunkSize=b.chunkSize,this.smpl.dwManufacturer=this.readUInt32(a),this.smpl.dwProduct=this.readUInt32(a),this.smpl.dwSamplePeriod=this.readUInt32(a),this.smpl.dwMIDIUnityNote=this.readUInt32(a),this.smpl.dwMIDIPitchFraction=this.readUInt32(a),this.smpl.dwSMPTEFormat= this.readUInt32(a),this.smpl.dwSMPTEOffset=this.readUInt32(a),this.smpl.dwNumSampleLoops=this.readUInt32(a),this.smpl.dwSamplerData=this.readUInt32(a),b=0;b<this.smpl.dwNumSampleLoops;b++)this.smpl.loops.push({dwName:this.readUInt32(a),dwType:this.readUInt32(a),dwStart:this.readUInt32(a),dwEnd:this.readUInt32(a),dwFraction:this.readUInt32(a),dwPlayCount:this.readUInt32(a)})};n.prototype.readDataChunk_=function(a,b){var c=this.findChunk("data");if(c)this.data.chunkId="data",this.data.chunkSize=c.chunkSize, b&&(this.data.samples=a.slice(c.chunkData.start,c.chunkData.end));else throw Error('Could not find the "data" chunk');};n.prototype.readBextChunk_=function(a){var b=this.findChunk("bext");b&&(this.head=b.chunkData.start,this.bext.chunkId=b.chunkId,this.bext.chunkSize=b.chunkSize,this.bext.description=this.readString(a,256),this.bext.originator=this.readString(a,32),this.bext.originatorReference=this.readString(a,32),this.bext.originationDate=this.readString(a,10),this.bext.originationTime=this.readString(a, 8),this.bext.timeReference=[this.readUInt32(a),this.readUInt32(a)],this.bext.version=this.readUInt16_(a),this.bext.UMID=this.readString(a,64),this.bext.loudnessValue=this.readUInt16_(a),this.bext.loudnessRange=this.readUInt16_(a),this.bext.maxTruePeakLevel=this.readUInt16_(a),this.bext.maxMomentaryLoudness=this.readUInt16_(a),this.bext.maxShortTermLoudness=this.readUInt16_(a),this.bext.reserved=this.readString(a,180),this.bext.codingHistory=this.readString(a,this.bext.chunkSize-602))};n.prototype.readDs64Chunk_= function(a){var b=this.findChunk("ds64");if(b)this.head=b.chunkData.start,this.ds64.chunkId=b.chunkId,this.ds64.chunkSize=b.chunkSize,this.ds64.riffSizeHigh=this.readUInt32(a),this.ds64.riffSizeLow=this.readUInt32(a),this.ds64.dataSizeHigh=this.readUInt32(a),this.ds64.dataSizeLow=this.readUInt32(a),this.ds64.originationTime=this.readUInt32(a),this.ds64.sampleCountHigh=this.readUInt32(a),this.ds64.sampleCountLow=this.readUInt32(a);else if("RF64"==this.container)throw Error('Could not find the "ds64" chunk'); };n.prototype.readLISTChunk_=function(a){var b=this.findChunk("LIST",!0);if(null!==b)for(var c=0;c<b.length;c++){var d=b[c];this.LIST.push({chunkId:d.chunkId,chunkSize:d.chunkSize,format:d.format,subChunks:[]});for(var e=0;e<d.subChunks.length;e++)this.readLISTSubChunks_(d.subChunks[e],d.format,a)}};n.prototype.readLISTSubChunks_=function(a,b,c){"adtl"==b?-1<["labl","note","ltxt"].indexOf(a.chunkId)&&(this.head=a.chunkData.start,b={chunkId:a.chunkId,chunkSize:a.chunkSize,dwName:this.readUInt32(c)}, "ltxt"==a.chunkId&&(b.dwSampleLength=this.readUInt32(c),b.dwPurposeID=this.readUInt32(c),b.dwCountry=this.readUInt16_(c),b.dwLanguage=this.readUInt16_(c),b.dwDialect=this.readUInt16_(c),b.dwCodePage=this.readUInt16_(c)),b.value=this.readZSTR_(c,this.head),this.LIST[this.LIST.length-1].subChunks.push(b)):"INFO"==b&&(this.head=a.chunkData.start,this.LIST[this.LIST.length-1].subChunks.push({chunkId:a.chunkId,chunkSize:a.chunkSize,value:this.readZSTR_(c,this.head)}))};n.prototype.readJunkChunk_=function(a){var b= this.findChunk("junk");b&&(this.junk={chunkId:b.chunkId,chunkSize:b.chunkSize,chunkData:[].slice.call(a.slice(b.chunkData.start,b.chunkData.end))})};n.prototype.readZSTR_=function(a,b){for(var c=b=void 0===b?0:b;c<a.length&&(this.head++,0!==a[c]);c++);return l(a,b,this.head-1)};n.prototype.readUInt16_=function(a){a=z(a,this.uInt16,this.head);this.head+=2;return a};var m=function(){return n.apply(this,arguments)||this};$jscomp.inherits(m,n);m.prototype.toBuffer=function(){this.uInt16.be="RIFX"===this.container; this.uInt32.be=this.uInt16.be;for(var a=[this.getJunkBytes_(),this.getDs64Bytes_(),this.getBextBytes_(),this.getFmtBytes_(),this.getFactBytes_(),p(this.data.chunkId),e(this.data.samples.length,this.uInt32),this.data.samples,this.getCueBytes_(),this.getSmplBytes_(),this.getLISTBytes_()],b=0,c=0;c<a.length;c++)b+=a[c].length;c=new Uint8Array(b+12);var d=A(this.container,c,0);d=D(b+4,this.uInt32,c,d);d=A(this.format,c,d);for(b=0;b<a.length;b++)c.set(a[b],d),d+=a[b].length;return c};m.prototype.getBextBytes_= function(){var a=[];this.enforceBext_();this.bext.chunkId&&(this.bext.chunkSize=602+this.bext.codingHistory.length,a=a.concat(p(this.bext.chunkId),e(602+this.bext.codingHistory.length,this.uInt32),r(this.bext.description,256),r(this.bext.originator,32),r(this.bext.originatorReference,32),r(this.bext.originationDate,10),r(this.bext.originationTime,8),e(this.bext.timeReference[0],this.uInt32),e(this.bext.timeReference[1],this.uInt32),e(this.bext.version,this.uInt16),r(this.bext.UMID,64),e(this.bext.loudnessValue, this.uInt16),e(this.bext.loudnessRange,this.uInt16),e(this.bext.maxTruePeakLevel,this.uInt16),e(this.bext.maxMomentaryLoudness,this.uInt16),e(this.bext.maxShortTermLoudness,this.uInt16),r(this.bext.reserved,180),r(this.bext.codingHistory,this.bext.codingHistory.length)));return a};m.prototype.enforceBext_=function(){for(var a in this.bext)if(this.bext.hasOwnProperty(a)&&this.bext[a]&&"timeReference"!=a){this.bext.chunkId="bext";break}if(this.bext.timeReference[0]||this.bext.timeReference[1])this.bext.chunkId= "bext"};m.prototype.getDs64Bytes_=function(){var a=[];this.ds64.chunkId&&(a=a.concat(p(this.ds64.chunkId),e(this.ds64.chunkSize,this.uInt32),e(this.ds64.riffSizeHigh,this.uInt32),e(this.ds64.riffSizeLow,this.uInt32),e(this.ds64.dataSizeHigh,this.uInt32),e(this.ds64.dataSizeLow,this.uInt32),e(this.ds64.originationTime,this.uInt32),e(this.ds64.sampleCountHigh,this.uInt32),e(this.ds64.sampleCountLow,this.uInt32)));return a};m.prototype.getCueBytes_=function(){var a=[];if(this.cue.chunkId){var b=this.getCuePointsBytes_(); a=a.concat(p(this.cue.chunkId),e(b.length+4,this.uInt32),e(this.cue.dwCuePoints,this.uInt32),b)}return a};m.prototype.getCuePointsBytes_=function(){for(var a=[],b=0;b<this.cue.dwCuePoints;b++)a=a.concat(e(this.cue.points[b].dwName,this.uInt32),e(this.cue.points[b].dwPosition,this.uInt32),p(this.cue.points[b].fccChunk),e(this.cue.points[b].dwChunkStart,this.uInt32),e(this.cue.points[b].dwBlockStart,this.uInt32),e(this.cue.points[b].dwSampleOffset,this.uInt32));return a};m.prototype.getSmplBytes_=function(){var a= [];if(this.smpl.chunkId){var b=this.getSmplLoopsBytes_();a=a.concat(p(this.smpl.chunkId),e(b.length+36,this.uInt32),e(this.smpl.dwManufacturer,this.uInt32),e(this.smpl.dwProduct,this.uInt32),e(this.smpl.dwSamplePeriod,this.uInt32),e(this.smpl.dwMIDIUnityNote,this.uInt32),e(this.smpl.dwMIDIPitchFraction,this.uInt32),e(this.smpl.dwSMPTEFormat,this.uInt32),e(this.smpl.dwSMPTEOffset,this.uInt32),e(this.smpl.dwNumSampleLoops,this.uInt32),e(this.smpl.dwSamplerData,this.uInt32),b)}return a};m.prototype.getSmplLoopsBytes_= function(){for(var a=[],b=0;b<this.smpl.dwNumSampleLoops;b++)a=a.concat(e(this.smpl.loops[b].dwName,this.uInt32),e(this.smpl.loops[b].dwType,this.uInt32),e(this.smpl.loops[b].dwStart,this.uInt32),e(this.smpl.loops[b].dwEnd,this.uInt32),e(this.smpl.loops[b].dwFraction,this.uInt32),e(this.smpl.loops[b].dwPlayCount,this.uInt32));return a};m.prototype.getFactBytes_=function(){var a=[];this.fact.chunkId&&(a=a.concat(p(this.fact.chunkId),e(this.fact.chunkSize,this.uInt32),e(this.fact.dwSampleLength,this.uInt32))); return a};m.prototype.getFmtBytes_=function(){var a=[];if(this.fmt.chunkId)return a.concat(p(this.fmt.chunkId),e(this.fmt.chunkSize,this.uInt32),e(this.fmt.audioFormat,this.uInt16),e(this.fmt.numChannels,this.uInt16),e(this.fmt.sampleRate,this.uInt32),e(this.fmt.byteRate,this.uInt32),e(this.fmt.blockAlign,this.uInt16),e(this.fmt.bitsPerSample,this.uInt16),this.getFmtExtensionBytes_());throw Error('Could not find the "fmt " chunk');};m.prototype.getFmtExtensionBytes_=function(){var a=[];16<this.fmt.chunkSize&& (a=a.concat(e(this.fmt.cbSize,this.uInt16)));18<this.fmt.chunkSize&&(a=a.concat(e(this.fmt.validBitsPerSample,this.uInt16)));20<this.fmt.chunkSize&&(a=a.concat(e(this.fmt.dwChannelMask,this.uInt32)));24<this.fmt.chunkSize&&(a=a.concat(e(this.fmt.subformat[0],this.uInt32),e(this.fmt.subformat[1],this.uInt32),e(this.fmt.subformat[2],this.uInt32),e(this.fmt.subformat[3],this.uInt32)));return a};m.prototype.getLISTBytes_=function(){for(var a=[],b=0;b<this.LIST.length;b++){var c=this.getLISTSubChunksBytes_(this.LIST[b].subChunks, this.LIST[b].format);a=a.concat(p(this.LIST[b].chunkId),e(c.length+4,this.uInt32),p(this.LIST[b].format),c)}return a};m.prototype.getLISTSubChunksBytes_=function(a,b){for(var c=[],d=0;d<a.length;d++)"INFO"==b?(c=c.concat(p(a[d].chunkId),e(a[d].value.length+1,this.uInt32),r(a[d].value,a[d].value.length)),c.push(0)):"adtl"==b&&(-1<["labl","note"].indexOf(a[d].chunkId)?(c=c.concat(p(a[d].chunkId),e(a[d].value.length+4+1,this.uInt32),e(a[d].dwName,this.uInt32),r(a[d].value,a[d].value.length)),c.push(0)): "ltxt"==a[d].chunkId&&(c=c.concat(this.getLtxtChunkBytes_(a[d])))),c.length%2&&c.push(0);return c};m.prototype.getLtxtChunkBytes_=function(a){return[].concat(p(a.chunkId),e(a.value.length+20,this.uInt32),e(a.dwName,this.uInt32),e(a.dwSampleLength,this.uInt32),e(a.dwPurposeID,this.uInt32),e(a.dwCountry,this.uInt16),e(a.dwLanguage,this.uInt16),e(a.dwDialect,this.uInt16),e(a.dwCodePage,this.uInt16),r(a.value,a.value.length))};m.prototype.getJunkBytes_=function(){var a=[];return this.junk.chunkId?a.concat(p(this.junk.chunkId), e(this.junk.chunkData.length,this.uInt32),this.junk.chunkData):a};var q=function(a,b){a=void 0===a?null:a;b=void 0===b?!0:b;var c=m.call(this)||this;c.bitDepth="0";c.dataType={be:!1,bits:0,fp:!1,signed:!1};c.WAV_AUDIO_FORMATS={4:17,8:1,"8a":6,"8m":7,16:1,24:1,32:1,"32f":3,64:3};a&&c.fromBuffer(a,b);return c};$jscomp.inherits(q,m);q.prototype.fromBuffer=function(a,b){m.prototype.fromBuffer.call(this,a,void 0===b?!0:b);this.bitDepthFromFmt_();this.updateDataType_()};q.prototype.toBuffer=function(){this.validateWavHeader_(); return m.prototype.toBuffer.call(this)};q.prototype.fromScratch=function(a,b,c,d,e){e=void 0===e?{}:e;this.clearHeaders();this.newWavFile_(a,b,c,d,e)};q.prototype.newWavFile_=function(a,b,c,d,e){e=void 0===e?{}:e;e.container||(e.container="RIFF");this.container=e.container;this.bitDepth=c;if(0<d.length&&d[0].constructor===Array){for(var g=[],f=0,h=d[0].length;f<h;f++)for(var k=0,l=d.length;k<l;k++)g.push(d[k][f]);d=g}this.updateDataType_();g=this.dataType.bits/8;this.data.samples=new Uint8Array(d.length* g);B(d,this.dataType,this.data.samples);this.makeWavHeader_(c,a,b,g,this.data.samples.length,e);this.data.chunkId="data";this.data.chunkSize=this.data.samples.length;this.validateWavHeader_()};q.prototype.makeWavHeader_=function(a,b,c,d,e,f){"4"==a?this.createADPCMHeader_(a,b,c,d,e,f):"8a"==a||"8m"==a?this.createALawMulawHeader_(a,b,c,d,e,f):-1==Object.keys(this.WAV_AUDIO_FORMATS).indexOf(a)||2<b?this.createExtensibleHeader_(a,b,c,d,e,f):this.createPCMHeader_(a,b,c,d,e,f)};q.prototype.createPCMHeader_= function(a,b,c,d,e,f){this.container=f.container;this.chunkSize=36+e;this.format="WAVE";this.bitDepth=a;this.fmt={chunkId:"fmt ",chunkSize:16,audioFormat:this.WAV_AUDIO_FORMATS[a]||65534,numChannels:b,sampleRate:c,byteRate:b*d*c,blockAlign:b*d,bitsPerSample:parseInt(a,10),cbSize:0,validBitsPerSample:0,dwChannelMask:0,subformat:[]}};q.prototype.createADPCMHeader_=function(a,b,c,d,e,f){this.createPCMHeader_(a,b,c,d,e,f);this.chunkSize=40+e;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*e}};q.prototype.createExtensibleHeader_=function(a,b,c,d,e,f){this.createPCMHeader_(a,b,c,d,e,f);this.chunkSize=60+e;this.fmt.chunkSize=40;this.fmt.bitsPerSample=(parseInt(a,10)-1|7)+1;this.fmt.cbSize=22;this.fmt.validBitsPerSample=parseInt(a,10);a=this.fmt;c=0;1===b?c=4:2===b?c=3:4===b?c=51:6===b?c=63:8===b&&(c=1599);a.dwChannelMask=c;this.fmt.subformat=[1,1048576, 2852126848,1905997824]};q.prototype.createALawMulawHeader_=function(a,b,c,d,e,f){this.createPCMHeader_(a,b,c,d,e,f);this.chunkSize=40+e;this.fmt.chunkSize=20;this.fmt.cbSize=2;this.fmt.validBitsPerSample=8;this.fact={chunkId:"fact",chunkSize:4,dwSampleLength:e}};q.prototype.bitDepthFromFmt_=function(){this.bitDepth=3===this.fmt.audioFormat&&32===this.fmt.bitsPerSample?"32f":6===this.fmt.audioFormat?"8a":7===this.fmt.audioFormat?"8m":""+this.fmt.bitsPerSample};q.prototype.validateBitDepth_=function(){if(!this.WAV_AUDIO_FORMATS[this.bitDepth]){if(8< parseInt(this.bitDepth,10)&&54>parseInt(this.bitDepth,10))return!0;throw Error("Invalid bit depth.");}return!0};q.prototype.updateDataType_=function(){this.dataType={bits:(parseInt(this.bitDepth,10)-1|7)+1,fp:"32f"==this.bitDepth||"64"==this.bitDepth,signed:"8"!=this.bitDepth,be:"RIFX"==this.container};-1<["4","8a","8m"].indexOf(this.bitDepth)&&(this.dataType.bits=8,this.dataType.signed=!1)};q.prototype.validateWavHeader_=function(){this.validateBitDepth_();var a=this.fmt.numChannels;a=1>a||65535< a*this.fmt.bitsPerSample/8?!1:!0;if(!a)throw Error("Invalid number of channels.");a=this.fmt.sampleRate;a=1>a||4294967295<this.fmt.bitsPerSample/8*this.fmt.numChannels*a?!1:!0;if(!a)throw Error("Invalid sample rate.");};d.WaveFileCreator=q;Object.defineProperty(d,"__esModule",{value:!0})});