-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
170 lines (150 loc) · 6.61 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
let started = false;
let start_btn = document.getElementById('start_btn');
let metronome_bpm_input = document.getElementById('metronome-bpm');
let harmonic_field_input = document.getElementById('harmonic-field');
let harmonic_bpm_input = document.getElementById('harmonic-bpm');
let sequential_chords_input = document.getElementById('sequential-chords');
let basic_mode_input = document.getElementById('basic-mode');
let play_metronome_sound_input = document.getElementById('play-metronome-sound');
let instrument_input = document.getElementById('instrument');
start_btn.addEventListener('click', () => {
if(!started) {
start_btn.innerHTML = 'Parar';
start_btn.style.backgroundColor = '#ff0000';
start_btn.style.borderColor = '#ff0000';
harmonicFields('play');
} else {
start_btn.innerHTML = 'Iniciar';
start_btn.style.backgroundColor = '#50c019';
start_btn.style.borderColor = '#50c019';
harmonicFields('stop');
}
started = !started;
});
function showData(data){
// Campos de dados
let field = document.getElementById('field');
let field_chords = document.getElementById('field-chords');
let instrument_data = document.getElementById('instrument_data');
let chords_notes = document.getElementById('chords-notes');
let chord_example_image = document.getElementById('chord-example-image');
field.innerHTML = data.field+ " - " + data.name;
field_chords.innerHTML = data.chord;
instrument_data.innerHTML = data.instrument_name;
if(data.notes.length > 0) {
chords_notes.innerHTML = data.notes.join(', ');
chord_example_image.src = 'assets/images/chords/'+ data.instrument +'/'+ data.image + '.jpeg';
} else {
chords_notes.innerHTML = '';
chord_example_image.src = '';
}
}
function showPreData(data){
let field = document.getElementById('field');
let field_chords = document.getElementById('field-chords');
let instrument_data = document.getElementById('instrument_data');
let chords_notes = document.getElementById('chords-notes');
let chord_example_image = document.getElementById('chord-example-image');
field.innerHTML = data.title;
field_chords.innerHTML = data.count;
instrument_data.innerHTML = "";
chords_notes.innerHTML = "";
chord_example_image.src = "";
}
function showHiddenFields(basic_mode){
let chords_notes = document.getElementById('chords-notes');
let chord_example_image = document.getElementById('chord-example-image');
if(basic_mode == 'false') {
chords_notes.style.display = 'none';
chord_example_image.style.display = 'none';
} else {
chords_notes.style.display = 'revert';
chord_example_image.style.display = 'revert';
}
}
// Aplicação
let metronome = new Audio('assets/sounds/metronome.wav');
let metronome_beat;
let harmonic_beat;
function harmonicFields(action) {
let harmonic_field = harmonic_field_input.value;
let metronome_bpm = metronome_bpm_input.value;
let harmonic_bpm = harmonic_bpm_input.value;
let sequential_chords = sequential_chords_input.value;
let basic_mode = basic_mode_input.value;
let play_metronome_sound = play_metronome_sound_input.checked;
let instrument = instrument_input.value;
showHiddenFields(basic_mode);
if(action == 'play') {
showPreData({
title: "Prepare-se! Já vamos começar...",
count: "1"
});
fetch("assets/data/harmonic_fields.json").then(response => {
return response.json();
}).then(function(data){
data.map(function(field) {
if(field.field == harmonic_field && field.instrument == instrument) {
if(play_metronome_sound) {
let count = 1;
metronome_beat = setInterval(function() {
metronome.play();
if(count < harmonic_bpm) {
showPreData({
title: "Prepare-se! Já vamos começar...",
count: count+1
});
}
count++;
} , 60000 / metronome_bpm);
}
let seq = 0;
lastChord = 0;
harmonic_beat = setInterval(function() {
if(seq == field.chords.length) {
seq = 0;
}
let chord = field.chords[seq];
if(sequential_chords == 'false') {
chord = field.chords[Math.floor(Math.random() * field.chords.length)];
while(chord == lastChord) {
chord = field.chords[Math.floor(Math.random() * field.chords.length)];
}
lastChord = chord;
}
fetch("assets/data/"+instrument+"/chords_notes.json").then(response => {
return response.json();
}).then(function(chords){
chords.map(function(chord_notes) {
if(chord_notes.chord == chord) {
notes = [];
image = '';
if(basic_mode == 'true') {
notes = chord_notes.notes;
image = chord_notes.image;
}
let data = {
field: field.field,
name: field.name,
instrument: field.instrument,
instrument_name: field.instrument_name,
chord: chord,
notes: notes,
image: image
}
showData(data);
}
});
});
seq++;
} , (harmonic_bpm * (60000 / metronome_bpm)));
}
});
});
}
if(action == 'stop') {
clearInterval(metronome_beat);
clearInterval(harmonic_beat);
metronome.pause();
}
}