-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.es6
83 lines (81 loc) · 2.32 KB
/
main.es6
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
var ctx = new (window.AudioContext || window.webkitAudioContext)();
var instruments = [];
function instrumentUpdate(ins) {
instruments.push(ins);
if(play) {
stopTwo(play[0])
play = startTwo(ctx, instruments);
}
}
function instrumentRemove(ins) {
instruments = instruments.splice(instruments.indexOf(ins), 1);
if(play) {
stopTwo(play[0])
play = startTwo(ctx, instruments);
}
}
var play;
var inputElements = document.getElementsByClassName('instrument');
for(var i = 0; i < inputElements.length; i++){
if(inputElements[i].checked) {
instrumentUpdate(inputElements[i].value);
}
inputElements[i].onclick = function() {
if(this.checked) {
instrumentUpdate(this.value);
} else {
instrumentRemove(this.value);
}
};
}
play = startTwo(ctx, instruments);
function startTwo(ctx, instruments) {
var random_score = []
current_note = ["c", 4]
for(var i = 0;i < 50; i++) {
var current_note = musicLearner.next_note(current_note);
var note = new Note(current_note[0].toUpperCase()+current_note[1].toString());
random_score.push(note);
}
var node = new Waver(ctx, 32.0, function(t) {
var i = new Instrument(t);
var sum = 0;
for(var j = 0; j < instruments.length; j++) {
switch(instruments[j]) {
case "chord":
sum += i.chord(random_score[Math.floor(t%random_score.length)].freq)
break;
case "clarinet":
sum += i.clarinet(random_score[Math.floor(t%random_score.length)].freq)
break;
case "violin":
sum += i.violin(random_score[Math.floor(t%random_score.length)].freq*2)
break;
case "pluck":
sum += i.pluck(random_score[Math.floor(t%random_score.length)].freq*3/2, 0.5, 10, 10);
break;
case "drum":
sum += i.drums() + (i.sin(3) + i.sin(4));
break;
case "bass":
sum += i.bass(random_score[Math.floor(t%random_score.length)].freq%200);
break;
case "lowDrum":
sum += i.lowDrums(100) + (i.sin(2) + i.square(3));
break;
}
}
return sum;
});
var song = ctx.createBufferSource();
var synthDelay = ctx.createDelay(5.0);
song.loop = true;
song.buffer = node.sourceBuffer;
song.start();
song.connect(ctx.destination);
return [song, node];
};
function stopTwo(song) {
song.stop();
song.disconnect();
}