-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
82 lines (65 loc) · 2.47 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
var hasRhythm = false;
var hasMelody = false;
var rhythmString = null;
var synthController = new ABCJS.synth.SynthController();
function onClickRhythmButton(event) {
const tonic = document.getElementById("tonic").value;
const scale = document.getElementById("scale").value;
const headers = Generator.generateAbcHeaders(tonic, scale, 120);
const rhythmTonic = Generator.generateRandomRhythmWithTonic(tonic, scale);
loadNewABC(headers + rhythmTonic);
hasRhythm = true;
hasMelody = false;
rhythmString = rhythmTonic;
}
function onClickMelodyButton(event) {
const tonic = document.getElementById("tonic").value;
const scale = document.getElementById("scale").value;
if (hasRhythm === false) {
rhythmString = Generator.generateRandomRhythmWithTonic(tonic, scale);
hasRhythm = true;
}
melodyString = Generator.generateRandomMelody(rhythmString, tonic, scale);
headers = Generator.generateAbcHeaders(tonic, scale, 120);
loadNewABC(headers + melodyString);
hasMelody = true;
}
function loadNewABC(abcString) {
synthController.restart();
const cursorControl = { /* nani? */ };
const abcOptions = { add_classes: true };
const audioParams = { chordsOff: true };
const visualObj = ABCJS.renderAbc("sheetmusic", abcString, abcOptions);
if (!ABCJS.synth.supportsAudio()) {
document.querySelector("#midiplayer").innerHTML = "Audio is not supported in this browser.";
return;
}
synthController.load("#midiplayer",
cursorControl,
{
displayLoop: true,
displayRestart: true,
displayPlay: true,
displayProgress: true,
displayWarp: true
}
);
const createSynth = new ABCJS.synth.CreateSynth();
createSynth.init({ visualObj: visualObj[0] })
.then(() => {
synthController.setTune(visualObj[0], false, audioParams)
.then(() => {
console.log("Audio successfully loaded.")
}).catch((error) => {
console.warn("Audio problem:", error);
});
}).catch((error) => {
console.warn("Audio problem:", error);
});
}
(() => {
const rhythmButton = document.getElementById("rhythmButton");
rhythmButton.addEventListener("click", onClickRhythmButton);
const melodyButton = document.getElementById("melodyButton");
melodyButton.addEventListener("click", onClickMelodyButton);
})()