From 2e96db20bd3d882e442554ab9cc6fd2094f7875c Mon Sep 17 00:00:00 2001 From: Rafael Rocha Date: Mon, 7 May 2018 00:42:08 -0300 Subject: [PATCH] WaveFile.toBase64() and WaveFile.toDataURI() --- CHANGELOG.md | 3 + README.md | 41 ++++- dist/wavefile-min.js | 113 ++++++------ docs/WaveFile.html | 332 +++++++++++++++++++++++++++++++---- docs/example/index.html | 329 +++++++++++++++++++++++----------- docs/example/wavefile-min.js | 114 ++++++------ docs/index.html | 25 ++- docs/index.js.html | 29 ++- index.js | 25 ++- package.json | 3 +- test/write/toBase64.js | 23 +++ 11 files changed, 781 insertions(+), 256 deletions(-) create mode 100644 test/write/toBase64.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 5859591..7889610 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +## version 6.4.0 (2018-05-07) +- WaveFile.toBase64() and WaveFile.toDataURI() to export the file as a Base64 string or a DataURI. + ## version 6.3.0 (2018-05-06) - fromIMAADPCM, fromALaw and fromMuLaw can receive an optional parameter: the bit depth of the output. If ommited, defaults to "16". - Fix: argument validation in fromScratch diff --git a/README.md b/README.md index 5c36e3a..0ae78a3 100644 --- a/README.md +++ b/README.md @@ -82,14 +82,51 @@ Some bit depths may not be supported by your browser, like 32-bit floating point ## Use ```javascript -let fs = require("fs"); -let Wavefile = require("wavefile"); +const fs = require("fs"); +const Wavefile = require("wavefile"); +// Load a wav file from disk into a WaveFile object let wav = new Wavefile(fs.readFileSync("file.wav")); + +// Check some of the file properties console.log(wav.container); console.log(wav.chunkSize); console.log(wav.fmt.chunkId); + +// Call toBuffer() to get the bytes of the file. +// You can write the output straight to disk: fs.writeFileSync(path, wav.toBuffer()); + +// Call toDataURI() to get the file as a base64-encoded +// DataURI to load the file a web browser: +wavDataURI = wav.toDataURI(); +``` + +### Main methods: + +#### WaveFile.fromBuffer() +Load a .wav file from a byte buffer into a WaveFile object: +```javascript +let wav = new Wavefile(buffer); +``` + +#### WaveFile.fromScratch() +Create a WaveFile object with the arguments you pass: +```javascript +// A mono, 44.1 kHz, 32-bit .wav file with just 4 samples: +wav.fromScratch(1, 44100, '32', [0, -2147483648, 2147483647, 4]); +``` + +#### WaveFile.toBuffer() +Return a Uint8Array with the WaveFile object data. The buffer is a .wav file and can be written to disk: +```javascript +buffer = wav.toBuffer(); +``` + +#### WaveFile.toDataURI() +Return a DataURI string with the WaveFile object data. The DataURI is a .wav file and can be played in browsers: +```javascript +wavDataURI = wav.toDataURI(); ``` ### Create wave files from scratch diff --git a/dist/wavefile-min.js b/dist/wavefile-min.js index eb71163..802ad81 100644 --- a/dist/wavefile-min.js +++ b/dist/wavefile-min.js @@ -5,58 +5,61 @@ https://github.com/rochars/wavefile */ -var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,d,g){a!=Array.prototype&&a!=Object.prototype&&(a[d]=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,d,g,k){if(d){g=$jscomp.global;a=a.split(".");for(k=0;k>10;var e=b&1023;return(a?Math.pow(2,a-15)*(1+e/1024): -e/1024*.00006103515625)*(b>>15?-1:1)}function e(b,a){u[0]=q.read(b,a);return y[0]}function h(b,a){u[0]=q.read(b,a);u[1]=q.read(b,a+4);return A[0]}function f(b,a){for(var e="",c=0;c>16&32768;var d=c>>12&2047;c=c>>23&255;103<=c&&(a=(a|c-112<<10|d>>1)+(d&1));b[e++]=a&255;b[e++]=a>>>8&255;return e}function b(b,a,e){y[0]=a;return q.write(b,u[0],e)}function t(b,a,e){A[0]= -a;e=q.write(b,u[0],e);return q.write(b,u[1],e)}function r(b,a,e){for(var c=0;ca.bits||a.bits%2)throw Error("Wrong offset for type char.");}else if(1>a.bits||53a.bits?1:Math.ceil(a.bits/8);a["float"]?16==a.bits?v=m:32==a.bits?v=e:64== -a.bits&&(v=h):v=a["char"]?f:p;a["float"]?16==a.bits?w=n:32==a.bits?w=b:64==a.bits&&(w=t):w=a["char"]?r:l;a["char"]?q.offset=8>a.bits?1:Math.ceil(a.bits/8):q=new C(64==a.bits?32:a.bits,a["float"]?!1:a.signed)}function B(b,a){b.constructor==String&&(b=b.length>=a.offset?b.slice(0,a.offset):"");return b}var z=g(5),C=g(6);d=new Int8Array(8);var u=new Uint32Array(d.buffer),y=new Float32Array(d.buffer),A=new Float64Array(d.buffer),v,w,q={};a.exports.chr={bits:8,"char":!0};a.exports.fourCC={bits:32,"char":!0}; -a.exports.bool={bits:1};a.exports.int2={bits:2,signed:!0};a.exports.uInt2={bits:2};a.exports.int4={bits:4,signed:!0};a.exports.uInt4={bits:4};a.exports.int8={bits:8,signed:!0};a.exports.uInt8={bits:8};a.exports.int16={bits:16,signed:!0};a.exports.uInt16={bits:16};a.exports.float16={bits:16,"float":!0};a.exports.int24={bits:24,signed:!0};a.exports.uInt24={bits:24};a.exports.int32={bits:32,signed:!0};a.exports.uInt32={bits:32};a.exports.float32={bits:32,"float":!0};a.exports.int40={bits:40,signed:!0}; -a.exports.uInt40={bits:40};a.exports.int48={bits:48,signed:!0};a.exports.uInt48={bits:48};a.exports.float64={bits:64,"float":!0};a.exports.int16BE={bits:16,signed:!0,be:!0};a.exports.uInt16BE={bits:16,be:!0};a.exports.float16BE={bits:16,"float":!0,be:!0};a.exports.int24BE={bits:24,signed:!0,be:!0};a.exports.uInt24BE={bits:24,be:!0};a.exports.int32BE={bits:32,signed:!0,be:!0};a.exports.uInt32BE={bits:32,be:!0};a.exports.float32BE={bits:32,"float":!0,be:!0};a.exports.int40BE={bits:40,signed:!0,be:!0}; -a.exports.uInt40BE={bits:40,be:!0};a.exports.int48BE={bits:48,signed:!0,be:!0};a.exports.uInt48BE={bits:48,be:!0};a.exports.float64BE={bits:64,"float":!0,be:!0};a.exports.pack=function(b,a,e){e=void 0===e?10:e;x(a);var d=[];if(void 0===b)return d;b=B(b,a);return c([b],a,e)};a.exports.unpack=function(b,a,e){e=void 0===e?10:e;x(a);return(b=k(b.slice(0,a.offset),a,e))?b[0]:a["char"]?"":null};a.exports.packArray=function(b,a,e){e=void 0===e?10:e;x(a);if(a["char"])for(var d=b.length,r=0;rparseInt(this.bitDepth,10))return!0;throw Error("Invalid bit depth.");}return!0};d.prototype.validateNumChannels_=function(){var b=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(1>this.fmt.numChannels||65535this.fmt.sampleRate||4294967295parseInt(a,10)||"53">>0;return this.overflow_(this.sign_(c))};d.prototype.write=function(a,d,c){c=void 0===c?0:c;d=this.overflow_(d);var g=255;c=this.writeFirstByte_(a,d,c);for(var m=2;m<=this.offset;m++)m==this.offset&&(g=this.lastByteMask_),a[c++]=Math.floor(d/Math.pow(2,8*(m-1)))&g;return c};d.prototype.readBits_=function(a,d){d=void 0===d?0:d; -for(var c="",g=0;gthis.bits?1:Math.ceil(this.realBits_/8)};d.prototype.sign_=function(a){a>this.max&&(a-=2*this.max+2);return a};d.prototype.overflow_=function(a){a>this.max?a=this.max:athis.bits||64this.bits?a[c++]=0>d?d+Math.pow(2,this.bits): -d:a[c++]=d&255;return c};a.exports=d},function(a,d){function g(a){return 32768>3;c>b&&(a|=4,c-=b,d+=b);b>>=1;c>b&&(a|=2,c-=b,d+=b);b>>=1;c>b&&(a|=1,d+=b);c=a;f=c&8?f-d:f+d;-32768>f?f=-32768:32767l?l=0:88>1);a&1&&(c+=t>>2);c+=t>>3;a&8&&(c=-c);n+=c;32767n&&(n=-32767);b+=e[a];0>b?b=0:88>8&255);b.push(l);b.push(0);for(c=3;cb.length;)b.push(0);return b}function m(a){n=g(a[1]<<8|a[0]);b=a[2];t=h[b];for(var e=[n,g(a[3]<<8|a[2])],d=4;d>4;e.push(c(m<<4^f));e.push(c(m))}return e}var e=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],h=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279, -307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767],f=0,l=0,n=0,b=0,t=7;a.exports.encode=function(a){for(var c=[],b=[],e=0;e>8&128)||(a*=-1); -32635>8&127],a=c<<4|a>>c+3&15):a>>=4;return a^d^85}function k(a){var c=0;a^=85;a&128&&(a&=-129,c=-1);var d=((a&240)>>4)+4;a=4!=d?1<a){a=132-a;var c=127}else a+=132, -c=255;32767>7;e&240&&(e>>=4,d+=4);e&12&&(e>>=2,d+=2);e&2&&(d+=1);e=d;return(e<<4|a>>e+3&15)^c}function k(a){a=~a;var c=((a&15)<<3)+132<<((a&112)>>4);return a&128?132-c:c-132}a.exports.encodeSample=g;a.exports.decodeSample=k;a.exports.encode=function(a){for(var c=[],d=0;d>10;var f=d&1023;return(c?Math.pow(2,c-15)*(1+f/1024): +f/1024*.00006103515625)*(d>>15?-1:1)}function c(d,c){t[0]=r.read(d,c);return y[0]}function m(d,c){t[0]=r.read(d,c);t[1]=r.read(d,c+4);return A[0]}function h(d,c){for(var f="",a=0;a>16&32768;var b=a>>12&2047;a=a>>23&255;103<=a&&(c=(c|a-112<<10|b>>1)+(b&1));d[f++]=c&255;d[f++]=c>>>8&255;return f}function q(d,c,f){y[0]=c;return r.write(d,t[0],f)}function d(d,c,f){A[0]= +c;f=r.write(d,t[0],f);return r.write(d,t[1],f)}function w(d,c,f){for(var a=0;aa.bits||a.bits%2)throw Error("Wrong offset for type char.");}else if(1>a.bits||53a.bits?1:Math.ceil(a.bits/8);a["float"]?16==a.bits?u=n:32==a.bits?u=c:64== +a.bits&&(u=m):u=a["char"]?h:p;a["float"]?16==a.bits?v=l:32==a.bits?v=q:64==a.bits&&(v=d):v=a["char"]?w:f;a["char"]?r.offset=8>a.bits?1:Math.ceil(a.bits/8):r=new C(64==a.bits?32:a.bits,a["float"]?!1:a.signed)}function B(d,a){d.constructor==String&&(d=d.length>=a.offset?d.slice(0,a.offset):"");return d}var z=g(5),C=g(6);e=new Int8Array(8);var t=new Uint32Array(e.buffer),y=new Float32Array(e.buffer),A=new Float64Array(e.buffer),u,v,r={};b.exports.chr={bits:8,"char":!0};b.exports.fourCC={bits:32,"char":!0}; +b.exports.bool={bits:1};b.exports.int2={bits:2,signed:!0};b.exports.uInt2={bits:2};b.exports.int4={bits:4,signed:!0};b.exports.uInt4={bits:4};b.exports.int8={bits:8,signed:!0};b.exports.uInt8={bits:8};b.exports.int16={bits:16,signed:!0};b.exports.uInt16={bits:16};b.exports.float16={bits:16,"float":!0};b.exports.int24={bits:24,signed:!0};b.exports.uInt24={bits:24};b.exports.int32={bits:32,signed:!0};b.exports.uInt32={bits:32};b.exports.float32={bits:32,"float":!0};b.exports.int40={bits:40,signed:!0}; +b.exports.uInt40={bits:40};b.exports.int48={bits:48,signed:!0};b.exports.uInt48={bits:48};b.exports.float64={bits:64,"float":!0};b.exports.int16BE={bits:16,signed:!0,be:!0};b.exports.uInt16BE={bits:16,be:!0};b.exports.float16BE={bits:16,"float":!0,be:!0};b.exports.int24BE={bits:24,signed:!0,be:!0};b.exports.uInt24BE={bits:24,be:!0};b.exports.int32BE={bits:32,signed:!0,be:!0};b.exports.uInt32BE={bits:32,be:!0};b.exports.float32BE={bits:32,"float":!0,be:!0};b.exports.int40BE={bits:40,signed:!0,be:!0}; +b.exports.uInt40BE={bits:40,be:!0};b.exports.int48BE={bits:48,signed:!0,be:!0};b.exports.uInt48BE={bits:48,be:!0};b.exports.float64BE={bits:64,"float":!0,be:!0};b.exports.pack=function(d,c,f){f=void 0===f?10:f;x(c);var b=[];if(void 0===d)return b;d=B(d,c);return a([d],c,f)};b.exports.unpack=function(d,a,c){c=void 0===c?10:c;x(a);return(d=k(d.slice(0,a.offset),a,c))?d[0]:a["char"]?"":null};b.exports.packArray=function(d,c,f){f=void 0===f?10:f;x(c);if(c["char"])for(var b=d.length,e=0;eparseInt(this.bitDepth,10))return!0;throw Error("Invalid bit depth.");}return!0};e.prototype.validateNumChannels_=function(){var d=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(1>this.fmt.numChannels||65535this.fmt.sampleRate||4294967295parseInt(a,10)||"53">>0;return this.overflow_(this.sign_(a))};e.prototype.write=function(b,e,a){a=void 0===a?0:a;e=this.overflow_(e); +var g=255;a=this.writeFirstByte_(b,e,a);for(var n=2;n<=this.offset;n++)n==this.offset&&(g=this.lastByteMask_),b[a++]=Math.floor(e/Math.pow(2,8*(n-1)))&g;return a};e.prototype.readBits_=function(b,e){e=void 0===e?0:e;for(var a="",g=0;gthis.bits? +1:Math.ceil(this.realBits_/8)};e.prototype.sign_=function(b){b>this.max&&(b-=2*this.max+2);return b};e.prototype.overflow_=function(b){b>this.max?b=this.max:bthis.bits||64this.bits?b[a++]=0>e?e+Math.pow(2,this.bits):e:b[a++]=e&255;return a};b.exports=e},function(b,e){function g(a){return 32768>3;b>d&&(a|=4,b-=d,e+=d);d>>=1;b>d&&(a|=2,b-=d,e+=d);d>>=1;b>d&&(a|=1,e+=d);b=a;h=b&8?h-e:h+e;-32768>h?h=-32768:32767f?f=0:88>1);a&1&&(b+=d>>2);b+=d>>3;a&8&&(b=-b);l+=b;32767l&&(l=-32767);q+=c[a];0>q?q=0:88>8&255);c.push(f);c.push(0);for(b=3;bc.length;)c.push(0);return c}function n(b){l=g(b[1]<<8|b[0]);q=b[2];d=m[q];for(var c=[l,g(b[3]<<8|b[2])],e=4;e>4;c.push(a(p<<4^f));c.push(a(p))}return c}var c=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],m=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767],h=0,f=0,l=0,q=0,d=7;b.exports.encode= +function(a){for(var b=[],c=[],d=0;d>8&128)||(a*=-1);32635>8&127],a=b<<4|a>>b+3&15):a>>=4;return a^e^85}function k(a){var b=0;a^=85;a&128&&(a&=-129,b=-1);var e=((a&240)>>4)+4;a=4!=e?1<a){a=132-a;var b=127}else a+=132,b=255;32767>7;c&240&&(c>>=4,e+=4);c&12&&(c>>=2,e+=2);c&2&&(e+=1);c=e;return(c<<4|a>>c+3&15)^b}function k(a){a=~a;var b=((a&15)<<3)+132<<((a&112)>>4);return a&128?132-b:b-132}b.exports.encodeSample=g;b.exports.decodeSample=k;b.exports.encode=function(a){for(var b=[],e=0;e< +a.length;e++)b.push(g(a[e]));return b};b.exports.decode=function(a){for(var b=[],e=0;ek;k++)b["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(k)]=k;e.encode=function(a){a=new Uint8Array(a);var b,e=a.length,c="";for(b=0;b>2],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[b]& +3)<<4|a[b+1]>>4],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[b+1]&15)<<2|a[b+2]>>6],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[b+2]&63];2===e%3?c=c.substring(0,c.length-1)+"\x3d":1===e%3&&(c=c.substring(0,c.length-2)+"\x3d\x3d");return c};e.decode=function(a){var e=.75*a.length,g=a.length,c=0;"\x3d"===a[a.length-1]&&(e--,"\x3d"===a[a.length-2]&&e--);var k=new ArrayBuffer(e),h=new Uint8Array(k);for(e=0;e>4;h[c++]=(l&15)<<4|q>>2;h[c++]=(q&3)<<6|d&63}return k}})()}]); \ No newline at end of file diff --git a/docs/WaveFile.html b/docs/WaveFile.html index f7a3ce0..25c621d 100644 --- a/docs/WaveFile.html +++ b/docs/WaveFile.html @@ -22,7 +22,7 @@
@@ -69,7 +69,7 @@

