forked from jergason/Recorderjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |