-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
104 lines (70 loc) · 2.06 KB
/
index.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
module.exports = function (context, fn, bufSize) {
if (typeof context === 'function') {
fn = context;
context = new webkitAudioContext() ;
}
if(!bufSize) bufSize = 4096;
var self = context.createScriptProcessor(bufSize, 1, 1);
self.fn = fn
var tt = 0.0
var ii = 0
const rate = context.sampleRate
self.i = self.t = 0
window._SAMPLERATE = self.sampleRate = self.rate = context.sampleRate;
self.duration = Infinity;
self.recording = false;
self.onaudioprocess = function(e){
var output = e.outputBuffer.getChannelData(0)
, input = e.inputBuffer.getChannelData(0);
self.tick(output, input);
};
self.tick = function (output, input) { // a fill-a-buffer function
for (var i = 0; i < output.length; i += 1) {
tt = ii / rate
ii = ii + 1
output[i] = self.fn(tt, ii, input[i]);
}
return output
};
self.stop = function(){
self.disconnect();
self.playing = false;
if(self.recording) {}
};
self.play = function(opts){
if (self.playing) return;
self.connect(self.context.destination);
self.playing = true;
return
};
self.record = function(){
};
self.reset = function(){
self.i = self.t = 0
};
self.createSample = function(duration){
self.reset();
var buffer = self.context.createBuffer(1, duration, self.context.sampleRate)
var blob = buffer.getChannelData(0);
self.tick(blob);
return buffer
};
return self;
};
function mergeArgs (opts, args) {
Object.keys(opts || {}).forEach(function (key) {
args[key] = opts[key];
});
return Object.keys(args).reduce(function (acc, key) {
var dash = key.length === 1 ? '-' : '--';
return acc.concat(dash + key, args[key]);
}, []);
}
function signed (n) {
if (isNaN(n)) return 0;
var b = Math.pow(2, 15);
return n > 0
? Math.min(b - 1, Math.floor((b * n) - 1))
: Math.max(-b, Math.ceil((b * n) - 1))
;
}