new WaveFile<
Source:
@@ -213,7 +213,7 @@

(non-null) bextSource:
@@ -286,7 +286,7 @@

(non-null) bi
Source:
@@ -355,7 +355,7 @@

(non-null) c
Source:
@@ -424,7 +424,7 @@

(non-null) c
Source:
@@ -497,7 +497,7 @@

(non-null) cueSource:
@@ -570,7 +570,7 @@

dataSource:
@@ -643,7 +643,7 @@

(non-null) ds64Source:
@@ -716,7 +716,7 @@

(non-null) factSource:
@@ -789,7 +789,7 @@

(non-null) fmtSource:
@@ -862,7 +862,7 @@

(non-null) form
Source:
@@ -935,7 +935,7 @@

(non-null) Source:
@@ -1018,7 +1018,7 @@

deInterle
Source:
@@ -1102,7 +1102,7 @@

fromALawSource:
@@ -1243,7 +1243,7 @@

fromBuffer<
Source:
@@ -1489,7 +1489,7 @@

fromIMAAD
Source:
@@ -1630,7 +1630,7 @@

fromMuLawSource:
@@ -1771,7 +1771,7 @@

fromScratc
Source:
@@ -2002,7 +2002,7 @@

interleave<
Source:
@@ -2086,7 +2086,7 @@

