forked from rssilva/web-audio-image-filtering
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter.js
54 lines (42 loc) · 1.52 KB
/
filter.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
class Filter {
constructor (audioContext) {
this.audioContext = audioContext
}
filterSignal (signal, type = 'highpass', frequency = 10000) {
const node = this.audioContext.createBufferSource()
const buffer = this.audioContext.createBuffer(1, signal.length, this.audioContext.sampleRate)
const data = buffer.getChannelData(0)
const processed = []
const filter = this.audioContext.createBiquadFilter()
filter.type = type
filter.frequency.value = frequency
// filter.Q.value = 0.2
const processorNode = this.audioContext.createScriptProcessor(4096, 2, 2)
const length = signal.length
const promise = new Promise((resolve, reject) => {
processorNode.onaudioprocess = this.onAudioProcess.bind(this, processed, processorNode, length, resolve)
})
for (let i = 0; i < length; i++) {
data[i] = signal[i]
}
node.buffer = buffer
node.connect(filter)
// node.connect(this.audioContext.destination)
filter.connect(processorNode)
processorNode.connect(this.audioContext.destination)
node.start(this.audioContext.currentTime)
return promise
}
onAudioProcess (processed, processorNode, finalLength, resolve, ev) {
const inputBuffer = ev.inputBuffer
const inputData = inputBuffer.getChannelData(0)
const length = inputData.length
for (let sample = 0; sample < length; sample++) {
processed.push(inputData[sample])
}
if (processed.length >= finalLength) {
processorNode.disconnect()
resolve(processed)
}
}
}