-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsound.js
More file actions
136 lines (122 loc) · 3.74 KB
/
sound.js
File metadata and controls
136 lines (122 loc) · 3.74 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
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
132
133
134
135
136
import { settings } from "./game.js";
const audioCtx = new AudioContext();
const globalGain = audioCtx.createGain();
globalGain.connect(audioCtx.destination);
const volumeSliderButton = document.getElementById("volumeSliderButton");
const volumeSlider = document.getElementById("volumeSlider");
volumeSliderButton.onclick = () => {
if (settings.volume == 0) {
settings.volume = 100;
}
else {
settings.volume = 0;
}
volumeSlider.value = settings.volume;
volumeSlider.oninput();
};
volumeSlider.oninput = () => {
settings.volume = Number(volumeSlider.value);
if (settings.volume == 0) {
volumeSliderButton.style.backgroundPosition = "0% 0%";
}
else if (settings.volume < 50) {
volumeSliderButton.style.backgroundPosition = "-100% 0%";
}
else {
volumeSliderButton.style.backgroundPosition = "-200% 0%";
}
globalGain.gain.value = settings.volume / 100;
};
// playSound function
// playMusic function
let sounds = {};
let currentMusic = null;
let nextMusicTimeout = null;
function playSound(id, volume = 1) {
if (sounds[id].length == 0) {
setTimeout(() => {
playSound(id, volume)
}, 100);
return;
}
let source = new AudioBufferSourceNode(audioCtx, {
buffer: sounds[id][Math.floor(Math.random() * sounds[id].length)].buffer,
});
let gain = audioCtx.createGain();
gain.gain.value = volume;
source.connect(gain);
gain.connect(globalGain);
source.onended = () => {
source.disconnect();
gain.disconnect();
};
source.start();
};
function playMusic(id) {
if (id != null && sounds[id].length == 0) {
setTimeout(() => {
playMusic(id);
}, 100);
return;
}
if (currentMusic != null) {
clearTimeout(nextMusicTimeout);
currentMusic.gain.gain.linearRampToValueAtTime(0, audioCtx.currentTime + 1);
let lastMusic = currentMusic;
setTimeout(() => {
lastMusic.source.onended = () => {};
lastMusic.source.stop();
lastMusic.source.disconnect();
lastMusic.gain.disconnect();
}, 1000);
}
if (id != null) {
let sound = sounds[id][Math.floor(Math.random() * sounds[id].length)];
let source = new AudioBufferSourceNode(audioCtx, {
buffer: sound.buffer,
});
let gain = audioCtx.createGain();
source.connect(gain);
gain.connect(globalGain);
function loop() {
source.disconnect();
sound = sounds[id][Math.floor(Math.random() * sounds[id].length)];
source = new AudioBufferSourceNode(audioCtx, {
buffer: sound.buffer,
});
source.connect(gain);
source.start();
nextMusicTimeout = setTimeout(loop, sound.buffer.duration * 1000 + Math.random() * 10000 + 5000);
};
nextMusicTimeout = setTimeout(loop, sound.buffer.duration * 1000 + Math.random() * 10000 + 5000);
if (currentMusic != null) {
setTimeout(() => {
// gain.gain.value = 0;
// gain.gain.linearRampToValueAtTime(1, audioCtx.currentTime + 1);
source.start();
}, 2000);
}
else {
source.start();
}
currentMusic = {
source: source,
gain: gain,
};
}
else {
currentMusic = null;
}
};
async function loadSound(id, src, detune = 0) {
if (sounds[id] == null) {
sounds[id] = [];
}
sounds[id].push({
buffer: await audioCtx.decodeAudioData(await (await fetch(src)).arrayBuffer()),
detune: detune,
});
};
document.onvisibilitychange = () => {
};
export { playMusic };