From 252707148cc6659da97c76624376846cfec692a3 Mon Sep 17 00:00:00 2001 From: Rafael Rocha Date: Wed, 25 Apr 2018 01:16:47 -0300 Subject: [PATCH] using riff-chunks ^3.1.0 to avoid duplicated dependencies --- dist/wavefile-min.js | 137 ++-- dist/wavefile.js | 1565 ++++++------------------------------------ docs/WaveFile.html | 2 +- docs/index.html | 2 +- docs/index.js.html | 2 +- package.json | 4 +- 6 files changed, 292 insertions(+), 1420 deletions(-) diff --git a/dist/wavefile-min.js b/dist/wavefile-min.js index 9ee749d..53279e8 100644 --- a/dist/wavefile-min.js +++ b/dist/wavefile-min.js @@ -4,11 +4,6 @@ Copyright (c) 2017-2018 Rafael da Silva Rocha. https://github.com/rochars/byte-data - endianness - Swap endianness in byte arrays. - Copyright (c) 2017 Rafael da Silva Rocha. - https://github.com/rochars/endianness - alawmulaw JavaScript A-Law and mu-Law codecs. Copyright (c) 2018 Rafael da Silva Rocha. @@ -19,6 +14,11 @@ Copyright (c) 2017-2018 Rafael da Silva Rocha. https://github.com/rochars/wavefile + endianness + Swap endianness in byte arrays. + Copyright (c) 2017 Rafael da Silva Rocha. + https://github.com/rochars/endianness + bitdepth Change the bit depth of samples to and from 8, 16, 24, 32 & 64-bit. Copyright (c) 2017 Rafael da Silva Rocha. @@ -29,11 +29,6 @@ Copyright (c) 2017 Rafael da Silva Rocha. https://github.com/rochars/riff-chunks - byte-data - Readable data to and from byte buffers. - Copyright (c) 2017 Rafael da Silva Rocha. - https://github.com/rochars/byte-data - imaadpcm JavaScript IMA ADPCM codec. Copyright (c) 2018 Rafael da Silva Rocha. @@ -45,70 +40,58 @@ https://wiki.multimedia.cx/index.php/IMA_ADPCM */ -var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.objectCreate=$jscomp.ASSUME_ES5||"function"==typeof Object.create?Object.create:function(b){var a=function(){};a.prototype=b;return new a};$jscomp.underscoreProtoCanBeSet=function(){var b={a:!0},a={};try{return a.__proto__=b,a.a}catch(k){}return!1}; -$jscomp.setPrototypeOf="function"==typeof Object.setPrototypeOf?Object.setPrototypeOf:$jscomp.underscoreProtoCanBeSet()?function(b,a){b.__proto__=a;if(b.__proto__!==a)throw new TypeError(b+" is not extensible");return b}:null; -$jscomp.inherits=function(b,a){b.prototype=$jscomp.objectCreate(a.prototype);b.prototype.constructor=b;if($jscomp.setPrototypeOf){var k=$jscomp.setPrototypeOf;k(b,a)}else for(k in a)if("prototype"!=k)if(Object.defineProperties){var e=Object.getOwnPropertyDescriptor(a,k);e&&Object.defineProperty(b,k,e)}else b[k]=a[k];b.superClass_=a.prototype};$jscomp.owns=function(b,a){return Object.prototype.hasOwnProperty.call(b,a)}; -$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(b,a,k){b!=Array.prototype&&b!=Object.prototype&&(b[a]=k.value)};$jscomp.getGlobal=function(b){return"undefined"!=typeof window&&window===b?b:"undefined"!=typeof global&&null!=global?global:b};$jscomp.global=$jscomp.getGlobal(this); -$jscomp.polyfill=function(b,a,k,e){if(a){k=$jscomp.global;b=b.split(".");for(e=0;ea.bits?a.bits:a.realBits/8),f.getType(a,g));return c=a.char?c.slice(0,a.bits/8):c[0]}var f=k(7),c=k(1);b.exports.pack=e;b.exports.unpack=h;b.exports.packArray=function(c,a,g){return f.toBytes(c,f.getType(a,void 0===g?10:g))};b.exports.unpackArray=function(c,a,g){return f.fromBytes(c,f.getType(a,void 0=== -g?10:g))};b.exports.unpackStruct=function(c,a,g){g=void 0===g?10:g;for(var d=c.length,n=0,b=0;ba[n].bits?1:a[n].realBits/8;d=d.concat(h(c.slice(b,b+f),a[n],g));b+=f;n++}return d};b.exports.packStruct=function(c,a,g){g=void 0===g?10:g;if(c.length>10;var d=c&1023;return(a?Math.pow(2,a-15)*(1+d/1024):d/1024*.00006103515625)* -(c>>15?-1:1)};a.prototype.read32F_=function(c,a){h[0]=this.read_(c,a,{bits:32,offset:4});return e[0]};a.prototype.read64F_=function(a,b){c[0]=this.read_(a,b,{bits:32,offset:4});c[1]=this.read_(a,b+4,{bits:32,offset:4});return f[0]};a.prototype.readChar_=function(c,a){for(var d="",b=0;b>16&32768;var g=b>>12&2047;b=b>>23&255;103<=b&&(a=(a|b-112<<10|g>>1)+(g&1));c[d++]=a&255;c[d++]=a>>>8&255;return d};a.prototype.writeChar_=function(c,a,d){c[d++]=a.charCodeAt(0);return d};a.prototype.buildType_=function(){this.setReader_();this.setWriter_();this.float&&(this.min=-Infinity,this.max=Infinity)};a.prototype.setReader_=function(){this.float?16==this.bits?this.reader=this.read16F_: -32==this.bits?this.reader=this.read32F_:64==this.bits&&(this.reader=this.read64F_):this.char?this.reader=this.readChar_:32this.max&&(a-=2*this.max+2);return a};a.prototype.overflow=function(a){a>this.max?a=this.max:athis.bits?1:this.realBits/8;this.setReader_();this.setWriter_();this.float||this.setMinMax_()}; -a.prototype.setReader_=function(){this.reader=this.char?e.BitReader.readChar:e.BitReader["read"+(8>this.bits?8:this.realBits)+"Bit"+(this.float?"Float":"")]};a.prototype.setWriter_=function(){this.writer=this.char?e.BitWriter.writeString:e.BitWriter["write"+this.realBits+"Bit"+(this.float?"Float":"")]};a.prototype.setMinMax_=function(){var a=Math.pow(2,this.bits);this.signed?(this.max=a/2-1,this.min=-a/2):(this.max=a-1,this.min=0)};a.prototype.setRealBits_=function(){this.realBits=8= -this.bits?16:24>=this.bits?24:32>=this.bits?32:40>=this.bits?40:48>=this.bits?48:53>=this.bits?53:64:this.bits};b.exports=a},function(b,a,k){b.exports.alaw=k(17);b.exports.mulaw=k(18)},function(b,a,k){k(0);var e=k(9),h=k(3),f=k(10),c=k(12),l=k(16),m=k(5).alaw,g=k(5).mulaw;b=function(d){var a=f.call(this)||this;d&&a.fromBuffer(d);return a};$jscomp.inherits(b,f);b.prototype.fromScratch=function(d,a,c,b,g){g=void 0===g?{}:g;this.validBitsPerSample=this.cbSize=0;this.factChunkId="";this.factChunkSize= -0;this.factChunkData=[];this.dwSampleLength=0;g.container||(g.container="RIFF");var n=parseInt(c,10)/8;this.chunkSize=36+b.length*n;this.fmtChunkSize=16;this.byteRate=d*n*a;this.blockAlign=d*n;this.chunkId=g.container;this.format="WAVE";this.fmtChunkId="fmt ";this.audioFormat=this.headerFormats_[c];this.numChannels=d;this.sampleRate=a;this.bitsPerSample=parseInt(c,10);this.dataChunkId="data";this.dataChunkSize=b.length*n;this.samples=b;this.bitDepth=c;"4"==c&&(this.chunkSize=44+b.length,this.fmtChunkSize= -20,this.byteRate=4055,this.blockAlign=256,this.bitsPerSample=4,this.dataChunkSize=b.length,this.cbSize=2,this.validBitsPerSample=505,this.factChunkId="fact",this.factChunkSize=4,this.dwSampleLength=2*b.length);if("8a"==c||"8m"==c)this.chunkSize=44+b.length,this.fmtChunkSize=20,this.cbSize=2,this.validBitsPerSample=8,this.factChunkId="fact",this.factChunkSize=4,this.dwSampleLength=b.length};b.prototype.fromBuffer=function(d){this.readRIFFChunk_(d);var a="RIFX"==this.chunkId;d=c.read(d,a);this.readFmtChunk_(d.subChunks); -this.readFactChunk_(d.subChunks);this.readBextChunk_(d.subChunks);this.readCueChunk_(d.subChunks);this.readDataChunk_(d.subChunks,{be:a,single:!0});this.bitDepth=3==this.audioFormat&&32==this.bitsPerSample?"32f":this.bitsPerSample.toString()};b.prototype.toBuffer=function(){this.checkWriteInput_();return new Uint8Array(this.createWaveFile_())};b.prototype.toRIFF=function(){this.chunkId="RIFF";this.LEorBE()};b.prototype.toRIFX=function(){this.chunkId="RIFX";this.LEorBE()};b.prototype.toBitDepth=function(d){e.toBitDepth(this.samples, -this.bitDepth,d);this.fromScratch(this.numChannels,this.sampleRate,d,this.samples,{container:this.chunkId})};b.prototype.interleave=function(){var d=[],a,c,b=this.samples[0].length;for(a=0;athis.numChannels||65535this.sampleRate||4294967295this.max&&(a-=2*this.max+2);return a};a.prototype.overflow=function(a){a>this.max?a=this.max:a>>0;return this.overflow(this.sign(f))};a.prototype.readBits_=function(a,b,h){h=void 0===h?this:h;for(var f="",c=0;cthis.bits?1:Math.ceil(this.realBits/8)};a.prototype.setMinMax_=function(){var a=Math.pow(2,this.bits);this.signed?(this.max=a/2-1,this.min=-a/2):(this.max=a-1,this.min=0)};a.prototype.validateWordSize_=function(){if(1>this.bits||64=this.bits?16:24>=this.bits?24:32>=this.bits? -32:40>=this.bits?40:48>=this.bits?48:56>=this.bits?56:64:this.bits};a.prototype.setLastByteMask_=function(){var a=8-(this.realBits-this.bits);this.lastByteMask=Math.pow(2,0f.bits?a[h++]=0>b?b+Math.pow(2,f.bits):b:a[h++]=b&255;return h};b.exports=a},function(b,a){var k=new Float32Array(1),e={8:256,16:65536,24:16777216,32:4294967296,"32f":1,64:1},h={intToInt:function(a,c){return a=0c.bits?c.bits:c.realBits/8),f.getType(c,b));return a=c.char?a.slice(0,c.bits/8):a[0]}var f=k(14),c=k(4);a.pack=e;a.unpack=h;a.packArray=function(a,c,b){return f.toBytes(a,f.getType(c,void 0===b?10:b))};a.unpackArray=function(a,c,b){return f.fromBytes(a,f.getType(c,void 0===b?10:b))};a.unpackStruct= -function(a,c,b){b=void 0===b?10:b;for(var d=a.length,g=0,f=0;fc[g].bits?1:c[g].realBits/8;d=d.concat(h(a.slice(f,f+e),c[g],b));f+=e;g++}return d};a.packStruct=function(a,c,b){b=void 0===b?10:b;if(a.lengthc.realBits?(2==c.base?c.bits:5>c.bits?1:2)+1:(2==c.base?8:2)+1;a>10;c=b&1023;return(a?Math.pow(2,a-15)*(1+c/1024):c/1024*.00006103515625)*(b>>15?-1:1)},read24Bit:function(a, -c){return a[2+c]<<16|l.read16Bit(a,c)},read32Bit:function(a,c){return(a[3+c]<<24|l.read24Bit(a,c))>>>0},read32BitFloat:function(a,c){h[0]=l.read32Bit(a,c);return e[0]},read40Bit:function(a,c){return k(a,c,5)},read48Bit:function(a,c){return k(a,c,6)},read53Bit:function(a,c){return k(a,c,7)},read64BitFloat:function(a,b){c[0]=l.read32Bit(a,b);c[1]=l.read32Bit(a,b+4);return f[0]},readChar:function(a,c,b){var d="",e=0;for(b=b.bits/8;e>>24&255; -return b},write24Bit:function(a,c,b){b=m.write16Bit(a,c,b);a[b++]=c>>>16&255;return b},write16Bit:function(a,c,b){a[b++]=c&255;a[b++]=c>>>8&255;return b},write16BitFloat:function(a,c,b){e[0]=c;var d=h[0];c=d>>16&32768;var f=d>>12&2047;d=d>>23&255;103<=d&&(c=(c|d-112<<10|f>>1)+(f&1));a[b]=c&255;a[b+1]=c>>>8&255;return b+2},write8Bit:function(a,c,b){a[b++]=c&255;return b},write4Bit:function(a,c,b){a[b++]=c&15;return b},write2Bit:function(a,c,b){a[b++]=0>c?c+4:c;return b},write1Bit:function(a,c,b){a[b++]= -c?1:0;return b},writeString:function(a,c,b){a[b++]=c.charCodeAt(0);return b}};a.BitWriter=m;a.BitReader=l},function(b,a,k){function e(a){a-=n;if(0<=a)var c=0;else c=8,a=-a;var b=d[r],e=b>>3;a>b&&(c|=4,a-=b,e+=b);b>>=1;a>b&&(c|=2,a-=b,e+=b);b>>=1;a>b&&(c|=1,e+=b);n=c&8?n-e:n+e;-32768>n?n=-32768:32767r?r=0:88>1);a&1&&(c+=t>>2);c+=t>>3;a&8&&(c=-c);p+=c;32767p&&(p=-32767);q+=g[a];0>q?q=0:88b.length;)b.push(0);return b}function c(a){p=l.unpack([a[0],a[1]],m);q=a[2];t=d[q];for(var c=[p,l.unpack([a[2],a[3]],m)],b=4;b>4;c.push(h(f<<4^e));c.push(h(f))}return c}var l=k(0),m=l.int16,g=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],d=[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],n=0,r=0,p=0,q=0,t=7;b.exports.encode=function(a){for(var c=[],b=[],d=0;d>8&128)||(a*=-1);32635>8&127],a=b<<4|a>>b+3&15):a>>=4;return a^c^85}function e(a){var b=0;a^=85;a&128&&(a&=-129,b=-1);var c=((a&240)>>4)+4;a=4!=c?1< -a){a=132-a;var b=127}else a+=132,b=255;32767>7;e&240&&(e>>=4,c+=4);e&12&&(e>>=2,c+=2);e&2&&(c+=1);e=c;return(e<<4|a>>e+3&15)^b}function e(a){a=~a;var b=((a&15)<<3)+132<<((a&112)>>4);return a&128?132-b:b-132}b.exports.encodeSample=k;b.exports.decodeSample=e;b.exports.encode=function(a){for(var b=[],c=0;cb.bits?b.bits:b.realBits/8),e.getType(b,c));return a=b.char?a.slice(0,b.bits/8):a[0]}var e=f(5),b=f(1);a.exports.pack=c;a.exports.unpack=h;a.exports.packArray=function(a,b,c){return e.toBytes(a,e.getType(b,void 0===c?10:c))};a.exports.unpackArray=function(a,b,c){return e.fromBytes(a,e.getType(b,void 0=== +c?10:c))};a.exports.unpackStruct=function(a,b,c){c=void 0===c?10:c;for(var g=a.length,e=0,d=0;db[e].bits?1:b[e].realBits/8;g=g.concat(h(a.slice(d,d+f),b[e],c));d+=f;e++}return g};a.exports.packStruct=function(a,b,e){e=void 0===e?10:e;if(a.length>10;var g=a&1023;return(b?Math.pow(2,b-15)*(1+g/1024):g/1024*.00006103515625)* +(a>>15?-1:1)};d.prototype.read32F_=function(a,b){h[0]=this.read_(a,b,{bits:32,offset:4});return c[0]};d.prototype.read64F_=function(a,c){b[0]=this.read_(a,c,{bits:32,offset:4});b[1]=this.read_(a,c+4,{bits:32,offset:4});return e[0]};d.prototype.readChar_=function(a,b){for(var g="",c=0;c>16&32768;var d=e>>12&2047;e=e>>23&255;103<=e&&(b=(b|e-112<<10|d>>1)+(d&1));a[g++]=b&255;a[g++]=b>>>8&255;return g};d.prototype.writeChar_=function(a,b,g){a[g++]=b.charCodeAt(0);return g};d.prototype.buildType_=function(){this.setReader_();this.setWriter_();this.float&&(this.min=-Infinity,this.max=Infinity)};d.prototype.setReader_=function(){this.float?16==this.bits?this.reader=this.read16F_: +32==this.bits?this.reader=this.read32F_:64==this.bits&&(this.reader=this.read64F_):this.char?this.reader=this.readChar_:32this.numChannels||65535this.sampleRate||4294967295this.max&&(a-=2*this.max+2);return a};d.prototype.overflow=function(a){a>this.max?a=this.max: +a>>0;return this.overflow(this.sign(e))};d.prototype.readBits_=function(a,c,d){d=void 0===d?this:d;for(var e="",b=0;bthis.bits?1:Math.ceil(this.realBits/8)};d.prototype.setMinMax_=function(){var a=Math.pow(2,this.bits);this.signed?(this.max=a/2-1,this.min=-a/2):(this.max=a-1,this.min=0)};d.prototype.validateWordSize_=function(){if(1>this.bits||64=this.bits?16:24>=this.bits?24:32>=this.bits?32:40>=this.bits?40:48>=this.bits?48:56>=this.bits?56:64:this.bits};d.prototype.setLastByteMask_=function(){var a=8-(this.realBits-this.bits);this.lastByteMask=Math.pow(2,0e.bits?a[d++]=0>c?c+Math.pow(2,e.bits):c:a[d++]=c&255;return d};a.exports=d},function(a,d){a.exports=function(a,c){for(var d=a.length,e=0;e< +d;){for(var b=a,f=e,l=0,m=c-1,g=parseInt(c/2,10);l>3;a>c&&(b|=4,a-=c,d+=c);c>>=1;a>c&&(b|=2,a-=c,d+=c);c>>=1;a>c&&(b|=1,d+=c);n=b&8?n-d:n+d;-32768>n?n=-32768:32767r?r=0:88>1);a&1&&(b+=t>>2);b+=t>>3;a&8&&(b=-b);p+=b;32767p&&(p=-32767);q+=m[a];0>q?q=0:88d.length;)d.push(0);return d}function b(a){p=k.unpack([a[0],a[1]],l);q=a[2];t=g[q];for(var b=[p,k.unpack([a[2],a[3]],l)],c=4;c>4;b.push(h(e<< +4^d));b.push(h(e))}return b}var k=f(0),l=k.int16,m=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],g=[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],n=0,r=0,p=0,q=0,t=7;a.exports.encode= +function(a){for(var b=[],c=[],d=0;d>8&128)||(a*=-1);32635>8&127],a=c<<4|a>>c+3&15):a>>=4;return a^b^85}function c(a){var c=0;a^=85;a&128&&(a&=-129,c=-1);var b=((a&240)>>4)+4;a=4!=b?1<a){a=132-a;var c=127}else a+=132,c=255;32767>7;d&240&&(d>>=4,b+=4);d&12&&(d>>=2,b+=2);d&2&&(b+=1);d=b;return(d<<4|a>>d+3&15)^c}function c(a){a=~a;var c=((a&15)<<3)+132<<((a&112)>>4);return a&128?132-c:c-132}a.exports.encodeSample=f;a.exports.decodeSample=c;a.exports.encode=function(a){for(var c=[],b=0;b|!Array|Uint8Array} bytes The bytes. - * @param {number} offset The number of bytes of each unit of information. - */ -function endianness(bytes, offset) { - let len = bytes.length; - let i = 0; - while (i < len) { - swap(bytes, offset, i); - i += offset; - } -} - -/** - * Swap the endianness of a unit of information in a byte array. - * The original array is modified in-place. - * @param {!Array|!Array|Uint8Array} bytes The bytes. - * @param {number} offset The number of bytes of the unit of information. - * @param {number} index The start index of the unit of information. - */ -function swap(bytes, offset, index) { - let x = 0; - let y = offset - 1; - let limit = parseInt(offset / 2, 10); - while(x < limit) { - let theByte = bytes[index + x]; - bytes[index + x] = bytes[index + y]; - bytes[index + y] = theByte; - x++; - y--; - } -} - -module.exports = endianness; - - -/***/ }), -/* 3 */ -/***/ (function(module, exports) { - /* * wavefile * Read & write wave files with 8, 16, 24, 32 & 64-bit data. @@ -727,212 +677,7 @@ module.exports = { }; /***/ }), -/* 4 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * type: The Type class. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - - -const bitParser = __webpack_require__(15); - -/** - * A class to represent byte-data types. - */ -class Type { - - /** - * @param {Object} options The type definition. - * @param {number} options.bits Number of bits used by data of this type. - * @param {boolean} options.char True for string/char types. - * @param {boolean} options.float True for float types. - * Available only for 16, 32 and 64-bit data. - * @param {boolean} options.be True for signed types. - * @param {boolean} options.signed True for signed types. - */ - constructor(options) { - /** - * The max number of bits used by data of this type. - * @type {number} - */ - this.bits = options["bits"]; - /** - * If this type represent floating-point values or not. - * @type {boolean} - */ - this.char = options["char"]; - /** - * If this type it is signed or not. - * @type {boolean} - */ - this.float = options["float"]; - /** - * If this type is big-endian or not. - * @type {boolean} - */ - this.be = options["be"]; - /** - * If this type it is signed or not. - * @type {boolean} - */ - this.signed = this.float ? true : options["signed"]; - /** - * The function to read values of this type from buffers. - * @type {Function} - */ - this.reader = null; - /** - * The function to write values of this type to buffers. - * @type {Function} - */ - this.writer = null; - /** - * The number of bytes used by data of this type. - * @type {number} - */ - this.offset = 0; - /** - * The base used to represent data of this type. - * Default is 10. - * @type {number} - */ - this.base = options["base"] ? options["base"] : 10; - /** - * Min value for numbers of this type. - * @type {number} - */ - this.min = -Infinity; - /** - * Max value for numbers of this type. - * @type {number} - */ - this.max = Infinity; - this.build_(); - this.realBits = this.bits; - } - - /** - * Sign a number according to the type. - * @param {number} num The number. - * @return {number} - */ - sign(num) { - if (num > this.max) { - num -= (this.max * 2) + 2; - } - return num; - } - - /** - * Limit the value according to the bit depth in case of - * overflow or underflow. - * @param {number} value The data. - * @return {number} - */ - overflow(value) { - if (value > this.max) { - value = this.max; - } else if (value < this.min) { - value = this.min; - } - return value; - } - - /** - * Build the type. - * @private - */ - build_() { - this.setRealBits_(); - this.offset = this.bits < 8 ? 1 : this.realBits / 8; - this.setReader_(); - this.setWriter_(); - if (!this.float) { - this.setMinMax_(); - } - } - - /** - * Set the function to read data of this type. - * @private - */ - setReader_() { - if (this.char) { - this.reader = bitParser.BitReader["readChar"]; - } else { - this.reader = bitParser.BitReader[ - 'read' + (this.bits < 8 ? 8 : this.realBits) + - 'Bit' + (this.float ? "Float" : "")]; - } - } - - /** - * Set the function to write data of this type. - * @private - */ - setWriter_() { - if (this.char) { - this.writer = bitParser.BitWriter["writeString"]; - } else { - this.writer = bitParser.BitWriter[ - 'write' + this.realBits + 'Bit' + (this.float ? "Float" : "")]; - } - } - - /** - * Set the minimum and maximum values for the type. - * @private - */ - setMinMax_() { - let max = Math.pow(2, this.bits); - if (this.signed) { - this.max = (max / 2) -1; - this.min = -max / 2; - } else { - this.max = max - 1; - this.min = 0; - } - } - - /** - * Set the real bit depth for data with bit count different from the - * standard types (1, 2, 4, 8, 16, 32, 40, 48, 64): the closest bigger - * standard number of bits. The data is then treated as data of the - * standard type on all aspects except for the min and max values. - * Ex: a 11-bit uInt is treated as 16-bit uInt with a max value of 2048. - * @private - */ - setRealBits_() { - if (this.bits > 8) { - if (this.bits <= 16) { - this.realBits = 16; - } else if (this.bits <= 24) { - this.realBits = 24; - } else if (this.bits <= 32) { - this.realBits = 32; - } else if (this.bits <= 40) { - this.realBits = 40; - } else if (this.bits <= 48) { - this.realBits = 48; - } else if (this.bits <= 53) { - this.realBits = 53; - } else { - this.realBits = 64; - } - } else { - this.realBits = this.bits; - } - } -} - -module.exports = Type; - - -/***/ }), -/* 5 */ +/* 3 */ /***/ (function(module, exports, __webpack_require__) { /*! @@ -943,12 +688,12 @@ module.exports = Type; * */ -module.exports.alaw = __webpack_require__(17); -module.exports.mulaw = __webpack_require__(18); +module.exports.alaw = __webpack_require__(13); +module.exports.mulaw = __webpack_require__(14); /***/ }), -/* 6 */ +/* 4 */ /***/ (function(module, exports, __webpack_require__) { /*! @@ -961,13 +706,13 @@ module.exports.mulaw = __webpack_require__(18); const byteData = __webpack_require__(0); const uInt8 = byteData.uInt8; -const bitDepthLib = __webpack_require__(9); -const WaveErrors = __webpack_require__(3); -const WaveFileReaderWriter = __webpack_require__(10); -const riffChunks = __webpack_require__(12); -const adpcm = __webpack_require__(16); -const alaw = __webpack_require__(5).alaw; -const mulaw = __webpack_require__(5).mulaw; +const bitDepthLib = __webpack_require__(8); +const WaveErrors = __webpack_require__(2); +const WaveFileReaderWriter = __webpack_require__(9); +const riffChunks = __webpack_require__(11); +const adpcm = __webpack_require__(12); +const alaw = __webpack_require__(3).alaw; +const mulaw = __webpack_require__(3).mulaw; /** * WaveFile @@ -1277,7 +1022,7 @@ window['WaveFile'] = WaveFile; /***/ }), -/* 7 */ +/* 5 */ /***/ (function(module, exports, __webpack_require__) { /* @@ -1286,7 +1031,7 @@ window['WaveFile'] = WaveFile; */ const Type = __webpack_require__(1); -const endianness = __webpack_require__(2); +const endianness = __webpack_require__(7); /** * Turn a byte buffer into what the bytes represent. @@ -1420,7 +1165,7 @@ module.exports.fromBytes = fromBytes; /***/ }), -/* 8 */ +/* 6 */ /***/ (function(module, exports) { /* @@ -1695,25 +1440,75 @@ module.exports = GInt; /***/ }), -/* 9 */ +/* 7 */ /***/ (function(module, exports) { /*! - * bitdepth - * Change the bit depth of samples to and from 8, 16, 24, 32 & 64-bit. + * endianness + * Swap endianness in byte arrays. * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/bitdepth + * https://github.com/rochars/endianness * */ -const f64f32 = new Float32Array(1); - /** - * Max number of different values for each bit depth. - * @enum {number} - */ -const BitDepthMaxValues = { - "8": 256, + * Swap the endianness of units of information in a byte array. + * The original array is modified in-place. + * @param {!Array|!Array|Uint8Array} bytes The bytes. + * @param {number} offset The number of bytes of each unit of information. + */ +function endianness(bytes, offset) { + let len = bytes.length; + let i = 0; + while (i < len) { + swap(bytes, offset, i); + i += offset; + } +} + +/** + * Swap the endianness of a unit of information in a byte array. + * The original array is modified in-place. + * @param {!Array|!Array|Uint8Array} bytes The bytes. + * @param {number} offset The number of bytes of the unit of information. + * @param {number} index The start index of the unit of information. + */ +function swap(bytes, offset, index) { + let x = 0; + let y = offset - 1; + let limit = parseInt(offset / 2, 10); + while(x < limit) { + let theByte = bytes[index + x]; + bytes[index + x] = bytes[index + y]; + bytes[index + y] = theByte; + x++; + y--; + } +} + +module.exports = endianness; + + +/***/ }), +/* 8 */ +/***/ (function(module, exports) { + +/*! + * bitdepth + * Change the bit depth of samples to and from 8, 16, 24, 32 & 64-bit. + * Copyright (c) 2017 Rafael da Silva Rocha. + * https://github.com/rochars/bitdepth + * + */ + +const f64f32 = new Float32Array(1); + +/** + * Max number of different values for each bit depth. + * @enum {number} + */ +const BitDepthMaxValues = { + "8": 256, "16": 65536, "24": 16777216, "32": 4294967296, @@ -1885,7 +1680,7 @@ module.exports.BitDepthMaxValues = BitDepthMaxValues; /***/ }), -/* 10 */ +/* 9 */ /***/ (function(module, exports, __webpack_require__) { /* @@ -1900,12 +1695,12 @@ module.exports.BitDepthMaxValues = BitDepthMaxValues; */ const byteData = __webpack_require__(0); -const WaveErrors = __webpack_require__(3); +const WaveErrors = __webpack_require__(2); const uInt8 = byteData.uInt8; const uInt16 = byteData.uInt16; const uInt32 = byteData.uInt32; const chr = byteData.chr; -let WaveFileHeader = __webpack_require__(11); +let WaveFileHeader = __webpack_require__(10); /** * Read and write wave files. @@ -2339,7 +2134,7 @@ module.exports = WaveFileReaderWriter; /***/ }), -/* 11 */ +/* 10 */ /***/ (function(module, exports) { /* @@ -2457,7 +2252,7 @@ module.exports = WaveFileHeader; /***/ }), -/* 12 */ +/* 11 */ /***/ (function(module, exports, __webpack_require__) { /*! @@ -2468,7 +2263,7 @@ module.exports = WaveFileHeader; * */ -const byteData = __webpack_require__(13); +const byteData = __webpack_require__(0); const uInt32 = byteData.uInt32; const chr = byteData.chr; @@ -2599,1076 +2394,170 @@ module.exports.write = write; /***/ }), -/* 13 */ +/* 12 */ /***/ (function(module, exports, __webpack_require__) { /*! - * byte-data - * Readable data to and from byte buffers. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data + * imaadpcm + * JavaScript IMA ADPCM codec. + * Copyright (c) 2018 Rafael da Silva Rocha. + * https://github.com/rochars/imaadpcm * + * References: + * http://www.cs.columbia.edu/~hgs/audio/dvi/ + * https://github.com/acida/pyima + * https://wiki.multimedia.cx/index.php/IMA_ADPCM + * */ -/** @private */ -const rw = __webpack_require__(14); -let Type = __webpack_require__(4); +const byteData = __webpack_require__(0); +const int16 = byteData.int16; + +var indexTable = [ + -1, -1, -1, -1, 2, 4, 6, 8, + -1, -1, -1, -1, 2, 4, 6, 8]; + +var stepTable = [ + 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]; + +var encoderPredicted = 0; +var encoderIndex = 0; +var encoderStep = 7; +var decoderPredicted = 0; +var decoderIndex = 0; +var decoderStep = 7; /** - * Turn a number or fixed-length string into a byte buffer. - * @param {number|string} value The value. - * @param {Object} type One of the available types. - * @param {number} base The base of the output. Optional. Default is 10. - * Possible values are 2, 10 or 16. - * @return {!Array|!Array} + * Compress a 16-bit PCM sample into a 4-bit ADPCM sample. + * @param {number} sample The sample. + * @return {number} */ -function pack(value, type, base=10) { - let values = []; - if (type.char) { - values = type.char ? value.slice(0, type.realBits / 8) : value; - } else if (!Array.isArray(value)) { - values = [value]; +function encodeSample(sample) { + let delta = sample - encoderPredicted; + let value = 0; + if (delta >= 0) { + value = 0; } - return rw.toBytes(values, rw.getType(type, base)); + else { + value = 8; + delta = -delta; + } + let step = stepTable[encoderIndex]; + let diff = step >> 3; + if (delta > step) { + value |= 4; + delta -= step; + diff += step; + } + step >>= 1; + if (delta > step) { + value |= 2; + delta -= step; + diff += step; + } + step >>= 1; + if (delta > step) { + value |= 1; + diff += step; + } + if (value & 8) { + encoderPredicted -= diff; + } + else { + encoderPredicted += diff; + } + if (encoderPredicted < -0x8000) { + encoderPredicted = -0x8000; + } else if (encoderPredicted > 0x7fff) { + encoderPredicted = 0x7fff; + } + encoderIndex += indexTable[value & 7]; + if (encoderIndex < 0) { + encoderIndex = 0; + } else if (encoderIndex > 88) { + encoderIndex = 88; + } + return value; } /** - * Turn a byte buffer into a number or a fixed-length string. - * @param {!Array|!Array|Uint8Array} buffer An array of bytes. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. Optional. Default is 10. - * Possible values are 2, 10 or 16. - * @return {number|string} + * Decode a 4-bit ADPCM sample into a 16-bit PCM sample. + * @param {number} nibble A 4-bit adpcm sample. + * @return {number} */ -function unpack(buffer, type, base=10) { - let offset = type.bits < 8 ? type.bits : type.realBits / 8; - let values = rw.fromBytes( - buffer.slice(0, offset), - rw.getType(type, base) - ); - if (type.char) { - values = values.slice(0, type.bits / 8); - } else { - values = values[0]; +function decodeSample(nibble) { + let difference = 0; + if (nibble & 4) { + difference += decoderStep; } - return values; + if (nibble & 2) { + difference += decoderStep >> 1; + } + if (nibble & 1) { + difference += decoderStep >> 2; + } + difference += decoderStep >> 3; + if (nibble & 8) { + difference = -difference; + } + decoderPredicted += difference; + if (decoderPredicted > 32767) { + decoderPredicted = 32767; + } else if (decoderPredicted < -32767) { + decoderPredicted = -32767; + } + decoderIndex += indexTable[nibble]; + if (decoderIndex < 0) { + decoderIndex = 0; + } else if (decoderIndex > 88) { + decoderIndex = 88; + } + decoderStep = stepTable[decoderIndex]; + return decoderPredicted; } /** - * Turn a array of numbers or a string into a byte buffer. - * @param {!Array|string} values The values. - * @param {Object} type One of the available types. - * @param {number} base The base of the output. Optional. Default is 10. - * Possible values are 2, 10 or 16. - * @return {!Array|!Array} + * Return the head of a ADPCM sample block. + * @param {number} sample The first sample of the block. + * @return {!Array} */ -function packArray(values, type, base=10) { - return rw.toBytes(values, rw.getType(type, base)); +function blockHead(sample) { + encodeSample(sample); + let adpcmSamples = []; + adpcmSamples.push(byteData.pack(sample, int16)[0]); + adpcmSamples.push(byteData.pack(sample, int16)[1]); + adpcmSamples.push(encoderIndex); + adpcmSamples.push(0); + return adpcmSamples; } /** - * Turn a byte buffer into a array of numbers or a string. - * @param {!Array|!Array|Uint8Array} buffer The byte array. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. Optional. Default is 10. - * Possible values are 2, 10 or 16. - * @return {!Array|string} + * Encode a block of 505 16-bit samples as 4-bit ADPCM samples. + * @param {!Array} block A sample block of 505 samples. + * @return {!Array} */ -function unpackArray(buffer, type, base=10) { - return rw.fromBytes(buffer, rw.getType(type, base)); -} - -/** - * Find and return the start index of some string. - * Return -1 if the string is not found. - * @param {!Array|Uint8Array} buffer A byte buffer. - * @param {string} text Some string to look for. - * @return {number} The start index of the first occurrence, -1 if not found - */ -function findString(buffer, text) { - let found = ""; - for (let i = 0; i < buffer.length; i++) { - found = unpack( - buffer.slice(i, i + text.length + 1), - new Type({"bits": text.length * 8, "char": true}) - ); - if (found == text) { - return i; - } - } - return -1; -} - -/** - * Turn a struct into a byte buffer. - * A struct is an array of values of not necessarily the same type. - * @param {Array} struct The struct values. - * @param {!Array} def The struct type definition. - * @param {number} base The base of the output. Optional. Default is 10. - * Possible values are 2, 10 or 16. - * @return {!Array|!Array} - */ -function packStruct(struct, def, base=10) { - if (struct.length < def.length) { - return []; - } - let bytes = []; - for (let i = 0; i < def.length; i++) { - bytes = bytes.concat(pack(struct[i], def[i], base)); - } - return bytes; -} - -/** - * Turn a byte buffer into a struct. - * A struct is an array of values of not necessarily the same type. - * @param {!Array|!Array|Uint8Array} buffer The byte buffer. - * @param {!Array} def The struct type definition. - * @param {number} base The base of the input. Optional. Default is 10. - * Possible values are 2, 10 or 16. - * @return {Array} - */ -function unpackStruct(buffer, def, base=10) { - if (buffer.length < getStructDefSize(def)) { - return []; - } - let struct = []; - let i = 0; - let j = 0; - while (i < def.length) { - let bits = def[i].bits < 8 ? 1 : def[i].realBits / 8; - struct = struct.concat( - unpack(buffer.slice(j, j + bits), def[i], base) - ); - j += bits; - i++; - } - return struct; -} - -/** - * Get the length in bytes of a struct definition. - * @param {!Array} def The struct type definition. - * @return {number} The length of the structure in bytes. - * @private - */ -function getStructDefSize(def) { - let bits = 0; - for (let i = 0; i < def.length; i++) { - bits += def[i].realBits / 8; - } - return bits; -} - -// interface -exports.pack = pack; -exports.unpack = unpack; -exports.packArray = packArray; -exports.unpackArray = unpackArray; -exports.unpackStruct = unpackStruct; -exports.packStruct = packStruct; -exports.findString = findString; -exports.Type = Type; - -// types -/** - * A char. - * @type {!Type} - */ -exports.chr = new Type({"bits": 8, "char": true}); -/** - * A 4-char string - * @type {!Type} - */ -exports.fourCC = new Type({"bits": 32, "char": true}); -/** - * Booleans - * @type {!Type} - */ -exports.bool = new Type({"bits": 1}); -/** - * Signed 2-bit integers - * @type {!Type} - */ -exports.int2 = new Type({"bits": 2, "signed": true}); -/** - * Unsigned 2-bit integers - * @type {!Type} - */ -exports.uInt2 = new Type({"bits": 2}); -/** - * Signed 4-bit integers - * @type {!Type} - */ -exports.int4 = new Type({"bits": 4, "signed": true}); -/** - * Unsigned 4-bit integers - * @type {!Type} - */ -exports.uInt4 = new Type({"bits": 4}); -/** - * Signed 8-bit integers - * @type {!Type} - */ -exports.int8 = new Type({"bits": 8, "signed": true}); -/** - * Unsigned 4-bit integers - * @type {!Type} - */ -exports.uInt8 = new Type({"bits": 8}); -// LE -/** - * Signed 16-bit integers little-endian - * @type {!Type} - */ -exports.int16 = new Type({"bits": 16, "signed": true}); -/** - * Unsigned 16-bit integers little-endian - * @type {!Type} - */ -exports.uInt16 = new Type({"bits": 16}); -/** - * Half-precision floating-point numbers little-endian - * @type {!Type} - */ -exports.float16 = new Type({"bits": 16, "float": true}); -/** - * Signed 24-bit integers little-endian - * @type {!Type} - */ -exports.int24 = new Type({"bits": 24, "signed": true}); -/** - * Unsigned 24-bit integers little-endian - * @type {!Type} - */ -exports.uInt24 = new Type({"bits": 24}); -/** - * Signed 32-bit integers little-endian - * @type {!Type} - */ -exports.int32 = new Type({"bits": 32, "signed": true}); -/** - * Unsigned 32-bit integers little-endian - * @type {!Type} - */ -exports.uInt32 = new Type({"bits": 32}); -/** - * Single-precision floating-point numbers little-endian - * @type {!Type} - */ -exports.float32 = new Type({"bits": 32, "float": true}); -/** - * Signed 40-bit integers little-endian - * @type {!Type} - */ -exports.int40 = new Type({"bits": 40, "signed": true}); -/** - * Unsigned 40-bit integers little-endian - * @type {!Type} - */ -exports.uInt40 = new Type({"bits": 40}); -/** - * Signed 48-bit integers little-endian - * @type {!Type} - */ -exports.int48 = new Type({"bits": 48, "signed": true}); -/** - * Unsigned 48-bit integers little-endian - * @type {!Type} - */ -exports.uInt48 = new Type({"bits": 48}); -/** - * Double-precision floating-point numbers little-endian - * @type {!Type} - */ -exports.float64 = new Type({"bits": 64, "float": true}); -// BE -/** - * Signed 16-bit integers big-endian - * @type {!Type} - */ -exports.int16BE = new Type({"bits": 16, "signed": true, "be": true}); -/** - * Unsigned 16-bit integers big-endian - * @type {!Type} - */ -exports.uInt16BE = new Type({"bits": 16, "be": true}); -/** - * Half-precision floating-point numbers big-endian - * @type {!Type} - */ -exports.float16BE = new Type({"bits": 16, "float": true, "be": true}); -/** - * Signed 24-bit integers big-endian - * @type {!Type} - */ -exports.int24BE = new Type({"bits": 24, "signed": true, "be": true}); -/** - * Unsigned 24-bit integers big-endian - * @type {!Type} - */ -exports.uInt24BE = new Type({"bits": 24, "be": true}); -/** - * Signed 32-bit integers big-endian - * @type {!Type} - */ -exports.int32BE = new Type({"bits": 32, "signed": true, "be": true}); -/** - * Unsigned 32-bit integers big-endian - * @type {!Type} - */ -exports.uInt32BE = new Type({"bits": 32, "be": true}); -/** - * Single-precision floating-point numbers big-endian - * @type {!Type} - */ -exports.float32BE = new Type({"bits": 32, "float": true, "be": true}); -/** - * Signed 40-bit integers big-endian - * @type {!Type} - */ -exports.int40BE = new Type({"bits": 40, "signed": true, "be": true}); -/** - * Unsigned 40-bit integers big-endian - * @type {!Type} - */ -exports.uInt40BE = new Type({"bits": 40, "be": true}); -/** - * Signed 48-bit integers big-endian - * @type {!Type} - */ -exports.int48BE = new Type({"bits": 48, "signed": true, "be": true}); -/** - * Unsigned 48-bit integers big-endian - * @type {!Type} - */ -exports.uInt48BE = new Type({"bits": 48, "be": true}); -/** - * Double-precision floating-point numbers big-endian - * @type {!Type} - */ -exports.float64BE = new Type({"bits": 64, "float": true, "be": true}); - - -/***/ }), -/* 14 */ -/***/ (function(module, exports, __webpack_require__) { - -/** - * from-bytes: Numbers and strings from bytes. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -const Type = __webpack_require__(4); -const endianness = __webpack_require__(2); - -/** - * Turn a byte buffer into what the bytes represent. - * @param {!Array|!Array|Uint8Array} buffer An array of bytes. - * @param {Object} type One of the available types. - * @return {!Array|number|string} - */ -function fromBytes(buffer, type) { - if (type.be) { - endianness(buffer, type.offset); - } - if (type.base != 10) { - bytesFromBase(buffer, type.base); - } - return readBytes(buffer, type); -} - -/** - * Turn numbers and strings to bytes. - * @param {!Array|number|string} values The data. - * @param {Object} type One of the available types. - * @return {!Array|!Array} the data as a byte buffer. - */ -function toBytes(values, type) { - let bytes = writeBytes(values, type); - if (type.be) { - endianness(bytes, type.offset); - } - if (type.base != 10) { - bytesToBase(bytes, type.base); - formatOutput(bytes, type); - } - return bytes; -} - -/** - * Turn a array of bytes into an array of what the bytes should represent. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {Object} type The type. - * @return {!Array|string} - */ -function readBytes(bytes, type) { - let values = []; - let i = 0; - let len = bytes.length - (type.offset - 1); - while (i < len) { - values.push( - type.overflow(type.sign(type.reader(bytes, i, type))) - ); - i += type.offset; - } - if (type.char) { - values = values.join(""); - } - return values; -} - -/** - * Write values as bytes. - * @param {!Array|number|string} values The data. - * @param {Object} type One of the available types. - * @return {!Array} the bytes. - */ -function writeBytes(values, type) { - let i = 0; - let j = 0; - let len = values.length; - let bytes = []; - while (i < len) { - j = type.writer(bytes, type.overflow(values[i]), j); - i++; - } - return bytes; -} - -/** - * Get the full type spec for the reading/writing. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. - * @return {Object} - */ -function getType(type, base) { - let theType = Object.assign(new Type({}), type); - theType.base = base; - return theType; -} - -/** - * Turn bytes to base 10 from base 2 or 16. - * @param {!Array|Uint8Array} bytes The bytes as binary or hex strings. - * @param {number} base The base. - */ -function bytesFromBase(bytes, base) { - let i = 0; - let len = bytes.length; - while(i < len) { - bytes[i] = parseInt(bytes[i], base); - i++; - } -} - -/** - * Turn the output to the correct base. - * @param {Array} bytes The bytes. - * @param {Object} type The type. - */ -function formatOutput(bytes, type) { - let i = 0; - let len = bytes.length; - let offset = getOutputByteOffset(type); - while(i < len) { - bytes[i] = Array(offset - bytes[i].length).join("0") + bytes[i]; - i++; - } -} - -/** - * Get the number of chars a non-string output should have - * according to the number of bits used by the type. - * @param {Object} type The type. - * @return {number} - */ -function getOutputByteOffset(type) { - if (type.realBits < 8) { - return (type.base == 2 ? type.bits : type.bits < 5 ? 1 : 2) + 1; - } - return (type.base == 2 ? 8 : 2) + 1; -} - -/** - * Turn bytes from base 10 to base 2 or 16. - * @param {!Array|Array} bytes The bytes. - * @param {number} base The base. - */ -function bytesToBase(bytes, base) { - let i = 0; - let len = bytes.length; - while (i < len) { - bytes[i] = bytes[i].toString(base); - i++; - } -} - -exports.getType = getType; -exports.toBytes = toBytes; -exports.fromBytes = fromBytes; - - -/***/ }), -/* 15 */ -/***/ (function(module, exports) { - -/** - * bit-parser: Functions to read and write bytes. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - * Floats based on int-bits: https://github.com/Jam3/int-bits - * Future: fix https://github.com/majimboo/c-struct 40 and 48-bit - */ - -/** @private */ -let f32 = new Float32Array(1); -/** @private */ -let i32 = new Int32Array(f32.buffer); -/** @private */ -let f64 = new Float64Array(1); -/** @private */ -let ui32 = new Uint32Array(f64.buffer); - -/** - * Functions to read data from bytes. - * @enum {Function} - */ -const BitReader = { - - /** - * Read 1 8-bit int from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read8Bit": function (bytes, i) { - return bytes[i]; - }, - - /** - * Read 1 16-bit int from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read16Bit": function (bytes, i) { - return bytes[1 + i] << 8 | bytes[i]; - }, - - /** - * Read 1 16-bit float from from bytes. - * Thanks https://stackoverflow.com/a/8796597 - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read16BitFloat": function (bytes, i) { - let binary = parseInt(getBinary([bytes[i], bytes[i+1]]), 2); - let exponent = (binary & 0x7C00) >> 10; - let fraction = binary & 0x03FF; - let floatValue; - if (exponent) { - floatValue = Math.pow(2, exponent - 15) * (1 + fraction / 0x400); - } else { - floatValue = 6.103515625e-5 * (fraction / 0x400); - } - return floatValue * (binary >> 15 ? -1 : 1); - }, - - /** - * Read 1 24-bit int from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read24Bit": function (bytes, i) { - return bytes[2 + i] << 16 | BitReader["read16Bit"](bytes, i); - }, - - /** - * Read 1 32-bit int from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read32Bit": function (bytes, i) { - return (bytes[3 + i] << 24 | - BitReader["read24Bit"](bytes, i)) >>> 0; - }, - - /** - * Read 1 32-bit float from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read32BitFloat": function (bytes, i) { - i32[0] = BitReader["read32Bit"](bytes, i); - return f32[0]; - }, - - /** - * Read 1 40-bit int from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read40Bit": function (bytes, i) { - return readBytesAsBits(bytes, i, 5); - }, - - /** - * Read 1 48-bit int from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read48Bit": function (bytes, i) { - return readBytesAsBits(bytes, i, 6); - }, - - /** - * Read 1 53-bit int from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read53Bit": function (bytes, i) { - return readBytesAsBits(bytes, i, 7); - }, - - /** - * Read 1 64-bit double from bytes. - * Thanks https://gist.github.com/kg/2192799 - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ - "read64BitFloat": function (bytes, i) { - ui32[0] = BitReader["read32Bit"](bytes, i); - ui32[1] = BitReader["read32Bit"](bytes, i + 4); - return f64[0]; - }, - - /** - * Read 1 char from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @param {Object} type The index to read. - * @return {string} - */ - "readChar": function (bytes, i, type) { - let chrs = ""; - let j = 0; - let len = type.bits / 8; - while(j < len) { - chrs += String.fromCharCode(bytes[i+j]); - j++; - } - return chrs; - } -}; - -/** - * Functions to write data to bytes. - * @enum {Function} - */ -let BitWriter = { - - /** - * Write one 64-bit float as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write64BitFloat": function(bytes, number, j) { - f64[0] = number; - j = BitWriter["write32Bit"](bytes, ui32[0], j); - return BitWriter["write32Bit"](bytes, ui32[1], j); - }, - - /** - * Write one 53-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write53Bit": function (bytes, number, j) { - j = BitWriter["write48Bit"](bytes, number, j); - bytes[j++] = Math.floor(number / Math.pow(2, 48)) & 0xFF; - return j; - }, - - /** - * Write one 48-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write48Bit": function (bytes, number, j) { - j = BitWriter["write40Bit"](bytes, number, j); - bytes[j++] = Math.floor(number / Math.pow(2, 40)) & 0xFF; - return j; - }, - - /** - * Write one 40-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write40Bit": function (bytes, number, j) { - j = BitWriter["write32Bit"](bytes, number, j); - bytes[j++] = Math.floor(number / Math.pow(2, 32)) & 0xFF; - return j; - }, - - /** - * Write one 32-bit float as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write32BitFloat": function (bytes, number, j) { - f32[0] = number; - j = BitWriter["write32Bit"](bytes, i32[0], j); - return j; - }, - - /** - * Write one 32-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write32Bit": function (bytes, number, j) { - j = BitWriter["write24Bit"](bytes, number, j); - bytes[j++] = number >>> 24 & 0xFF; - return j; - }, - - /** - * Write one 24-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write24Bit": function (bytes, number, j) { - j = BitWriter["write16Bit"](bytes, number, j); - bytes[j++] = number >>> 16 & 0xFF; - return j; - }, - - /** - * Write one 16-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write16Bit": function (bytes, number, j) { - bytes[j++] = number & 0xFF; - bytes[j++] = number >>> 8 & 0xFF; - return j; - }, - - /** - * Write one 16-bit float as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write16BitFloat": function (bytes, number, j) { - f32[0] = number; - let x = i32[0]; - let bits = (x >> 16) & 0x8000; - let m = (x >> 12) & 0x07ff; - let e = (x >> 23) & 0xff; - if (e >= 103) { - bits |= ((e - 112) << 10) | (m >> 1); - bits += m & 1; - } - bytes[j] = bits & 0xFF; - bytes[j+1] = bits >>> 8 & 0xFF; - return j+2; - }, - - /** - * Write one 8-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write8Bit": function (bytes, number, j) { - bytes[j++] = number & 0xFF; - return j; - }, - - /** - * Write one 4-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write4Bit": function (bytes, number, j) { - bytes[j++] = number & 0xF; - return j; - }, - - /** - * Write one 2-bit integer as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write2Bit": function (bytes, number, j) { - bytes[j++] = number < 0 ? number + 4 : number; - return j; - }, - - /** - * Write one boolean as a binary value. - * @param {!Array} bytes An array of bytes. - * @param {number} number The number to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "write1Bit": function (bytes, number, j) { - bytes[j++] = number ? 1 : 0; - return j; - }, - - /** - * Write one char as a byte. - * @param {!Array} bytes An array of bytes. - * @param {string} string The string to write as bytes. - * @param {number} j The index being written in the byte buffer. - * @return {number} The next index to write on the byte buffer. - */ - "writeString": function (bytes, string, j) { - bytes[j++] = string.charCodeAt(0); - return j; - } -}; - -/** - * Get a binary string representation of a value described as bytes. - * @param {Array|number} bytes The bytes. - * @return {string} - */ -function getBinary(bytes) { - let binary = ""; - let i = 0; - let bytesLength = bytes.length; - while(i < bytesLength) { - let bits = bytes[i].toString(2); - binary = Array(9 - bits.length).join("0") + bits + binary; - i++; - } - return binary; -} - -/** - * Read a group of bytes by turning it to bits. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @param {number} numBytes The number of bytes - * (1 for 8-bit, 2 for 16-bit, etc). - * @return {number} - */ -function readBytesAsBits(bytes, i, numBytes) { - let j = numBytes-1; - let byte = ""; - while (j >= 0) { - let bits = bytes[j + i].toString(2); - byte += Array(9 - bits.length).join("0") + bits; - j--; - } - return parseInt(byte, 2); -} - -exports.BitWriter = BitWriter; -exports.BitReader = BitReader; - - -/***/ }), -/* 16 */ -/***/ (function(module, exports, __webpack_require__) { - -/*! - * imaadpcm - * JavaScript IMA ADPCM codec. - * Copyright (c) 2018 Rafael da Silva Rocha. - * https://github.com/rochars/imaadpcm - * - * References: - * http://www.cs.columbia.edu/~hgs/audio/dvi/ - * https://github.com/acida/pyima - * https://wiki.multimedia.cx/index.php/IMA_ADPCM - * - */ - -const byteData = __webpack_require__(0); -const int16 = byteData.int16; - -var indexTable = [ - -1, -1, -1, -1, 2, 4, 6, 8, - -1, -1, -1, -1, 2, 4, 6, 8]; - -var stepTable = [ - 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]; - -var encoderPredicted = 0; -var encoderIndex = 0; -var encoderStep = 7; -var decoderPredicted = 0; -var decoderIndex = 0; -var decoderStep = 7; - -/** - * Compress a 16-bit PCM sample into a 4-bit ADPCM sample. - * @param {number} sample The sample. - * @return {number} - */ -function encodeSample(sample) { - let delta = sample - encoderPredicted; - let value = 0; - if (delta >= 0) { - value = 0; - } - else { - value = 8; - delta = -delta; - } - let step = stepTable[encoderIndex]; - let diff = step >> 3; - if (delta > step) { - value |= 4; - delta -= step; - diff += step; - } - step >>= 1; - if (delta > step) { - value |= 2; - delta -= step; - diff += step; - } - step >>= 1; - if (delta > step) { - value |= 1; - diff += step; - } - if (value & 8) { - encoderPredicted -= diff; - } - else { - encoderPredicted += diff; - } - if (encoderPredicted < -0x8000) { - encoderPredicted = -0x8000; - } else if (encoderPredicted > 0x7fff) { - encoderPredicted = 0x7fff; - } - encoderIndex += indexTable[value & 7]; - if (encoderIndex < 0) { - encoderIndex = 0; - } else if (encoderIndex > 88) { - encoderIndex = 88; - } - return value; -} - -/** - * Decode a 4-bit ADPCM sample into a 16-bit PCM sample. - * @param {number} nibble A 4-bit adpcm sample. - * @return {number} - */ -function decodeSample(nibble) { - let difference = 0; - if (nibble & 4) { - difference += decoderStep; - } - if (nibble & 2) { - difference += decoderStep >> 1; - } - if (nibble & 1) { - difference += decoderStep >> 2; - } - difference += decoderStep >> 3; - if (nibble & 8) { - difference = -difference; - } - decoderPredicted += difference; - if (decoderPredicted > 32767) { - decoderPredicted = 32767; - } else if (decoderPredicted < -32767) { - decoderPredicted = -32767; - } - decoderIndex += indexTable[nibble]; - if (decoderIndex < 0) { - decoderIndex = 0; - } else if (decoderIndex > 88) { - decoderIndex = 88; - } - decoderStep = stepTable[decoderIndex]; - return decoderPredicted; -} - -/** - * Return the head of a ADPCM sample block. - * @param {number} sample The first sample of the block. - * @return {!Array} - */ -function blockHead(sample) { - encodeSample(sample); - let adpcmSamples = []; - adpcmSamples.push(byteData.pack(sample, int16)[0]); - adpcmSamples.push(byteData.pack(sample, int16)[1]); - adpcmSamples.push(encoderIndex); - adpcmSamples.push(0); - return adpcmSamples; -} - -/** - * Encode a block of 505 16-bit samples as 4-bit ADPCM samples. - * @param {!Array} block A sample block of 505 samples. - * @return {!Array} - */ -function encodeBlock(block) { - let adpcmSamples = blockHead(block[0]); - for (let i=3; iThrows:
- Documentation generated by JSDoc 3.5.5 on Wed Apr 25 2018 00:52:38 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Wed Apr 25 2018 01:14:55 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/index.html b/docs/index.html index 0c94b45..738483d 100644 --- a/docs/index.html +++ b/docs/index.html @@ -208,7 +208,7 @@

LICENSE

Copyright (c) 2017-2018 Rafael da Silva Rocha.


- Documentation generated by JSDoc 3.5.5 on Wed Apr 25 2018 00:52:38 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Wed Apr 25 2018 01:14:55 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/index.js.html b/docs/index.js.html index ade51b1..dd23946 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -372,7 +372,7 @@

index.js


- Documentation generated by JSDoc 3.5.5 on Wed Apr 25 2018 00:52:38 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Wed Apr 25 2018 01:14:55 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/package.json b/package.json index 17c0d57..26ef678 100644 --- a/package.json +++ b/package.json @@ -68,9 +68,9 @@ "dependencies": { "alawmulaw": "^2.0.1", "bitdepth": "^1.0.4", - "byte-data": "^4.0.3", + "byte-data": "^4.0.2", "imaadpcm": "^1.1.0", - "riff-chunks": "^3.0.3" + "riff-chunks": "^3.1.0" }, "main": "index.js" }