soiz1 commited on
Commit
8f29b8d
·
verified ·
1 Parent(s): 43b7bbe

Update src/addons/addons/vol-slider/module.js

Browse files
src/addons/addons/vol-slider/module.js CHANGED
@@ -9,76 +9,76 @@ let globalVm;
9
  const callbacks = [];
10
 
11
  export const setVolume = (newVolume) => {
12
- if (gainNode) {
13
- gainNode.value = newVolume;
14
- // literally any other extension
15
- for (const audioData of globalVm.runtime._extensionAudioObjects.values()) {
16
- if (audioData.gainNode) {
17
- audioData.gainNode.gain.value = gainNode.value;
18
- }
 
 
 
19
  }
20
- } else {
21
- volumeBeforeFinishSetup = newVolume;
22
- }
23
- callbacks.forEach((i) => i());
24
  };
25
 
26
  export const getVolume = () => {
27
- if (gainNode) {
28
- return gainNode.value;
29
- }
30
- return volumeBeforeFinishSetup;
31
  };
32
 
33
  export const isMuted = () => {
34
- return getVolume() === 0;
35
  };
36
 
37
  export const setUnmutedVolume = (newUnmuteVolume) => {
38
- unmuteVolume = newUnmuteVolume;
39
  };
40
 
41
  export const setMuted = (newMuted) => {
42
- if (newMuted) {
43
- setUnmutedVolume(getVolume());
44
- setVolume(0);
45
- } else {
46
- setVolume(unmuteVolume);
47
- }
48
  };
49
 
50
  export const onVolumeChanged = (callback) => {
51
- callbacks.push(callback);
52
  };
53
 
54
  const gotAudioEngine = (audioEngine) => {
55
- if (!audioEngine) {
56
- console.error('could not get audio engine; sound-related addons will not work');
57
- return;
58
- }
59
- gainNode = audioEngine.inputNode.gain;
60
- gainNode.value = volumeBeforeFinishSetup;
61
- // literally any other extension
62
- for (const audioData of globalVm.runtime._extensionAudioObjects.values()) {
63
- if (audioData.gainNode) {
64
- audioData.gainNode.gain.value = gainNode.value;
 
65
  }
66
- }
67
  };
68
 
69
  export const setup = (vm) => {
70
- if (hasSetup) {
71
- return;
72
- }
73
- hasSetup = true;
74
- globalVm = vm;
75
-
76
- const audioEngine = vm.runtime.audioEngine;
77
- if (audioEngine) {
78
- gotAudioEngine(audioEngine);
79
- } else {
80
- vm.runtime.once("PROJECT_LOADED", () => {
81
- gotAudioEngine(vm.runtime.audioEngine);
82
- });
83
- }
84
- };
 
9
  const callbacks = [];
10
 
11
  export const setVolume = (newVolume) => {
12
+ if (gainNode) {
13
+ gainNode.value = newVolume;
14
+ // literally any other extension
15
+ for (const audioData of globalVm.runtime._extensionAudioObjects.values()) {
16
+ if (audioData.gainNode) {
17
+ audioData.gainNode.gain.value = gainNode.value;
18
+ }
19
+ }
20
+ } else {
21
+ volumeBeforeFinishSetup = newVolume;
22
  }
23
+ callbacks.forEach((i) => i());
 
 
 
24
  };
25
 
26
  export const getVolume = () => {
27
+ if (gainNode) {
28
+ return gainNode.value;
29
+ }
30
+ return volumeBeforeFinishSetup;
31
  };
32
 
33
  export const isMuted = () => {
34
+ return getVolume() === 0;
35
  };
36
 
37
  export const setUnmutedVolume = (newUnmuteVolume) => {
38
+ unmuteVolume = newUnmuteVolume;
39
  };
40
 
41
  export const setMuted = (newMuted) => {
42
+ if (newMuted) {
43
+ setUnmutedVolume(getVolume());
44
+ setVolume(0);
45
+ } else {
46
+ setVolume(unmuteVolume);
47
+ }
48
  };
49
 
50
  export const onVolumeChanged = (callback) => {
51
+ callbacks.push(callback);
52
  };
53
 
54
  const gotAudioEngine = (audioEngine) => {
55
+ if (!audioEngine) {
56
+ console.error('could not get audio engine; sound-related addons will not work');
57
+ return;
58
+ }
59
+ gainNode = audioEngine.inputNode.gain;
60
+ gainNode.value = volumeBeforeFinishSetup;
61
+ // literally any other extension
62
+ for (const audioData of globalVm.runtime._extensionAudioObjects.values()) {
63
+ if (audioData.gainNode) {
64
+ audioData.gainNode.gain.value = gainNode.value;
65
+ }
66
  }
 
67
  };
68
 
69
  export const setup = (vm) => {
70
+ if (hasSetup) {
71
+ return;
72
+ }
73
+ hasSetup = true;
74
+ globalVm = vm;
75
+
76
+ const audioEngine = vm.runtime.audioEngine;
77
+ if (audioEngine) {
78
+ gotAudioEngine(audioEngine);
79
+ } else {
80
+ vm.runtime.once("PROJECT_LOADED", () => {
81
+ gotAudioEngine(vm.runtime.audioEngine);
82
+ });
83
+ }
84
+ };