├── .gitignore ├── CHANGELOG.md ├── README.md ├── example_simple_exportwav.html ├── package.json ├── recorder.js └── recorderWorker.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | v1.0.1 - Tue, 09 Sep 2014 15:13:20 GMT 2 | -------------------------------------- 3 | 4 | - [6af7364](../../commit/6af7364) [fixed] use self instead of this for worker to work with workerify 5 | - [b1fe90c](../../commit/b1fe90c) [fixed] hard-code path to worker so it gets picked up by workerify 6 | - [1337c9d](../../commit/1337c9d) [fixed] make browserify and workerify peer deps 7 | 8 | 9 | v1.0.0 - Sun, 07 Sep 2014 14:25:06 GMT 10 | -------------------------------------- 11 | 12 | - [927af5d](../../commit/927af5d) [added] npm and browserify support 13 | - [0580cdc](../../commit/0580cdc) [fixed] use correct RIFF chunk size 14 | 15 | 16 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Recorder.js 2 | 3 | ## A module for recording/exporting the output of Web Audio API nodes 4 | 5 | ##Installation 6 | 7 | Install with npm, consume with [browserify](https://github.com/substack/node-browserify). 8 | 9 | It uses [workerify](https://github.com/shama/workerify) to inline the WebWorker 10 | so things remain modular. 11 | 12 | 13 | ```bash 14 | npm install recorderjs 15 | ``` 16 | 17 | 18 | ### Syntax 19 | #### Constructor 20 | var rec = new Recorder(source [, config]) 21 | 22 | Creates a recorder instance. 23 | 24 | - **source** - The node whose output you wish to capture 25 | - **config** - (*optional*) A configuration object (see **config** section below) 26 | 27 | --------- 28 | #### Config 29 | 30 | - **workerPath** - Path to recorder.js worker script. Defaults to 'js/recorderjs/recorderWorker.js' 31 | - **bufferLen** - The length of the buffer that the internal JavaScriptNode uses to capture the audio. Can be tweaked if experiencing performance issues. Defaults to 4096. 32 | - **callback** - A default callback to be used with `exportWAV`. 33 | - **type** - The type of the Blob generated by `exportWAV`. Defaults to 'audio/wav'. 34 | 35 | --------- 36 | #### Instance Methods 37 | 38 | rec.record() 39 | rec.stop() 40 | 41 | Pretty self-explanatory... **record** will begin capturing audio and **stop** will cease capturing audio. Subsequent calls to **record** will add to the current recording. 42 | 43 | rec.clear() 44 | 45 | This will clear the recording. 46 | 47 | rec.exportWAV([callback][, type]) 48 | 49 | This will generate a Blob object containing the recording in WAV format. The callback will be called with the Blob as its sole argument. If a callback is not specified, the default callback (as defined in the config) will be used. If no default has been set, an error will be thrown. 50 | 51 | In addition, you may specify the type of Blob to be returned (defaults to 'audio/wav'). 52 | 53 | rec.getBuffer([callback]) 54 | 55 | 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: 56 | 57 | function getBufferCallback( buffers ) { 58 | var newSource = audioContext.createBufferSource(); 59 | var newBuffer = audioContext.createBuffer( 2, buffers[0].length, audioContext.sampleRate ); 60 | newBuffer.getChannelData(0).set(buffers[0]); 61 | newBuffer.getChannelData(1).set(buffers[1]); 62 | newSource.buffer = newBuffer; 63 | 64 | newSource.connect( audioContext.destination ); 65 | newSource.start(0); 66 | } 67 | 68 | This sample code will play back the stereo buffer. 69 | 70 | 71 | rec.configure(config) 72 | 73 | This will set the configuration for Recorder by passing in a config object. 74 | 75 | #### Utility Methods (static) 76 | 77 | Recorder.forceDownload(blob[, filename]) 78 | 79 | This method will force a download using the new anchor link *download* attribute. Filename defaults to 'output.wav'. 80 | 81 | ## License (MIT) 82 | 83 | Copyright © 2013 Matt Diamond 84 | 85 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 86 | 87 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 88 | 89 | 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. 90 | -------------------------------------------------------------------------------- /example_simple_exportwav.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Live input record and playback 7 | 11 | 12 | 13 | 14 |

Recorder.js simple WAV export example

15 | 16 |

Make sure you are using a recent version of Google Chrome, at the moment this only works with Google Chrome Canary.

17 |

Also before you enable microphone input either plug in headphones or turn the volume down if you want to avoid ear splitting feedback!

18 | 19 | 20 | 21 | 22 |

Recordings

23 | 24 | 25 |

