-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (54 loc) · 1.75 KB
/
main.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
// Set Variables
const KEYS = ["Random", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"];
const GENERATOR = new ChordGenerator();
const SYNTH = new Tone.PolySynth(Tone.Synth).toDestination();
let lastProgression;
async function playTones(progression)
{
for (let i = 0; i < progression.length; ++i)
{
SYNTH.triggerAttackRelease(progression[i], "4n", Tone.now() + Tone.Time("4n").toSeconds() * i);
}
}
// Generate on click
$("#generatebutton").click(() =>
{
// get values
const isMajor = $("#majorminorrange").val() == 0;
const key = $("#keyoutput").text();
// get progression and set last progression
const progression = GENERATOR.makeProgression(isMajor, key);
lastProgression = GENERATOR.convertProgressionToSynthChords(progression);
// set text
$("#screen h1").text(progression.join(" "));
// play tones
playTones(lastProgression);
});
// Replay on click
$("#replaybutton").click(() =>
{
if (lastProgression) playTones(lastProgression);
});
// Set text of label for majorminorrange slider on change
$("#majorminorrange").on("input", () =>
{
const value = ($("#majorminorrange").val() == 0) ? "Major" : "Minor";
$("#majorminoroutput").text(value);
// if major, convert to minor if not already.
// if minor, convert to major if not already.
const note = $("#keyoutput").text();
if (value == "Major" && !GENERATOR.isNoteMajor(note))
{
$("#keyoutput").text(GENERATOR.convertMinorToMajor(note));
}
else if (value == "Minor" && GENERATOR.isNoteMajor(note))
{
$("#keyoutput").text(GENERATOR.convertMajorToMinor(note));
}
});
// Set text of label for key slider on change
$("#keyrange").on("input", () =>
{
const isMajor = $("#majorminorrange").val() == 0;
$("#keyoutput").text(KEYS[$("#keyrange").val()] + (isMajor ? "" : "m"));
});