realBitD
Source:
@@ -2237,7 +2237,7 @@

toALawSource:
@@ -2302,6 +2302,141 @@

toALawtoBase64() → {string}

+ + + + + + +
+ + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ Return a base64 string representig the WaveFile object as a wav file. +
+ + + + + + + + + + + + + + + + + + + + + +
Throws:
+ + + +
+
+
+ If any property of the object appears invalid. +
+
+
+
+
+
+ Type +
+
+ +Error + + +
+
+
+
+
+ + + + + +
Returns:
+ + +
+ A .wav file as a base64 string. +
+ + + +
+
+ Type +
+
+ +string + + +
+
+ + @@ -2321,7 +2456,7 @@

toBitDepth<
Source:
@@ -2518,7 +2653,7 @@

toBufferSource:
@@ -2641,6 +2776,141 @@
Returns:
+

toDataURI() → {string}

+ + + + + + +
+ + +
Source:
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +
+ Return a base64 string representig the WaveFile object as a wav file. The return value of this method can be used to load the audio in browsers. +
+ + + + + + + + + + + + + + + + + + + + + +
Throws:
+ + + +
+
+
+ If any property of the object appears invalid. +
+
+
+
+
+
+ Type +
+
+ +Error + + +
+
+
+
+
+ + + + + +
Returns:
+ + +
+ A .wav file as a DataURI. +
+ + + +
+
+ Type +
+
+ +string + + +
+
+ + + + + + + + +

