-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
53 lines (38 loc) · 1.56 KB
/
app.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
// Event listeners for each button
document.getElementById('pianoButton1').addEventListener('click', function() {
playPianoSound('Samples/runaway_1.mp3');
});
document.getElementById('pianoButton2').addEventListener('click', function() {
playPianoSound('Samples/runaway_2.mp3');
});
document.getElementById('pianoButton3').addEventListener('click', function() {
playPianoSound('Samples/runaway_3.mp3');
});
document.getElementById('pianoButton4').addEventListener('click', function() {
playPianoSound('Samples/runaway_4.mp3');
});
// Function to play piano sound
function playPianoSound(audioFilePath) {
// Your existing playPianoSound function
}
// Function to play piano sound and trigger animation
function playPianoSound(audioFilePath, buttonId) {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const pianoSound = audioContext.createBufferSource();
const request = new XMLHttpRequest();
request.open('GET', audioFilePath, true);
request.responseType = 'arraybuffer';
request.onload = function () {
audioContext.decodeAudioData(request.response, function (buffer) {
pianoSound.buffer = buffer;
pianoSound.connect(audioContext.destination);
pianoSound.start(0);
// Trigger animation on click
document.getElementById(buttonId).classList.add('animated');
setTimeout(function () {
document.getElementById(buttonId).classList.remove('animated');
}, 500);
});
};
request.send();
}