Skip to content

Commit

Permalink
[added] npm and browserify support
Browse files Browse the repository at this point in the history
  • Loading branch information
jergason committed Sep 7, 2014
1 parent 3490475 commit 927af5d
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 84 deletions.
35 changes: 24 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# Recorder.js

## A plugin for recording/exporting the output of Web Audio API nodes
## A module for recording/exporting the output of Web Audio API nodes

##Installation

Install with npm, consume with [browserify](https://github.com/substack/node-browserify).

It uses [workerify](https://github.com/shama/workerify) to inline the WebWorker
so things remain modular.


```bash
npm install recorderjs
```


### Syntax
#### Constructor
Expand Down Expand Up @@ -41,16 +54,16 @@ In addition, you may specify the type of Blob to be returned (defaults to 'audio

This will pass the recorded stereo buffer (as an array of two Float32Arrays, for the separate left and right channels) to the callback. It can be played back by creating a new source buffer and setting these buffers as the separate channel data:

function getBufferCallback( buffers ) {
var newSource = audioContext.createBufferSource();
var newBuffer = audioContext.createBuffer( 2, buffers[0].length, audioContext.sampleRate );
newBuffer.getChannelData(0).set(buffers[0]);
newBuffer.getChannelData(1).set(buffers[1]);
newSource.buffer = newBuffer;
function getBufferCallback( buffers ) {
var newSource = audioContext.createBufferSource();
var newBuffer = audioContext.createBuffer( 2, buffers[0].length, audioContext.sampleRate );
newBuffer.getChannelData(0).set(buffers[0]);
newBuffer.getChannelData(1).set(buffers[1]);
newSource.buffer = newBuffer;

newSource.connect( audioContext.destination );
newSource.start(0);
}
newSource.connect( audioContext.destination );
newSource.start(0);
}

This sample code will play back the stereo buffer.

Expand All @@ -73,4 +86,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "recorderjs",
"version": "0.0.0",
"description": "Record audio from a node in the Web Audio API",
"main": "recorder.js",
"scripts": {
"test": "echo \"Everything is fine\""
},
"browserify": {
"transform": "workerify"
},
"repository": {
"type": "git",
"url": "https://github.com/jergason/Recorderjs.git"
},
"keywords": [
"web-audio-api",
"record",
"browserify"
],
"author": "Jamison Dance <jergason@gmail.com> (http://jamisondance.com)",
"license": "MIT",
"bugs": {
"url": "https://github.com/jergason/Recorderjs/issues"
},
"homepage": "https://github.com/jergason/Recorderjs",
"devDependencies": {
"browserify": "^5.11.1",
"workerify": "^0.3.0"
}
}
142 changes: 69 additions & 73 deletions recorder.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,85 @@
(function(window){
var WORKER_PATH = './recorderWorker.js';

var WORKER_PATH = 'recorderWorker.js';
var Recorder = function(source, cfg){
var config = cfg || {};
var bufferLen = config.bufferLen || 4096;
this.context = source.context;
this.node = (this.context.createScriptProcessor ||
this.context.createJavaScriptNode).call(this.context,
bufferLen, 2, 2);
var worker = new Worker(config.workerPath || WORKER_PATH);
worker.postMessage({
command: 'init',
config: {
sampleRate: this.context.sampleRate
}
});
var recording = false,
currCallback;

var Recorder = function(source, cfg){
var config = cfg || {};
var bufferLen = config.bufferLen || 4096;
this.context = source.context;
this.node = (this.context.createScriptProcessor ||
this.context.createJavaScriptNode).call(this.context,
bufferLen, 2, 2);
var worker = new Worker(config.workerPath || WORKER_PATH);
this.node.onaudioprocess = function(e){
if (!recording) return;
worker.postMessage({
command: 'init',
config: {
sampleRate: this.context.sampleRate
}
command: 'record',
buffer: [
e.inputBuffer.getChannelData(0),
e.inputBuffer.getChannelData(1)
]
});
var recording = false,
currCallback;

this.node.onaudioprocess = function(e){
if (!recording) return;
worker.postMessage({
command: 'record',
buffer: [
e.inputBuffer.getChannelData(0),
e.inputBuffer.getChannelData(1)
]
});
}
}

this.configure = function(cfg){
for (var prop in cfg){
if (cfg.hasOwnProperty(prop)){
config[prop] = cfg[prop];
}
this.configure = function(cfg){
for (var prop in cfg){
if (cfg.hasOwnProperty(prop)){
config[prop] = cfg[prop];
}
}
}

this.record = function(){
recording = true;
}

this.stop = function(){
recording = false;
}

this.clear = function(){
worker.postMessage({ command: 'clear' });
}
this.record = function(){
recording = true;
}

this.getBuffer = function(cb) {
currCallback = cb || config.callback;
worker.postMessage({ command: 'getBuffer' })
}
this.stop = function(){
recording = false;
}

this.exportWAV = function(cb, type){
currCallback = cb || config.callback;
type = type || config.type || 'audio/wav';
if (!currCallback) throw new Error('Callback not set');
worker.postMessage({
command: 'exportWAV',
type: type
});
}
this.clear = function(){
worker.postMessage({ command: 'clear' });
}

worker.onmessage = function(e){
var blob = e.data;
currCallback(blob);
}
this.getBuffer = function(cb) {
currCallback = cb || config.callback;
worker.postMessage({ command: 'getBuffer' })
}

source.connect(this.node);
this.node.connect(this.context.destination); //this should not be necessary
};
this.exportWAV = function(cb, type){
currCallback = cb || config.callback;
type = type || config.type || 'audio/wav';
if (!currCallback) throw new Error('Callback not set');
worker.postMessage({
command: 'exportWAV',
type: type
});
}

Recorder.forceDownload = function(blob, filename){
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var link = window.document.createElement('a');
link.href = url;
link.download = filename || 'output.wav';
var click = document.createEvent("Event");
click.initEvent("click", true, true);
link.dispatchEvent(click);
worker.onmessage = function(e){
var blob = e.data;
currCallback(blob);
}

window.Recorder = Recorder;
source.connect(this.node);
this.node.connect(this.context.destination); //this should not be necessary
};

Recorder.forceDownload = function(blob, filename){
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var link = window.document.createElement('a');
link.href = url;
link.download = filename || 'output.wav';
var click = document.createEvent("Event");
click.initEvent("click", true, true);
link.dispatchEvent(click);
}

})(window);
module.exports = Recorder;

0 comments on commit 927af5d

Please sign in to comment.