toIMAADPCM()

@@ -2653,7 +2923,7 @@

toIMAADPCM<
Source:
@@ -2793,7 +3063,7 @@

toMuLawSource:
@@ -2877,7 +3147,7 @@

toRIFFSource:
@@ -2917,7 +3187,7 @@

toRIFF - Force a file as RIFF. Do not work with RF64 files. + Force a file as RIFF.

@@ -2961,7 +3231,7 @@

toRIFXSource:
@@ -3001,7 +3271,7 @@

toRIFX - Force a file as RIFX. Do not work with RF64 files. + Force a file as RIFX. @@ -3046,7 +3316,7 @@

toRIFX
- Documentation generated by JSDoc 3.5.5 on Sun May 06 2018 17:22:34 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Mon May 07 2018 00:36:13 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/example/index.html b/docs/example/index.html index fab2c84..3cf6297 100644 --- a/docs/example/index.html +++ b/docs/example/index.html @@ -2,125 +2,254 @@ + wavefile © 2018 Rafael da Silva Rocha. MIT License. - - + + +

wavefile

+

A demonstration of the use of wavefile and wavesurfer to create a player that supports 4-bit IMA ADPCM, mu-Law, A-Law, 11-bit wav files, RF64 files and more.

+ + +
+

Drop files here and click on play

+
+
-

wavefile

+
+ Convert to: +
+ + + + + + + + + + + +
+ +
-

A demonstration of the use of wavefile and wavesurfer to create a player that supports 4-bit IMA ADPCM, mu-Law, A-Law, 11-bit wav files, 64-bit wav files and more:

+ + +
+ - -
-

Drop files here then click on "play"

-
- + +
+
+ + +
+

File info:

+
    +
  • Container:
  • +
  • Format:
  • +
  • Bits per sample:
  • +
  • Valid bits per sample:
  • +
  • Audio format:
  • +
  • Sample rate:
  • +
  • Number of samples:
  • +
- + + + + + + - - -

Supported bit depths:

-
    -
  • 4-bit (encoded as IMA-ADPCM)
  • -
  • 8-bit
  • -
  • 16-bit
  • -
  • 24-bit
  • -
  • 32-bit integer
  • -
  • Anything between 8-bit and 32-bit (11-bit, 12-bit, etc)
  • -
  • 32-bit floating-point
  • -
  • 64-bit floating-point
  • -
- -

Supported containers:

