-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
88 lines (56 loc) · 2.03 KB
/
Copy pathscript.js
File metadata and controls
88 lines (56 loc) · 2.03 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
const audioContext = new AudioContext();
const gainNode = audioContext.createGain();
gainNode.connect(audioContext.destination);
const buttons = document.querySelectorAll(".sound-btn");
const volumeSlider = document.getElementById("volume");
const volumeText = document.getElementById("volume-percentage");
const muteBtn = document.querySelector(".mute");
let audioBuffers = {};
let currentSource = null;
let isMuted = false;
let lastVolume = 0.5;
async function loadSound(name, url) {
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
audioBuffers[name] = audioBuffer;
}
loadSound("dog", "dog.wav");
loadSound("cat", "cat.wav");
loadSound("laugh", "laugh.wav");
loadSound("lambo", "lambo.mp3");
loadSound("clap", "clap.wav");
loadSound("bell", "bell.wav");
function playSound(name) {
if (audioContext.state === "suspended") audioContext.resume();
if (currentSource) currentSource.stop();
const source = audioContext.createBufferSource();
source.buffer = audioBuffers[name];
source.connect(gainNode);
source.start();
currentSource = source;
}
buttons.forEach(btn => {
btn.addEventListener("click", () => {
const soundName = btn.getAttribute("data-sound");
playSound(soundName);
});
});
volumeSlider.addEventListener("input", () => {
const vol = parseFloat(volumeSlider.value);
gainNode.gain.value = vol;
volumeText.textContent = Math.round(vol * 100) + "%";
if (!isMuted) lastVolume = vol;
});
muteBtn.addEventListener("click", () => {
isMuted = !isMuted;
if (isMuted) {
gainNode.gain.value = 0;
volumeSlider.value = 0;
volumeText.textContent = "0%";
} else {
gainNode.gain.value = lastVolume;
volumeSlider.value = lastVolume;
volumeText.textContent = Math.round(lastVolume * 100) + "%";
}
});