Log

26 |

 27 | 
 28 |   
103 | 
104 |   
105 | 
106 | 
107 | 


--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
 1 | {
 2 |   "name": "recorderjs",
 3 |   "version": "1.0.1",
 4 |   "description": "Record audio from a node in the Web Audio API",
 5 |   "main": "recorder.js",
 6 |   "scripts": {
 7 |     "test": "echo \"Everything is fine\""
 8 |   },
 9 |   "browserify": {
10 |     "transform": "workerify"
11 |   },
12 |   "repository": {
13 |     "type": "git",
14 |     "url": "https://github.com/jergason/Recorderjs.git"
15 |   },
16 |   "keywords": [
17 |     "web-audio-api",
18 |     "record",
19 |     "browserify"
20 |   ],
21 |   "author": "Jamison Dance  (http://jamisondance.com)",
22 |   "license": "MIT",
23 |   "bugs": {
24 |     "url": "https://github.com/jergason/Recorderjs/issues"
25 |   },
26 |   "homepage": "https://github.com/jergason/Recorderjs",
27 |   "peerDependencies": {
28 |     "browserify": "^5.11.1",
29 |     "workerify": "^0.3.0"
30 |   },
31 |   "dependencies": {}
32 | }


--------------------------------------------------------------------------------
/recorder.js:
--------------------------------------------------------------------------------
 1 | var WORKER_PATH = './recorderWorker.js';
 2 | 
 3 | var Recorder = function(source, cfg){
 4 |   var config = cfg || {};
 5 |   var bufferLen = config.bufferLen || 4096;
 6 |   this.context = source.context;
 7 |   this.node = (this.context.createScriptProcessor ||
 8 |                this.context.createJavaScriptNode).call(this.context,
 9 |                                                        bufferLen, 2, 2);
10 |   var worker = new Worker(WORKER_PATH);
11 |   worker.onmessage = function(e){
12 |     var blob = e.data;
13 |     currCallback(blob);
14 |   }
15 | 
16 |   worker.postMessage({
17 |     command: 'init',
18 |     config: {
19 |       sampleRate: this.context.sampleRate
20 |     }
21 |   });
22 |   var recording = false,
23 |     currCallback;
24 | 
25 |   this.node.onaudioprocess = function(e){
26 |     if (!recording) return;
27 |     worker.postMessage({
28 |       command: 'record',
29 |       buffer: [
30 |         e.inputBuffer.getChannelData(0),
31 |         e.inputBuffer.getChannelData(1)
32 |       ]
33 |     });
34 |   }
35 | 
36 |   this.configure = function(cfg){
37 |     for (var prop in cfg){
38 |       if (cfg.hasOwnProperty(prop)){
39 |         config[prop] = cfg[prop];
40 |       }
41 |     }
42 |   }
43 | 
44 |   this.record = function(){
45 |     recording = true;
46 |   }
47 | 
48 |   this.stop = function(){
49 |     recording = false;
50 |   }
51 | 
52 |   this.clear = function(){
53 |     worker.postMessage({ command: 'clear' });
54 |   }
55 | 
56 |   this.getBuffer = function(cb) {
57 |     currCallback = cb || config.callback;
58 |     worker.postMessage({ command: 'getBuffer' })
59 |   }
60 | 
61 |   this.exportWAV = function(cb, type){
62 |     currCallback = cb || config.callback;
63 |     type = type || config.type || 'audio/wav';
64 |     if (!currCallback) throw new Error('Callback not set');
65 |     worker.postMessage({
66 |       command: 'exportWAV',
67 |       type: type
68 |     });
69 |   }
70 | 
71 |   source.connect(this.node);
72 |   this.node.connect(this.context.destination);    //this should not be necessary
73 | };
74 | 
75 | Recorder.forceDownload = function(blob, filename){
76 |   var url = (window.URL || window.webkitURL).createObjectURL(blob);
77 |   var link = window.document.createElement('a');
78 |   link.href = url;
79 |   link.download = filename || 'output.wav';
80 |   var click = document.createEvent("Event");
81 |   click.initEvent("click", true, true);
82 |   link.dispatchEvent(click);
83 | }
84 | 
85 | module.exports = Recorder;
86 | 


