-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.js
More file actions
58 lines (45 loc) · 1.46 KB
/
engine.js
File metadata and controls
58 lines (45 loc) · 1.46 KB
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
class AudioEngine {
constructor({ impulseUrl }) {
this.context = new (window.AudioContext || window.webkitAudioContext)();
// === MASTER ===
this.masterGain = this.context.createGain();
this.masterGain.gain.value = 1.0;
// === DRY BUS ===
this.dryBus = this.context.createGain();
this.dryBus.gain.value = 1.0;
// === REVERB BUS ===
this.reverbBus = this.context.createGain();
this.reverbBus.gain.value = 1.0;
this.convolver = this.context.createConvolver();
// === Wiring ===
this.dryBus.connect(this.masterGain);
this.reverbBus.connect(this.convolver);
this.convolver.connect(this.masterGain);
this.masterGain.connect(this.context.destination);
// === Listener defaults ===
const listener = this.context.listener;
listener.positionX.value = 0;
listener.positionY.value = 0;
listener.positionZ.value = 0;
listener.forwardX.value = 0;
listener.forwardY.value = 0;
listener.forwardZ.value = -1;
listener.upX.value = 0;
listener.upY.value = 1;
listener.upZ.value = 0;
// === Load impulse response ===
this.ready = this._loadImpulse(impulseUrl);
}
async _loadImpulse(url) {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to load IR: ${url}`);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await this.context.decodeAudioData(arrayBuffer);
this.convolver.buffer = audioBuffer;
}
async resume() {
if (this.context.state === 'suspended') {
await this.context.resume();
}
}
}