-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsounds.js
56 lines (51 loc) · 2.45 KB
/
sounds.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
const AudioContext = window.AudioContext || window.webkitAudioContext;
const audioCtx = new AudioContext();
const metronomeGain = new GainNode(audioCtx, {gain: 0.5});
const SOUNDS = {
"metronome_1_accent": { "path": "/assets/audio/metronome_beat_1_accent.mp3", "buffer": null },
"metronome_1": { "path": "/assets/audio/metronome_beat_1.mp3", "buffer": null },
"metronome_2_accent": { "path": "/assets/audio/metronome_beat_2_accent.wav", "buffer": null },
"metronome_2": { "path": "/assets/audio/metronome_beat_2.wav", "buffer": null },
"metronome_3_accent": { "path": "/assets/audio/metronome_beat_3_accent.wav", "buffer": null },
"metronome_3": { "path": "/assets/audio/metronome_beat_3.wav", "buffer": null },
"metronome_4_accent": { "path": "/assets/audio/metronome_beat_4_accent.wav", "buffer": null },
"metronome_4": { "path": "/assets/audio/metronome_beat_4.wav", "buffer": null },
"kick": {
"path": "/assets/audio/kick.wav", "buffer": null, fineTune: -6, originalPitch: 3600
},
"snare": {
"path": "/assets/audio/snare.wav", "buffer": null, fineTune: 0, originalPitch: 3800
},
"open_hihat": {
"path": "/assets/audio/hihat-open.wav", "buffer": null, fineTune: 0, originalPitch: 4600
},
"crash_cymbal": {
"path": "/assets/audio/crash.wav", "buffer": null, fineTune: 0, originalPitch: 4900
},
"closed_hithat": {
"path": "/assets/audio/hihat-closed.wav", "buffer": null, fineTune: 0, originalPitch: 4200
},
"tom": {
"path": "/assets/audio/tom.wav", "buffer": null, fineTune: 0, originalPitch: 5000
},
};
const DEFAULT_SOUNDS = new Set(["metronome_1_accent", "metronome_1"]);
function loadSound(soundName) {
data = SOUNDS[soundName];
fetch(data.path).then((resp) => {
return resp.arrayBuffer();
}).then((buffer) => {
audioCtx.decodeAudioData(buffer, (decodedData) => SOUNDS[soundName]["buffer"] = decodedData);
});
}
function loadDefaultSound() {
DEFAULT_SOUNDS.forEach((sound) => loadSound(sound));
}
function loadOtherSounds() {
const OTHER_SOUNDS = Object.keys(SOUNDS).filter(x => !DEFAULT_SOUNDS.has(x));
OTHER_SOUNDS.forEach((sound) => loadSound(sound));
}
loadDefaultSound();
// loading this here so it downloads the script quickly and I have my worker initiated when user clicks start.
// a hack until I introduce webpack/bundler to reduce resource requests on website load.
const timerWorker = new Worker("worker.js");