-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple_note_inputs V2.js
More file actions
140 lines (112 loc) · 4.5 KB
/
multiple_note_inputs V2.js
File metadata and controls
140 lines (112 loc) · 4.5 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
class Staff {
constructor(note_name, octave, dom_id) {
this.note_name = note_name;
this.octave = octave;
this.dom_id = dom_id;
}
updateNote(note_name, octave) {
this.note_name = note_name;
this.octave = octave;
this.draw();
}
draw() {
const VF = Vex.Flow;
const div = document.getElementById(this.dom_id);
// Clear the div to start fresh
div.innerHTML = '';
const noteObj = Tonal.Note.get(this.note_name + this.octave);
const clef = (noteObj.oct < 4) ? "bass" : "treble";
// Set up renderer and context again
const renderer = new VF.Renderer(div, VF.Renderer.Backends.SVG);
renderer.resize(360, 350);
const context = renderer.getContext();
context.setFont("Arial", 10, "").setBackgroundFillStyle("#eed");
// Draw the stave
const stave = new VF.Stave(10, 80, 400);
stave.addClef(clef).setContext(context).draw();
// Extract base note without accidental
const baseNote = noteObj.pc[0].toLowerCase(); // e.g. "e" from "Eb"
const singleNote = new VF.StaveNote({
clef: clef,
keys: [baseNote + "/" + noteObj.oct], // "e/4"
duration: "w",
});
// Add accidental if needed
if (noteObj.acc === '#') {
singleNote.addModifier(new VF.Accidental('#'), 0);
} else if (noteObj.acc === 'b') {
singleNote.addModifier(new VF.Accidental('b'), 0);
}
// Create and format the voice
const voice = new VF.Voice({ num_beats: 4, beat_value: 4 }).addTickables([singleNote]);
new VF.Formatter().joinVoices([voice]).format([voice], 300);
// Render the voice
voice.draw(context, stave);
}
}
class NotePicker {
constructor(id, staffid, noteAnswer, octaveAnswer) {
this.note_name = 'C'
this.octave = '4'
this.dom_id = id
// console.log(this.note_name)
document.getElementById(this.dom_id).innerHTML = this.make_core_html_with_id()
this.bind_listeners()
this.staff = new Staff(this.note_name, this.octave, staffid)
this.staff.draw()
}
make_core_html_with_id() {
var html = `
<select id="${this.dom_id}_note">
<option value="C">C</option>
<option value="C#">C#</option>
<option value="Db">Db</option>
<option value="D">D</option>
<option value="D#">D#</option>
<option value="Eb">Eb</option>
<option value="E">E</option>
<option value="F">F</option>
<option value="F#">F#</option>
<option value="Gb">Gb</option>
<option value="G">G</option>
<option value="G#">G#</option>
<option value="Ab">Ab</option>
<option value="A">A</option>
<option value="A#">A#</option>
<option value="Bb">Bb</option>
<option value="B">B</option>
</select>
<select id="${this.dom_id}_octave">
<option value="2">2</option>
<option value="3">3</option>
<option value="4" selected>4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<div id="${this.dom_id}_staff"></div>`;
return html;
}
// Define the handler method
handleNoteChange(event) {
this.note_name = event.target.value; // Update the instance's note_name property
this.staff.updateNote(this.note_name, this.octave)
console.log(`Selected note: ${this.note_name}`);
console.log(`Selected octave: ${this.octave}`);
}
handleOctaveChange(event) {
this.octave = event.target.value; // Update the instance's octave property
this.staff.updateNote(this.note_name, this.octave)
console.log(`Selected note: ${this.note_name}`);
console.log(`Selected octave: ${this.octave}`);
}
bind_listeners() {
// Listener on note name
const noteSelect = document.getElementById(`${this.dom_id}_note`);
noteSelect.addEventListener('change', this.handleNoteChange.bind(this));
// Listener on octave
const octaveSelect = document.getElementById(`${this.dom_id}_octave`);
octaveSelect.addEventListener('change', this.handleOctaveChange.bind(this));
}
check_note(){
}
}