-
    -
  • RIFF
  • -
  • RIFX
  • -
- -

Supported compression types:

-
    -
  • IMA ADPCM
  • -
  • mu-Law
  • -
  • A-Law
  • -
-

Copyright © 2017-2018 Rafael da Silva Rocha.
- https://github.com/rochars/wavefile

+ function changeContainer() { + var container = $('input[name=container]:checked').val(); + if (container == "RIFX" && wav.container != "RIFX") { + wav.toRIFX(); + loadFileInfo() + wav.toRIFF(); + } else if (container == "RIFF") { + wav.toRIFF(); + loadFileInfo() + } + wavesurfer.load(wav.toDataURI()); + } + + function loadFileInfo() { + $("#chunkId").text(wav.container); + $("#format").text(wav.format); + $("#bitsPerSample").text(wav.fmt.bitsPerSample); + $("#validBitsPerSample").text(wav.fmt.validBitsPerSample ? wav.fmt.validBitsPerSample : "Not applicable"); + $("#audioFormat").text(wav.fmt.audioFormat); + $("#sampleRate").text(wav.fmt.sampleRate); + $("#sampleCount").text(wav.data.samples.length); + } + // Setup the dnd listeners. + var dropZone = document.getElementById('drop-zone'); + dropZone.addEventListener('dragover', handleDragOver, false); + dropZone.addEventListener('drop', handleFileSelect, false); + diff --git a/docs/example/wavefile-min.js b/docs/example/wavefile-min.js index af43bed..802ad81 100644 --- a/docs/example/wavefile-min.js +++ b/docs/example/wavefile-min.js @@ -5,59 +5,61 @@ https://github.com/rochars/wavefile */ -var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,g){a!=Array.prototype&&a!=Object.prototype&&(a[b]=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,b,g,h){if(b){g=$jscomp.global;a=a.split(".");for(h=0;h=d.offset&&(e=l.toBytes_(a.slice(0,d.offset),d)):e=l.toBytes_([a],d);return e}function e(a,d,b){l.assureType_(d,void 0===b?10:b);a=l.fromBytes_(a.slice(0,d.offset),d);return a=d["char"]?a.slice(0,d.offset):a?a[0]:null}var l=g(5);a.exports.pack=h;a.exports.unpack=e;a.exports.packArray=function(a,d,b){l.assureType_(d,void 0===b?10:b);return l.toBytes_(a,d)};a.exports.unpackArray=function(a,d,b){l.assureType_(d, -void 0===b?10:b);return l.fromBytes_(a,d)};a.exports.unpackStruct=function(a,d,b){b=void 0===b?10:b;if(a.lengthparseInt(this.bitDepth,10))return!0;throw Error("Invalid bit depth.");}return!0};b.prototype.validateNumChannels_=function(){var c=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(1>this.fmt.numChannels||65535this.fmt.sampleRate||4294967295< -c)throw Error("Invalid sample rate.");return!0};b.prototype.clearHeader_=function(){this.fmt.cbSize=0;this.fmt.validBitsPerSample=0;this.fact.chunkId="";this.ds64.chunkId=""};b.prototype.assure16Bit_=function(){this.assureUncompressed_();"16"!=this.bitDepth&&this.toBitDepth("16")};b.prototype.assureUncompressed_=function(){"8a"==this.bitDepth?this.fromALaw():"8m"==this.bitDepth?this.fromMuLaw():"4"==this.bitDepth&&this.fromIMAADPCM()};b.prototype.assureInterleaved_=function(){this.isInterleaved|| -this.interleave()};b.prototype.LEorBE_=function(){var c="RIFX"===this.container;m.be=c;k.be=c;return f.be=c};b.prototype.findChunk_=function(c,d){for(var a=0;aparseInt(a,10)||"53">10;var b=a&1023;return(c?Math.pow(2,c-15)*(1+b/1024):b/1024*.00006103515625)*(a>>15?-1:1)}function l(a,c){x[0]=p.read(a,c);return y[0]}function n(a,c){w[0]=p.read(a,c);w[1]=p.read(a,c+4);return z[0]}function d(a,c){for(var b="",d=0;d>16&32768;var e=d>>12&2047;d=d>>23&255;103<=d&&(c=(c|d-112<<10|e>>1)+(e&1));a[b++]=c&255;a[b++]=c>>>8&255;return b}function f(a,c,b){y[0]=c;return b=p.write(a,x[0],b)}function c(a,c,b){z[0]=c;b=p.write(a,w[0],b);return p.write(a,w[1],b)}function q(a,c,b){a[b++]=c.charCodeAt(0);return b}var r=g(6),v=g(7);b=new Int8Array(4);var x=new Int32Array(b.buffer,0,1),y=new Float32Array(b.buffer,0,1);b=new Int8Array(8);var z=new Float64Array(b.buffer),w=new Uint32Array(b.buffer), -t,u,p;a.exports.toBytes_=function(a,c){for(var b=0,d=[],e=0;ea.bits?1:Math.ceil(a.bits/8);a.base=b;a["float"]?16==a.bits?t=e:32==a.bits?t=l:64==a.bits&&(t=n):t=a["char"]?d:h;a["float"]?16==a.bits?u=k:32==a.bits?u=f:64==a.bits&&(u=c):u=a["char"]?q:m;p=a["float"]?64==a.bits?new v(32,!1):32==a.bits?new v(32,!0):new v(16,!1):new v(a.bits,a.signed)}},function(a,b){a.exports=function(a,b){for(var e=a.length,g=0;g< -e;){for(var h=a,d=g,m=0,k=b-1,f=parseInt(b/2,10);m>>0;return this.overflow_(this.sign_(e))}; -b.prototype.write=function(a,b,e){e=void 0===e?0:e;b=this.overflow_(b);var g=255;e=this.writeFirstByte_(a,b,e);for(var h=2;h<=this.offset;h++)h==this.offset&&(g=this.lastByteMask_),a[e++]=Math.floor(b/Math.pow(2,8*(h-1)))&g;return e};b.prototype.readBits_=function(a,b){b=void 0===b?0:b;for(var e="",g=0;gthis.bits?1:Math.ceil(this.realBits_/8)};b.prototype.sign_=function(a){a>this.max&&(a-=2*this.max+2);return a};b.prototype.overflow_=function(a){a>this.max?a=this.max:athis.bits||64this.bits?a[e++]=0>b?b+Math.pow(2,this.bits):b:a[e++]=b&255;return e};a.exports=b},function(a,b){function g(a){return 32768>3;b>c&&(a|=4,b-=c,e+=c);c>>=1;b>c&&(a|=2,b-=c,e+=c);c>>=1;b>c&&(a|=1,e+=c);b=a;k=b& -8?k-e:k+e;-32768>k?k=-32768:32767f?f=0:88>1);a&1&&(b+=r>>2);b+=r>>3;a&8&&(b=-b);c+=b;32767c&&(c=-32767);q+=d[a];0>q?q=0:88>8&255);c.push(f);c.push(0);for(b=3;bc.length;)c.push(0);return c}function n(a){c=g(a[1]<<8|a[0]);q=a[2];r=m[q];for(var b= -[c,g(a[3]<<8|a[2])],d=4;d>4;b.push(e(h<<4^f));b.push(e(h))}return b}var d=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],m=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623, -27086,29794,32767],k=0,f=0,c=0,q=0,r=7;a.exports.encode=function(a){for(var b=[],c=[],d=0;d>8&128)||(a*=-1);32635>8&127],a=b<<4|a>>b+3&15):a>>=4;return a^e^85}function h(a){var b=0;a^=85;a&128&&(a&=-129,b=-1);var e=((a&240)>>4)+4;a=4!=e?1<a){a=132-a;var b=127}else a+=132,b=255;32767>7;d&240&&(d>>=4,e+=4);d&12&&(d>>=2,e+=2);d&2&&(e+=1);d=e;return(d<<4|a>>d+3&15)^b}function h(a){a=~a;var b=((a&15)<<3)+132<<((a&112)>>4);return a&128?132-b:b-132}a.exports.encodeSample=g;a.exports.decodeSample= -h;a.exports.encode=function(a){for(var b=[],e=0;e>10;var f=d&1023;return(c?Math.pow(2,c-15)*(1+f/1024): +f/1024*.00006103515625)*(d>>15?-1:1)}function c(d,c){t[0]=r.read(d,c);return y[0]}function m(d,c){t[0]=r.read(d,c);t[1]=r.read(d,c+4);return A[0]}function h(d,c){for(var f="",a=0;a>16&32768;var b=a>>12&2047;a=a>>23&255;103<=a&&(c=(c|a-112<<10|b>>1)+(b&1));d[f++]=c&255;d[f++]=c>>>8&255;return f}function q(d,c,f){y[0]=c;return r.write(d,t[0],f)}function d(d,c,f){A[0]= +c;f=r.write(d,t[0],f);return r.write(d,t[1],f)}function w(d,c,f){for(var a=0;aa.bits||a.bits%2)throw Error("Wrong offset for type char.");}else if(1>a.bits||53a.bits?1:Math.ceil(a.bits/8);a["float"]?16==a.bits?u=n:32==a.bits?u=c:64== +a.bits&&(u=m):u=a["char"]?h:p;a["float"]?16==a.bits?v=l:32==a.bits?v=q:64==a.bits&&(v=d):v=a["char"]?w:f;a["char"]?r.offset=8>a.bits?1:Math.ceil(a.bits/8):r=new C(64==a.bits?32:a.bits,a["float"]?!1:a.signed)}function B(d,a){d.constructor==String&&(d=d.length>=a.offset?d.slice(0,a.offset):"");return d}var z=g(5),C=g(6);e=new Int8Array(8);var t=new Uint32Array(e.buffer),y=new Float32Array(e.buffer),A=new Float64Array(e.buffer),u,v,r={};b.exports.chr={bits:8,"char":!0};b.exports.fourCC={bits:32,"char":!0}; +b.exports.bool={bits:1};b.exports.int2={bits:2,signed:!0};b.exports.uInt2={bits:2};b.exports.int4={bits:4,signed:!0};b.exports.uInt4={bits:4};b.exports.int8={bits:8,signed:!0};b.exports.uInt8={bits:8};b.exports.int16={bits:16,signed:!0};b.exports.uInt16={bits:16};b.exports.float16={bits:16,"float":!0};b.exports.int24={bits:24,signed:!0};b.exports.uInt24={bits:24};b.exports.int32={bits:32,signed:!0};b.exports.uInt32={bits:32};b.exports.float32={bits:32,"float":!0};b.exports.int40={bits:40,signed:!0}; +b.exports.uInt40={bits:40};b.exports.int48={bits:48,signed:!0};b.exports.uInt48={bits:48};b.exports.float64={bits:64,"float":!0};b.exports.int16BE={bits:16,signed:!0,be:!0};b.exports.uInt16BE={bits:16,be:!0};b.exports.float16BE={bits:16,"float":!0,be:!0};b.exports.int24BE={bits:24,signed:!0,be:!0};b.exports.uInt24BE={bits:24,be:!0};b.exports.int32BE={bits:32,signed:!0,be:!0};b.exports.uInt32BE={bits:32,be:!0};b.exports.float32BE={bits:32,"float":!0,be:!0};b.exports.int40BE={bits:40,signed:!0,be:!0}; +b.exports.uInt40BE={bits:40,be:!0};b.exports.int48BE={bits:48,signed:!0,be:!0};b.exports.uInt48BE={bits:48,be:!0};b.exports.float64BE={bits:64,"float":!0,be:!0};b.exports.pack=function(d,c,f){f=void 0===f?10:f;x(c);var b=[];if(void 0===d)return b;d=B(d,c);return a([d],c,f)};b.exports.unpack=function(d,a,c){c=void 0===c?10:c;x(a);return(d=k(d.slice(0,a.offset),a,c))?d[0]:a["char"]?"":null};b.exports.packArray=function(d,c,f){f=void 0===f?10:f;x(c);if(c["char"])for(var b=d.length,e=0;eparseInt(this.bitDepth,10))return!0;throw Error("Invalid bit depth.");}return!0};e.prototype.validateNumChannels_=function(){var d=this.fmt.numChannels*this.fmt.bitsPerSample/8;if(1>this.fmt.numChannels||65535this.fmt.sampleRate||4294967295parseInt(a,10)||"53">>0;return this.overflow_(this.sign_(a))};e.prototype.write=function(b,e,a){a=void 0===a?0:a;e=this.overflow_(e); +var g=255;a=this.writeFirstByte_(b,e,a);for(var n=2;n<=this.offset;n++)n==this.offset&&(g=this.lastByteMask_),b[a++]=Math.floor(e/Math.pow(2,8*(n-1)))&g;return a};e.prototype.readBits_=function(b,e){e=void 0===e?0:e;for(var a="",g=0;gthis.bits? +1:Math.ceil(this.realBits_/8)};e.prototype.sign_=function(b){b>this.max&&(b-=2*this.max+2);return b};e.prototype.overflow_=function(b){b>this.max?b=this.max:bthis.bits||64this.bits?b[a++]=0>e?e+Math.pow(2,this.bits):e:b[a++]=e&255;return a};b.exports=e},function(b,e){function g(a){return 32768>3;b>d&&(a|=4,b-=d,e+=d);d>>=1;b>d&&(a|=2,b-=d,e+=d);d>>=1;b>d&&(a|=1,e+=d);b=a;h=b&8?h-e:h+e;-32768>h?h=-32768:32767f?f=0:88>1);a&1&&(b+=d>>2);b+=d>>3;a&8&&(b=-b);l+=b;32767l&&(l=-32767);q+=c[a];0>q?q=0:88>8&255);c.push(f);c.push(0);for(b=3;bc.length;)c.push(0);return c}function n(b){l=g(b[1]<<8|b[0]);q=b[2];d=m[q];for(var c=[l,g(b[3]<<8|b[2])],e=4;e>4;c.push(a(p<<4^f));c.push(a(p))}return c}var c=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],m=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086,29794,32767],h=0,f=0,l=0,q=0,d=7;b.exports.encode= +function(a){for(var b=[],c=[],d=0;d>8&128)||(a*=-1);32635>8&127],a=b<<4|a>>b+3&15):a>>=4;return a^e^85}function k(a){var b=0;a^=85;a&128&&(a&=-129,b=-1);var e=((a&240)>>4)+4;a=4!=e?1<a){a=132-a;var b=127}else a+=132,b=255;32767>7;c&240&&(c>>=4,e+=4);c&12&&(c>>=2,e+=2);c&2&&(e+=1);c=e;return(c<<4|a>>c+3&15)^b}function k(a){a=~a;var b=((a&15)<<3)+132<<((a&112)>>4);return a&128?132-b:b-132}b.exports.encodeSample=g;b.exports.decodeSample=k;b.exports.encode=function(a){for(var b=[],e=0;e< +a.length;e++)b.push(g(a[e]));return b};b.exports.decode=function(a){for(var b=[],e=0;ek;k++)b["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charCodeAt(k)]=k;e.encode=function(a){a=new Uint8Array(a);var b,e=a.length,c="";for(b=0;b>2],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[b]& +3)<<4|a[b+1]>>4],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(a[b+1]&15)<<2|a[b+2]>>6],c+="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[a[b+2]&63];2===e%3?c=c.substring(0,c.length-1)+"\x3d":1===e%3&&(c=c.substring(0,c.length-2)+"\x3d\x3d");return c};e.decode=function(a){var e=.75*a.length,g=a.length,c=0;"\x3d"===a[a.length-1]&&(e--,"\x3d"===a[a.length-2]&&e--);var k=new ArrayBuffer(e),h=new Uint8Array(k);for(e=0;e>4;h[c++]=(l&15)<<4|q>>2;h[c++]=(q&3)<<6|d&63}return k}})()}]); \ No newline at end of file diff --git a/docs/index.html b/docs/index.html index 034eb9c..b732ba7 100644 --- a/docs/index.html +++ b/docs/index.html @@ -22,7 +22,7 @@
@@ -92,14 +92,29 @@

