-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
executable file
·85 lines (73 loc) · 1.98 KB
/
content.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
// [min, max)
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
var NOTES = [
'C',
'C♯/D♭',
'D',
'D♯/E♭',
'E',
'F',
'F♯/G♭',
'G',
'G♯/A♭',
'A',
'A♯/B♭',
'B'
];
var MOVEMENT = [
'↗',
'↘',
'→'
];
function generateMotiv(num_notes, movement) {
if (!num_notes) {
return [false, "You need to specify a number of notes to be generated!"];
}
var result = "";
var prev_note_index = null;
for (var i = 0; i < num_notes; i++) {
var note_index = getRandomInt(0, NOTES.length);
if (i > 0) {
if (movement) {
result += " ";
var is_same_note = prev_note_index === note_index ? 0 : 1;
var movement_index = getRandomInt(0, MOVEMENT.length - is_same_note);
result += MOVEMENT[movement_index];
result += " ";
} else {
result += ", ";
}
}
prev_note_index = note_index;
result += NOTES[note_index];
}
return [true, result];
} // function generateMotiv(num_notes, movement)
function selectStyle(styles) {
if (!styles) {
return [false, ""];
}
styles = styles.split('\n');
var style_index = getRandomInt(0, styles.length);
return [true, styles[style_index]];
} // function selectStyle(styles)
function generate() {
var num_notes = document.getElementById("num_notes").value;
var movement = document.getElementById("movement").checked;
var motiv_result = generateMotiv(num_notes, movement);
if (!motiv_result[0]){
document.getElementById("result").value = motiv_result[1];
return;
}
var result = motiv_result[1];
var styles = document.getElementById("styles").value;
var style_result = selectStyle(styles);
if (style_result[0]){
result += " (style: " + style_result[1] + ")";
}
document.getElementById("result").value = result;
} // function generate()