-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruments.js
executable file
·59 lines (45 loc) · 1.17 KB
/
instruments.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 audioFiles = {
'drum': "1.wav",
'kick': "2.wav",
'doom': "doom.wav",
'bass': 'bass.wav',
'bongo': 'bongo.wav',
'bop' : 'bop.wav',
'clap': 'clap.wav',
'cowbell': 'cowbell.wav',
'doom': 'doom.wav',
'drum': 'drum.mp3',
'fart': 'fart.wav',
'knife': 'knife.wav',
'tick': 'tick.mp3'
}
var audiopath = "samples/"
files = Object.values(audioFiles)
files = files.map(f => audiopath + f)
class Instrument {
constructor(buffer) {
this.buffer = buffer
}
trigger(time) {
let source = context.createBufferSource();
source.buffer = this.buffer;
source.connect(context.destination);
source.start(time);
}
}
const context = new (window.AudioContext || window.webkitAudioContext)();
var dookie = 'suki'
function fetchBuffers() {
var p = Promise.all(
files.map(url => fetch(url)
.then(r => r.arrayBuffer())
.then(buf => context.decodeAudioData(buf))
.then(audioBuf => new Instrument(audioBuf))
)
);
return p
}
async function loadInstruments() {
const instruments = await fetchBuffers();
return instruments;
}