Creating wave files in the browser:

https://tr2099.github.io/

Hit "Load in player" to generate wave files.

This website uses wavefile to create the files. The effects are provided by other libraries.

Some bit depths may not be supported by your browser, like 32-bit floating point or 64-bit floating point (WaveFile is used just to create the files, not to play them).

-

Use

let fs = require("fs");
-let Wavefile = require("wavefile");
+

Use

const fs = require("fs");
+const Wavefile = require("wavefile");
 
+// Load a wav file from disk into a WaveFile object
 let wav = new Wavefile(fs.readFileSync("file.wav"));
+
+// Check some of the file properties
 console.log(wav.container);
 console.log(wav.chunkSize);
 console.log(wav.fmt.chunkId);
-fs.writeFileSync(path, wav.toBuffer());

Create wave files from scratch

You must inform the number of channels, the sample rate, the bit depth and the samples (in this order). The samples should be represented as an array of numbers. The array may be multidimensional if there is more than one channel.

+ +// Call toBuffer() to get the bytes of the file. +// You can write the output straight to disk: +fs.writeFileSync(path, wav.toBuffer()); + +// Call toDataURI() to get the file as a base64-encoded +// DataURI to load the file a web browser: +wavDataURI = wav.toDataURI();

Main methods:

WaveFile.fromBuffer()

