Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
pcprince committed Oct 16, 2018
2 parents 20cafea + dcdc67d commit bbfcad0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ reader.onload = function() {
var arrayBuffer = reader.result;
wavSpectro.drawSpectrogram(arrayBuffer, canvasElem, 'jet');
wavSpectro.drawSpectrogram({arrayBuffer: arrayBuffer, canvasElem: canvasElem, cmap: 'jet'}, function () {
console.log("Done.");
});
};
Expand All @@ -25,7 +29,7 @@ reader.readAsArrayBuffer(fileInput.files[0]);

### Functions ###

The draw function can be run with the following parameters.
The draw function must be handed parameters in an object, named as such. Parameters with default values can be left out.

Option | Default | Description
---------------|-------------|------------
Expand All @@ -35,3 +39,5 @@ Option | Default | Description
`nfft` | 512 | Buffer size of Fast Fourier Transform
`frameLengthMs`| 0.1 | Length of frames signal is divided into before FFT is applied (given in milliseconds)
`frameStepMs` | 0.005 | Size of steps forward each frame takes (if less than `frameLengthMs` then frames overlap)

As well as the params object, the draw function accepts a callback function.
13 changes: 11 additions & 2 deletions wav-spectrogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,16 @@ function medianFilter(array) {

}

function drawSpectrogram(arrayBuffer, canvasElem, cmap, nfft = 512, frameLengthMs = 0.1, frameStepMs = 0.005) {
function drawSpectrogram(params, callback) {

var err, sampleRate, samples, sampleArray, frameLength, frameStep, numFrames, paddedArrayLength, frames, i, maxValue, minValue, spectrumFrames, spectrum, m, n, a, o, p, ctx, specWidth, specHeight, colours;
var arrayBuffer, canvasElem, cmap, nfft, frameLengthMs, frameStepMs, err, sampleRate, samples, sampleArray, frameLength, frameStep, numFrames, paddedArrayLength, frames, i, maxValue, minValue, spectrumFrames, spectrum, m, n, a, o, p, ctx, specWidth, specHeight, colours;

arrayBuffer = params.arrayBuffer;
canvasElem = params.canvasElem;
cmap = params.cmap;
nfft = params.nfft || 512;
frameLengthMs = params.frameLengthMs || 0.1;
frameStepMs = params.frameStepMs || 0.005;

decode(arrayBuffer, (err, audioBuffer) => {

Expand Down Expand Up @@ -174,6 +181,8 @@ function drawSpectrogram(arrayBuffer, canvasElem, cmap, nfft = 512, frameLengthM

}

typeof callback === 'function' && callback();

});

}
Expand Down

0 comments on commit bbfcad0

Please sign in to comment.