diff --git a/LICENSE b/LICENSE index c0b38dc..71fbdc1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2017 Rafael da Silva Rocha. +Copyright (c) 2017-2018 Rafael da Silva Rocha. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/dist/wavefile-min.js b/dist/wavefile-min.js index dc4ab33..15d2a6c 100644 --- a/dist/wavefile-min.js +++ b/dist/wavefile-min.js @@ -1,14 +1,14 @@ /* - wavefile - Read & write wave files with 8, 16, 24, 32 & 64-bit data. - Copyright (c) 2017 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 + wavefile + Read & write wave files with 8, 16, 24, 32 & 64-bit data. + Copyright (c) 2017-2018 Rafael da Silva Rocha. + https://github.com/rochars/wavefile + bitdepth Change the bit depth of samples to and from 8, 16, 24, 32 & 64-bit. Copyright (c) 2017 Rafael da Silva Rocha. @@ -25,70 +25,66 @@ https://github.com/rochars/riff-chunks */ -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(a){var k=function(){};k.prototype=a;return new k};$jscomp.underscoreProtoCanBeSet=function(){var a={a:!0},k={};try{return k.__proto__=a,k.a}catch(g){}return!1}; -$jscomp.setPrototypeOf="function"==typeof Object.setPrototypeOf?Object.setPrototypeOf:$jscomp.underscoreProtoCanBeSet()?function(a,k){a.__proto__=k;if(a.__proto__!==k)throw new TypeError(a+" is not extensible");return a}:null; -$jscomp.inherits=function(a,k){a.prototype=$jscomp.objectCreate(k.prototype);a.prototype.constructor=a;if($jscomp.setPrototypeOf){var g=$jscomp.setPrototypeOf;g(a,k)}else for(g in k)if("prototype"!=g)if(Object.defineProperties){var h=Object.getOwnPropertyDescriptor(k,g);h&&Object.defineProperty(a,g,h)}else a[g]=k[g];a.superClass_=k.prototype}; -$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,k,g){a!=Array.prototype&&a!=Object.prototype&&(a[k]=g.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this); -$jscomp.polyfill=function(a,k,g,h){if(k){g=$jscomp.global;a=a.split(".");for(h=0;hc[b].length&&(c[b]=Array(5-c[b].length).join("0")+c[b])}function e(c,b,d){(2==b||16==b)&&2>c[d].length&&(c[d]="0"+c[d])}function b(c,d){for(;c.lengthparseInt(b/2,10)-1&&(c-=b);return c};a.exports.padding=h;a.exports.paddingNibble=f;a.exports.paddingCrumb=e;a.exports.bytePadding=l;a.exports.lPadZeros=b;a.exports.fixFloat16Endianness=function(c,b){b.float&&16==b.bits&&d(c,2)};a.exports.buildType=function(c,b){64==b&&(c.float=!0);c.float&&(c.signed=!0);c.bits=b}},function(a,k,g){function h(c,b,d){c[d]=l(c[d],b)}function l(c, -d){var a=c.length+1;2==d?a=8:16==d&&(a=2);return b(c,a)}function f(c,b,d){2==b&&4>c[d].length&&(c[d]=Array(5-c[d].length).join("0")+c[d])}function e(c,b,d){(2==b||16==b)&&2>c[d].length&&(c[d]="0"+c[d])}function b(c,b){for(;c.lengthparseInt(b/2,10)-1&&(c-=b);return c};a.exports.padding=h;a.exports.paddingNibble=f;a.exports.paddingCrumb=e;a.exports.bytePadding=l;a.exports.lPadZeros=b;a.exports.fixFloat16Endianness=function(c,b){b.float&&16==b.bits&&d(c,2)};a.exports.buildType=function(c,b){64==b&&(c.float=!0);c.float&&(c.signed=!0);c.bits=b}},function(a,k){a.exports.BitDepthOffsets={1:1,2:1,4:1,8:1,16:2,24:3,32:4,40:5,48:6,64:8}; -a.exports.BitDepthMaxValues={1:2,2:4,4:16,8:256,16:65536,24:16777216,32:4294967296,40:1099511627776,48:281474976710656,64:Infinity}},function(a,k){function g(a){h[0]=a;return l[0]}k=new Int8Array(4);var h=new Int32Array(k.buffer,0,1),l=new Float32Array(k.buffer,0,1);a.exports=g;a.exports.pack=g;a.exports.unpack=function(a){l[0]=a;return h[0]}},function(a,k){a.exports.BitDepthOffsets={1:1,2:1,4:1,8:1,16:2,24:3,32:4,40:5,48:6,64:8};a.exports.BitDepthMaxValues={1:2,2:4,4:16,8:256,16:65536,24:16777216, -32:4294967296,40:1099511627776,48:281474976710656,64:Infinity}},function(a,k){a.exports={format:"Not a supported format.",wave:"Could not find the 'WAVE' format identifier","fmt ":"Could not find the 'fmt ' chunk",data:"Could not find the 'data' chunk",fact:"Could not find the 'fact' chunk",bitDepth:"Invalid bit depth.",numChannels:"Invalid number of channels.",sampleRate:"Invalid sample rate."}},function(a,k,g){function h(b,c){c=void 0===c?!1:c;for(var d="",a=0,e=b.length;a>10,d=b&1023;return(c?Math.pow(2,c-15)*(1+d/1024):d/1024*.00006103515625)*(b>>15?-1:1)};a.exports.decodeFloat64=function(b){if("0,0,0,0,0,0,0,0"==b.toString())return 0;b=h(b);for(var c="1"+b.substr(12,52),d=1,a=0,e=0;e=b&&(c=2147483648,b=-b);var d=Math.floor(Math.log(b)/Math.log(2)),a=Math.floor(b/Math.pow(2,d)*Math.pow(2,52));b=a&4294967295;a/=Math.pow(2,32);return[c|d+1023<<20|a&1048575,b]};a.exports.toHalf=function(b){f[0]=b;var c=e[0];b=c>>16&32768;var d=c>>12&2047;c=c>>23&255;return 103>c?b:(b|c-112<<10|d>>1)+(d&1)}},function(a,k){a.exports=function(a,h){for(var g=a.length,f=0;f>10,d=b&1023;return(c?Math.pow(2,c-15)*(1+d/1024):d/1024*.00006103515625)*(b>>15?-1:1)};a.exports.decodeFloat64=function(b){if("0,0,0,0,0,0,0,0"==b.toString())return 0; -b=h(b);for(var c="1"+b.substr(12,52),d=1,a=0,e=0;e=b&&(c=2147483648,b=-b);var d=Math.floor(Math.log(b)/Math.log(2)),a=Math.floor(b/Math.pow(2,d)*Math.pow(2,52));b=a&4294967295;a/=Math.pow(2,32);return[c|d+1023<<20|a&1048575,b]};a.exports.toHalf=function(b){f[0]=b;var c=e[0];b=c>>16&32768;var d=c>>12&2047;c=c>>23& -255;return 103>c?b:(b|c-112<<10|d>>1)+(d&1)}},function(a,k,g){var h=g(10),l=g(5),f=g(11),e=g(19);a=function(b){var c=f.call(this)||this;b&&c.fromBuffer(b);return c};$jscomp.inherits(a,f);a.read64BitFloat=f.read64BitFloat;a.read48Bit=f.read48Bit;a.read40Bit=f.read40Bit;a.read32BitFloat=f.read32BitFloat;a.read32Bit=f.read32Bit;a.read24Bit=f.read24Bit;a.read16BitFloat=f.read16BitFloat;a.read16Bit=f.read16Bit;a.read8Bit=f.read8Bit;a.read1Bit=f.read1Bit;a.readChar=f.readChar;a.writeString=f.writeString; -a.write1Bit=f.write1Bit;a.write2Bit=f.write2Bit;a.write4Bit=f.write4Bit;a.write8Bit=f.write8Bit;a.write16BitFloat=f.write16BitFloat;a.write16Bit=f.write16Bit;a.write24Bit=f.write24Bit;a.write32Bit=f.write32Bit;a.write32BitFloat=f.write32BitFloat;a.write40Bit=f.write40Bit;a.write48Bit=f.write48Bit;a.write64BitFloat=f.write64BitFloat;a.BitDepthMaxValues=f.BitDepthMaxValues;a.BitDepthOffsets=f.BitDepthOffsets;a.unpackNibbles=f.unpackNibbles;a.packNibbles=f.packNibbles;a.unpackCrumbs=f.unpackCrumbs;a.packCrumbs= -f.packCrumbs;a.unpackBooleans=f.unpackBooleans;a.packBooleans=f.packBooleans;a.fromBytes=f.fromBytes;a.toBytes=f.toBytes;a.findString=f.findString;a.float64=f.float64;a.uInt48=f.uInt48;a.int48=f.int48;a.uInt40=f.uInt40;a.int40=f.int40;a.float32=f.float32;a.uInt32=f.uInt32;a.int32=f.int32;a.uInt24=f.uInt24;a.int24=f.int24;a.float16=f.float16;a.uInt16=f.uInt16;a.int16=f.int16;a.uInt8=f.uInt8;a.int8=f.int8;a.uInt4=f.uInt4;a.int4=f.int4;a.uInt2=f.uInt2;a.int2=f.int2;a.bool=f.bool;a.chr=f.chr;a.unpackArray= -f.unpackArray;a.packArray=f.packArray;a.unpack=f.unpack;a.pack=f.pack;a.prototype.fromScratch=function(b,c,d,a,e){e=void 0===e?{}:e;e.container||(e.container="RIFF");var f=parseInt(d,10)/8;this.chunkSize=36+a.length*f;this.fmtChunkSize=16;this.byteRate=b*f*c;this.blockAlign=b*f;this.chunkId=e.container;this.format="WAVE";this.fmtChunkId="fmt ";this.audioFormat=this.headerFormats_[d];this.numChannels=b;this.sampleRate=c;this.bitsPerSample=parseInt(d,10);this.dataChunkId="data";this.dataChunkSize=a.length* -f;this.samples_=a;this.bitDepth_=d};a.prototype.fromBuffer=function(b){this.readRIFFChunk_(b);var c="RIFX"==this.chunkId;b=e.read(b,c);this.readFmtChunk_(b.subChunks);this.readFactChunk_(b.subChunks);this.readBextChunk_(b.subChunks);this.readCueChunk_(b.subChunks);this.readDataChunk_(b.subChunks,{be:c,single:!0});this.bitDepth_=3==this.audioFormat&&32==this.bitsPerSample?"32f":this.bitsPerSample.toString()};a.prototype.toBuffer=function(){this.checkWriteInput_();return new Uint8Array(this.createWaveFile_())}; -a.prototype.toRIFF=function(){this.chunkId="RIFF";this.LEorBE()};a.prototype.toRIFX=function(){this.chunkId="RIFX";this.LEorBE()};a.prototype.toBitDepth=function(b){h.toBitDepth(this.samples_,this.bitDepth_,b);this.fromScratch(this.numChannels,this.sampleRate,b,this.samples_,{container:this.chunkId})};a.prototype.interleave=function(){var b=[],c,a,e=this.samples_[0].length;for(c=0;cthis.numChannels|| -65535this.sampleRate||4294967295r.max?p[q]=r.max:p[q]>>8&255;a[d++]=b[c]>>>16&255;a[d++]=b[c]>>>24&255;return d}var l=g(6),f=g(3);a.exports.write64BitFloat=function(a,b,c,d){b=l.toFloat64(b[c]);d=h(a,b,1,d);return h(a,b,0,d)};a.exports.write48Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>8&255;a[d++]=b[c]>>16&255;a[d++]=b[c]>>24&255;a[d++]=b[c]/4294967296&255;a[d++]=b[c]/1099511627776&255;return d};a.exports.write40Bit=function(a,b,c,d){a[d++]= -b[c]&255;a[d++]=b[c]>>8&255;a[d++]=b[c]>>16&255;a[d++]=b[c]>>24&255;a[d++]=b[c]/4294967296&255;return d};a.exports.write32BitFloat=function(a,b,c,d){b=f.unpack(b[c]);a[d++]=b&255;a[d++]=b>>>8&255;a[d++]=b>>>16&255;a[d++]=b>>>24&255;return d};a.exports.write32Bit=h;a.exports.write24Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>>8&255;a[d++]=b[c]>>>16&255;return d};a.exports.write16Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>>8&255;return d};a.exports.write16BitFloat=function(a,b,c,d){b= -l.toHalf(b[c]);a[d++]=b>>>8&255;a[d++]=b&255;return d};a.exports.write8Bit=function(a,b,c,d){a[d++]=b[c]&255;return d};a.exports.write4Bit=function(a,b,c,d){a[d++]=b[c]&15;return d};a.exports.write2Bit=function(a,b,c,d){a[d++]=0>b[c]?b[c]+4:b[c];return d};a.exports.write1Bit=function(a,b,c,d){a[d++]=b[c]?1:0;return d};a.exports.writeString=function(a,b,c,d){a[d++]=b.charCodeAt(c);return d}},function(a,k,g){function h(a,c,d){var b=[],h=0,g=0,k=f.BitDepthOffsets[c.bits],l=a.length-(k-1),m=f.BitDepthMaxValues[c.bits]; -for(c=c.signed&&!c.float?e.signed:function(a,c){return a};h>>0}var f=g(0),e=g(6),b=g(3);a.exports.readChar=function(a,b){return String.fromCharCode(a[b])};a.exports.read1Bit=function(a,b){return parseInt(a[b],2)};a.exports.read8Bit=function(a,b){return a[b]};a.exports.read16Bit=function(a,b){return a[1+b]<<8|a[b]};a.exports.read16BitFloat=function(a,b){return e.decodeFloat16(a.slice(b,b+2))};a.exports.read24Bit=function(a, -b){return a[2+b]<<16|a[1+b]<<8|a[b]};a.exports.read32Bit=l;a.exports.read32BitFloat=function(a,d){return b.pack(l(a,d))};a.exports.read40Bit=function(a,b){return h(a,b,5)};a.exports.read48Bit=function(a,b){return h(a,b,6)};a.exports.read64BitFloat=function(a,b){return e.decodeFloat64(a.slice(b,b+8))}},function(a,k,g){function h(a,e){var b=0,c=a.length%e;if(c)for(c=-1*(c-e);br.max?p[q]=r.max:p[q]>>8&255;a[d++]=b[c]>>>16&255;a[d++]=b[c]>>>24&255;return d}var l=g(8), -f=g(3);a.exports.write64BitFloat=function(a,b,c,d){b=l.toFloat64(b[c]);d=h(a,b,1,d);return h(a,b,0,d)};a.exports.write48Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>8&255;a[d++]=b[c]>>16&255;a[d++]=b[c]>>24&255;a[d++]=b[c]/4294967296&255;a[d++]=b[c]/1099511627776&255;return d};a.exports.write40Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>8&255;a[d++]=b[c]>>16&255;a[d++]=b[c]>>24&255;a[d++]=b[c]/4294967296&255;return d};a.exports.write32BitFloat=function(a,b,c,d){b=f.unpack(b[c]);a[d++]= -b&255;a[d++]=b>>>8&255;a[d++]=b>>>16&255;a[d++]=b>>>24&255;return d};a.exports.write32Bit=h;a.exports.write24Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>>8&255;a[d++]=b[c]>>>16&255;return d};a.exports.write16Bit=function(a,b,c,d){a[d++]=b[c]&255;a[d++]=b[c]>>>8&255;return d};a.exports.write16BitFloat=function(a,b,c,d){b=l.toHalf(b[c]);a[d++]=b>>>8&255;a[d++]=b&255;return d};a.exports.write8Bit=function(a,b,c,d){a[d++]=b[c]&255;return d};a.exports.write4Bit=function(a,b,c,d){a[d++]=b[c]&15; -return d};a.exports.write2Bit=function(a,b,c,d){a[d++]=0>b[c]?b[c]+4:b[c];return d};a.exports.write1Bit=function(a,b,c,d){a[d++]=b[c]?1:0;return d};a.exports.writeString=function(a,b,c,d){a[d++]=b.charCodeAt(c);return d}},function(a,k,g){function h(a,c,d){var b=[],g=0,h=0,k=f.BitDepthOffsets[c.bits],l=a.length-(k-1),m=f.BitDepthMaxValues[c.bits];for(c=c.signed&&!c.float?e.signed:function(a,c){return a};g>>0}var f= -g(1),e=g(8),b=g(3);a.exports.readChar=function(a,b){return String.fromCharCode(a[b])};a.exports.read1Bit=function(a,b){return parseInt(a[b],2)};a.exports.read8Bit=function(a,b){return a[b]};a.exports.read16Bit=function(a,b){return a[1+b]<<8|a[b]};a.exports.read16BitFloat=function(a,b){return e.decodeFloat16(a.slice(b,b+2))};a.exports.read24Bit=function(a,b){return a[2+b]<<16|a[1+b]<<8|a[b]};a.exports.read32Bit=l;a.exports.read32BitFloat=function(a,d){return b.pack(l(a,d))};a.exports.read40Bit=function(a, -b){return h(a,b,5)};a.exports.read48Bit=function(a,b){return h(a,b,6)};a.exports.read64BitFloat=function(a,b){return e.decodeFloat64(a.slice(b,b+8))}},function(a,k,g){function h(a,e){var b=0,c=a.length%e;if(c)for(c=-1*(c-e);ba[b].length&&(a[b]=Array(5-a[b].length).join("0")+a[b])}function k(a,f,b){(2==f||16==f)&&2>a[b].length&&(a[b]="0"+a[b])}function c(a,f){for(;a.lengthparseInt(f/2,10)-1&&(a-=f);return a};a.exports.padding=g;a.exports.paddingNibble=e;a.exports.paddingCrumb=k;a.exports.bytePadding=l;a.exports.lPadZeros=c;a.exports.fixFloat16Endianness=function(a,f){f.float&&16==f.bits&&m(a,2)};a.exports.getType=function(a,f,b){a=Object.assign({},a);a.base=f;a.single=b;64==a.bits&&(a.float=!0);a.float&&(a.signed=!0);a.offset=8>a.bits? +1:a.bits/8;return a};a.exports.turnToArray=function(a){Array.isArray(a)||"string"==typeof a||(a=[a]);return a}},function(a,d){a.exports.BitDepthOffsets={1:1,2:1,4:1,8:1,16:2,24:3,32:4,40:5,48:6,64:8};a.exports.BitDepthMaxValues={1:2,2:4,4:16,8:256,16:65536,24:16777216,32:4294967296,40:1099511627776,48:281474976710656,64:Infinity}},function(a,d){a.exports={format:"Not a supported format.",wave:"Could not find the 'WAVE' format identifier","fmt ":"Could not find the 'fmt ' chunk",data:"Could not find the 'data' chunk", +fact:"Could not find the 'fact' chunk",bitDepth:"Invalid bit depth.",numChannels:"Invalid number of channels.",sampleRate:"Invalid sample rate."}},function(a,d,h){var g=h(13),l=h(0),e=h(1);a.exports.toBytes=function(a,c){var b=c.char?g.writeString:g["write"+c.bits+"Bit"+(c.float?"Float":"")];var m=0,f=0,k=a.length,d=[];if(c.signed){var h=e.BitDepthMaxValues[c.bits]/2-1;var p=e.BitDepthMaxValues[c.bits]/2*-1}else h=e.BitDepthMaxValues[c.bits]-1,p=0;for(;mh?n=h:n>10,c=a&1023;return(b?Math.pow(2,b-15)*(1+c/1024):c/1024*.00006103515625)* +(a>>15?-1:1)};a.exports.decodeFloat64=function(a){if("0,0,0,0,0,0,0,0"==a.toString())return 0;a=g(a);for(var b="1"+a.substr(12,52),c=1,f=0,e=0;e=a&&(b=2147483648,a=-a);var c=Math.floor(Math.log(a)/Math.log(2)),f=Math.floor(a/Math.pow(2,c)*Math.pow(2,52));a=f&4294967295;f/=Math.pow(2,32);return[b|c+1023<<20|f&1048575, +a]};a.exports.toHalf=function(a){e[0]=a;var b=k[0];a=b>>16&32768;var c=b>>12&2047;b=b>>23&255;return 103>b?a:(a|b-112<<10|c>>1)+(c&1)}},function(a,d){a.exports=function(a,d){for(var g=a.length,e=0;ec?8:c)+"Bit"+(b.float?"Float":"")]);f=[];d=0;for(var h=a.length-(b.offset-1),r=g(b);dthis.max&&(a-=2*this.max+2);return a};d.prototype.overflow=function(a){a>this.max?a=this.max:athis.bits?1:this.realBits/8;this.setReader_();this.setWriter_();this.float||this.setMinMax_()};d.prototype.setReader_=function(){this.reader=this.char?g.BitReader.readChar:g.BitReader["read"+(8>this.bits?8:this.realBits)+"Bit"+(this.float?"Float":"")]};d.prototype.setWriter_=function(){this.writer=this.char?g.BitWriter.writeString:g.BitWriter["write"+this.realBits+"Bit"+(this.float?"Float":"")]};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.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};a.exports=d},function(a,d,h){var g=h(10),l=h(2),e=h(11),k=h(18);a=function(a){var b=e.call(this)||this;a&&b.fromBuffer(a);return b};$jscomp.inherits(a,e);a.read64BitFloat=e.read64BitFloat;a.read48Bit=e.read48Bit;a.read40Bit= +e.read40Bit;a.read32BitFloat=e.read32BitFloat;a.read32Bit=e.read32Bit;a.read24Bit=e.read24Bit;a.read16BitFloat=e.read16BitFloat;a.read16Bit=e.read16Bit;a.read8Bit=e.read8Bit;a.readChar=e.readChar;a.writeString=e.writeString;a.write1Bit=e.write1Bit;a.write2Bit=e.write2Bit;a.write4Bit=e.write4Bit;a.write8Bit=e.write8Bit;a.write16BitFloat=e.write16BitFloat;a.write16Bit=e.write16Bit;a.write24Bit=e.write24Bit;a.write32Bit=e.write32Bit;a.write32BitFloat=e.write32BitFloat;a.write40Bit=e.write40Bit;a.write48Bit= +e.write48Bit;a.write64BitFloat=e.write64BitFloat;a.unpackNibbles=e.unpackNibbles;a.packNibbles=e.packNibbles;a.unpackCrumbs=e.unpackCrumbs;a.packCrumbs=e.packCrumbs;a.unpackBooleans=e.unpackBooleans;a.packBooleans=e.packBooleans;a.fromBytes=e.fromBytes;a.toBytes=e.toBytes;a.float64BE=e.float64BE;a.uInt48BE=e.uInt48BE;a.int48BE=e.int48BE;a.uInt40BE=e.uInt40BE;a.int40BE=e.int40BE;a.float32BE=e.float32BE;a.uInt32BE=e.uInt32BE;a.int32BE=e.int32BE;a.uInt24BE=e.uInt24BE;a.int24BE=e.int24BE;a.float16BE= +e.float16BE;a.uInt16BE=e.uInt16BE;a.int16BE=e.int16BE;a.float64=e.float64;a.uInt48=e.uInt48;a.int48=e.int48;a.uInt40=e.uInt40;a.int40=e.int40;a.float32=e.float32;a.uInt32=e.uInt32;a.int32=e.int32;a.uInt24=e.uInt24;a.int24=e.int24;a.float16=e.float16;a.uInt16=e.uInt16;a.int16=e.int16;a.uInt8=e.uInt8;a.int8=e.int8;a.uInt4=e.uInt4;a.int4=e.int4;a.uInt2=e.uInt2;a.int2=e.int2;a.bool=e.bool;a.fourCC=e.fourCC;a.chr=e.chr;a.packStruct=e.packStruct;a.unpackStruct=e.unpackStruct;a.unpackArray=e.unpackArray; +a.packArray=e.packArray;a.unpack=e.unpack;a.findString=e.findString;a.pack=e.pack;a.prototype.fromScratch=function(a,b,m,f,e){e=void 0===e?{}:e;e.container||(e.container="RIFF");var c=parseInt(m,10)/8;this.chunkSize=36+f.length*c;this.fmtChunkSize=16;this.byteRate=a*c*b;this.blockAlign=a*c;this.chunkId=e.container;this.format="WAVE";this.fmtChunkId="fmt ";this.audioFormat=this.headerFormats_[m];this.numChannels=a;this.sampleRate=b;this.bitsPerSample=parseInt(m,10);this.dataChunkId="data";this.dataChunkSize= +f.length*c;this.samples=f;this.bitDepth=m};a.prototype.fromBuffer=function(a){this.readRIFFChunk_(a);var b="RIFX"==this.chunkId;a=k.read(a,b);this.readFmtChunk_(a.subChunks);this.readFactChunk_(a.subChunks);this.readBextChunk_(a.subChunks);this.readCueChunk_(a.subChunks);this.readDataChunk_(a.subChunks,{be:b,single:!0});this.bitDepth=3==this.audioFormat&&32==this.bitsPerSample?"32f":this.bitsPerSample.toString()};a.prototype.toBuffer=function(){this.checkWriteInput_();return new Uint8Array(this.createWaveFile_())}; +a.prototype.toRIFF=function(){this.chunkId="RIFF";this.LEorBE()};a.prototype.toRIFX=function(){this.chunkId="RIFX";this.LEorBE()};a.prototype.toBitDepth=function(a){g.toBitDepth(this.samples,this.bitDepth,a);this.fromScratch(this.numChannels,this.sampleRate,a,this.samples,{container:this.chunkId})};a.prototype.interleave=function(){var a=[],b,e,f=this.samples[0].length;for(b=0;bthis.numChannels|| +65535this.sampleRate||4294967295>>8&255;a[b++]=c>>>16&255;a[b++]=c>>>24&255;return b}var l=h(4),e=h(6);a.exports.write64BitFloat=function(a,c,b){c=l.toFloat64(c);b=g(a,c[1],b);return g(a,c[0],b)};a.exports.write48Bit=function(a,c,b){a[b++]=c&255;a[b++]=c>>8&255;a[b++]=c>>16&255;a[b++]=c>>24&255;a[b++]=c/4294967296&255;a[b++]=c/1099511627776&255;return b};a.exports.write40Bit=function(a, +c,b){a[b++]=c&255;a[b++]=c>>8&255;a[b++]=c>>16&255;a[b++]=c>>24&255;a[b++]=c/4294967296&255;return b};a.exports.write32BitFloat=function(a,c,b){c=e.unpack(c);a[b++]=c&255;a[b++]=c>>>8&255;a[b++]=c>>>16&255;a[b++]=c>>>24&255;return b};a.exports.write32Bit=g;a.exports.write24Bit=function(a,c,b){a[b++]=c&255;a[b++]=c>>>8&255;a[b++]=c>>>16&255;return b};a.exports.write16Bit=function(a,c,b){a[b++]=c&255;a[b++]=c>>>8&255;return b};a.exports.write16BitFloat=function(a,c,b){c=l.toHalf(c);a[b++]=c>>>8&255; +a[b++]=c&255;return b};a.exports.write8Bit=function(a,c,b){a[b++]=c&255;return b};a.exports.write4Bit=function(a,c,b){a[b++]=c&15;return b};a.exports.write2Bit=function(a,c,b){a[b++]=0>c?c+4:c;return b};a.exports.write1Bit=function(a,c,b){a[b++]=c?1:0;return b};a.exports.writeString=function(a,c,b){a[b++]=c.charCodeAt(0);return b}},function(a,d,h){function g(a,c,f){--f;for(var b="";0<=f;)b+=e.bytePadding(a[f+c].toString(2),2),f--;return parseInt(b,2)}function l(a,c){return(a[3+c]<<24|a[2+c]<<16|a[1+ +c]<<8|a[c])>>>0}var e=h(0),k=h(4),c=h(6);a.exports.readChar=function(a,c,f){var b="",e=0;for(f=f.bits/8;ec[e].bits?1:c[e].bits/8;b=b.concat(l(a.slice(d,d+m),c[e],f));d+=m;e++}return b};a.exports.packStruct= +function(a,c,f){f=void 0===f?10:f;if(a.lengthb.bits?b.bits:b.realBits/8),e.getType(b,d));return a=b.char?a.slice(0,b.bits/8):a[0]}var e=h(20),k=h(8);d.pack=g;d.unpack=l;d.packArray=function(a, +b,d){return e.toBytes(a,e.getType(b,void 0===d?10:d))};d.unpackArray=function(a,b,d){return e.fromBytes(a,e.getType(b,void 0===d?10:d))};d.unpackStruct=function(a,b,d){d=void 0===d?10:d;for(var c=a.length,e=0,k=0;kb[e].bits?1:b[e].realBits/8;c=c.concat(l(a.slice(k,k+m),b[e],d));k+=m;e++}return c};d.packStruct=function(a,b,e){e=void 0===e?10:e;if(a.lengthd.realBits?(2==d.base?d.bits:5>d.bits?1:2)+1:(2==d.base?8:2)+1;a>10;b=c&1023;return(a?Math.pow(2,a-15)*(1+b/1024):b/1024*.00006103515625)*(c>>15?-1:1)},read24Bit:function(a,b){return a[2+b]<<16|c.read16Bit(a,b)},read32Bit:function(a,b){return(a[3+b]<<24|c.read24Bit(a,b))>>>0},read32BitFloat:function(a,b){l[0]=c.read32Bit(a,b);return g[0]},read40Bit:function(a,b){return h(a,b,5)},read48Bit:function(a,b){return h(a,b,6)},read53Bit:function(a,b){return h(a,b,7)},read64BitFloat:function(a,b){k[0]=c.read32Bit(a, +b);k[1]=c.read32Bit(a,b+4);return e[0]},readChar:function(a,b,c){var d="",e=0;for(c=c.bits/8;e>>24&255;return d},write24Bit:function(a,c,d){d=b.write16Bit(a,c,d);a[d++]=c>>>16&255;return d},write16Bit:function(a,b,c){a[c++]=b&255;a[c++]=b>>>8&255;return c},write16BitFloat:function(a,b,c){g[0]=b;var d=l[0];b=d>>16&32768;var e=d>>12&2047;d=d>>23&255;103<=d&&(b=(b|d-112<<10|e>>1)+(e&1));a[c]=b&255;a[c+1]=b>>>8&255;return c+2},write8Bit:function(a, +b,c){a[c++]=b&255;return c},write4Bit:function(a,b,c){a[c++]=b&15;return c},write2Bit:function(a,b,c){a[c++]=0>b?b+4:b;return c},write1Bit:function(a,b,c){a[c++]=b?1:0;return c},writeString:function(a,b,c){a[c++]=b.charCodeAt(0);return c}};d.BitWriter=b;d.BitReader=c}]); diff --git a/dist/wavefile.js b/dist/wavefile.js index 67abbd8..3e6d597 100644 --- a/dist/wavefile.js +++ b/dist/wavefile.js @@ -73,8 +73,8 @@ * https://github.com/rochars/byte-data */ -const endianness = __webpack_require__(7); -const bitDepths = __webpack_require__(2); +const endianness = __webpack_require__(5); +const bitDepths = __webpack_require__(1); /** * Padding for binary strings. @@ -213,195 +213,37 @@ function fixFloat16Endianness(bytes, options) { } /** - * Build a type based on the arguments. - * @param {Object} options The type. - * @param {number} bitDepth The bit depth. - */ -function buildType(options, bitDepth) { - if (bitDepth == 64) { - options.float = true; - } - if (options.float) { - options.signed = true; - } - options.bits = bitDepth; -} - -module.exports.makeBigEndian = makeBigEndian; -module.exports.bytesToBase = bytesToBase; -module.exports.outputToBase = outputToBase; -module.exports.signed = signed; -module.exports.padding = padding; -module.exports.paddingNibble = paddingNibble; -module.exports.paddingCrumb = paddingCrumb; -module.exports.bytePadding = bytePadding; -module.exports.lPadZeros = lPadZeros; -module.exports.fixFloat16Endianness = fixFloat16Endianness; -module.exports.buildType = buildType; - - -/***/ }), -/* 1 */ -/***/ (function(module, exports, __webpack_require__) { - -/* - * helpers: functions to work with bytes and byte arrays. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -const endianness = __webpack_require__(7); -const bitDepths = __webpack_require__(4); - -/** - * Padding for binary strings. - * @param {!Array} bytes The bytes as binary strings. - * @param {number} base The base. - * @param {number} index The byte to pad. - */ -function padding(bytes, base, index) { - bytes[index] = bytePadding(bytes[index], base); -} - -/** - * Padding with 0s for byte strings. - * @param {string} theByte The byte as a binary or hex string. - * @param {number} base The base. - * @returns {string} The padded byte. - */ -function bytePadding(theByte, base) { - let offset = theByte.length + 1; - if (base == 2) { - offset = 8; - } else if (base == 16) { - offset = 2; - } - return lPadZeros(theByte, offset); -} - -/** - * Fix the size of nibbles. - * @param {!Array} nibbles The nibble as a binary or hex string. - * @param {number} base The base. - * @param {number} index The nibble offset. - */ -function paddingNibble(nibbles, base, index) { - if (base == 2 && nibbles[index].length < 4) { - nibbles[index] = - new Array((5 - nibbles[index].length)).join("0") + nibbles[index]; - } -} - -/** - * Fix the size of crumbs. - * @param {!Array} crumbs The nibble as a binary or hex string. - * @param {number} base The base. - * @param {number} index The nibble offset. - */ -function paddingCrumb(crumbs, base, index) { - if ((base == 2 || base == 16) && crumbs[index].length < 2) { - crumbs[index] = '0' + crumbs[index]; - } -} - -/** - * Pad a string with zeros to the left. - * TODO: This should support both arrays and strings. - * @param {string} value The string (representing a binary or hex value). - * @param {number} numZeros the max number of zeros. - * For 1 binary byte string it should be 8. - */ -function lPadZeros(value, numZeros) { - while (value.length < numZeros) { - value = '0' + value; - } - return value; -} - -/** - * Swap the endianness to big endian. - * @param {!Array} bytes The values. - * @param {boolean} isBigEndian True if the bytes should be big endian. - * @param {number} bitDepth The bitDepth of the data. - */ -function makeBigEndian(bytes, isBigEndian, bitDepth) { - if (isBigEndian) { - endianness(bytes, bitDepths.BitDepthOffsets[bitDepth]); - } -} - -/** - * Turn bytes to base 2, 10 or 16. - * @param {!Array|!Array} bytes The bytes. - * @param {number} base The base. - * @param {Function} padFunction The function to use for padding. - */ -function bytesToBase(bytes, base, padFunction=padding) { - if (base != 10) { - let i = 0; - let len = bytes.length; - while (i < len) { - bytes[i] = bytes[i].toString(base); - padFunction(bytes, base, i); - i++; - } - } -} - -/** - * Turn the output to the correct base. - * @param {!Array} bytes The bytes. - * @param {number} bitDepth The bit depth of the data. - * @param {number} base The desired base for the output data. - */ -function outputToBase(bytes, bitDepth, base) { - if (bitDepth == 4) { - bytesToBase(bytes, base, paddingNibble); - } else if (bitDepth == 2) { - bytesToBase(bytes, base, paddingCrumb); - } else if(bitDepth == 1) { - bytesToBase(bytes, base, function(){}); - }else { - bytesToBase(bytes, base); - } -} - -/** - * Turn a unsigned number to a signed number. - * @param {number} num The number. - * @param {number} maxValue The max range for the number bit depth. + * Get the full type spec for the reading/writing. + * @param {Object} atype One of the available types. + * @param {number} base The base of the input. + * @param {boolean} single True if its a single value, false for array. + * @return {Object} */ -function signed(num, maxValue) { - if (num > parseInt(maxValue / 2, 10) - 1) { - num -= maxValue; +function getType(atype, base, single) { + let theType = Object.assign({}, atype); + theType.base = base; + theType.single = single; + if (theType.bits == 64) { + theType.float = true; } - return num; -} - -/** - * Fix the endianness of float16 bytes (r/w is always big-endian). - * @param {!Array|Uint8Array} bytes The bytes. - * @param {Object} options The type. - */ -function fixFloat16Endianness(bytes, options) { - if (options.float && options.bits == 16) { - endianness(bytes, 2); + if (theType.float) { + theType.signed = true; } + theType.offset = theType.bits < 8 ? 1 : theType.bits / 8; + return theType; } /** - * Build a type based on the arguments. - * @param {Object} options The type. - * @param {number} bitDepth The bit depth. + * Make a single value an array in case it is not. + * If the value is a string it stays a string. + * @param {!Array|number|string} values The value or values. + * @return {!Array|string} */ -function buildType(options, bitDepth) { - if (bitDepth == 64) { - options.float = true; - } - if (options.float) { - options.signed = true; +function turnToArray(values) { + if (!Array.isArray(values) && typeof values != "string") { + values = [values]; } - options.bits = bitDepth; + return values; } module.exports.makeBigEndian = makeBigEndian; @@ -414,11 +256,12 @@ module.exports.paddingCrumb = paddingCrumb; module.exports.bytePadding = bytePadding; module.exports.lPadZeros = lPadZeros; module.exports.fixFloat16Endianness = fixFloat16Endianness; -module.exports.buildType = buildType; +module.exports.getType = getType; +module.exports.turnToArray = turnToArray; /***/ }), -/* 2 */ +/* 1 */ /***/ (function(module, exports) { /* @@ -466,80 +309,10 @@ module.exports.BitDepthMaxValues = BitDepthMaxValues; /***/ }), -/* 3 */ -/***/ (function(module, exports) { - -var int8 = new Int8Array(4) -var int32 = new Int32Array(int8.buffer, 0, 1) -var float32 = new Float32Array(int8.buffer, 0, 1) - -function pack(i) { - int32[0] = i - return float32[0] -} - -function unpack(f) { - float32[0] = f - return int32[0] -} - -module.exports = pack -module.exports.pack = pack -module.exports.unpack = unpack - -/***/ }), -/* 4 */ +/* 2 */ /***/ (function(module, exports) { /* - * bit-depth: Configurations based on bit depth. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -/** - * Offset for reading each bit depth. - * @enum {number} - */ -const BitDepthOffsets = { - 1: 1, - 2: 1, - 4: 1, - 8: 1, - 16: 2, - 24: 3, - 32: 4, - 40: 5, - 48: 6, - 64: 8 -}; - -/** - * Max value for each bit depth. - * @enum {number} - */ -const BitDepthMaxValues = { - 1: 2, - 2: 4, - 4: 16, - 8: 256, - 16: 65536, - 24: 16777216, - 32: 4294967296, - 40: 1099511627776, - 48: 281474976710656, - 64: Infinity // FIXME 53-bit limit! -}; - -module.exports.BitDepthOffsets = BitDepthOffsets; -module.exports.BitDepthMaxValues = BitDepthMaxValues; - - -/***/ }), -/* 5 */ -/***/ (function(module, exports) { - -/*! * wavefile * Read & write wave files with 8, 16, 24, 32 & 64-bit data. * Copyright (c) 2017 Rafael da Silva Rocha. @@ -563,21 +336,114 @@ module.exports = { }; /***/ }), -/* 6 */ +/* 3 */ /***/ (function(module, exports, __webpack_require__) { /* - * float: Functions to work with 16, 32 & 64 bit floats. + * to-bytes: bytes to numbers and strings. * Copyright (c) 2017 Rafael da Silva Rocha. * https://github.com/rochars/byte-data */ +const writer = __webpack_require__(13); const helpers = __webpack_require__(0); +const bitDepthLib = __webpack_require__(1); -function getBinary(bytes, rev=false) { - let binary = ""; - let i = 0; - let bytesLength = bytes.length; +/** + * Turn numbers and strings to bytes. + * @param {!Array|number|string} values The data. + * @param {Object} type One of the available types. + * @return {!Array|!Array|Uint8Array} the data as a byte buffer. + */ +function toBytes(values, type) { + let bytes = writeBytes(values, type); + helpers.makeBigEndian(bytes, type.be, type.bits); + helpers.outputToBase(bytes, type.bits, type.base); + helpers.fixFloat16Endianness(bytes, type); + return bytes; +} + +/** + * 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 bitWriter; + if (type.char) { + bitWriter = writer.writeString; + } else { + bitWriter = writer[ + 'write' + type.bits + 'Bit' + (type.float ? "Float" : "")]; + } + let i = 0; + let j = 0; + let len = values.length; + let bytes = []; + let minMax = getBitDepthMinMax(type); + while (i < len) { + j = bitWriter(bytes, checkOverflow(values[i], minMax, type), j); + i++; + } + return bytes; +} + +/** + * Get the minimum and maximum values accordind to the type. + * This should be defined in bit-depth. + * @param {Object} type The options according to the type. + * @return {Object} + */ +function getBitDepthMinMax(type) { + let minMax = {}; + if (type.signed) { + minMax.max = (bitDepthLib.BitDepthMaxValues[type.bits] / 2) - 1; + minMax.min = (bitDepthLib.BitDepthMaxValues[type.bits] / 2) * -1; + } else { + minMax.max = bitDepthLib.BitDepthMaxValues[type.bits] - 1; + minMax.min = 0; + } + return minMax; +} + +/** + * Limit the value according to the bit depth in case of + * overflow or underflow. + * @param {!Array|number|string} value The data. + * @param {Object} minMax The minimum value. + * @param {Object} options The maximum value. + */ +function checkOverflow(value, minMax, options) { + if (!options.float) { + if (value > minMax.max) { + value = minMax.max; + } else if(value < minMax.min) { + value = minMax.min; + } + } + return value; +} + +module.exports.toBytes = toBytes; + + +/***/ }), +/* 4 */ +/***/ (function(module, exports, __webpack_require__) { + +/* + * float: Functions to work with 16, 32 & 64 bit floats. + * Copyright (c) 2017 Rafael da Silva Rocha. + * https://github.com/rochars/byte-data + */ + +const helpers = __webpack_require__(0); + +function getBinary(bytes, rev=false) { + let binary = ""; + let i = 0; + let bytesLength = bytes.length; while(i < bytesLength) { let bits = helpers.lPadZeros(bytes[i].toString(2), 8); if (rev) { @@ -595,7 +461,7 @@ function getBinary(bytes, rev=false) { * Thanks https://stackoverflow.com/a/8796597 * @param {number} bytes 2 bytes representing a float 16. */ -function decodeFloat16 (bytes) { +function decodeFloat16(bytes) { let binary = parseInt(getBinary(bytes, true), 2); let exponent = (binary & 0x7C00) >> 10; let fraction = binary & 0x03FF; @@ -691,7 +557,7 @@ module.exports.toHalf = toHalf; /***/ }), -/* 7 */ +/* 5 */ /***/ (function(module, exports) { /*! @@ -741,160 +607,387 @@ module.exports = endianness; /***/ }), -/* 8 */ +/* 6 */ +/***/ (function(module, exports) { + +var int8 = new Int8Array(4) +var int32 = new Int32Array(int8.buffer, 0, 1) +var float32 = new Float32Array(int8.buffer, 0, 1) + +function pack(i) { + int32[0] = i + return float32[0] +} + +function unpack(f) { + float32[0] = f + return int32[0] +} + +module.exports = pack +module.exports.pack = pack +module.exports.unpack = unpack + +/***/ }), +/* 7 */ /***/ (function(module, exports, __webpack_require__) { /* - * float: Functions to work with 16, 32 & 64 bit floats. + * from-bytes: convert bytes to numbers and strings. * Copyright (c) 2017 Rafael da Silva Rocha. * https://github.com/rochars/byte-data */ -const helpers = __webpack_require__(1); +const reader = __webpack_require__(14); +const bitDepths = __webpack_require__(1); +const helpers = __webpack_require__(0); -function getBinary(bytes, rev=false) { - let binary = ""; - let i = 0; - let bytesLength = bytes.length; - while(i < bytesLength) { - let bits = helpers.lPadZeros(bytes[i].toString(2), 8); - if (rev) { - binary = binary + bits; - } else { - binary = bits + binary; - } - i++; +/** + * 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) { + let bitDepth = type.bits; + helpers.fixFloat16Endianness(buffer, type); + helpers.makeBigEndian(buffer, type.be, bitDepth); + bytesToInt(buffer, type.base); + let values = readBytes( + buffer, + type, + getBitReader(type) + ); + if (type.single) { + values = getSingleValue(values, type); } - return binary; + return values; } /** - * Turn bytes to a float 16.. - * Thanks https://stackoverflow.com/a/8796597 - * @param {number} bytes 2 bytes representing a float 16. + * Return a function to read binary data. + * @param {Object} type One of the available types. + * @return {Function} */ -function decodeFloat16 (bytes) { - let binary = parseInt(getBinary(bytes, true), 2); - let exponent = (binary & 0x7C00) >> 10; - let fraction = binary & 0x03FF; - let floatValue; - if (exponent) { - floatValue = Math.pow(2, exponent - 15) * (1 + fraction / 0x400); +function getBitReader(type) { + return type.char ? + reader.readChar : reader[getReaderName(type.bits, type.float)]; +} + +/** + * Return the first value from the result value array. + * @param {!Array|string} values The values. + * @param {Object} type One of the available types. + * @return {number|string} + */ +function getSingleValue(values, type) { + if (type.char) { + values = values.slice(0, type.bits / 8); } else { - floatValue = 6.103515625e-5 * (fraction / 0x400); + values = values[0]; } - return floatValue * (binary >> 15 ? -1 : 1); + return values; } /** - * Turn an array of bytes into a float 64. - * Thanks https://gist.github.com/kg/2192799 - * @param {!Array} bytes 8 bytes representing a float 64. + * 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. + * @param {Function} bitReader The function to read the bytes. + * @return {!Array|string} */ -function decodeFloat64(bytes) { - if (bytes.toString() == "0,0,0,0,0,0,0,0") { - return 0; - } - let binary = getBinary(bytes); - let significandBin = "1" + binary.substr(1 + 11, 52); - let val = 1; - let significand = 0; +function readBytes(bytes, type, bitReader) { + let values = []; let i = 0; - while (i < significandBin.length) { - significand += val * parseInt(significandBin.charAt(i), 10); - val = val / 2; - i++; + let len = bytes.length - (type.offset -1); + let theSignFunction = signFunction(type); + while (i < len) { + values.push( + theSignFunction( + bitReader(bytes, i, type), + bitDepths.BitDepthMaxValues[type.bits] + ) + ); + i += type.offset; } - let sign = (binary.charAt(0) == "1") ? -1 : 1; - let doubleValue = sign * significand * - Math.pow(2, parseInt(binary.substr(1, 11), 2) - 1023); - return doubleValue; + if (type.char) { + values = values.join(""); + } + return values; } /** - * Unpack a 64 bit float into two words. - * Thanks https://stackoverflow.com/a/16043259 - * @param {number} value A float64 number. + * Return the function to apply sign or not to a type. + * @param {Object} type The type. + * @return {Function} */ -function toFloat64(value) { - if (value == 0) { - return [0, 0]; - } - let hiWord = 0; - let loWord = 0; - if (value <= 0.0) { - hiWord = 0x80000000; - value = -value; - } - let exponent = Math.floor( - Math.log(value) / Math.log(2)); - let significand = Math.floor( - (value / Math.pow(2, exponent)) * Math.pow(2, 52)); - loWord = significand & 0xFFFFFFFF; - significand /= Math.pow(2, 32); - exponent += 1023; - hiWord = hiWord | (exponent << 20); - hiWord = hiWord | (significand & ~(-1 << 20)); - return [hiWord, loWord]; +function signFunction(type) { + return type.signed && !type.float ? + helpers.signed : function(x){return x;}; } -let floatView = new Float32Array(1); -let int32View = new Int32Array(floatView.buffer); +/** + * Build a bit reading function name based on the arguments. + * @param {number} bits The bit depth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. + * @param {boolean} float True if the values are IEEE floating point numbers. + * @return {string} + */ +function getReaderName(bits, float) { + return 'read' + (bits < 8 ? 8 : bits) + + 'Bit' + (float ? "Float" : ""); +} /** - * to-half: int bits of half-precision floating point values - * Based on: - * https://mail.mozilla.org/pipermail/es-discuss/2017-April/047994.html - * https://github.com/rochars/byte-data + * Turn bytes to base 10. + * @param {!Array|Uint8Array} bytes The bytes as binary or hex strings. + * @param {number} base The base. */ -function toHalf(val) { - floatView[0] = val; - let x = int32View[0]; - let bits = (x >> 16) & 0x8000; - let m = (x >> 12) & 0x07ff; - let e = (x >> 23) & 0xff; - if (e < 103) { - return bits; +function bytesToInt(bytes, base) { + if (base != 10) { + let i = 0; + let len = bytes.length; + while(i < len) { + bytes[i] = parseInt(bytes[i], base); + i++; + } } - bits |= ((e - 112) << 10) | (m >> 1); - bits += m & 1; - return bits; } -module.exports.getBinary = getBinary; -module.exports.decodeFloat16 = decodeFloat16; -module.exports.decodeFloat64 = decodeFloat64; -module.exports.toFloat64 = toFloat64; -module.exports.toHalf = toHalf; +module.exports.fromBytes = fromBytes; /***/ }), -/* 9 */ +/* 8 */ /***/ (function(module, exports, __webpack_require__) { -/*! - * wavefile - * Read & write wave files with 8, 16, 24, 32 & 64-bit data. +/** + * type: The Type class. * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/wavefile - * + * https://github.com/rochars/byte-data */ -const bitDepthLib = __webpack_require__(10); -const WaveErrors = __webpack_require__(5); -const WaveFileReaderWriter = __webpack_require__(11); -const riffChunks = __webpack_require__(19); + +const bitParser = __webpack_require__(21); /** - * WaveFile + * A class to represent byte-data types. */ -class WaveFile extends WaveFileReaderWriter { +class Type { /** - * @param {Uint8Array} bytes A wave file buffer. + * @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(bytes) { - super(); + 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; + + +/***/ }), +/* 9 */ +/***/ (function(module, exports, __webpack_require__) { + +/*! + * wavefile + * Read & write wave files with 8, 16, 24, 32 & 64-bit data. + * Copyright (c) 2017-2018 Rafael da Silva Rocha. + * https://github.com/rochars/wavefile + * + */ + +const bitDepthLib = __webpack_require__(10); +const WaveErrors = __webpack_require__(2); +const WaveFileReaderWriter = __webpack_require__(11); +const riffChunks = __webpack_require__(18); + +/** + * WaveFile + */ +class WaveFile extends WaveFileReaderWriter { + + /** + * @param {Uint8Array} bytes A wave file buffer. + */ + constructor(bytes) { + super(); if(bytes) { this.fromBuffer(bytes); } @@ -930,8 +1023,8 @@ class WaveFile extends WaveFileReaderWriter { this.bitsPerSample = parseInt(bitDepth, 10); this.dataChunkId = "data"; this.dataChunkSize = samples.length * bytes; - this.samples_ = samples; - this.bitDepth_ = bitDepth; + this.samples = samples; + this.bitDepth = bitDepth; } /** @@ -951,9 +1044,9 @@ class WaveFile extends WaveFileReaderWriter { {"be": bigEndian, "single": true} ); if (this.audioFormat == 3 && this.bitsPerSample == 32) { - this.bitDepth_ = "32f"; + this.bitDepth = "32f"; }else { - this.bitDepth_ = this.bitsPerSample.toString(); + this.bitDepth = this.bitsPerSample.toString(); } } @@ -990,12 +1083,12 @@ class WaveFile extends WaveFileReaderWriter { * One of "8", "16", "24", "32", "32f", "64" */ toBitDepth(bitDepth) { - bitDepthLib.toBitDepth(this.samples_, this.bitDepth_, bitDepth); + bitDepthLib.toBitDepth(this.samples, this.bitDepth, bitDepth); this.fromScratch( this.numChannels, this.sampleRate, bitDepth, - this.samples_, + this.samples, {"container": this.chunkId} ); } @@ -1007,13 +1100,13 @@ class WaveFile extends WaveFileReaderWriter { let finalSamples = []; let i; let j; - let numChannels = this.samples_[0].length; + let numChannels = this.samples[0].length; for (i = 0; i < numChannels; i++) { - for (j = 0; j < this.samples_.length; j++) { - finalSamples.push(this.samples_[j][i]); + for (j = 0; j < this.samples.length; j++) { + finalSamples.push(this.samples[j][i]); } } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -1027,13 +1120,13 @@ class WaveFile extends WaveFileReaderWriter { } i = 0; let j; - while (i < this.samples_.length) { + while (i < this.samples.length) { for (j = 0; j < this.numChannels; j++) { - finalSamples[j].push(this.samples_[i+j]); + finalSamples[j].push(this.samples[i+j]); } i += j; } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -1051,7 +1144,7 @@ class WaveFile extends WaveFileReaderWriter { * @throws {Error} If any argument does not meet the criteria. */ validateBitDepth_() { - if (!this.headerFormats_[this.bitDepth_]) { + if (!this.headerFormats_[this.bitDepth]) { throw new Error(WaveErrors.bitDepth); } return true; @@ -1124,13 +1217,13 @@ const BitDepthFunctions = { * @param {Object} args Data about the original and target bit depths. * @return {number} */ - intToInt(sample, args) { + "intToInt": function(sample, args) { if (sample > 0) { sample = parseInt( - (sample / args.oldPositive) * args.newPositive, 10); + (sample / args["oldPositive"]) * args["newPositive"], 10); } else { sample = parseInt( - (sample / args.oldNegative) * args.newNegative, 10); + (sample / args["oldNegative"]) * args["newNegative"], 10); } return sample; }, @@ -1141,9 +1234,9 @@ const BitDepthFunctions = { * @param {Object} args Data about the original and target bit depths. * @return {number} */ - floatToInt(sample, args) { + "floatToInt": function(sample, args) { return sample > 0 ? - sample * args.newPositive : sample * args.newNegative; + sample * args["newPositive"] : sample * args["newNegative"]; }, /** @@ -1152,9 +1245,9 @@ const BitDepthFunctions = { * @param {Object} args Data about the original and target bit depths. * @return {number} */ - intToFloat(sample, args) { + "intToFloat": function(sample, args) { return sample > 0 ? - sample / args.oldPositive : sample / args.oldNegative; + sample / args["oldPositive"] : sample / args["oldNegative"]; }, /** @@ -1163,8 +1256,8 @@ const BitDepthFunctions = { * @param {Object} args Data about the original and target bit depths. * @return {number} */ - floatToFloat(sample, args) { - if (args.original == "64" && args.target == "32f") { + "floatToFloat": function(sample, args) { + if (args["original"] == "64" && args["target"] == "32f") { f64f32[0] = sample; sample = f64f32[0]; } @@ -1282,18 +1375,18 @@ module.exports.BitDepthMaxValues = BitDepthMaxValues; /* * WaveFileReaderWriter - * Copyright (c) 2017 Rafael da Silva Rocha. MIT License. + * Copyright (c) 2017-2018 Rafael da Silva Rocha. MIT License. * https://github.com/rochars/wavefile * */ const byteData = __webpack_require__(12); -const WaveErrors = __webpack_require__(5); +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__(18); +let WaveFileHeader = __webpack_require__(17); /** * Read and write wave files. @@ -1316,7 +1409,7 @@ class WaveFileReaderWriter extends WaveFileHeader { "64": 3 }; /** @type {!Array} */ - this.samples_ = []; + this.samples = []; } /** @@ -1458,7 +1551,7 @@ class WaveFileReaderWriter extends WaveFileHeader { options.signed = options.bits == 8 ? false : true; options.float = (this.audioFormat == 3 || this.bitsPerSample == 64) ? true : false; options.single = false; - this.samples_ = byteData.unpackArray(bytes, options); + this.samples = byteData.unpackArray(bytes, options); } /** @@ -1481,7 +1574,7 @@ class WaveFileReaderWriter extends WaveFileHeader { options.bits = this.bitsPerSample == 4 ? 8 : this.bitsPerSample; options.signed = options.bits == 8 ? false : true; options.float = (this.audioFormat == 3 || this.bitsPerSample == 64) ? true : false; - let bytes = byteData.packArray(this.samples_, options); + let bytes = byteData.packArray(this.samples, options); if (bytes.length % 2) { bytes.push(0); } @@ -1599,126 +1692,23 @@ module.exports = WaveFileReaderWriter; * */ -let toBytes = __webpack_require__(13); -let fromBytes = __webpack_require__(15); -let bitPacker = __webpack_require__(17); -let bitDepth = __webpack_require__(2); - -/** - * Find and return the start index of some string. - * Return -1 if the string is not found. - * @param {!Array|Uint8Array} bytes Array of bytes. - * @param {string} chunk Some string to look for. - * @return {number} The start index of the first occurrence, -1 if not found - */ -function findString(bytes, chunk) { - let found = ""; - for (let i = 0; i < bytes.length; i++) { - found = fromBytes.fromBytes(bytes.slice(i, i + chunk.length), - 8, {"char": true}); - if (found == chunk) { - return i; - } - } - return -1; -} - -/** - * Turn a number or 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 input. Optional. Default is 10. - * @return {!Array|!Array} - */ -function pack(value, type, base=10) { - let theType = getSingleType(type, base); - value = theType.char ? value[0] : value; - return toBytes.toBytes(turnToArray(value), theType.bits, theType); -} - -/** - * Turn a byte buffer into a readable value. - * @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. - * @return {number|string} - */ -function unpack(buffer, type, base=10) { - let theType = getSingleType(type, base); - return fromBytes.fromBytes(buffer, theType.bits, theType); -} - -/** - * Turn a array of numbers into a byte buffer. - * @param {!Array} values The values. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. Optional. Default is 10. - * @return {!Array|!Array} - */ -function packArray(values, type, base=10) { - let theType = getArrayType(type, base); - return toBytes.toBytes(values, theType.bits, theType); -} - -/** - * Turn a byte array into a sequence of readable values. - * @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. - * @return {!Array|string} - */ -function unpackArray(buffer, type, base=10) { - let theType = getArrayType(type, base); - return fromBytes.fromBytes(buffer, theType.bits, theType); -} - -/** - * Make the type a single value type on the specified base. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. - * @return {Object} - */ -function getSingleType(type, base) { - let theType = Object.assign({}, type); - theType.base = base; - theType.single = true; - return theType; -} - -/** - * Make the type a array with the specified base. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. - * @return {Object} - */ -function getArrayType(type, base) { - let theType = Object.assign({}, type); - theType.base = base; - theType.single = false; - return theType; -} - -/** - * Make a single value an array in case it is not. - * If the value is a string it stays a string. - * @param {!Array|number|string} values The value or values. - * @return {!Array|string} - */ -function turnToArray(values) { - if (!Array.isArray(values) && typeof values != "string") { - values = [values]; - } - return values; -} +let toBytes = __webpack_require__(3); +let fromBytes = __webpack_require__(7); +let bitPacker = __webpack_require__(15); +let api = __webpack_require__(16); // interface -module.exports.pack = pack; -module.exports.unpack = unpack; -module.exports.packArray = packArray; -module.exports.unpackArray = unpackArray; - -// types +module.exports.pack = api.pack; +module.exports.findString = api.findString; +module.exports.unpack = api.unpack; +module.exports.packArray = api.packArray; +module.exports.unpackArray = api.unpackArray; +module.exports.unpackStruct = api.unpackStruct; +module.exports.packStruct = api.packStruct; + +// types: LE module.exports.chr = {"bits": 8, "char": true, "single": true}; +module.exports.fourCC = {"bits": 32, "char": true, "single": true}; module.exports.bool = {"bits": 1, "single": true}; module.exports.int2 = {"bits": 2, "signed": true, "single": true}; module.exports.uInt2 = {"bits": 2, "single": true}; @@ -1740,7 +1730,34 @@ module.exports.int48 = {"bits": 48, "signed": true, "single": true}; module.exports.uInt48 = {"bits": 48, "single": true}; module.exports.float64 = {"bits": 64, "float": true, "single": true}; -module.exports.findString = findString; +// types: BE +module.exports.int16BE = { + "bits": 16, "signed": true, "single": true, "be": true}; +module.exports.uInt16BE = { + "bits": 16, "single": true, "be": true}; +module.exports.float16BE = { + "bits": 16, "float": true, "single": true, "be": true}; +module.exports.int24BE = { + "bits": 24, "signed": true, "single": true, "be": true}; +module.exports.uInt24BE = { + "bits": 24, "single": true, "be": true}; +module.exports.int32BE = { + "bits": 32, "signed": true, "single": true, "be": true}; +module.exports.uInt32BE = { + "bits": 32, "single": true, "be": true}; +module.exports.float32BE = { + "bits": 32, "float": true, "single": true, "be": true}; +module.exports.int40BE = { + "bits": 40, "signed": true, "single": true, "be": true}; +module.exports.uInt40BE = { + "bits": 40, "single": true, "be": true}; +module.exports.int48BE = { + "bits": 48, "signed": true, "single": true, "be": true}; +module.exports.uInt48BE = { + "bits": 48, "single": true, "be": true}; +module.exports.float64BE = { + "bits": 64, "float": true, "single": true, "be": true}; + module.exports.toBytes = toBytes.toBytes; module.exports.fromBytes = fromBytes.fromBytes; module.exports.packBooleans = bitPacker.packBooleans; @@ -1749,160 +1766,50 @@ module.exports.packCrumbs = bitPacker.packCrumbs; module.exports.unpackCrumbs = bitPacker.unpackCrumbs; module.exports.packNibbles = bitPacker.packNibbles; module.exports.unpackNibbles = bitPacker.unpackNibbles; -module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets; -module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues; /***/ }), /* 13 */ /***/ (function(module, exports, __webpack_require__) { -/* - * to-bytes: bytes to numbers and strings. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -const writer = __webpack_require__(14); -const helpers = __webpack_require__(0); -const bitDepthLib = __webpack_require__(2); - -/** - * Turn numbers and strings to bytes. - * @param {!Array|number|string} values The data. - * @param {number} bitDepth The bit depth of the data. - * Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. - * @param {Object} options The options: - * - "float": True for floating point numbers. Default is false. - * This option is available for 16, 32 and 64-bit numbers. - * - "signed": True for signed values. Default is false. - * - "base": The base of the output. Default is 10. Can be 2, 10 or 16. - * - "char": If the bytes represent a string. Default is false. - * - "be": If the values are big endian. Default is false (little endian). - * - "buffer": If the bytes should be returned as a Uint8Array. - * Default is false (bytes are returned as a regular array). - * @return {!Array|!Array|Uint8Array} the data as a byte buffer. - */ -function toBytes(values, bitDepth, options={"base": 10, "signed": false}) { - helpers.buildType(options, bitDepth); - let bytes = writeBytes(values, options, bitDepth); - helpers.makeBigEndian(bytes, options.be, bitDepth); - helpers.outputToBase(bytes, bitDepth, options.base); - helpers.fixFloat16Endianness(bytes, options); - if (options.buffer) { - bytes = new Uint8Array(bytes); - } - return bytes; -} - -/** - * Write values as bytes. - * @param {!Array|number|string} values The data. - * @param {Object} options The options according to the type. - * @param {number} bitDepth The bitDepth of the data. - * @return {!Array} the bytes. - */ -function writeBytes(values, options, bitDepth) { - let bitWriter; - if (options.char) { - bitWriter = writer.writeString; - } else { - bitWriter = writer['write' + bitDepth + 'Bit' + (options.float ? "Float" : "")]; - } - let i = 0; - let j = 0; - let len = values.length; - let bytes = []; - let minMax = getBitDepthMinMax(options, bitDepth); - while (i < len) { - checkOverflow(values, i, minMax, options); - j = bitWriter(bytes, values, i, j, options.signed); - i++; - } - return bytes; -} - -/** - * Get the minimum and maximum values accordind to the type. - * @param {Object} options The options according to the type. - * @param {number} bitDepth The bit depth of the data. - * @return {Object} - */ -function getBitDepthMinMax(options, bitDepth) { - let minMax = {}; - if (options.signed) { - minMax.max = (bitDepthLib.BitDepthMaxValues[bitDepth] / 2) - 1; - minMax.min = (bitDepthLib.BitDepthMaxValues[bitDepth] / 2) * -1; - } else { - minMax.max = bitDepthLib.BitDepthMaxValues[bitDepth] - 1; - minMax.min = 0; - } - return minMax; -} - -/** - * Limit the value according to the bit depth in case of - * overflow or underflow. - * @param {!Array|number|string} values The data. - * @param {number} index The index of the value in the array. - * @param {Object} minMax The minimum value. - * @param {Object} options The maximum value. - */ -function checkOverflow(values, index, minMax, options) { - if (!options.float) { - if (values[index] > minMax.max) { - values[index] = minMax.max; - } else if(values[index] < minMax.min) { - values[index] = minMax.min; - } - } -} - -module.exports.toBytes = toBytes; - - -/***/ }), -/* 14 */ -/***/ (function(module, exports, __webpack_require__) { - /* * write-bytes: Functions to turn data into bytes. * Copyright (c) 2017 Rafael da Silva Rocha. * https://github.com/rochars/byte-data */ -const floats = __webpack_require__(6); -const intBits = __webpack_require__(3); +const floats = __webpack_require__(4); +const intBits = __webpack_require__(6); -function write64BitFloat(bytes, numbers, i, j) { - let bits = floats.toFloat64(numbers[i]); - j = write32Bit(bytes, bits, 1, j); - return write32Bit(bytes, bits, 0, j); +function write64BitFloat(bytes, number, j) { + let bits = floats.toFloat64(number); + j = write32Bit(bytes, bits[1], j); + return write32Bit(bytes, bits[0], j); } // https://github.com/majimboo/c-struct -function write48Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >> 8 & 0xFF; - bytes[j++] = numbers[i] >> 16 & 0xFF; - bytes[j++] = numbers[i] >> 24 & 0xFF; - bytes[j++] = numbers[i] / 0x100000000 & 0xFF; - bytes[j++] = numbers[i] / 0x10000000000 & 0xFF; +function write48Bit(bytes, number, j) { + bytes[j++] = number & 0xFF; + bytes[j++] = number >> 8 & 0xFF; + bytes[j++] = number >> 16 & 0xFF; + bytes[j++] = number >> 24 & 0xFF; + bytes[j++] = number / 0x100000000 & 0xFF; + bytes[j++] = number / 0x10000000000 & 0xFF; return j; } // https://github.com/majimboo/c-struct -function write40Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >> 8 & 0xFF; - bytes[j++] = numbers[i] >> 16 & 0xFF; - bytes[j++] = numbers[i] >> 24 & 0xFF; - bytes[j++] = numbers[i] / 0x100000000 & 0xFF; +function write40Bit(bytes, number, j) { + bytes[j++] = number & 0xFF; + bytes[j++] = number >> 8 & 0xFF; + bytes[j++] = number >> 16 & 0xFF; + bytes[j++] = number >> 24 & 0xFF; + bytes[j++] = number / 0x100000000 & 0xFF; return j; } -function write32BitFloat(bytes, numbers, i, j) { - let bits = intBits.unpack(numbers[i]); +function write32BitFloat(bytes, number, j) { + let bits = intBits.unpack(number); bytes[j++] = bits & 0xFF; bytes[j++] = bits >>> 8 & 0xFF; bytes[j++] = bits >>> 16 & 0xFF; @@ -1910,56 +1817,56 @@ function write32BitFloat(bytes, numbers, i, j) { return j; } -function write32Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >>> 8 & 0xFF; - bytes[j++] = numbers[i] >>> 16 & 0xFF; - bytes[j++] = numbers[i] >>> 24 & 0xFF; +function write32Bit(bytes, number, j) { + bytes[j++] = number & 0xFF; + bytes[j++] = number >>> 8 & 0xFF; + bytes[j++] = number >>> 16 & 0xFF; + bytes[j++] = number >>> 24 & 0xFF; return j; } -function write24Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >>> 8 & 0xFF; - bytes[j++] = numbers[i] >>> 16 & 0xFF; +function write24Bit(bytes, number, j) { + bytes[j++] = number & 0xFF; + bytes[j++] = number >>> 8 & 0xFF; + bytes[j++] = number >>> 16 & 0xFF; return j; } -function write16Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >>> 8 & 0xFF; +function write16Bit(bytes, number, j) { + bytes[j++] = number & 0xFF; + bytes[j++] = number >>> 8 & 0xFF; return j; } -function write16BitFloat(bytes, numbers, i, j) { - let bits = floats.toHalf(numbers[i]); +function write16BitFloat(bytes, number, j) { + let bits = floats.toHalf(number); bytes[j++] = bits >>> 8 & 0xFF; bytes[j++] = bits & 0xFF; return j; } -function write8Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; +function write8Bit(bytes, number, j) { + bytes[j++] = number & 0xFF; return j; } -function write4Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xF; +function write4Bit(bytes, number, j) { + bytes[j++] = number & 0xF; return j; } -function write2Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] < 0 ? numbers[i] + 4 : numbers[i]; +function write2Bit(bytes, number, j) { + bytes[j++] = number < 0 ? number + 4 : number; return j; } -function write1Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] ? 1 : 0; +function write1Bit(bytes, number, j) { + bytes[j++] = number ? 1 : 0; return j; } -function writeString(bytes, string, i, j) { - bytes[j++] = string.charCodeAt(i); +function writeString(bytes, string, j) { + bytes[j++] = string.charCodeAt(0); return j; } @@ -1979,129 +1886,7 @@ module.exports.writeString = writeString; /***/ }), -/* 15 */ -/***/ (function(module, exports, __webpack_require__) { - -/* - * from-bytes: convert bytes to numbers and strings. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -const reader = __webpack_require__(16); -const bitDepths = __webpack_require__(2); -const helpers = __webpack_require__(0); - -/** - * Turn a byte buffer into what the bytes represent. - * @param {!Array|!Array|Uint8Array} buffer An array of bytes. - * @param {number} bitDepth The bit depth of the data. - * Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. - * @param {Object} options The options. They are: - * - "signed": If the numbers are signed. Default is false (unsigned). - * - "float": True for floating point numbers. Default is false. - * This option is available for 16, 32 and 64-bit numbers. - * - "base": The base of the input. Default is 10. Can be 2, 10 or 16. - * - "char": If the bytes represent a string. Default is false. - * - "be": If the values are big endian. Default is false (little endian). - * - "single": If it should return a single value instead of an array. - * Default is false. - * @return {!Array|string} - */ -function fromBytes(buffer, bitDepth, options={"base": 10}) { - helpers.buildType(options, bitDepth); - helpers.fixFloat16Endianness(buffer, options); - helpers.makeBigEndian(buffer, options.be, bitDepth); - bytesToInt(buffer, options.base); - let values = readBytes( - buffer, - options, - getBitReader(bitDepth, options.float, options.char) - ); - if (options.char) { - values = values.join(""); - } - if (options.single) { - values = values[0]; - } - return values; -} - -/** - * 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. - * @param {Function} bitReader The function to read the bytes. - * @return {!Array|string} - */ -function readBytes(bytes, type, bitReader) { - let values = []; - let i = 0; - let j = 0; - let offset = bitDepths.BitDepthOffsets[type.bits]; - let len = bytes.length - (offset -1); - let maxBitDepthValue = bitDepths.BitDepthMaxValues[type.bits]; - let signFunction = type.signed && !type.float ? - helpers.signed : function(x,y){return x;}; - while (i < len) { - values[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); - i += offset; - j++; - } - return values; -} - -/** - * Return a function to read binary data. - * @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. - * @param {boolean} isFloat True if the values are IEEE floating point numbers. - * @param {boolean} isChar True if it is a string. - * @return {Function} - */ -function getBitReader(bitDepth, isFloat, isChar) { - let bitReader; - if (isChar) { - bitReader = reader.readChar; - } else { - bitReader = reader[getReaderFunctionName(bitDepth, isFloat)]; - } - return bitReader; -} - -/** - * Build a bit reading function name based on the arguments. - * @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. - * @param {boolean} isFloat True if the values are IEEE floating point numbers. - * @return {string} - */ -function getReaderFunctionName(bitDepth, isFloat) { - return 'read' + - ((bitDepth == 2 || bitDepth == 4) ? 8 : bitDepth) + - 'Bit' + - (isFloat ? "Float" : ""); -} - -/** - * Turn bytes to base 10. - * @param {!Array|Uint8Array} bytes The bytes as binary or hex strings. - * @param {number} base The base. - */ -function bytesToInt(bytes, base) { - if (base != 10) { - let i = 0; - let len = bytes.length; - while(i < len) { - bytes[i] = parseInt(bytes[i], base); - i++; - } - } -} - -module.exports.fromBytes = fromBytes; - - -/***/ }), -/* 16 */ +/* 14 */ /***/ (function(module, exports, __webpack_require__) { /* @@ -2111,13 +1896,12 @@ module.exports.fromBytes = fromBytes; */ let helpers = __webpack_require__(0); -const floats = __webpack_require__(6); -const intBits = __webpack_require__(3); +const floats = __webpack_require__(4); +const intBits = __webpack_require__(6); /** * Read a group of bytes by turning it to bits. * Useful for 40 & 48-bit, but underperform. - * TODO find better alternative for 40 & 48-bit. * @param {!Array|Uint8Array} bytes An array of bytes. * @param {number} i The index to read. * @param {number} numBytes The number of bytes @@ -2134,16 +1918,6 @@ function readBytesAsBits(bytes, i, numBytes) { return parseInt(bits, 2); } -/** - * Read 1 1-bit int from from booleans. - * @param {!Array|Uint8Array} bytes An array of booleans. - * @param {number} i The index to read. - * @return {number} - */ -function read1Bit(bytes, i) { - return parseInt(bytes[i], 2); -} - /** * Read 1 8-bit int from from bytes. * @param {!Array|Uint8Array} bytes An array of bytes. @@ -2245,12 +2019,18 @@ function read64BitFloat(bytes, i) { * @param {number} i The index to read. * @return {string} */ -function readChar(bytes, i) { - return String.fromCharCode(bytes[i]); +function readChar(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; } module.exports.readChar = readChar; -module.exports.read1Bit = read1Bit; module.exports.read8Bit = read8Bit; module.exports.read16Bit = read16Bit; module.exports.read16BitFloat = read16BitFloat; @@ -2263,7 +2043,7 @@ module.exports.read64BitFloat = read64BitFloat; /***/ }), -/* 17 */ +/* 15 */ /***/ (function(module, exports, __webpack_require__) { /* @@ -2437,7 +2217,151 @@ module.exports.unpackNibbles = unpackNibbles; /***/ }), -/* 18 */ +/* 16 */ +/***/ (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 + * + */ + +let toBytes = __webpack_require__(3); +let fromBytes = __webpack_require__(7); +let helpers = __webpack_require__(0); + +/** + * Turn a number or 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. + * @return {!Array|!Array} + */ +function pack(value, type, base=10) { + let theType = helpers.getType(type, base, true); + value = theType.char ? value.slice(0, type.bits / 8) : value; + return toBytes.toBytes(helpers.turnToArray(value), theType); +} + +/** + * Turn a byte buffer into a readable value. + * @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. + * @return {number|string} + */ +function unpack(buffer, type, base=10) { + return fromBytes.fromBytes(buffer, helpers.getType(type, base, true)); +} + +/** + * Turn a array of numbers 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. + * @return {!Array|!Array} + */ +function packArray(values, type, base=10) { + return toBytes.toBytes(values, helpers.getType(type, base, false)); +} + +/** + * Turn a byte array into a sequence of readable values. + * @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. + * @return {!Array|string} + */ +function unpackArray(buffer, type, base=10) { + return fromBytes.fromBytes(buffer, helpers.getType(type, base, false)); +} + +/** + * 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), + {"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. + * @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 structure. + * 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. + * @return {Array} + */ +function unpackStruct(buffer, def, base=10) { + if (buffer.length < getStructBits(def)) { + return []; + } + let struct = []; + let i = 0; + let j = 0; + while (i < def.length) { + let bits = def[i].bits < 8 ? 1 : def[i].bits / 8; + struct = struct.concat( + unpack(buffer.slice(j, j + bits), def[i], base) + ); + j += bits; + i++; + } + return struct; +} + +function getStructBits(def) { + let bits = 0; + for (let i = 0; i < def.length; i++) { + bits += def[i].bits / 8; + } + return bits; +} + +module.exports.pack = pack; +module.exports.findString = findString; +module.exports.unpack = unpack; +module.exports.packArray = packArray; +module.exports.unpackArray = unpackArray; +module.exports.unpackStruct = unpackStruct; +module.exports.packStruct = packStruct; + + +/***/ }), +/* 17 */ /***/ (function(module, exports) { /* @@ -2533,7 +2457,7 @@ module.exports = WaveFileHeader; /***/ }), -/* 19 */ +/* 18 */ /***/ (function(module, exports, __webpack_require__) { /*! @@ -2544,7 +2468,7 @@ module.exports = WaveFileHeader; * */ -const byteData = __webpack_require__(20); +const byteData = __webpack_require__(19); const uInt32 = byteData.uInt32; const chr = byteData.chr; @@ -2553,21 +2477,21 @@ const chr = byteData.chr; * @param {Object} chunks A structure like the return of riffChunks.read(). * @param {boolean} bigEndian if the bytes should be big endian. * "RIFX" chunkId will always set bigEndian to true. - * @return {Array|Uint8Array} The: - * - file bytes as Uint8Array when chunkId is "RIFF" or "RIFX" or - * - chunk bytes as Array if chunkId is "LIST". + * @return {Array|Uint8Array} The file bytes as Uint8Array when + * chunkId is "RIFF" or "RIFX" or the chunk bytes as Array + * when chunkId is "LIST". */ function write(chunks, bigEndian=false) { if (!bigEndian) { - uInt32.be = chunks.chunkId == "RIFX"; + uInt32["be"] = chunks["chunkId"] == "RIFX"; } let bytes = - byteData.packArray(chunks.chunkId, chr).concat( - byteData.pack(chunks.chunkSize, uInt32), - byteData.packArray(chunks.format, chr), - writeSubChunks(chunks.subChunks, uInt32.be) + byteData.packArray(chunks["chunkId"], chr).concat( + byteData.pack(chunks["chunkSize"], uInt32), + byteData.packArray(chunks["format"], chr), + writeSubChunks(chunks["subChunks"], uInt32.be) ); - if (chunks.chunkId == "RIFF" || chunks.chunkId == "RIFX" ) { + if (chunks["chunkId"] == "RIFF" || chunks["chunkId"] == "RIFX" ) { bytes = new Uint8Array(bytes); } return bytes; @@ -2576,16 +2500,15 @@ function write(chunks, bigEndian=false) { /** * Get the chunks of a RIFF/RIFX file. * @param {Uint8Array|!Array} buffer The file bytes. - * @return {Object} + * @return {Object} The chunk. */ function read(buffer) { buffer = [].slice.call(buffer); let chunkId = getChunkId(buffer, 0); - uInt32.be = chunkId == "RIFX"; - let chunkSize = getChunkSize(buffer, 0); + uInt32["be"] = chunkId == "RIFX"; return { "chunkId": chunkId, - "chunkSize": chunkSize, + "chunkSize": getChunkSize(buffer, 0), "format": byteData.unpackArray(buffer.slice(8, 12), chr), "subChunks": getSubChunks(buffer) }; @@ -2595,19 +2518,19 @@ function read(buffer) { * Write the sub chunks of a RIFF file. * @param {Array} chunks The chunks. * @param {boolean} bigEndian true if its RIFX. - * @return {Array} + * @return {Array} The chunk bytes. */ function writeSubChunks(chunks, bigEndian) { let subChunks = []; let i = 0; while (i < chunks.length) { - if (chunks[i].chunkId == "LIST") { + if (chunks[i]["chunkId"] == "LIST") { subChunks = subChunks.concat(write(chunks[i], bigEndian)); } else { subChunks = subChunks.concat( - byteData.packArray(chunks[i].chunkId, chr), - byteData.pack(chunks[i].chunkSize, uInt32), - chunks[i].chunkData + byteData.packArray(chunks[i]["chunkId"], chr), + byteData.pack(chunks[i]["chunkSize"], uInt32), + chunks[i]["chunkData"] ); } i++; @@ -2618,7 +2541,7 @@ function writeSubChunks(chunks, bigEndian) { /** * Get the sub chunks of a RIFF file. * @param {Uint8Array|!Array} buffer the RIFF file bytes. - * @return {Object} + * @return {Object} The subchunks of a RIFF/RIFX or LIST chunk. */ function getSubChunks(buffer) { let chunks = []; @@ -2634,7 +2557,7 @@ function getSubChunks(buffer) { * Get a sub chunk from a RIFF file. * @param {Uint8Array|!Array} buffer the RIFF file bytes. * @param {number} index The start index of the chunk. - * @return {Object} + * @return {Object} A subchunk of a RIFF/RIFX or LIST chunk. */ function getSubChunk(buffer, index) { let chunk = { @@ -2655,7 +2578,7 @@ function getSubChunk(buffer, index) { * Return the FourCC of a chunk. * @param {Uint8Array|!Array} buffer the RIFF file bytes. * @param {number} index The start index of the chunk. - * @return {string} + * @return {string} The id of the chunk. */ function getChunkId(buffer, index) { return byteData.unpackArray(buffer.slice(index, index + 4), chr); @@ -2665,7 +2588,7 @@ function getChunkId(buffer, index) { * Return the size of a chunk. * @param {Uint8Array|!Array} buffer the RIFF file bytes. * @param {number} index The start index of the chunk. - * @return {number} + * @return {number} The size of the chunk without the id and size fields. */ function getChunkSize(buffer, index) { return byteData.unpack(buffer.slice(index + 4, index + 8), uInt32); @@ -2676,7 +2599,7 @@ module.exports.write = write; /***/ }), -/* 20 */ +/* 19 */ /***/ (function(module, exports, __webpack_require__) { /*! @@ -2687,841 +2610,898 @@ module.exports.write = write; * */ -let toBytes = __webpack_require__(21); -let fromBytes = __webpack_require__(23); -let bitPacker = __webpack_require__(25); -let bitDepth = __webpack_require__(4); - -/** - * Find and return the start index of some string. - * Return -1 if the string is not found. - * @param {!Array|Uint8Array} bytes Array of bytes. - * @param {string} chunk Some string to look for. - * @return {number} The start index of the first occurrence, -1 if not found - */ -function findString(bytes, chunk) { - let found = ""; - for (let i = 0; i < bytes.length; i++) { - found = fromBytes.fromBytes(bytes.slice(i, i + chunk.length), - 8, {"char": true}); - if (found == chunk) { - return i; - } - } - return -1; -} +/** @private */ +const rw = __webpack_require__(20); +let Type = __webpack_require__(8); /** - * Turn a number or string into a byte buffer. + * 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 input. Optional. Default is 10. + * @param {number} base The base of the output. Optional. Default is 10. + * Possible values are 2, 10 or 16. * @return {!Array|!Array} */ function pack(value, type, base=10) { - let theType = getSingleType(type, base); - value = theType.char ? value[0] : value; - return toBytes.toBytes(turnToArray(value), theType.bits, theType); + let values = []; + if (type.char) { + values = type.char ? value.slice(0, type.realBits / 8) : value; + } else if (!Array.isArray(value)) { + values = [value]; + } + return rw.toBytes(values, rw.getType(type, base)); } /** - * Turn a byte buffer into a readable 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} */ function unpack(buffer, type, base=10) { - let theType = getSingleType(type, base); - return fromBytes.fromBytes(buffer, theType.bits, theType); + 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]; + } + return values; } /** - * Turn a array of numbers into a byte buffer. - * @param {!Array} values The values. + * 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 input. Optional. Default is 10. + * @param {number} base The base of the output. Optional. Default is 10. + * Possible values are 2, 10 or 16. * @return {!Array|!Array} */ function packArray(values, type, base=10) { - let theType = getArrayType(type, base); - return toBytes.toBytes(values, theType.bits, theType); + return rw.toBytes(values, rw.getType(type, base)); } /** - * Turn a byte array into a sequence of readable values. + * 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} */ function unpackArray(buffer, type, base=10) { - let theType = getArrayType(type, base); - return fromBytes.fromBytes(buffer, theType.bits, theType); + return rw.fromBytes(buffer, rw.getType(type, base)); } /** - * Make the type a single value type on the specified base. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. - * @return {Object} + * 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 getSingleType(type, base) { - let theType = Object.assign({}, type); - theType.base = base; - theType.single = true; - return theType; +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; } /** - * Make the type a array with the specified base. - * @param {Object} type One of the available types. - * @param {number} base The base of the input. - * @return {Object} + * 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 getArrayType(type, base) { - let theType = Object.assign({}, type); - theType.base = base; - theType.single = false; - return theType; +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; } /** - * Make a single value an array in case it is not. - * If the value is a string it stays a string. - * @param {!Array|number|string} values The value or values. - * @return {!Array|string} + * 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 turnToArray(values) { - if (!Array.isArray(values) && typeof values != "string") { - values = [values]; +function unpackStruct(buffer, def, base=10) { + if (buffer.length < getStructDefSize(def)) { + return []; } - return values; + 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 -module.exports.pack = pack; -module.exports.unpack = unpack; -module.exports.packArray = packArray; -module.exports.unpackArray = unpackArray; +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 -module.exports.chr = {"bits": 8, "char": true, "single": true}; -module.exports.bool = {"bits": 1, "single": true}; -module.exports.int2 = {"bits": 2, "signed": true, "single": true}; -module.exports.uInt2 = {"bits": 2, "single": true}; -module.exports.int4 = {"bits": 4, "signed": true, "single": true}; -module.exports.uInt4 = {"bits": 4, "single": true}; -module.exports.int8 = {"bits": 8, "signed": true, "single": true}; -module.exports.uInt8 = {"bits": 8, "single": true}; -module.exports.int16 = {"bits": 16, "signed": true, "single": true}; -module.exports.uInt16 = {"bits": 16, "single": true}; -module.exports.float16 = {"bits": 16, "float": true, "single": true}; -module.exports.int24 = {"bits": 24, "signed": true, "single": true}; -module.exports.uInt24 = {"bits": 24, "single": true}; -module.exports.int32 = {"bits": 32, "signed": true, "single": true}; -module.exports.uInt32 = {"bits": 32, "single": true}; -module.exports.float32 = {"bits": 32, "float": true, "single": true}; -module.exports.int40 = {"bits": 40, "signed": true, "single": true}; -module.exports.uInt40 = {"bits": 40, "single": true}; -module.exports.int48 = {"bits": 48, "signed": true, "single": true}; -module.exports.uInt48 = {"bits": 48, "single": true}; -module.exports.float64 = {"bits": 64, "float": true, "single": true}; - -module.exports.findString = findString; -module.exports.toBytes = toBytes.toBytes; -module.exports.fromBytes = fromBytes.fromBytes; -module.exports.packBooleans = bitPacker.packBooleans; -module.exports.unpackBooleans = bitPacker.unpackBooleans; -module.exports.packCrumbs = bitPacker.packCrumbs; -module.exports.unpackCrumbs = bitPacker.unpackCrumbs; -module.exports.packNibbles = bitPacker.packNibbles; -module.exports.unpackNibbles = bitPacker.unpackNibbles; -module.exports.BitDepthOffsets = bitDepth.BitDepthOffsets; -module.exports.BitDepthMaxValues = bitDepth.BitDepthMaxValues; +/** + * 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}); /***/ }), -/* 21 */ +/* 20 */ /***/ (function(module, exports, __webpack_require__) { -/* - * to-bytes: bytes to numbers and strings. +/** + * from-bytes: Numbers and strings from bytes. * Copyright (c) 2017 Rafael da Silva Rocha. * https://github.com/rochars/byte-data */ -const writer = __webpack_require__(22); -const helpers = __webpack_require__(1); -const bitDepthLib = __webpack_require__(4); +const Type = __webpack_require__(8); +const endianness = __webpack_require__(5); + +/** + * 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 {number} bitDepth The bit depth of the data. - * Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. - * @param {Object} options The options: - * - "float": True for floating point numbers. Default is false. - * This option is available for 16, 32 and 64-bit numbers. - * - "signed": True for signed values. Default is false. - * - "base": The base of the output. Default is 10. Can be 2, 10 or 16. - * - "char": If the bytes represent a string. Default is false. - * - "be": If the values are big endian. Default is false (little endian). - * - "buffer": If the bytes should be returned as a Uint8Array. - * Default is false (bytes are returned as a regular array). - * @return {!Array|!Array|Uint8Array} the data as a byte buffer. + * @param {Object} type One of the available types. + * @return {!Array|!Array} the data as a byte buffer. */ -function toBytes(values, bitDepth, options={"base": 10, "signed": false}) { - helpers.buildType(options, bitDepth); - let bytes = writeBytes(values, options, bitDepth); - helpers.makeBigEndian(bytes, options.be, bitDepth); - helpers.outputToBase(bytes, bitDepth, options.base); - helpers.fixFloat16Endianness(bytes, options); - if (options.buffer) { - bytes = new Uint8Array(bytes); +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} options The options according to the type. - * @param {number} bitDepth The bitDepth of the data. + * @param {Object} type One of the available types. * @return {!Array} the bytes. */ -function writeBytes(values, options, bitDepth) { - let bitWriter; - if (options.char) { - bitWriter = writer.writeString; - } else { - bitWriter = writer['write' + bitDepth + 'Bit' + (options.float ? "Float" : "")]; - } +function writeBytes(values, type) { let i = 0; let j = 0; let len = values.length; let bytes = []; - let minMax = getBitDepthMinMax(options, bitDepth); while (i < len) { - checkOverflow(values, i, minMax, options); - j = bitWriter(bytes, values, i, j, options.signed); + j = type.writer(bytes, type.overflow(values[i]), j); i++; } return bytes; } /** - * Get the minimum and maximum values accordind to the type. - * @param {Object} options The options according to the type. - * @param {number} bitDepth The bit depth of the data. + * 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 getBitDepthMinMax(options, bitDepth) { - let minMax = {}; - if (options.signed) { - minMax.max = (bitDepthLib.BitDepthMaxValues[bitDepth] / 2) - 1; - minMax.min = (bitDepthLib.BitDepthMaxValues[bitDepth] / 2) * -1; - } else { - minMax.max = bitDepthLib.BitDepthMaxValues[bitDepth] - 1; - minMax.min = 0; +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++; } - return minMax; } /** - * Limit the value according to the bit depth in case of - * overflow or underflow. - * @param {!Array|number|string} values The data. - * @param {number} index The index of the value in the array. - * @param {Object} minMax The minimum value. - * @param {Object} options The maximum value. + * Turn the output to the correct base. + * @param {Array} bytes The bytes. + * @param {Object} type The type. */ -function checkOverflow(values, index, minMax, options) { - if (!options.float) { - if (values[index] > minMax.max) { - values[index] = minMax.max; - } else if(values[index] < minMax.min) { - values[index] = minMax.min; - } +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++; } } -module.exports.toBytes = toBytes; +/** + * 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; /***/ }), -/* 22 */ -/***/ (function(module, exports, __webpack_require__) { +/* 21 */ +/***/ (function(module, exports) { -/* - * write-bytes: Functions to turn data into bytes. +/** + * 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 */ -const floats = __webpack_require__(8); -const intBits = __webpack_require__(3); +/** @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); -function write64BitFloat(bytes, numbers, i, j) { - let bits = floats.toFloat64(numbers[i]); - j = write32Bit(bytes, bits, 1, j); - return write32Bit(bytes, bits, 0, j); -} +/** + * Functions to read data from bytes. + * @enum {Function} + */ +const BitReader = { -// https://github.com/majimboo/c-struct -function write48Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >> 8 & 0xFF; - bytes[j++] = numbers[i] >> 16 & 0xFF; - bytes[j++] = numbers[i] >> 24 & 0xFF; - bytes[j++] = numbers[i] / 0x100000000 & 0xFF; - bytes[j++] = numbers[i] / 0x10000000000 & 0xFF; - return j; -} + /** + * 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]; + }, -// https://github.com/majimboo/c-struct -function write40Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >> 8 & 0xFF; - bytes[j++] = numbers[i] >> 16 & 0xFF; - bytes[j++] = numbers[i] >> 24 & 0xFF; - bytes[j++] = numbers[i] / 0x100000000 & 0xFF; - return j; -} + /** + * 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]; + }, -function write32BitFloat(bytes, numbers, i, j) { - let bits = intBits.unpack(numbers[i]); - bytes[j++] = bits & 0xFF; - bytes[j++] = bits >>> 8 & 0xFF; - bytes[j++] = bits >>> 16 & 0xFF; - bytes[j++] = bits >>> 24 & 0xFF; - return j; -} + /** + * 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); + }, -function write32Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >>> 8 & 0xFF; - bytes[j++] = numbers[i] >>> 16 & 0xFF; - bytes[j++] = numbers[i] >>> 24 & 0xFF; - return j; -} + /** + * 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); + }, -function write24Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >>> 8 & 0xFF; - bytes[j++] = numbers[i] >>> 16 & 0xFF; - return j; -} + /** + * 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; + }, -function write16Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - bytes[j++] = numbers[i] >>> 8 & 0xFF; - return j; -} - -function write16BitFloat(bytes, numbers, i, j) { - let bits = floats.toHalf(numbers[i]); - bytes[j++] = bits >>> 8 & 0xFF; - bytes[j++] = bits & 0xFF; - return j; -} - -function write8Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xFF; - return j; -} - -function write4Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] & 0xF; - return j; -} - -function write2Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] < 0 ? numbers[i] + 4 : numbers[i]; - return j; -} - -function write1Bit(bytes, numbers, i, j) { - bytes[j++] = numbers[i] ? 1 : 0; - return j; -} - -function writeString(bytes, string, i, j) { - bytes[j++] = string.charCodeAt(i); - return j; -} - -module.exports.write64BitFloat = write64BitFloat; -module.exports.write48Bit = write48Bit; -module.exports.write40Bit = write40Bit; -module.exports.write32BitFloat = write32BitFloat; -module.exports.write32Bit = write32Bit; -module.exports.write24Bit = write24Bit; -module.exports.write16Bit = write16Bit; -module.exports.write16BitFloat = write16BitFloat; -module.exports.write8Bit = write8Bit; -module.exports.write4Bit = write4Bit; -module.exports.write2Bit = write2Bit; -module.exports.write1Bit = write1Bit; -module.exports.writeString = writeString; - - -/***/ }), -/* 23 */ -/***/ (function(module, exports, __webpack_require__) { - -/* - * from-bytes: convert bytes to numbers and strings. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -const reader = __webpack_require__(24); -const bitDepths = __webpack_require__(4); -const helpers = __webpack_require__(1); + /** + * 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]; + }, -/** - * Turn a byte buffer into what the bytes represent. - * @param {!Array|!Array|Uint8Array} buffer An array of bytes. - * @param {number} bitDepth The bit depth of the data. - * Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. - * @param {Object} options The options. They are: - * - "signed": If the numbers are signed. Default is false (unsigned). - * - "float": True for floating point numbers. Default is false. - * This option is available for 16, 32 and 64-bit numbers. - * - "base": The base of the input. Default is 10. Can be 2, 10 or 16. - * - "char": If the bytes represent a string. Default is false. - * - "be": If the values are big endian. Default is false (little endian). - * - "single": If it should return a single value instead of an array. - * Default is false. - * @return {!Array|string} - */ -function fromBytes(buffer, bitDepth, options={"base": 10}) { - helpers.buildType(options, bitDepth); - helpers.fixFloat16Endianness(buffer, options); - helpers.makeBigEndian(buffer, options.be, bitDepth); - bytesToInt(buffer, options.base); - let values = readBytes( - buffer, - options, - getBitReader(bitDepth, options.float, options.char) - ); - if (options.char) { - values = values.join(""); - } - if (options.single) { - values = values[0]; - } - return values; -} + /** + * 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); + }, -/** - * 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. - * @param {Function} bitReader The function to read the bytes. - * @return {!Array|string} - */ -function readBytes(bytes, type, bitReader) { - let values = []; - let i = 0; - let j = 0; - let offset = bitDepths.BitDepthOffsets[type.bits]; - let len = bytes.length - (offset -1); - let maxBitDepthValue = bitDepths.BitDepthMaxValues[type.bits]; - let signFunction = type.signed && !type.float ? - helpers.signed : function(x,y){return x;}; - while (i < len) { - values[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); - i += offset; - j++; - } - return values; -} + /** + * 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); + }, -/** - * Return a function to read binary data. - * @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. - * @param {boolean} isFloat True if the values are IEEE floating point numbers. - * @param {boolean} isChar True if it is a string. - * @return {Function} - */ -function getBitReader(bitDepth, isFloat, isChar) { - let bitReader; - if (isChar) { - bitReader = reader.readChar; - } else { - bitReader = reader[getReaderFunctionName(bitDepth, isFloat)]; - } - return bitReader; -} + /** + * 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); + }, -/** - * Build a bit reading function name based on the arguments. - * @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. - * @param {boolean} isFloat True if the values are IEEE floating point numbers. - * @return {string} - */ -function getReaderFunctionName(bitDepth, isFloat) { - return 'read' + - ((bitDepth == 2 || bitDepth == 4) ? 8 : bitDepth) + - 'Bit' + - (isFloat ? "Float" : ""); -} + /** + * 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]; + }, -/** - * Turn bytes to base 10. - * @param {!Array|Uint8Array} bytes The bytes as binary or hex strings. - * @param {number} base The base. - */ -function bytesToInt(bytes, base) { - if (base != 10) { - let i = 0; - let len = bytes.length; - while(i < len) { - bytes[i] = parseInt(bytes[i], base); - i++; + /** + * 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; } -} - -module.exports.fromBytes = fromBytes; - - -/***/ }), -/* 24 */ -/***/ (function(module, exports, __webpack_require__) { - -/* - * read-bytes: Function to read data from bytes. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ - -let helpers = __webpack_require__(1); -const floats = __webpack_require__(8); -const intBits = __webpack_require__(3); - -/** - * Read a group of bytes by turning it to bits. - * Useful for 40 & 48-bit, but underperform. - * TODO find better alternative for 40 & 48-bit. - * @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 bits = ""; - while (j >= 0) { - bits += helpers.bytePadding(bytes[j + i].toString(2), 2); - j--; - } - return parseInt(bits, 2); -} - -/** - * Read 1 1-bit int from from booleans. - * @param {!Array|Uint8Array} bytes An array of booleans. - * @param {number} i The index to read. - * @return {number} - */ -function read1Bit(bytes, i) { - return parseInt(bytes[i], 2); -} - -/** - * 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} - */ -function read8Bit(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} - */ -function read16Bit(bytes, i) { - return bytes[1 + i] << 8 | bytes[i]; -} - -/** - * Read 1 16-bit float from from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ -function read16BitFloat(bytes, i) { - return floats.decodeFloat16(bytes.slice(i,i+2)); -} - -/** - * 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} - */ -function read24Bit(bytes, i) { - return bytes[2 + i] << 16 | - bytes[1 + i] << 8 | - 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} - */ -function read32Bit(bytes, i) { - return (bytes[3 + i] << 24 | - bytes[2 + i] << 16 | - bytes[1 + i] << 8 | - 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} + * Functions to write data to bytes. + * @enum {Function} */ -function read32BitFloat(bytes, i) { - return intBits.pack(read32Bit(bytes, i)); -} +let BitWriter = { -/** - * 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} - */ -function read40Bit(bytes, i) { - return readBytesAsBits(bytes, i, 5); -} + /** + * 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); + }, -/** - * Read 1 48-bit int from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ -function read48Bit(bytes, i) { - return readBytesAsBits(bytes, i, 6); -} + /** + * 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; + }, -/** - * Read 1 64-bit double from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {number} - */ -function read64BitFloat(bytes, i) { - return floats.decodeFloat64(bytes.slice(i,i+8)); -} + /** + * 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; + }, -/** - * Read 1 char from bytes. - * @param {!Array|Uint8Array} bytes An array of bytes. - * @param {number} i The index to read. - * @return {string} - */ -function readChar(bytes, i) { - return String.fromCharCode(bytes[i]); -} + /** + * 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; + }, -module.exports.readChar = readChar; -module.exports.read1Bit = read1Bit; -module.exports.read8Bit = read8Bit; -module.exports.read16Bit = read16Bit; -module.exports.read16BitFloat = read16BitFloat; -module.exports.read24Bit = read24Bit; -module.exports.read32Bit = read32Bit; -module.exports.read32BitFloat = read32BitFloat; -module.exports.read40Bit = read40Bit; -module.exports.read48Bit = read48Bit; -module.exports.read64BitFloat = read64BitFloat; + /** + * 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; + }, -/***/ }), -/* 25 */ -/***/ (function(module, exports, __webpack_require__) { + /** + * 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; + }, -/* - * bit-packer: Pack and unpack nibbles, crumbs and booleans into bytes. - * Copyright (c) 2017 Rafael da Silva Rocha. - * https://github.com/rochars/byte-data - */ + /** + * 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; + }, -let helpers = __webpack_require__(1); + /** + * 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; + }, -/** - * Pack 2 nibbles in 1 byte. - * @param {!Array} nibbles Array of nibbles. - * @return {!Array} Pairs of neebles packed as one byte. - */ -function packNibbles(nibbles) { - let packed = []; - let i = 0; - let j = 0; - let len = nibbles.length; - if (len % 2) { - nibbles.push(0); - } - while (i < len) { - packed[j++] = parseInt( - nibbles[i].toString(16) + nibbles[i+1].toString(16), 16); - i+=2; - } - return packed; -} + /** + * 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; + }, -/** - * Unpack a byte into 2 nibbles. - * @param {!Array|Uint8Array} bytes Array of bytes. - * @return {!Array} The nibbles. - */ -function unpackNibbles(bytes) { - let unpacked = []; - let i = 0; - let j = 0; - let len = bytes.length; - while (i < len) { - unpacked[j++] = parseInt(bytes[i].toString(16)[0], 16); - unpacked[j++] = parseInt(bytes[i].toString(16)[1], 16); - i++; - } - return unpacked; -} + /** + * 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; + }, -/** - * Pack 4 crumbs in 1 byte. - * @param {!Array} crumbs Array of crumbs. - * @return {!Array} 4 crumbs packed as one byte. - */ -function packCrumbs(crumbs) { - let packed = []; - let i = 0; - let j = 0; - fixByteArraySize(crumbs, 4); - let len = crumbs.length - 3; - while (i < len) { - packed[j++] = parseInt( - helpers.lPadZeros(crumbs[i].toString(2), 2) + - helpers.lPadZeros(crumbs[i+1].toString(2), 2) + - helpers.lPadZeros(crumbs[i+2].toString(2), 2) + - helpers.lPadZeros(crumbs[i+3].toString(2), 2), 2); - i+=4; - } - return packed; -} + /** + * 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; + }, -/** - * Unpack a byte into 4 crumbs. - * @param {!Array|Uint8Array} crumbs Array of bytes. - * @return {!Array} The crumbs. - */ -function unpackCrumbs(crumbs) { - let unpacked = []; - let i = 0; - let j = 0; - let len = crumbs.length; - let bitCrumb; - while (i < len) { - bitCrumb = helpers.lPadZeros(crumbs[i].toString(2), 8); - unpacked[j++] = parseInt(bitCrumb[0] + bitCrumb[1], 2); - unpacked[j++] = parseInt(bitCrumb[2] + bitCrumb[3], 2); - unpacked[j++] = parseInt(bitCrumb[4] + bitCrumb[5], 2); - unpacked[j++] = parseInt(bitCrumb[6] + bitCrumb[7], 2); - i++; - } - return unpacked; -} + /** + * 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; + }, -/** - * Pack 8 booleans in 1 byte. - * @param {!Array} booleans Array of booleans. - * @return {!Array} 4 crumbs packed as one byte. - */ -function packBooleans(booleans) { - let packed = []; - let i = 0; - let j = 0; - fixByteArraySize(booleans, 8); - let len = booleans.length - 7; - while (i < len) { - packed[j++] = parseInt( - booleans[i].toString(2) + - booleans[i+1].toString(2) + - booleans[i+2].toString(2) + - booleans[i+3].toString(2) + - booleans[i+4].toString(2) + - booleans[i+5].toString(2) + - booleans[i+6].toString(2) + - booleans[i+7].toString(2), 2); - i+=8; + /** + * 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; } - return packed; -} +}; /** - * Unpack a byte into 8 booleans. - * @param {!Array|Uint8Array} booleans Array of bytes. - * @return {!Array} The booleans. + * Get a binary string representation of a value described as bytes. + * @param {Array|number} bytes The bytes. + * @return {string} */ -function unpackBooleans(booleans) { - let unpacked = []; +function getBinary(bytes) { + let binary = ""; let i = 0; - let j = 0; - let len = booleans.length; - let bitBoolean; - while (i < len) { - bitBoolean = helpers.lPadZeros(booleans[i].toString(2), 8); - unpacked[j++] = parseInt(bitBoolean[0], 2); - unpacked[j++] = parseInt(bitBoolean[1], 2); - unpacked[j++] = parseInt(bitBoolean[2], 2); - unpacked[j++] = parseInt(bitBoolean[3], 2); - unpacked[j++] = parseInt(bitBoolean[4], 2); - unpacked[j++] = parseInt(bitBoolean[5], 2); - unpacked[j++] = parseInt(bitBoolean[6], 2); - unpacked[j++] = parseInt(bitBoolean[7], 2); + let bytesLength = bytes.length; + while(i < bytesLength) { + let bits = bytes[i].toString(2); + binary = Array(9 - bits.length).join("0") + bits + binary; i++; } - return unpacked; + return binary; } /** - * Pad a array with zeros to the right. - * @param {!Array} byteArray The array. - * @param {number} numZeros the max number of zeros. - * For 1 binary byte string it should be 8. - * TODO: better explanation of numZeros + * 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 fixByteArraySize(byteArray, numZeros) { - let i = 0; - let fix = byteArray.length % numZeros; - if (fix) { - fix = (fix - numZeros) * -1; - while(i < fix) { - byteArray.push(0); - i++; - } +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); } -module.exports.packBooleans = packBooleans; -module.exports.unpackBooleans = unpackBooleans; -module.exports.packCrumbs = packCrumbs; -module.exports.unpackCrumbs = unpackCrumbs; -module.exports.packNibbles = packNibbles; -module.exports.unpackNibbles = unpackNibbles; +exports.BitWriter = BitWriter; +exports.BitReader = BitReader; /***/ }) diff --git a/docs/WaveFile.html b/docs/WaveFile.html index 710892e..a4aa978 100644 --- a/docs/WaveFile.html +++ b/docs/WaveFile.html @@ -1575,7 +1575,7 @@
Throws:

- Documentation generated by JSDoc 3.5.5 on Mon Dec 04 2017 15:25:52 GMT-0200 (Horário brasileiro de verão) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Fri Mar 23 2018 09:43:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/docs/index.js.html b/docs/docs/index.js.html index 4a9c079..8f78b54 100644 --- a/docs/docs/index.js.html +++ b/docs/docs/index.js.html @@ -95,7 +95,7 @@

index.js

this.bitsPerSample = parseInt(bitDepth, 10); this.dataChunkId = "data"; this.dataChunkSize = samples.length * bytes; - this.samples_ = samples; + this.samples = samples; this.bitDepth_ = bitDepth; } @@ -155,12 +155,12 @@

index.js

* One of "8", "16", "24", "32", "32f", "64" */ toBitDepth(bitDepth) { - bitDepthLib.toBitDepth(this.samples_, this.bitDepth_, bitDepth); + bitDepthLib.toBitDepth(this.samples, this.bitDepth_, bitDepth); this.fromScratch( this.numChannels, this.sampleRate, bitDepth, - this.samples_, + this.samples, {"container": this.chunkId} ); } @@ -172,13 +172,13 @@

index.js

let finalSamples = []; let i; let j; - let numChannels = this.samples_[0].length; + let numChannels = this.samples[0].length; for (i = 0; i < numChannels; i++) { - for (j = 0; j < this.samples_.length; j++) { - finalSamples.push(this.samples_[j][i]); + for (j = 0; j < this.samples.length; j++) { + finalSamples.push(this.samples[j][i]); } } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -192,13 +192,13 @@

index.js

} i = 0; let j; - while (i < this.samples_.length) { + while (i < this.samples.length) { for (j = 0; j < this.numChannels; j++) { - finalSamples[j].push(this.samples_[i+j]); + finalSamples[j].push(this.samples[i+j]); } i += j; } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** diff --git a/docs/index.html b/docs/index.html index ed20452..4a6b16c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -158,7 +158,7 @@

LICENSE

Copyright (c) 2017 Rafael da Silva Rocha.


- Documentation generated by JSDoc 3.5.5 on Mon Dec 04 2017 15:25:52 GMT-0200 (Horário brasileiro de verão) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Fri Mar 23 2018 09:43:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/index.js.html b/docs/index.js.html index ec6b82f..78619a8 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -40,7 +40,7 @@

index.js

/*!
  * wavefile
  * Read & write wave files with 8, 16, 24, 32 & 64-bit data.
- * Copyright (c) 2017 Rafael da Silva Rocha.
+ * Copyright (c) 2017-2018 Rafael da Silva Rocha.
  * https://github.com/rochars/wavefile
  *
  */
@@ -95,8 +95,8 @@ 

index.js

this.bitsPerSample = parseInt(bitDepth, 10); this.dataChunkId = "data"; this.dataChunkSize = samples.length * bytes; - this.samples_ = samples; - this.bitDepth_ = bitDepth; + this.samples = samples; + this.bitDepth = bitDepth; } /** @@ -116,9 +116,9 @@

index.js

{"be": bigEndian, "single": true} ); if (this.audioFormat == 3 && this.bitsPerSample == 32) { - this.bitDepth_ = "32f"; + this.bitDepth = "32f"; }else { - this.bitDepth_ = this.bitsPerSample.toString(); + this.bitDepth = this.bitsPerSample.toString(); } } @@ -155,12 +155,12 @@

index.js

* One of "8", "16", "24", "32", "32f", "64" */ toBitDepth(bitDepth) { - bitDepthLib.toBitDepth(this.samples_, this.bitDepth_, bitDepth); + bitDepthLib.toBitDepth(this.samples, this.bitDepth, bitDepth); this.fromScratch( this.numChannels, this.sampleRate, bitDepth, - this.samples_, + this.samples, {"container": this.chunkId} ); } @@ -172,13 +172,13 @@

index.js

let finalSamples = []; let i; let j; - let numChannels = this.samples_[0].length; + let numChannels = this.samples[0].length; for (i = 0; i < numChannels; i++) { - for (j = 0; j < this.samples_.length; j++) { - finalSamples.push(this.samples_[j][i]); + for (j = 0; j < this.samples.length; j++) { + finalSamples.push(this.samples[j][i]); } } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -192,13 +192,13 @@

index.js

} i = 0; let j; - while (i < this.samples_.length) { + while (i < this.samples.length) { for (j = 0; j < this.numChannels; j++) { - finalSamples[j].push(this.samples_[i+j]); + finalSamples[j].push(this.samples[i+j]); } i += j; } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -216,7 +216,7 @@

index.js

* @throws {Error} If any argument does not meet the criteria. */ validateBitDepth_() { - if (!this.headerFormats_[this.bitDepth_]) { + if (!this.headerFormats_[this.bitDepth]) { throw new Error(WaveErrors.bitDepth); } return true; @@ -261,7 +261,7 @@

index.js


- Documentation generated by JSDoc 3.5.5 on Mon Dec 04 2017 15:25:52 GMT-0200 (Horário brasileiro de verão) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Fri Mar 23 2018 09:43:43 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/index.js b/index.js index 4667508..3801199 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ /*! * wavefile * Read & write wave files with 8, 16, 24, 32 & 64-bit data. - * Copyright (c) 2017 Rafael da Silva Rocha. + * Copyright (c) 2017-2018 Rafael da Silva Rocha. * https://github.com/rochars/wavefile * */ @@ -56,8 +56,8 @@ class WaveFile extends WaveFileReaderWriter { this.bitsPerSample = parseInt(bitDepth, 10); this.dataChunkId = "data"; this.dataChunkSize = samples.length * bytes; - this.samples_ = samples; - this.bitDepth_ = bitDepth; + this.samples = samples; + this.bitDepth = bitDepth; } /** @@ -77,9 +77,9 @@ class WaveFile extends WaveFileReaderWriter { {"be": bigEndian, "single": true} ); if (this.audioFormat == 3 && this.bitsPerSample == 32) { - this.bitDepth_ = "32f"; + this.bitDepth = "32f"; }else { - this.bitDepth_ = this.bitsPerSample.toString(); + this.bitDepth = this.bitsPerSample.toString(); } } @@ -116,12 +116,12 @@ class WaveFile extends WaveFileReaderWriter { * One of "8", "16", "24", "32", "32f", "64" */ toBitDepth(bitDepth) { - bitDepthLib.toBitDepth(this.samples_, this.bitDepth_, bitDepth); + bitDepthLib.toBitDepth(this.samples, this.bitDepth, bitDepth); this.fromScratch( this.numChannels, this.sampleRate, bitDepth, - this.samples_, + this.samples, {"container": this.chunkId} ); } @@ -133,13 +133,13 @@ class WaveFile extends WaveFileReaderWriter { let finalSamples = []; let i; let j; - let numChannels = this.samples_[0].length; + let numChannels = this.samples[0].length; for (i = 0; i < numChannels; i++) { - for (j = 0; j < this.samples_.length; j++) { - finalSamples.push(this.samples_[j][i]); + for (j = 0; j < this.samples.length; j++) { + finalSamples.push(this.samples[j][i]); } } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -153,13 +153,13 @@ class WaveFile extends WaveFileReaderWriter { } i = 0; let j; - while (i < this.samples_.length) { + while (i < this.samples.length) { for (j = 0; j < this.numChannels; j++) { - finalSamples[j].push(this.samples_[i+j]); + finalSamples[j].push(this.samples[i+j]); } i += j; } - this.samples_ = finalSamples; + this.samples = finalSamples; } /** @@ -177,7 +177,7 @@ class WaveFile extends WaveFileReaderWriter { * @throws {Error} If any argument does not meet the criteria. */ validateBitDepth_() { - if (!this.headerFormats_[this.bitDepth_]) { + if (!this.headerFormats_[this.bitDepth]) { throw new Error(WaveErrors.bitDepth); } return true; diff --git a/package.json b/package.json index fdf639d..5e330a2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wavefile", - "version": "1.0.4", + "version": "2.0.0", "description": "Read & write wave files with 8, 16, 24, 32 & 64-bit data.", "homepage": "https://github.com/rochars/wavefile", "author": "Rafael da Silva Rocha ", diff --git a/src/wave-errors.js b/src/wave-errors.js index b461238..0719a5e 100644 --- a/src/wave-errors.js +++ b/src/wave-errors.js @@ -1,4 +1,4 @@ -/*! +/* * wavefile * Read & write wave files with 8, 16, 24, 32 & 64-bit data. * Copyright (c) 2017 Rafael da Silva Rocha. diff --git a/src/wavefile-reader-writer.js b/src/wavefile-reader-writer.js index 66c2e82..234e42b 100644 --- a/src/wavefile-reader-writer.js +++ b/src/wavefile-reader-writer.js @@ -1,6 +1,6 @@ /* * WaveFileReaderWriter - * Copyright (c) 2017 Rafael da Silva Rocha. MIT License. + * Copyright (c) 2017-2018 Rafael da Silva Rocha. MIT License. * https://github.com/rochars/wavefile * */ @@ -34,7 +34,7 @@ class WaveFileReaderWriter extends WaveFileHeader { "64": 3 }; /** @type {!Array} */ - this.samples_ = []; + this.samples = []; } /** @@ -176,7 +176,7 @@ class WaveFileReaderWriter extends WaveFileHeader { options.signed = options.bits == 8 ? false : true; options.float = (this.audioFormat == 3 || this.bitsPerSample == 64) ? true : false; options.single = false; - this.samples_ = byteData.unpackArray(bytes, options); + this.samples = byteData.unpackArray(bytes, options); } /** @@ -199,7 +199,7 @@ class WaveFileReaderWriter extends WaveFileHeader { options.bits = this.bitsPerSample == 4 ? 8 : this.bitsPerSample; options.signed = options.bits == 8 ? false : true; options.float = (this.audioFormat == 3 || this.bitsPerSample == 64) ? true : false; - let bytes = byteData.packArray(this.samples_, options); + let bytes = byteData.packArray(this.samples, options); if (bytes.length % 2) { bytes.push(0); } diff --git a/test/cue/16bit-read.js b/test/cue/16bit-read.js index 39f9222..39952a6 100644 --- a/test/cue/16bit-read.js +++ b/test/cue/16bit-read.js @@ -73,7 +73,7 @@ describe("16-bit cue reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -145,6 +145,6 @@ describe("16-bit cue reading (file with 2 markers)", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); \ No newline at end of file diff --git a/test/errors.js b/test/errors.js index 1e49cb4..e68a92a 100644 --- a/test/errors.js +++ b/test/errors.js @@ -105,7 +105,7 @@ describe('errors', function() { let wBytes = fs.readFileSync(path + "32bit-48kHz-noBext-mono.wav"); let wav = new WaveFile(); wav.fromBuffer(wBytes); - wav.bitDepth_ = 3; + wav.bitDepth = 3; wav.toBuffer(); }; expect(testFunc).to.throw("Invalid bit depth."); diff --git a/test/from-scratch/16bit-from-scratch.js b/test/from-scratch/16bit-from-scratch.js index c87b51a..ff058c0 100644 --- a/test/from-scratch/16bit-from-scratch.js +++ b/test/from-scratch/16bit-from-scratch.js @@ -63,12 +63,12 @@ describe('create 16-bit wave files from scratch', function() { assert.equal(wav.dataChunkSize, 8); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, 1, -32768, 32767]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, 1, -32768, 32767]); }); - it('bitDepth_ should be "16"', function() { - assert.equal(wav.bitDepth_, "16"); + it('bitDepth should be "16"', function() { + assert.equal(wav.bitDepth, "16"); }); /* diff --git a/test/from-scratch/24bit-from-scratch.js b/test/from-scratch/24bit-from-scratch.js index 4bd8def..f7dbaa8 100644 --- a/test/from-scratch/24bit-from-scratch.js +++ b/test/from-scratch/24bit-from-scratch.js @@ -63,12 +63,12 @@ describe('create 24-bit wave files from scratch', function() { assert.equal(wav.dataChunkSize, 12); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, 1, -8388608, 8388607]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, 1, -8388608, 8388607]); }); - it('bitDepth_ should be "24"', function() { - assert.equal(wav.bitDepth_, "24"); + it('bitDepth should be "24"', function() { + assert.equal(wav.bitDepth, "24"); }); /* diff --git a/test/from-scratch/32bitIEEE-from-scratch.js b/test/from-scratch/32bitIEEE-from-scratch.js index a3a52b6..5f45532 100644 --- a/test/from-scratch/32bitIEEE-from-scratch.js +++ b/test/from-scratch/32bitIEEE-from-scratch.js @@ -63,12 +63,12 @@ describe('create 32-bit IEEE wave file from scratch', function() { assert.equal(wav.dataChunkSize, 16); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, 0.04029441, -0.04029440, 1]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, 0.04029441, -0.04029440, 1]); }); - it('bitDepth_ should be "24"', function() { - assert.equal(wav.bitDepth_, "32f"); + it('bitDepth should be "24"', function() { + assert.equal(wav.bitDepth, "32f"); }); /* diff --git a/test/from-scratch/32bitPCM-from-scratch.js b/test/from-scratch/32bitPCM-from-scratch.js index 2bb67ca..0ad3427 100644 --- a/test/from-scratch/32bitPCM-from-scratch.js +++ b/test/from-scratch/32bitPCM-from-scratch.js @@ -63,12 +63,12 @@ describe('create 32-bit PCM wave files from scratch', function() { assert.equal(wav.dataChunkSize, 16); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, -2147483648, 2147483647, 4]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, -2147483648, 2147483647, 4]); }); - it('bitDepth_ should be "24"', function() { - assert.equal(wav.bitDepth_, "32"); + it('bitDepth should be "24"', function() { + assert.equal(wav.bitDepth, "32"); }); /* diff --git a/test/from-scratch/64bit-from-scratch.js b/test/from-scratch/64bit-from-scratch.js index e9714d2..acdeb89 100644 --- a/test/from-scratch/64bit-from-scratch.js +++ b/test/from-scratch/64bit-from-scratch.js @@ -63,12 +63,12 @@ describe('create 64-bit wave file from scratch', function() { assert.equal(wav.dataChunkSize, 32); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0.0, 0.04029440055111987, -0.04029440055111987, 1.0]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0.0, 0.04029440055111987, -0.04029440055111987, 1.0]); }); - it('bitDepth_ should be "24"', function() { - assert.equal(wav.bitDepth_, "64"); + it('bitDepth should be "24"', function() { + assert.equal(wav.bitDepth, "64"); }); /* diff --git a/test/from-scratch/8bit-from-scratch.js b/test/from-scratch/8bit-from-scratch.js index b86a005..6473d25 100644 --- a/test/from-scratch/8bit-from-scratch.js +++ b/test/from-scratch/8bit-from-scratch.js @@ -63,12 +63,12 @@ describe('create 8-bit wave files from scratch', function() { assert.equal(wav.dataChunkSize, 4); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, 255, 2, 3]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, 255, 2, 3]); }); - it('bitDepth_ should be "8"', function() { - assert.equal(wav.bitDepth_, "8"); + it('bitDepth should be "8"', function() { + assert.equal(wav.bitDepth, "8"); }); /* diff --git a/test/interleave.js b/test/interleave.js index 7eb7cd7..0ff0de8 100644 --- a/test/interleave.js +++ b/test/interleave.js @@ -23,7 +23,7 @@ describe('interleave', function() { wav.fromScratch(2, 48000, '8', stereoSamples); wav.interleave(); let expected = [0, 0, -2, -1, 4, 4, 3, 3]; - assert.deepEqual(wav.samples_, expected); + assert.deepEqual(wav.samples, expected); }); it('should de-interleave the samples of a 2-channel wave file', @@ -37,7 +37,7 @@ describe('interleave', function() { [0, -2, 4, 3], [0, -1, 4, 3] ]; - assert.deepEqual(wav.samples_, expected); + assert.deepEqual(wav.samples, expected); }); it('should interleave the samples of a 3-channel wave file', @@ -51,7 +51,7 @@ describe('interleave', function() { wav.fromScratch(2, 48000, '8', stereoSamples); wav.interleave(); let expected = [0, 0, 0, -2, -1, -1, 4, 4, 5, 3, 3, 3]; - assert.deepEqual(wav.samples_, expected); + assert.deepEqual(wav.samples, expected); }); it('should de-interleave the samples of a 3-channel wave file', @@ -66,7 +66,7 @@ describe('interleave', function() { [0, -1, 4, 3], [0, -1, 5, 3] ]; - assert.deepEqual(wav.samples_, expected); + assert.deepEqual(wav.samples, expected); }); /* diff --git a/test/read/read-16bit.js b/test/read/read-16bit.js index 037edd5..493bbbd 100644 --- a/test/read/read-16bit.js +++ b/test/read/read-16bit.js @@ -64,6 +64,6 @@ describe("16-bit reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/read/read-24bit.js b/test/read/read-24bit.js index 8cd22e7..1d18bfb 100644 --- a/test/read/read-24bit.js +++ b/test/read/read-24bit.js @@ -64,6 +64,6 @@ describe("24-bit (with bwf) reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/read/read-32bitIEEE.js b/test/read/read-32bitIEEE.js index 8a6d004..b0939da 100644 --- a/test/read/read-32bitIEEE.js +++ b/test/read/read-32bitIEEE.js @@ -64,6 +64,6 @@ describe("32-bit IEEE (with bwf) reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/read/read-32bitPCM.js b/test/read/read-32bitPCM.js index 5d6caf9..4bb2453 100644 --- a/test/read/read-32bitPCM.js +++ b/test/read/read-32bitPCM.js @@ -64,6 +64,6 @@ describe("32-bit PCM reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/read/read-4bitIMA.js b/test/read/read-4bitIMA.js index c479623..7ce72e2 100644 --- a/test/read/read-4bitIMA.js +++ b/test/read/read-4bitIMA.js @@ -61,6 +61,6 @@ describe("4-bit IMA ADPCM reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/read/read-64bit.js b/test/read/read-64bit.js index 32bc570..f2b1115 100644 --- a/test/read/read-64bit.js +++ b/test/read/read-64bit.js @@ -64,6 +64,6 @@ describe("64-bit reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/read/read-8bit.js b/test/read/read-8bit.js index acd4d4c..9d552a7 100644 --- a/test/read/read-8bit.js +++ b/test/read/read-8bit.js @@ -64,6 +64,6 @@ describe("8-bit file (with bwf) reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); diff --git a/test/rifx/from-scratch-16bit.js b/test/rifx/from-scratch-16bit.js index 4879dc0..fb84a42 100644 --- a/test/rifx/from-scratch-16bit.js +++ b/test/rifx/from-scratch-16bit.js @@ -60,12 +60,12 @@ describe('create 16-bit wave files from scratch', function() { assert.equal(wav.dataChunkSize, 8); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, 1, -32768, 32767]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, 1, -32768, 32767]); }); - it('bitDepth_ should be "16"', function() { - assert.equal(wav.bitDepth_, "16"); + it('bitDepth should be "16"', function() { + assert.equal(wav.bitDepth, "16"); }); }); @@ -129,11 +129,11 @@ describe('create 16-bit wave files from scratch, write and read the file', funct assert.equal(wav.dataChunkSize, 8); }); - it('samples_ should be the same as the args', function() { - assert.deepEqual(wav.samples_, [0, 1, -32768, 32767]); + it('samples should be the same as the args', function() { + assert.deepEqual(wav.samples, [0, 1, -32768, 32767]); }); - it('bitDepth_ should be "16"', function() { - assert.equal(wav.bitDepth_, "16"); + it('bitDepth should be "16"', function() { + assert.equal(wav.bitDepth, "16"); }); }); diff --git a/test/rifx/read-16-bit.js b/test/rifx/read-16-bit.js index 1282c20..ba2a84d 100644 --- a/test/rifx/read-16-bit.js +++ b/test/rifx/read-16-bit.js @@ -67,10 +67,10 @@ describe("16-bit RIFX reading", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); - it("samples_ in RIFX file should be the same as in the RIFF file", + it("samples in RIFX file should be the same as in the RIFF file", function() { - assert.deepEqual(wav.samples_, riffWav.samples_); + assert.deepEqual(wav.samples, riffWav.samples); }); }); diff --git a/test/rifx/toRIFF-16bit.js b/test/rifx/toRIFF-16bit.js index c5fee43..a3609d3 100644 --- a/test/rifx/toRIFF-16bit.js +++ b/test/rifx/toRIFF-16bit.js @@ -73,14 +73,14 @@ describe("16-bit RIFX to RIFF", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); - it("samples_ in RIFF-from-RIFX file should be the same as in the RIFF file", + it("samples in RIFF-from-RIFX file should be the same as in the RIFF file", function() { - assert.deepEqual(wav.samples_, riffWav.samples_); + assert.deepEqual(wav.samples, riffWav.samples); }); - it("samples_ in RIFF-from-RIFX file should be the same as in the RIFX file", + it("samples in RIFF-from-RIFX file should be the same as in the RIFX file", function() { - assert.deepEqual(wav.samples_, rifxWav.samples_); + assert.deepEqual(wav.samples, rifxWav.samples); }); }); diff --git a/test/rifx/toRIFX-16bit.js b/test/rifx/toRIFX-16bit.js index 1095cae..a33bace 100644 --- a/test/rifx/toRIFX-16bit.js +++ b/test/rifx/toRIFX-16bit.js @@ -68,11 +68,11 @@ describe("16-bit RIFF to RIFX", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); - it("samples_ in RIFF-to-RIFX file should be the same as in the RIFF file", + it("samples in RIFF-to-RIFX file should be the same as in the RIFF file", function() { - assert.deepEqual(wav.samples_, riffWav.samples_); + assert.deepEqual(wav.samples, riffWav.samples); }); }); diff --git a/test/rifx/write-16bit.js b/test/rifx/write-16bit.js index b592a2b..5bc1f54 100644 --- a/test/rifx/write-16bit.js +++ b/test/rifx/write-16bit.js @@ -76,14 +76,14 @@ describe('read 16bit RIFX file from disk and write to new RIFX file', function() }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/to-bit-depth/16bit-to-24bit.js b/test/to-bit-depth/16bit-to-24bit.js index 2cd419d..b38ef16 100644 --- a/test/to-bit-depth/16bit-to-24bit.js +++ b/test/to-bit-depth/16bit-to-24bit.js @@ -66,7 +66,7 @@ describe("16-bit from file to 24-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("16-bit mono from scratch to 24-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-8388608, 8388607]", + it("samples should be [-8388608, 8388607]", function() { - assert.deepEqual(wav.samples_, [-8388608, 8388607]); + assert.deepEqual(wav.samples, [-8388608, 8388607]); }); }); @@ -147,8 +147,8 @@ describe("16-bit mono from scratch to 24-bit (0)", function() { wav.fromScratch(1, 8000, "16", samples); wav.toBitDepth("24"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/16bit-to-32bit.js b/test/to-bit-depth/16bit-to-32bit.js index 54ddba1..619dfea 100644 --- a/test/to-bit-depth/16bit-to-32bit.js +++ b/test/to-bit-depth/16bit-to-32bit.js @@ -66,7 +66,7 @@ describe("16-bit from file to 32-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -125,9 +125,9 @@ describe("16-bit mono from scratch to 32-bit (max range)", function() { function() { assert.equal(wav.dataChunkId, 'data'); }); - it("samples_ should be [-2147483648, 2147483647]", + it("samples should be [-2147483648, 2147483647]", function() { - assert.deepEqual(wav.samples_, [-2147483648, 2147483647]); + assert.deepEqual(wav.samples, [-2147483648, 2147483647]); }); }); @@ -143,8 +143,8 @@ describe("16-bit mono from scratch to 32-bit (0)", function() { wav.fromScratch(1, 8000, "16", samples); wav.toBitDepth("32"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/16bit-to-32bitIEEE.js b/test/to-bit-depth/16bit-to-32bitIEEE.js index ebf5b2b..b409e0b 100644 --- a/test/to-bit-depth/16bit-to-32bitIEEE.js +++ b/test/to-bit-depth/16bit-to-32bitIEEE.js @@ -66,7 +66,7 @@ describe("16-bit from file to 32-bit IEEE", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("16-bit mono from scratch to 32-bit IEEE (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-1, 1]", + it("samples should be [-1, 1]", function() { - assert.deepEqual(wav.samples_, [-1, 1]); + assert.deepEqual(wav.samples, [-1, 1]); }); }); @@ -146,8 +146,8 @@ describe("16-bit mono from scratch to 32-bit IEEE (0)", function() { wav.fromScratch(1, 8000, "16", samples); wav.toBitDepth("32f"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/16bit-to-64bit.js b/test/to-bit-depth/16bit-to-64bit.js index 14e2a31..570032d 100644 --- a/test/to-bit-depth/16bit-to-64bit.js +++ b/test/to-bit-depth/16bit-to-64bit.js @@ -66,7 +66,7 @@ describe("16-bit from file to 64-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("16-bit mono from scratch to 64-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-1, 1]", + it("samples should be [-1, 1]", function() { - assert.deepEqual(wav.samples_, [-1, 1]); + assert.deepEqual(wav.samples, [-1, 1]); }); }); @@ -146,8 +146,8 @@ describe("16-bit mono from scratch to 64-bit (0)", function() { wav.fromScratch(1, 8000, "16", samples); wav.toBitDepth("64"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/16bit-to-8bit.js b/test/to-bit-depth/16bit-to-8bit.js index 376da5c..064fd99 100644 --- a/test/to-bit-depth/16bit-to-8bit.js +++ b/test/to-bit-depth/16bit-to-8bit.js @@ -66,7 +66,7 @@ describe("16-bit from file to 8-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("16-bit mono from scratch to 8-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [0, 255]", + it("samples should be [0, 255]", function() { - assert.deepEqual(wav.samples_, [0, 255]); + assert.deepEqual(wav.samples, [0, 255]); }); }); @@ -146,8 +146,8 @@ describe("16-bit mono from scratch to 8-bit (0)", function() { wav.fromScratch(1, 8000, "16", samples); wav.toBitDepth("8"); - it("samples_ should be [128]", + it("samples should be [128]", function() { - assert.deepEqual(wav.samples_, [128]); + assert.deepEqual(wav.samples, [128]); }); }); diff --git a/test/to-bit-depth/24bit-to-16bit.js b/test/to-bit-depth/24bit-to-16bit.js index e0b6295..2dde708 100644 --- a/test/to-bit-depth/24bit-to-16bit.js +++ b/test/to-bit-depth/24bit-to-16bit.js @@ -66,7 +66,7 @@ describe("24-bit mono from file to 16-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("24-bit mono from scratch to 16-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-32768, 32767]", + it("samples should be [-32768, 32767]", function() { - assert.deepEqual(wav.samples_, [-32768, 32767]); + assert.deepEqual(wav.samples, [-32768, 32767]); }); }); @@ -146,8 +146,8 @@ describe("24-bit mono from scratch to 16-bit (0)", function() { wav.fromScratch(1, 8000, "24", samples); wav.toBitDepth("16"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/32bit-to-16bit.js b/test/to-bit-depth/32bit-to-16bit.js index 76dac02..fce7fc2 100644 --- a/test/to-bit-depth/32bit-to-16bit.js +++ b/test/to-bit-depth/32bit-to-16bit.js @@ -66,7 +66,7 @@ describe("32-bit mono from file to 16-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("32-bit mono from scratch to 16-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-32768, 32767]", + it("samples should be [-32768, 32767]", function() { - assert.deepEqual(wav.samples_, [-32768, 32767]); + assert.deepEqual(wav.samples, [-32768, 32767]); }); }); @@ -146,8 +146,8 @@ describe("32-bit mono from scratch to 16-bit (0)", function() { wav.fromScratch(1, 8000, "32", samples); wav.toBitDepth("16"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/32bitIEEE-to-16bit.js b/test/to-bit-depth/32bitIEEE-to-16bit.js index a3c36af..255d080 100644 --- a/test/to-bit-depth/32bitIEEE-to-16bit.js +++ b/test/to-bit-depth/32bitIEEE-to-16bit.js @@ -66,7 +66,7 @@ describe("32-bit IEEE from file to 16-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("32-bit IEEE mono from scratch to 16-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-32768, 32767]", + it("samples should be [-32768, 32767]", function() { - assert.deepEqual(wav.samples_, [-32768, 32767]); + assert.deepEqual(wav.samples, [-32768, 32767]); }); }); @@ -146,8 +146,8 @@ describe("32-bit IEEE mono from scratch to 16-bit (0)", function() { wav.fromScratch(1, 8000, "32f", samples); wav.toBitDepth("16"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/32bitIEEE-to-64bit.js b/test/to-bit-depth/32bitIEEE-to-64bit.js index 7ed6c20..bf0f9ca 100644 --- a/test/to-bit-depth/32bitIEEE-to-64bit.js +++ b/test/to-bit-depth/32bitIEEE-to-64bit.js @@ -66,7 +66,7 @@ describe("32-bit IEEE from file to 64-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("32-bit IEEE mono from scratch to 64-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-1, 1]", + it("samples should be [-1, 1]", function() { - assert.deepEqual(wav.samples_, [-1, 1]); + assert.deepEqual(wav.samples, [-1, 1]); }); }); @@ -146,8 +146,8 @@ describe("32-bit IEEE mono from scratch to 64-bit (128)", function() { wav.fromScratch(1, 8000, "32f", samples); wav.toBitDepth("64"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/32bitIEEE-to-8bit.js b/test/to-bit-depth/32bitIEEE-to-8bit.js index 290ee1c..c3043d5 100644 --- a/test/to-bit-depth/32bitIEEE-to-8bit.js +++ b/test/to-bit-depth/32bitIEEE-to-8bit.js @@ -66,7 +66,7 @@ describe("32-bit IEEE from file to 8-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("32-bit IEEE mono from scratch to 8-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [0, 255]", + it("samples should be [0, 255]", function() { - assert.deepEqual(wav.samples_, [0, 255]); + assert.deepEqual(wav.samples, [0, 255]); }); }); @@ -146,8 +146,8 @@ describe("32-bit IEEE mono from scratch to 8-bit (0)", function() { wav.fromScratch(1, 8000, "32f", samples); wav.toBitDepth("8"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [128]); + assert.deepEqual(wav.samples, [128]); }); }); diff --git a/test/to-bit-depth/64bit-to-16bit.js b/test/to-bit-depth/64bit-to-16bit.js index 8b1e8b3..aea48b4 100644 --- a/test/to-bit-depth/64bit-to-16bit.js +++ b/test/to-bit-depth/64bit-to-16bit.js @@ -66,7 +66,7 @@ describe("64-bit from file to 16-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("64-bit mono from scratch to 16-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-32768, 32767]", + it("samples should be [-32768, 32767]", function() { - assert.deepEqual(wav.samples_, [-32768, 32767]); + assert.deepEqual(wav.samples, [-32768, 32767]); }); }); @@ -146,8 +146,8 @@ describe("64-bit mono from scratch to 16-bit (0)", function() { wav.fromScratch(1, 8000, "64", samples); wav.toBitDepth("16"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/64bit-to-32bitIEEE.js b/test/to-bit-depth/64bit-to-32bitIEEE.js index f2c01f7..9514b4a 100644 --- a/test/to-bit-depth/64bit-to-32bitIEEE.js +++ b/test/to-bit-depth/64bit-to-32bitIEEE.js @@ -66,7 +66,7 @@ describe("64-bit from file to 32-bit IEEE", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("64-bit mono from scratch to 32-bit IEEE (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-1, 1]", + it("samples should be [-1, 1]", function() { - assert.deepEqual(wav.samples_, [-1, 1]); + assert.deepEqual(wav.samples, [-1, 1]); }); }); @@ -146,8 +146,8 @@ describe("64-bit mono from scratch to 32-bit IEEE (0)", function() { wav.fromScratch(1, 8000, "64", samples); wav.toBitDepth("32f"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/64bit-to-8bit.js b/test/to-bit-depth/64bit-to-8bit.js index a0b47d6..a9389c9 100644 --- a/test/to-bit-depth/64bit-to-8bit.js +++ b/test/to-bit-depth/64bit-to-8bit.js @@ -66,7 +66,7 @@ describe("32-bit IEEE from file to 8-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("64-bit mono from scratch to 8-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [0, 255]", + it("samples should be [0, 255]", function() { - assert.deepEqual(wav.samples_, [0, 255]); + assert.deepEqual(wav.samples, [0, 255]); }); }); @@ -146,8 +146,8 @@ describe("64-bit mono from scratch to 8-bit (0)", function() { wav.fromScratch(1, 8000, "64", samples); wav.toBitDepth("8"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [128]); + assert.deepEqual(wav.samples, [128]); }); }); diff --git a/test/to-bit-depth/8bit-to-16bit.js b/test/to-bit-depth/8bit-to-16bit.js index b50dd00..d1715a4 100644 --- a/test/to-bit-depth/8bit-to-16bit.js +++ b/test/to-bit-depth/8bit-to-16bit.js @@ -66,7 +66,7 @@ describe("8-bit from file to 16-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("8-bit mono from scratch to 16-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-32768, 32767]", + it("samples should be [-32768, 32767]", function() { - assert.deepEqual(wav.samples_, [-32768, 32767]); + assert.deepEqual(wav.samples, [-32768, 32767]); }); }); @@ -146,8 +146,8 @@ describe("8-bit mono from scratch to 16-bit (128)", function() { wav.fromScratch(1, 8000, "8", samples); wav.toBitDepth("16"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/8bit-to-32bitIEEE.js b/test/to-bit-depth/8bit-to-32bitIEEE.js index bd5af7f..a4f71b9 100644 --- a/test/to-bit-depth/8bit-to-32bitIEEE.js +++ b/test/to-bit-depth/8bit-to-32bitIEEE.js @@ -66,7 +66,7 @@ describe("8-bit from file to 32-bit IEEE", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("8-bit mono from scratch to 32-bit IEEE (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-1, 1]", + it("samples should be [-1, 1]", function() { - assert.deepEqual(wav.samples_, [-1, 1]); + assert.deepEqual(wav.samples, [-1, 1]); }); }); @@ -146,8 +146,8 @@ describe("8-bit mono from scratch to 32-bit IEEE (128)", function() { wav.fromScratch(1, 8000, "8", samples); wav.toBitDepth("32f"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/to-bit-depth/8bit-to-64bit.js b/test/to-bit-depth/8bit-to-64bit.js index bc32534..036dbc9 100644 --- a/test/to-bit-depth/8bit-to-64bit.js +++ b/test/to-bit-depth/8bit-to-64bit.js @@ -66,7 +66,7 @@ describe("8-bit from file to 64-bit", function() { }); it("samples.length should be > 0", function() { - assert.ok(wav.samples_.length > 0); + assert.ok(wav.samples.length > 0); }); }); @@ -129,9 +129,9 @@ describe("8-bit mono from scratch to 64-bit (max range)", function() { function() { assert.ok(wav.dataChunkSize > 0); }); - it("samples_ should be [-1, 1]", + it("samples should be [-1, 1]", function() { - assert.deepEqual(wav.samples_, [-1, 1]); + assert.deepEqual(wav.samples, [-1, 1]); }); }); @@ -146,8 +146,8 @@ describe("8-bit mono from scratch to 64-bit (128)", function() { wav.fromScratch(1, 8000, "8", samples); wav.toBitDepth("64"); - it("samples_ should be [0]", + it("samples should be [0]", function() { - assert.deepEqual(wav.samples_, [0]); + assert.deepEqual(wav.samples, [0]); }); }); diff --git a/test/write/write-16bit.js b/test/write/write-16bit.js index 51761ce..5cbf5fd 100644 --- a/test/write/write-16bit.js +++ b/test/write/write-16bit.js @@ -76,14 +76,14 @@ describe('read 16bit file from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/write/write-24bit.js b/test/write/write-24bit.js index 6d94bf2..42af0c7 100644 --- a/test/write/write-24bit.js +++ b/test/write/write-24bit.js @@ -66,14 +66,14 @@ describe('read 24bit file from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/write/write-32bitIEEE.js b/test/write/write-32bitIEEE.js index 054e1f9..be978f2 100644 --- a/test/write/write-32bitIEEE.js +++ b/test/write/write-32bitIEEE.js @@ -70,15 +70,15 @@ describe('read 32bit IEEE from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); @@ -148,14 +148,14 @@ describe('read 32bit IEEE with markers and regions and write to new file', funct }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); \ No newline at end of file diff --git a/test/write/write-32bitPCM.js b/test/write/write-32bitPCM.js index 9b41d60..b1b9ad7 100644 --- a/test/write/write-32bitPCM.js +++ b/test/write/write-32bitPCM.js @@ -66,14 +66,14 @@ describe('read 32bit PCM from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/write/write-4bit-reaper.js b/test/write/write-4bit-reaper.js index 4e87038..f9b6a65 100644 --- a/test/write/write-4bit-reaper.js +++ b/test/write/write-4bit-reaper.js @@ -80,14 +80,14 @@ describe('read 4-bit file from disk and write to new file (different APDCM sourc }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/write/write-4bit.js b/test/write/write-4bit.js index 02b83d3..bc99e94 100644 --- a/test/write/write-4bit.js +++ b/test/write/write-4bit.js @@ -84,15 +84,15 @@ describe('read 4-bit file from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); @@ -177,14 +177,14 @@ describe('read 4-bit 22050kHz file from disk and write to new file', function() }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file", + it("samples on the new file should have the same length as in the original file", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file", + it("samples on the new file should be same as the original file", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/write/write-64bit.js b/test/write/write-64bit.js index bb59205..5a93b2e 100644 --- a/test/write/write-64bit.js +++ b/test/write/write-64bit.js @@ -66,14 +66,14 @@ describe('read 64-bit file from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file ", + it("samples on the new file should have the same length as in the original file ", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file ", + it("samples on the new file should be same as the original file ", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); }); diff --git a/test/write/write-8bit.js b/test/write/write-8bit.js index 265d1b3..1248574 100644 --- a/test/write/write-8bit.js +++ b/test/write/write-8bit.js @@ -66,14 +66,14 @@ describe('read 8-bit file from disk and write to new file', function() { }); it("samples.length should be > 0", function() { - assert.ok(wav2.samples_.length > 0); + assert.ok(wav2.samples.length > 0); }); - it("samples_ on the new file should have the same length as in the original file ", + it("samples on the new file should have the same length as in the original file ", function() { - assert.equal(wav2.samples_.length, wav.samples_.length); + assert.equal(wav2.samples.length, wav.samples.length); }); - it("samples_ on the new file should be same as the original file ", + it("samples on the new file should be same as the original file ", function() { - assert.deepEqual(wav2.samples_, wav.samples_); + assert.deepEqual(wav2.samples, wav.samples); }); });