Load a .wav file from a byte buffer into a WaveFile object:

+
let wav = new Wavefile(buffer);

WaveFile.fromScratch()

Create a WaveFile object with the arguments you pass:

+
// A mono, 44.1 kHz, 32-bit .wav file with just 4 samples:
+wav.fromScratch(1, 44100, '32', [0, -2147483648, 2147483647, 4]);

WaveFile.toBuffer()

Return a Uint8Array with the WaveFile object data. The buffer is a .wav file and can be written to disk:

+
buffer = wav.toBuffer();

WaveFile.toDataURI()

Return a DataURI string with the WaveFile object data. The DataURI is a .wav file and can be played in browsers:

+
wavDataURI = wav.toDataURI();

Create wave files from scratch

You must inform the number of channels, the sample rate, the bit depth and the samples (in this order). The samples should be represented as an array of numbers. The array may be multidimensional if there is more than one channel.

Mono:

let wav = new WaveFile();
 
 // Create a mono wave file, 44.1 kHz, 32-bit and 4 samples
@@ -327,7 +342,7 @@ 

LICENSE

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


- Documentation generated by JSDoc 3.5.5 on Sun May 06 2018 17:22:34 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Mon May 07 2018 00:36:13 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/docs/index.js.html b/docs/index.js.html index 45defcd..ddac29c 100644 --- a/docs/index.js.html +++ b/docs/index.js.html @@ -22,7 +22,7 @@
@@ -56,6 +56,8 @@

