This repository has been archived by the owner on Nov 25, 2024. It is now read-only.
forked from normanzb/Recorderjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.js
112 lines (94 loc) · 2.85 KB
/
recorder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
define(['module'], function(module){
function getFolder(fullPath){
var slashIndex = fullPath.lastIndexOf('/');
return fullPath.substr(0, slashIndex);
}
var WORKER_PATH = getFolder(module.uri) + '/recorderWorker.js';
var Recorder = function(source, cfg){
var config = cfg || {isMono: false};
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;
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.isMono = function(value){
config.isMono = value;
};
this.record = function(){
recording = true;
};
this.stop = function(){
recording = false;
};
this.clear = function(){
worker.postMessage({ command: 'clear' });
};
this.getBuffer = function(cb) {
currCallback = cb || config.callback;
worker.postMessage({ command: 'getBuffer' })
};
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,
isMono: config.isMono
});
};
this.exportFLAC = function(cb, type){
currCallback = cb || config.callback;
type = type || config.type || 'audio/x-flac';
if (!currCallback) throw new Error('Callback not set');
worker.postMessage({
command: 'exportFLAC',
type: type
});
};
this.dispose = function(){
source.disconnect();
this.node.disconnect();
};
worker.onmessage = function(e){
var blob = e.data;
currCallback(blob);
}
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);
}
return Recorder;
});