-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.audio.stream.bench.js
83 lines (67 loc) · 1.96 KB
/
engine.audio.stream.bench.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
(function() {
// dependencies
var bench = self.bench;
var app = self.app;
var cfg = app.config;
var engine = app.engine;
var audio = engine.audio;
var stream = audio.stream;
var u = app.util;
// bench
setTimeout(next, 0);
var code = 'exports.dsp = function(t) { return Math.random() * 2 - 1; };';
var count = 100;
var context = new AudioContext;
var streamBuffer;
var outputBuffer;
var audioBufferSizeMin = 1024;
var audioBufferSizeMax = 8192;
var streamBufferSizeMin = 512;
var streamBufferSizeMax = 1024;
var audioBufferSize = Math.max(audioBufferSizeMin, streamBufferSizeMin);
function next(streamBufferSize) {
if (!streamBufferSize) {
streamBufferSize = streamBufferSizeMin;
} else {
streamBufferSize *= 2;
}
if (streamBufferSize > audioBufferSize || streamBufferSize > streamBufferSizeMax) {
audioBufferSize *= 2;
streamBufferSize = streamBufferSizeMin;
if (audioBufferSize > audioBufferSizeMax) return bench.measureAll();
}
cfg.audioBufferSize = audioBufferSize;
cfg.streamBufferSize = streamBufferSize;
audio.bufferSizeQuotient = cfg.audioBufferSize / cfg.streamBufferSize;
var caseKey = audioBufferSize + '/' + streamBufferSize;
console.log(caseKey);
outputBuffer = [
new Float32Array(cfg.audioBufferSize),
new Float32Array(cfg.audioBufferSize)
];
audio.onaudioprocess = u.noop;
audio.init(context);
audio.onstart = function() {
stream.reset();
stream.eval(code);
bench.time(caseKey);
bench.repeat(count, runner, function() {
bench.timeEnd(caseKey, count);
audio.destroy();
next(streamBufferSize);
});
};
audio.start();
}
function runner(next) {
stream.sendBuffers(function(err, buffers) {
stream.write(buffers);
for (var i = 0; i < audio.bufferSizeQuotient; i++) {
streamBuffer = stream.read();
outputBuffer[0].set(streamBuffer[0], i * cfg.streamBufferSize);
outputBuffer[1].set(streamBuffer[1], i * cfg.streamBufferSize);
}
next();
});
}
})();