index.js

/** @private */ const byteData_ = require("byte-data"); /** @private */ +const encodeBase64 = require("base64-arraybuffer").encode; +/** @private */ const uInt8_ = {"bits": 8}; /** @private */ const uInt16_ = {"bits": 16}; @@ -403,7 +405,26 @@

index.js

} /** - * Force a file as RIFF. Do not work with RF64 files. + * Return a base64 string representig the WaveFile object as a wav file. + * @return {string} A .wav file as a base64 string. + * @throws {Error} If any property of the object appears invalid. + */ + toBase64() { + return encodeBase64(this.toBuffer()); + } + + /** + * Return a base64 string representig the WaveFile object as a wav file. + * The return value of this method can be used to load the audio in browsers. + * @return {string} A .wav file as a DataURI. + * @throws {Error} If any property of the object appears invalid. + */ + toDataURI() { + return "data:audio/wav;base64," + this.toBase64(); + } + + /** + * Force a file as RIFF. */ toRIFF() { if (this.container == "RF64") { @@ -419,7 +440,7 @@

index.js

} /** - * Force a file as RIFX. Do not work with RF64 files. + * Force a file as RIFX. */ toRIFX() { if (this.container == "RF64") { @@ -1285,7 +1306,7 @@

index.js


- Documentation generated by JSDoc 3.5.5 on Sun May 06 2018 17:22:34 GMT-0300 (Hora oficial do Brasil) using the docdash theme. + Documentation generated by JSDoc 3.5.5 on Mon May 07 2018 00:36:13 GMT-0300 (Hora oficial do Brasil) using the docdash theme.
diff --git a/index.js b/index.js index 4268dab..451f6a5 100644 --- a/index.js +++ b/index.js @@ -17,6 +17,8 @@ const alawmulaw_ = require("alawmulaw"); /** @private */ const byteData_ = require("byte-data"); /** @private */ +const encodeBase64 = require("base64-arraybuffer").encode; +/** @private */ const uInt8_ = {"bits": 8}; /** @private */ const uInt16_ = {"bits": 16}; @@ -364,7 +366,26 @@ class WaveFile { } /** - * Force a file as RIFF. Do not work with RF64 files. + * Return a base64 string representig the WaveFile object as a wav file. + * @return {string} A .wav file as a base64 string. + * @throws {Error} If any property of the object appears invalid. + */ + toBase64() { + return encodeBase64(this.toBuffer()); + } + + /** + * Return a base64 string representig the WaveFile object as a wav file. + * The return value of this method can be used to load the audio in browsers. + * @return {string} A .wav file as a DataURI. + * @throws {Error} If any property of the object appears invalid. + */ + toDataURI() { + return "data:audio/wav;base64," + this.toBase64(); + } + + /** + * Force a file as RIFF. */ toRIFF() { if (this.container == "RF64") { @@ -380,7 +401,7 @@ class WaveFile { } /** - * Force a file as RIFX. Do not work with RF64 files. + * Force a file as RIFX. */ toRIFX() { if (this.container == "RF64") { diff --git a/package.json b/package.json index 5bc95c9..cb5dbdf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wavefile", - "version": "6.3.0", + "version": "6.4.0", "description": "Read & write wave files with 4, 8, 11, 12, 16, 20, 24, 32 & 64-bit data.", "homepage": "https://github.com/rochars/wavefile", "author": "Rafael da Silva Rocha ", @@ -70,6 +70,7 @@ }, "dependencies": { "alawmulaw": "^2.0.4", + "base64-arraybuffer": "^0.1.5", "bitdepth": "^3.1.1", "byte-data": "^8.0.0", "imaadpcm": "^1.1.4", diff --git a/test/write/toBase64.js b/test/write/toBase64.js new file mode 100644 index 0000000..68b0ae9 --- /dev/null +++ b/test/write/toBase64.js @@ -0,0 +1,23 @@ +/*! + * Copyright (c) 2017 Rafael da Silva Rocha. + * + */ + +var assert = require('assert'); + +describe('read 16bit file from disk and write to new file', function() { + + const fs = require("fs"); + const WaveFile = require("../../test/loader.js"); + const path = "test/files/"; + let wav = new WaveFile(fs.readFileSync(path + "16-bit-8kHz-noBext-mono.wav")); + + it("toBase64 should return a string", + function() { + assert.ok(wav.toBase64()); + }); + it("toDataURI should return a string", + function() { + assert.ok(wav.toDataURI()); + }); +});