-
Notifications
You must be signed in to change notification settings - Fork 122
/
app.js
83 lines (73 loc) · 2.08 KB
/
app.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const Application = function () {
this.initA4();
this.tuner = new Tuner(this.a4);
this.notes = new Notes(".notes", this.tuner);
this.meter = new Meter(".meter");
this.frequencyBars = new FrequencyBars(".frequency-bars");
this.update({
name: "A",
frequency: this.a4,
octave: 4,
value: 69,
cents: 0,
});
};
Application.prototype.initA4 = function () {
this.$a4 = document.querySelector(".a4 span");
this.a4 = parseInt(localStorage.getItem("a4")) || 440;
this.$a4.innerHTML = this.a4;
};
Application.prototype.start = function () {
const self = this;
this.tuner.onNoteDetected = function (note) {
if (self.notes.isAutoMode) {
if (self.lastNote === note.name) {
self.update(note);
} else {
self.lastNote = note.name;
}
}
};
swal.fire("Welcome to online tuner!").then(function () {
self.tuner.init();
self.frequencyData = new Uint8Array(self.tuner.analyser.frequencyBinCount);
});
this.$a4.addEventListener("click", function () {
swal
.fire({ input: "number", inputValue: self.a4 })
.then(function ({ value: a4 }) {
if (!parseInt(a4) || a4 === self.a4) {
return;
}
self.a4 = a4;
self.$a4.innerHTML = a4;
self.tuner.middleA = a4;
self.notes.createNotes();
self.update({
name: "A",
frequency: self.a4,
octave: 4,
value: 69,
cents: 0,
});
localStorage.setItem("a4", a4);
});
});
this.updateFrequencyBars();
document.querySelector(".auto input").addEventListener("change", () => {
this.notes.toggleAutoMode();
});
};
Application.prototype.updateFrequencyBars = function () {
if (this.tuner.analyser) {
this.tuner.analyser.getByteFrequencyData(this.frequencyData);
this.frequencyBars.update(this.frequencyData);
}
requestAnimationFrame(this.updateFrequencyBars.bind(this));
};
Application.prototype.update = function (note) {
this.notes.update(note);
this.meter.update((note.cents / 50) * 45);
};
const app = new Application();
app.start();