From 809ebffa20c03dbd9a2aa8bd98342a0121e1afa0 Mon Sep 17 00:00:00 2001 From: ThatcherC Date: Wed, 22 Jul 2015 17:27:59 -0400 Subject: [PATCH 1/3] Added code for the getSamples function --- wav.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/wav.js b/wav.js index fc8b7f5..cba1dde 100644 --- a/wav.js +++ b/wav.js @@ -15,6 +15,9 @@ NOTE: Does not auto-correct: @author David Lindkvist @twitter ffdead +@author Thatcher Chamberlin +Added code for the getSamples function + */ @@ -39,6 +42,8 @@ function wav(file) { this.file = file instanceof Blob ? file : undefined; this.buffer = file instanceof ArrayBuffer ? file : undefined;; + this.dataBuffer = file instanceof ArrayBuffer ? file : undefined;; + // format this.chunkID = undefined; // must be RIFF this.chunkSize = undefined; // size of file after this field @@ -75,7 +80,7 @@ wav.prototype.peek = function () { // only load the first 44 bytes of the header var headerBlob = this.sliceFile(0, 44); - reader.readAsArrayBuffer(headerBlob); + reader.readAsArrayBuffer(this.file); reader.onloadend = function() { that.buffer = this.result; @@ -87,6 +92,7 @@ wav.prototype.parseArrayBuffer = function () { try { this.parseHeader(); this.parseData(); + this.getSamples(); this.readyState = this.DONE; } catch (e) { @@ -185,16 +191,16 @@ wav.prototype.slice = function (start, length, callback) { /* * do we need direct access to samples? - * + * Yes, definitely + * Samples can be accessed from the dataSamples variable + */ wav.prototype.getSamples = function () { - - // TODO load data chunk into buffer + if (this.bitsPerSample === 8) - this.dataSamples = new Uint8Array(this.buffer, 44, chunkSize/this.blockAlign); + this.dataSamples = new Uint8Array(this.buffer, 44, this.dataLength/this.blockAlign); else if (this.bitsPerSample === 16) - this.dataSamples = new Int16Array(this.buffer, 44, chunkSize/this.blockAlign); + this.dataSamples = new Int16Array(this.buffer, 44, this.dataLength/this.blockAlign); } -*/ /** * Reads slice from buffer as String From 2c2acaed27bb08b53cf24810a90c4f755682b721 Mon Sep 17 00:00:00 2001 From: ThatcherC Date: Wed, 22 Jul 2015 17:32:53 -0400 Subject: [PATCH 2/3] Added info about how to get samples from a file --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 9c06aea..706736b 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,18 @@ Slice file into a smaller wav chunk: The success callback is passed the resulting slice as an ArrayBuffer - this buffer represents a new WAVE file of the slice (with WAVE headers). +Read samples from a wav file: + + var fileInput = document.getElementById('fileInput'); + var file = fileInput.files[0]; + var wavFile = new wav(file); + + wavFile.onloadend = function () { + //Print out all the samples + console.log(wavFile.dataSamples); + }; + + Example ----- From c409631906895fd4629cc17cba470cd4d637d715 Mon Sep 17 00:00:00 2001 From: ThatcherC Date: Wed, 22 Jul 2015 17:35:24 -0400 Subject: [PATCH 3/3] Changed sample-loading to be optional --- README.md | 2 ++ wav.js | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 706736b..478dfba 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,8 @@ Read samples from a wav file: var wavFile = new wav(file); wavFile.onloadend = function () { + //Load all samples + wavFile.getSamples(); //Print out all the samples console.log(wavFile.dataSamples); }; diff --git a/wav.js b/wav.js index cba1dde..d1dc3f1 100644 --- a/wav.js +++ b/wav.js @@ -92,7 +92,6 @@ wav.prototype.parseArrayBuffer = function () { try { this.parseHeader(); this.parseData(); - this.getSamples(); this.readyState = this.DONE; } catch (e) {