forked from Hubs-Foundation/hubs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathavatar-volume-controls.js
74 lines (63 loc) · 2.46 KB
/
avatar-volume-controls.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
import { VOLUME_LABELS } from "./media-views";
const MAX_VOLUME = 8;
const SMALL_STEP = 1 / (VOLUME_LABELS.length / 2);
const BIG_STEP = (MAX_VOLUME - 1) / (VOLUME_LABELS.length / 2);
AFRAME.registerComponent("avatar-volume-controls", {
schema: {
volume: { type: "number", default: 1.0 }
},
init() {
this.volumeUp = this.volumeUp.bind(this);
this.volumeDown = this.volumeDown.bind(this);
this.changeVolumeBy = this.changeVolumeBy.bind(this);
this.volumeUpButton = this.el.querySelector(".avatar-volume-up-button");
this.volumeDownButton = this.el.querySelector(".avatar-volume-down-button");
this.volumeLabel = this.el.querySelector(".avatar-volume-label");
this.volumeUpButton.object3D.addEventListener("interact", this.volumeUp);
this.volumeDownButton.object3D.addEventListener("interact", this.volumeDown);
this.update = this.update.bind(this);
window.APP.store.addEventListener("statechanged", this.update);
this.updateVolumeLabel();
},
remove() {
window.APP.store.removeEventListener("statechanged", this.update);
},
changeVolumeBy(v) {
this.el.setAttribute("avatar-volume-controls", "volume", THREE.Math.clamp(this.data.volume + v, 0, MAX_VOLUME));
this.updateVolumeLabel();
},
volumeUp() {
const step = this.data.volume > 1 - SMALL_STEP ? BIG_STEP : SMALL_STEP;
this.changeVolumeBy(step);
},
volumeDown() {
const step = this.data.volume > 1 + SMALL_STEP ? BIG_STEP : SMALL_STEP;
this.changeVolumeBy(-1 * step);
},
update() {
if (this.audio) {
const globalVoiceVolume =
window.APP.store.state.preferences.globalVoiceVolume !== undefined
? window.APP.store.state.preferences.globalVoiceVolume
: 100;
this.audio.gain.gain.value = (globalVoiceVolume / 100) * this.data.volume;
}
},
updateVolumeLabel() {
const numBars = Math.min(
VOLUME_LABELS.length - 1,
this.data.volume <= 1.001
? Math.floor(this.data.volume / SMALL_STEP)
: Math.floor(VOLUME_LABELS.length / 2 + (this.data.volume - 1) / BIG_STEP)
);
this.volumeLabel.setAttribute("text", "value", this.data.volume === 0 ? "Muted" : VOLUME_LABELS[numBars]);
},
tick() {
if (this.audio) return;
// Walk up to Spine and then search down.
const source = this.el.parentNode.parentNode.querySelector("[networked-audio-source]");
if (!source) return;
this.audio = source.components["networked-audio-source"].sound;
this.update();
}
});