-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
434 lines (381 loc) · 10.7 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
const TonePlayer = require('./TonePlayer.js');
const SimilarSongsRandomizer = require('./similarSongsRandomizer.js');
const View = require('./view.js');
const noAlt = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
const minor = [
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
];
const allPosKeys = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
];
let currentSong;
let nextSong;
let currentMeasure = 0;
let connectChords;
let connectChordsIndex = -1;
let lastChordView = 0;
exports.setCurrentMeasure = function (measureNum) {
//Refers to the current played measure by TonePlayer
currentMeasure = measureNum;
//console.log('App.js: ', currentMeasure, connectChordsIndex);
scrollSubView();
//Change nella view la misura illuminata
updateView(currentSong, viewedBlock);
};
exports.setCurrentMeasureConnect = function (measureNum) {
//Refers to the current played measure by TonePlayer
connectChordsIndex = measureNum;
console.log('App.js: ', currentMeasure, connectChordsIndex);
//Change nella view la misura illuminata
updateView(currentSong, viewedBlock);
};
exports.setFinalShiftZeroToStop = function () {
finalShift = 0;
};
const chordPanel = document.getElementById('chords');
exports.triggerNextSong = function () {
currentSong = nextSong;
measures = currentSong.music.measures;
measures = capChords(measures, currentSong.music.timeSignature);
TonePlayer.setNextSongCurrent(currentSong);
setKeyDropdown();
//Clear chord grid
for (let i = 0; i < chordPanel.children.length; i++) {
chordPanel.children[i].textContent = '';
for (let j = 0; j < chordPanel.children[i].classList.length; j++) {
if (chordPanel.children[i].classList[j] == 'selectedCell') {
chordPanel.children[i].classList.remove('selectedCell');
}
}
}
viewedBlock = [];
viewIndex = 0;
lastChordView = measures.length - 1;
finalShift = 0;
for (let i = 0; i < measures.length && i < maxSize; i++) {
viewedBlock.push(measures[i]);
}
updateView(currentSong, viewedBlock);
};
const chordPanelConnect = document.getElementById('connectChords');
exports.setConnectChords = function (cChords) {
connectChords = cChords;
//Remove previous children
while (chordPanelConnect.firstChild) {
chordPanelConnect.removeChild(chordPanelConnect.lastChild);
}
//Grid generation harmonic conenct
for (let i = 0; i < connectChords.length; i++) {
let div = document.createElement('div');
div.id = 'cellHarmonic' + i;
div.classList.add('cell');
chordPanelConnect.appendChild(div);
}
updateView(currentSong, viewedBlock);
};
function updateView(song, subMeasure) {
View.changeState(
song,
subMeasure,
viewIndex,
connectChords,
connectChordsIndex,
lastChordView
);
}
//Initialization
currentSong = SimilarSongsRandomizer.getFirstRandomSong();
//TODO -> CAP SU TUTTE LE NEXT SONG
let measures = currentSong.music.measures;
measures = capChords(measures, currentSong.music.timeSignature);
TonePlayer.setCurrentSong(currentSong);
setKeyDropdown();
let playBtn = document.getElementById('play');
let stopBtn = document.getElementById('stop');
let pauseBtn = document.getElementById('pause');
playBtn.onclick = function () {
TonePlayer.setState('play');
};
stopBtn.onclick = function () {
TonePlayer.setState('stop');
let measures = currentSong.music.measures;
//RESET CHORD VIEW
viewedBlock = [];
lastChordView = -1;
for (let i = 0; i < measures.length && i < maxSize; i++) {
viewedBlock.push(measures[i]);
}
updateView(currentSong, viewedBlock);
};
pauseBtn.onclick = function () {
TonePlayer.setState('pause');
};
//ONCLICK CHANGE SONG BUTTONS
let sameKeyBtn = document.getElementById('sameKey');
let similarKeyBtn = document.getElementById('similarKey');
let targetKeyBtn = document.getElementById('targetKey');
let targetKey = document.getElementById('keys').value;
let randomKeyBtn = document.getElementById('randomKey');
let changeKey = document.getElementById('changeKey');
sameKeyBtn.onclick = function () {
//DUMMY
nextSong = SimilarSongsRandomizer.getSameKeySong(currentSong);
setNextSong();
};
similarKeyBtn.onclick = function () {
//DUMMY
nextSong = SimilarSongsRandomizer.getSimilarKeySong(currentSong);
setNextSong();
};
targetKeyBtn.onclick = function () {
//DUMMY
let targetKey = document.getElementById('keys').value;
nextSong = SimilarSongsRandomizer.getTargetKeySong(targetKey);
setNextSong();
};
randomKeyBtn.onclick = function () {
//DUMMY
nextSong = SimilarSongsRandomizer.getRandomSong();
setNextSong();
};
changeKey.onclick = function () {
let semitones;
let nextKey = document.getElementById('keys').value;
if (currentSong.key == nextKey) {
console.log('NOP');
return;
} else {
if (currentSong.key.includes('-')) {
semitones = minor.indexOf(nextKey) - minor.indexOf(currentSong.key);
} else {
semitones = noAlt.indexOf(nextKey) - noAlt.indexOf(currentSong.key);
}
}
console.log(semitones);
let chromaFlat = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
];
let chromaSharp = [
'C',
'C#',
'D',
'D#',
'E',
'F',
'F#',
'G',
'G#',
'A',
'A#',
'B',
];
let usedChroma = [];
let tempChord = [];
for (let i = 0; i < measures.length; i++) {
let tempMea = [];
for (let j = 0; j < measures[i].length; j++) {
tempMea.push(measures[i][j]);
let root = measures[i][j][0];
let hasAlt = false;
if (measures[i][j][1] == 'b' || measures[i][j][1] == '#') {
hasAlt = true;
root = root + measures[i][j][1];
}
if (currentSong.key.includes('#')) {
usedChroma = chromaSharp;
} else {
usedChroma = chromaFlat;
}
let transposed = mod(usedChroma, usedChroma.indexOf(root), semitones);
tempMea[j] = tempMea[j].replace(root, transposed);
}
tempChord.push(tempMea);
}
let transposedSong = {};
Object.assign(transposedSong, currentSong);
transposedSong.key = nextKey;
transposedSong.music.measures = tempChord;
nextSong = transposedSong;
setNextSong();
};
let nextSongVis = document.getElementById('nextSongVis');
function setNextSong() {
nextSongVis.textContent = nextSong.title + ' in ' + nextSong.key;
TonePlayer.setNextSong(nextSong);
}
//GESTIONE BLOCCO VISUALLIZATO
const maxSize = 24;
let viewedBlock = [];
let viewIndex = 0;
lastChordView = measures.length - 1;
let finalShift = 0;
for (let i = 0; i < measures.length && i < maxSize; i++) {
viewedBlock.push(measures[i]);
}
updateView(currentSong, viewedBlock);
//FUNZIONI VIEW ACCORDI
function scrollSubView() {
viewIndex = (currentMeasure + finalShift) % maxSize;
if (measures.length < maxSize) return;
viewedBlock[circularMotion(viewIndex, -1, maxSize)] =
measures[circularMotion(currentMeasure, maxSize - 1, measures.length)];
//Impostazione lastCord
if (
circularMotion(currentMeasure, maxSize - 1, measures.length) ==
measures.length - 1
) {
lastChordView = circularMotion(viewIndex, -1, maxSize);
}
if (currentMeasure == measures.length - 1) {
finalShift = ((currentMeasure % maxSize) + 1 + finalShift) % maxSize;
}
}
function circularMotion(num, addSocNum, mod) {
let ris;
//num sempre postivo, il secgno di addSocNum decice se l'operazione è una somma o una sottrazione
if (addSocNum >= 0) {
ris = num + addSocNum;
ris = ris % mod;
} else {
let opposite = addSocNum + mod;
ris = num + opposite;
ris = ris % mod;
}
return ris;
}
function setKeyDropdown() {
let dropdown = document.getElementById('keys');
for (let i = 0; i < dropdown.children.length; i++) {
dropdown.children[i].textContent = allPosKeys[i];
dropdown.children[i].value = allPosKeys[i];
}
/*if (currentSong.key.includes("-")) {
for (let i = 0; i < dropdown.children.length; i++) {
dropdown.children[i].textContent = minor[i]
dropdown.children[i].value = minor[i]
}
} else {
for (let i = 0; i < dropdown.children.length; i++) {
dropdown.children[i].textContent = noAlt[i]
dropdown.children[i].value = noAlt[i]
}
}*/
}
/*document.getElementById("onClickSubmit").onclick = function () {
let semitones
let nextKey = document.getElementById("keys").value
if (currentSong.key == nextKey) {
console.log("NOP")
return
} else {
if (currentSong.key.includes("-")) {
semitones = minor.indexOf(nextKey) - minor.indexOf(currentSong.key)
} else {
semitones = noAlt.indexOf(nextKey) - noAlt.indexOf(currentSong.key)
}
}
console.log(semitones)
let chromaFlat = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B']
let chromaSharp = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B']
let usedChroma = []
let tempChord = []
for (let i = 0; i < measures.length; i++) {
let tempMea = []
for (let j = 0; j < measures[i].length; j++) {
tempMea.push(measures[i][j])
let root = measures[i][j][0]
let hasAlt = false
if (measures[i][j][1] == 'b' || measures[i][j][1] == '#') {
hasAlt = true
root = root + measures[i][j][1]
}
if (currentSong.key.includes('#')) {
usedChroma = chromaSharp
} else {
usedChroma = chromaFlat
}
let transposed = mod(usedChroma, usedChroma.indexOf(root), semitones)
tempMea[j] = tempMea[j].replace(root, transposed)
}
tempChord.push(tempMea)
}
let transposedSong = {}
Object.assign(transposedSong, currentSong)
transposedSong.key = nextKey
transposedSong.music.measures = tempChord
nextSong = transposedSong
setNextSong()
}*/
function mod(arr, num, index) {
let sum = index + num;
if (sum >= arr.length) return arr[sum % arr.length];
else if (sum < 0) {
sum = sum + arr.length;
return arr[sum];
}
return arr[sum];
}
//Cap sul massimo di accordi suonabili, per gestire il caso di accordi tra parentesi
//ritorna delle measure tagliate in base al timeSignature
function capChords(songMeasures, ts) {
let cap = extractTimeSignature(ts);
for (let i = 0; i < songMeasures.length; i++) {
let popNumber = songMeasures[i].length - cap;
if (popNumber > 0) {
for (let j = 0; j < popNumber; j++) {
songMeasures[i].pop();
}
}
}
return songMeasures;
}
function extractTimeSignature(ts) {
let ris = ts[0];
if (ts == 12) ris = 6;
return ris;
}