-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimilarSongsRandomizer.js
225 lines (210 loc) · 5.14 KB
/
similarSongsRandomizer.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
let songsByKey = require('./songsByKey.json');
const altKeys = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
];
const longAltKeys = [
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
'C',
'Db',
'D',
'Eb',
'E',
'F',
'Gb',
'G',
'Ab',
'A',
'Bb',
'B',
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
];
exports.getFirstRandomSong = function () {
let randKey;
let randSong;
do {
randKey = Math.floor(Math.random() * altKeys.length);
//console.log("ok, randkey = " + randKey)
randSong = Math.floor(Math.random() * songsByKey[altKeys[randKey]].length);
//console.log("ok, randSong = " + randSong)
firstSong = songsByKey[altKeys[randKey]][randSong];
//console.log("ok, firstSong = " + firstSong)
//console.log("ok, title = " + firstSong.title)
//console.log("ok, firstSong = " + firstSong)
// console.log("ok, title = " + firstSong.title)
} while (firstSong == undefined);
//firstSong = songsByKey['C'][1]
//console.log(firstSong)
return firstSong;
};
exports.getSameKeySong = function (songByKey) {
let key;
let keyIndex;
//let nextSong
do {
key = songByKey.key;
//console.log("ok, key = " + key)
keyIndex = altKeys.indexOf(key);
//console.log("ok, keyIndex = " + keyIndex)
//var nextSong = songs[Math.floor(Math.random() * songs.length)];
randInKeySong = Math.floor(
Math.random() * songsByKey[altKeys[keyIndex]].length
);
//console.log("ok, RandInKeySong = " + randInKeySong)
nextSong = songsByKey[altKeys[keyIndex]][randInKeySong];
//console.log("ok, nextSong = " + nextSong)
//console.log("ok nextKey = " + nextSong.key)
//console.log("ok, title = " + nextSong.title)
// it repeats the operation untill it gets the same key
} while (nextSong == undefined);
return nextSong;
};
// Arrays of Keys
const keys = ['C', 'Db', 'D', 'Eb', 'E', 'F', 'Gb', 'G', 'Ab', 'A', 'Bb', 'B'];
// The minor key has a different order in respect to the major ones for an easier implementation
const minorKeys = [
'A-',
'Bb-',
'B-',
'C-',
'C#-',
'D-',
'Eb-',
'E-',
'F-',
'F#-',
'G-',
'G#-',
];
// with similar Key I intended: same key, correspondend major-minor, its IV or V.
exports.getSimilarKeySong = function (songByKey) {
let key;
let currentKeyIndex;
let nextSong;
do {
// I'm searching the correspondent key on the two arrays
// If its a majorkey, the minor index will result -1 and viceversa
key = songByKey.key;
//console.log("key = " + key)
// sto assumendo che l'indexOf sia un firstIndexOf
currentKeyIndex = longAltKeys.indexOf(key);
//console.log("currentKeyIndex = " + currentKeyIndex)
// let nextSong
randomCase = Math.ceil(Math.random() * 3);
//console.log("randomCase = " + randomCase)
if (randomCase == 1) {
if (currentKeyIndex < 24) {
nextKeyIndex = currentKeyIndex + 24;
} else {
nextKeyIndex = currentKeyIndex - 24;
}
} else if (randomCase == 2) {
nextKeyIndex = currentKeyIndex + 5;
} else if (randomCase == 3) {
nextKeyIndex = currentKeyIndex + 7;
}
//console.log("nextKeyIndex = " + nextKeyIndex)
simKeySong = Math.floor(
Math.random() * songsByKey[longAltKeys[nextKeyIndex]].length
);
//console.log("ok, SimKeySong = " + simKeySong)
nextSong = songsByKey[longAltKeys[nextKeyIndex]][simKeySong];
//console.log("ok, nextSong = " + nextSong)
} while (nextSong == undefined);
//console.log("ok nextKey = " + nextSong.key)
//console.log("ok, title = " + nextSong.title)
return nextSong;
};
exports.getTargetKeySong = function (key) {
let targetKeyIndex;
let nextSong;
do {
//let nextSong
targetKeyIndex = altKeys.indexOf(key);
//console.log("targetKeyIndex = " + targetKeyIndex)
targetKeySong = Math.floor(
Math.random() * songsByKey[altKeys[targetKeyIndex]].length
);
//console.log("targetKeySong = " + targetKeySong)
nextSong = songsByKey[altKeys[targetKeyIndex]][targetKeySong];
//console.log("nextSong = " + nextSong)
// console.log("ok nextKey = " + nextSong.key)
// console.log("ok, title = " + nextSong.title)
} while (nextSong == undefined);
return nextSong;
};
exports.getRandomSong = function () {
let randKey;
let randSong;
let nextSong;
do {
//let nextSong
randKey = Math.floor(Math.random() * altKeys.length);
//console.log("ok, randkey = " + randKey)
randSong = Math.floor(Math.random() * songsByKey[altKeys[randKey]].length);
//console.log("ok, randSong = " + randSong)
nextSong = songsByKey[altKeys[randKey]][randSong];
//console.log("ok, nextSong = " + nextSong)
//console.log("ok, title = " + nextSong.title)
//console.log("ok, title = " + nextSong.key)
} while (nextSong == undefined);
//firstSong = songsByKey['F'][1]
//console.log(nextSong.key)
return nextSong;
};