diff --git a/README.md b/README.md index 9c06aea..478dfba 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,20 @@ 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 () { + //Load all samples + wavFile.getSamples(); + //Print out all the samples + console.log(wavFile.dataSamples); + }; + + Example ----- diff --git a/wav.js b/wav.js index fc8b7f5..d1dc3f1 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; @@ -185,16 +190,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