-
Notifications
You must be signed in to change notification settings - Fork 4
/
tuner.js
39 lines (26 loc) · 1.41 KB
/
tuner.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
function Tuner(sourceNode, destinationNode, audioContext, frequenciesExtractor, InjectedNotesFrequencies) {
this.sourceNode = sourceNode;
this.destinationNode = destinationNode;
this.audioContext = audioContext;
this.frequenciesExtractor = frequenciesExtractor || new FrequenciesExtractor();
this.NotesFrequencies = InjectedNotesFrequencies || NotesFrequencies;
this.count = 0;
this.start = function(callback) {
var analyserNode = this.audioContext.createAnalyser();
analyserNode.fftSize = 2048;
this.sourceNode.connect(analyserNode);
var javascriptNode = this.audioContext.createScriptProcessor(8192, 1, 1);
analyserNode.connect(javascriptNode);
var freqByteData = new Uint8Array(analyserNode.frequencyBinCount);
var self = this;
// binding the callback to window to avoid the GC to clean it
// https://bugzilla.mozilla.org/show_bug.cgi?id=916387
javascriptNode.onaudioprocess = window.audioProcess = function() {
analyserNode.getByteFrequencyData(freqByteData);
var higherValueFrequency = self.frequenciesExtractor.extractHigherValueFrequency(freqByteData, analyserNode.fftSize, self.audioContext.sampleRate);
var note = self.NotesFrequencies.findNoteByFrequency(higherValueFrequency);
callback(note);
}
javascriptNode.connect(this.destinationNode);
};
};