Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----

Expand Down
19 changes: 12 additions & 7 deletions wav.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ NOTE: Does not auto-correct:
@author David Lindkvist
@twitter ffdead

@author Thatcher Chamberlin
Added code for the getSamples function

*/


Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down