-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworklet.js
More file actions
36 lines (30 loc) · 1.02 KB
/
worklet.js
File metadata and controls
36 lines (30 loc) · 1.02 KB
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
import { getNote, getCents, getNoteFrequency, getNoteIndex } from './audio.js';
import { AMDF } from './detectors.js';
class TunerAudioProcessor extends AudioWorkletProcessor {
constructor(options) {
super(options)
this.buffer = []
this.bufferSize = 4096
this.sampleRate = options.processorOptions.sampleRate
}
process(inputs, outputs, parameters) {
const input = inputs[0].constructor === Float32Array ?
inputs[0] : Array.from(inputs[0][0])
this.buffer = this.buffer.concat(input)
if (this.buffer.length === this.bufferSize) {
let data = new Float32Array(this.buffer)
let frequency = AMDF({ sampleRate: this.sampleRate })(data)
if (frequency) {
let index = getNoteIndex(frequency)
this.port.postMessage({
frequency: frequency,
note: getNote(index),
cents: getCents(frequency, getNoteFrequency(index))
})
}
this.buffer = []
}
return true
}
}
registerProcessor('tuner-audio-processor', TunerAudioProcessor)