-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.audio.js
131 lines (106 loc) · 3 KB
/
engine.audio.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(function() {
// dependencies
var app = self.app;
var cfg = app.config;
var engine = app.engine;
var audio = engine.audio;
var stream = audio.stream = {};
var u = app.util;
// properties
audio.isPlaying = false;
audio.silenceBuffer = new Float32Array(cfg.audioBufferSize);
audio.bufferSizeQuotient = cfg.audioBufferSize / cfg.streamBufferSize;
// methods
audio.init = function init(context) {
audio.context = context || new AudioContext;
audio.numBuffersPerSecond = Math.round(audio.context.sampleRate / cfg.streamBufferSize);
audio.sampleRate = audio.numBuffersPerSecond * cfg.streamBufferSize;
audio.setBpm(60);
audio.node = audio.context.createScriptProcessor(cfg.audioBufferSize, 2, 2);
audio.node.onaudioprocess = audio.onaudioprocess;
audio.node.connect(audio.context.destination);
stream.init();
};
audio.setBpm = function setBpm(bpm) {
audio.bpm =
Math.floor(
Math.round(
Math.round(audio.sampleRate * 60 / bpm / cfg.streamBufferSize)
* cfg.streamBufferSize * bpm / 60
) / audio.sampleRate * bpm
);
audio.numBuffersPerBeat = Math.round(audio.sampleRate * 60 / audio.bpm / cfg.streamBufferSize);
audio.timeMultiplier = audio.bpm / 60;
if (stream.loopBuffer) {
stream.loopBuffer[0].numBuffersPerBeat = audio.numBuffersPerBeat;
stream.loopBuffer[1].numBuffersPerBeat = audio.numBuffersPerBeat;
}
};
audio.destroy = function destroy() {
audio.node.disconnect();
audio.ondestroy();
};
audio.eval = function eval(code) {
stream.eval(code);
audio.oneval();
};
audio.start = function start() {
audio.isPlaying = true;
audio.onstart(audio);
audio.play();
};
audio.play = function play() {
audio.isPlaying = true;
audio.onplay(audio);
};
audio.stop = function stop() {
audio.isPlaying = false;
audio.onstop(audio);
};
audio.togglePause = function togglePause() {
if (audio.isPlaying) {
audio.isPlaying = false;
audio.onpause(audio);
} else {
audio.play();
}
};
audio.restart = function restart() {
audio.stop();
audio.onrestart(audio);
audio.start();
};
// events
audio.oneval = u.noop;
audio.onstart = u.noop;
audio.onrestart = u.noop;
audio.onplay = u.noop;
audio.onstop = u.noop;
audio.onpause = u.noop;
audio.ondestroy = u.noop;
audio.onaudioprocess = (function() {
var L, R;
var i;
return function onaudioprocess(ev) {
L = ev.outputBuffer.getChannelData(0);
R = ev.outputBuffer.getChannelData(1);
if (!audio.isPlaying) {
/*
// these only work in firefox atm, but keep them for reference
// theoritically they should perform better
out.copyToChannel(audio.silenceBuffer, 0);
out.copyToChannel(audio.silenceBuffer, 1);
*/
L.set(audio.silenceBuffer, 0);
R.set(audio.silenceBuffer, 0);
return;
}
for (i = 0; i < audio.bufferSizeQuotient; i++) {
audio.buffer = stream.read();
if (!audio.buffer) return;
L.set(audio.buffer[0], i * cfg.streamBufferSize);
R.set(audio.buffer[1], i * cfg.streamBufferSize);
}
};
})();
})();