-
Notifications
You must be signed in to change notification settings - Fork 41
/
audio-utils.js
66 lines (50 loc) · 2.05 KB
/
audio-utils.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
57
58
59
60
61
62
63
64
65
66
const playSignal = (signal = [], audioContext, sampleRate) => {
const signalLength = signal.length
// This node will be used as the source to play the signal
const node = audioContext.createBufferSource()
// Let's create the buffer to be assigned to the above node
// The arguments are: the number of channels, the length of the buffer, which
// is the same from the signal and the sample rate
// Sample Rate is basically how many samples the sound array contains per second
// if sample rate is 3000 and you want to build a signal with 2 seconds, you'll need
// an array with length equals to 6000
const buffer = audioContext.createBuffer(1, signalLength, sampleRate)
// This is the data on the buffer, as we created an empty one, this a empty array so far
const data = buffer.getChannelData(0)
// Let's write the values from our signal to the buffer...
for (let i = 0; i < signalLength; i += 1) {
data[i] = signal[i]
}
// then assign the buffer to the node.buffer prop
node.buffer = buffer
// let's connect the buffer to the audioContext.destination, which means the speakers
// audioContext is the context that you need to handle all this fancy stuff Web Audio API related :)
node.connect(audioContext.destination)
// now we start to play!
node.start(audioContext.currentTime)
}
const loadSound = (path, audioContext) => {
const request = new XMLHttpRequest()
request.open('GET', path, true)
request.responseType = 'arraybuffer'
const promise = new Promise((resolve, reject) => {
request.onload = () => {
audioContext.decodeAudioData(
request.response,
(buffer) => resolve(buffer),
(error) => console.error(error)
)
}
request.onerror = (error) => reject(error)
})
request.send()
return promise
}
const playBuffer = (buffer, audioContext) => {
const startTime = audioContext.currentTime
const source = audioContext.createBufferSource()
source.buffer = buffer
source.connect(audioContext.destination)
source.start(startTime)
source.stop(startTime + 2)
}