wavencoder v1.0.0
wavencoder
This module will take sampled data and encode it as a wav file. This is based on https://github.com/mattdiamond/Recorderjs/
This module will use webworkers if they are available with no extra setup.
Usage
The following example uses wavencoder
and two other modules to record 3 seconds of audio from a users mic encode the recorded data as a wav file and allow the user to download it. (It should be noted that in Chrome you must run this code on https as getUserMedia
is only available on https)
var recordmic = require('recordmic');
var browsersavefile = require( 'browsersavefile' );
var wavencoder = require('wavencoder')();
if(recordmic.isAvailable) {
var recorder = recordmic({ onSampleData: function( left, right ) {
// console.log( left );
}}, function( error ) {
console.log('we\'re good', error);
if(!error) {
console.log('start recording');
recorder.start();
setTimeout(function() {
console.log('stop recording');
recorder.stop();
wavencoder.data(recorder.getStereoData());
wavencoder.export(function(error, data) {
browsersavefile('output.wav', data);
});
}, 3000);
}
});
} else {
throw new Error( 'not avaiable' );
}
API
var wavencoder = require('wavencoder')([ opts ])
To construct a wavencoder
just require and call a function.
You can pass optional options when insntantiating the encoder.
sampleRate
: This is the sameple rate of the recorded audio. Default:44100
channels
: How many channels of audio the recorded audio has. Default:2
noWorker
: If you'd like to force not using a web worker pass intrue
fornoWorker
wavencoder.data(interleavedData)
The data function should be passed interleaved audio data that will be exported.
wavencoder.export(cb)
The export function should be called when you'd like to encode your wav file based on the interleaved data passed to the data
function.
This function takes on parameter which is a callback function. This callback will passback an error if an error occurs and the encoded audio as the second parameter. The encoded data will be a blob.
License
MIT, see LICENSE.md for details.