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