--------------------------------------------------------------------------------
/recorderWorker.js:
--------------------------------------------------------------------------------
  1 | var recLength = 0,
  2 |   recBuffersL = [],
  3 |   recBuffersR = [],
  4 |   sampleRate;
  5 | 
  6 | 
  7 | self.onmessage = function(e) {
  8 |   switch(e.data.command){
  9 |     case 'init':
 10 |       init(e.data.config);
 11 |       break;
 12 |     case 'record':
 13 |       record(e.data.buffer);
 14 |       break;
 15 |     case 'exportWAV':
 16 |       exportWAV(e.data.type);
 17 |       break;
 18 |     case 'getBuffer':
 19 |       getBuffer();
 20 |       break;
 21 |     case 'clear':
 22 |       clear();
 23 |       break;
 24 |   }
 25 | };
 26 | 
 27 | function init(config){
 28 |   sampleRate = config.sampleRate;
 29 | }
 30 | 
 31 | function record(inputBuffer){
 32 |   recBuffersL.push(inputBuffer[0]);
 33 |   recBuffersR.push(inputBuffer[1]);
 34 |   recLength += inputBuffer[0].length;
 35 | }
 36 | 
 37 | function exportWAV(type){
 38 |   var bufferL = mergeBuffers(recBuffersL, recLength);
 39 |   var bufferR = mergeBuffers(recBuffersR, recLength);
 40 |   var interleaved = interleave(bufferL, bufferR);
 41 |   var dataview = encodeWAV(interleaved);
 42 |   var audioBlob = new Blob([dataview], { type: type });
 43 | 
 44 |   self.postMessage(audioBlob);
 45 | }
 46 | 
 47 | function getBuffer() {
 48 |   var buffers = [];
 49 |   buffers.push( mergeBuffers(recBuffersL, recLength) );
 50 |   buffers.push( mergeBuffers(recBuffersR, recLength) );
 51 |   self.postMessage(buffers);
 52 | }
 53 | 
 54 | function clear(){
 55 |   recLength = 0;
 56 |   recBuffersL = [];
 57 |   recBuffersR = [];
 58 | }
 59 | 
 60 | function mergeBuffers(recBuffers, recLength){
 61 |   var result = new Float32Array(recLength);
 62 |   var offset = 0;
 63 |   for (var i = 0; i < recBuffers.length; i++){
 64 |     result.set(recBuffers[i], offset);
 65 |     offset += recBuffers[i].length;
 66 |   }
 67 |   return result;
 68 | }
 69 | 
 70 | function interleave(inputL, inputR){
 71 |   var length = inputL.length + inputR.length;
 72 |   var result = new Float32Array(length);
 73 | 
 74 |   var index = 0,
 75 |     inputIndex = 0;
 76 | 
 77 |   while (index < length){
 78 |     result[index++] = inputL[inputIndex];
 79 |     result[index++] = inputR[inputIndex];
 80 |     inputIndex++;
 81 |   }
 82 |   return result;
 83 | }
 84 | 
 85 | function floatTo16BitPCM(output, offset, input){
 86 |   for (var i = 0; i < input.length; i++, offset+=2){
 87 |     var s = Math.max(-1, Math.min(1, input[i]));
 88 |     output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
 89 |   }
 90 | }
 91 | 
 92 | function writeString(view, offset, string){
 93 |   for (var i = 0; i < string.length; i++){
 94 |     view.setUint8(offset + i, string.charCodeAt(i));
 95 |   }
 96 | }
 97 | 
 98 | function encodeWAV(samples){
 99 |   var buffer = new ArrayBuffer(44 + samples.length * 2);
100 |   var view = new DataView(buffer);
101 | 
102 |   /* RIFF identifier */
103 |   writeString(view, 0, 'RIFF');
104 |   /* RIFF chunk length */
105 |   view.setUint32(4, 36 + samples.length * 2, true);
106 |   /* RIFF type */
107 |   writeString(view, 8, 'WAVE');
108 |   /* format chunk identifier */
109 |   writeString(view, 12, 'fmt ');
110 |   /* format chunk length */
111 |   view.setUint32(16, 16, true);
112 |   /* sample format (raw) */
113 |   view.setUint16(20, 1, true);
114 |   /* channel count */
115 |   view.setUint16(22, 2, true);
116 |   /* sample rate */
117 |   view.setUint32(24, sampleRate, true);
118 |   /* byte rate (sample rate * block align) */
119 |   view.setUint32(28, sampleRate * 4, true);
120 |   /* block align (channel count * bytes per sample) */
121 |   view.setUint16(32, 4, true);
122 |   /* bits per sample */
123 |   view.setUint16(34, 16, true);
124 |   /* data chunk identifier */
125 |   writeString(view, 36, 'data');
126 |   /* data chunk length */
127 |   view.setUint32(40, samples.length * 2, true);
128 | 
129 |   floatTo16BitPCM(view, 44, samples);
130 | 
131 |   return view;
132 | }
133 | 


--------------------------------------------------------------------------------