-
Notifications
You must be signed in to change notification settings - Fork 0
/
midiseq.js
413 lines (304 loc) · 9.28 KB
/
midiseq.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
var isOn;
var count=0;
var tempo = document.getElementById('tempo');
var checkbox = document.getElementById('play');
var mapRange = function(s, from, to) {
return to[0] + (s - from[0]) * (to[1] - to[0]) / (from[1] - from[0]);
};
checkbox.addEventListener('change',async function() {
if (this.checked) {
await Tone.start()
isOn = true;
} else {
Tone.stop;
}
});
const refreshRate = 1000 / 64;
var index=0;
document.getElementById('play').checked = false;
document.querySelector('play')?.addEventListener('checked', async () => {
await Tone.start()
console.log('audio is ready')
})
var notes =['C4','C#4','D4','D#4','E4','F4','F#4','G4','G#4','A4', 'A#4', "B4","rest"];
var notesB =['C1','C#1','D1','D#1','E1','F1','F#1','G1','G#1','A1', 'A#1', "B1"];
var notesD =['C1','D1','D#1','F#2'];
var notesToPlay = [];
var notesToPlayB = [];
var notesToPlayK = [];
var notesToPlayS = [];
var notesToPlayH = [];
var notesToPlayC = [];
const reverb = new Tone.Reverb({
"wet": .5,
"decay": 2.5,
"preDelay": 0.01
}).toDestination();
const filterLead = new Tone.Filter(2000, "lowpass").connect(reverb);
const filterBass = new Tone.Filter(500, "lowpass").toDestination();
const filterSnare = new Tone.Filter(3500, "bandpass").toDestination();
const filterClap = new Tone.Filter(5500, "bandpass").connect(reverb);
const filterHH = new Tone.Filter(6500, "highpass").toDestination();
const synth = new Tone.Synth({
"oscillator": {
"type": "sawtooth"
}
}).connect(filterLead);
synth.volume.value =-5;
const synthB = new Tone.Synth({
"oscillator": {
"type": "sawtooth"
}
}).connect(filterBass);
synthB.volume.value =-5;
const synthK = new Tone.Synth({
"oscillator": {
"type": "sine"
}
}).toDestination();
const synthSTone = new Tone.Synth({
"oscillator": {
"type": "sine"
}
}).connect(filterSnare);
synthSTone.volume.value =-2;
const synthSN = new Tone.NoiseSynth().connect(filterSnare);
const synthC = new Tone.NoiseSynth().connect(filterClap);
const synthH = new Tone.NoiseSynth().connect(filterHH);
var selected = document.getElementById('div'+index);
var selectedB = document.getElementById('divB'+index);
var selectedD = document.getElementById('divD'+index);
function printBtn() {
for (var i = 0; i < 16; i++) {
let div = document.createElement("div")
div.setAttribute("style", "display: block;width:300px;");
div.setAttribute("id","div"+i);
document.body.appendChild(div);
for (var j = 0; j < 12; j++) {
var btn = document.createElement("input");
btn.setAttribute("type", "radio");
btn.setAttribute("name", "group"+i);
btn.setAttribute("class", "btn");
btn.setAttribute("id","btn"+j);
btn.setAttribute("value",notes[j]);
// if(j===12){
// btn.setAttribute("checked",true);
// }
div.appendChild(btn);
}
}
}
function printBtnB() {
let br = document.createElement("br");
document.body.appendChild(br);
for (var i = 0; i < 16; i++) {
let divB = document.createElement("div")
divB.setAttribute("style", "display: block;width:270px;");
divB.setAttribute("id","divB"+i);
document.body.appendChild(divB);
for (var j = 0; j < 12; j++) {
var btnB = document.createElement("input");
btnB.setAttribute("type", "radio");
btnB.setAttribute("name", "groupB"+i);
btnB.setAttribute("class", "btnB");
btnB.setAttribute("id","btnB"+j);
btnB.setAttribute("value",notesB[j]);
divB.appendChild(btnB);
}
}
}
function printBtnD() {
let brB = document.createElement("br");
document.body.appendChild(brB);
for (var i = 0; i < 16; i++) {
let divD = document.createElement("div")
divD.setAttribute("style", "display: block;width:270px;");
divD.setAttribute("id","divD"+i);
document.body.appendChild(divD);
for (var j = 0; j < 4; j++) {
var btnD = document.createElement("input");
btnD.setAttribute("type", "checkbox");
btnD.setAttribute("name", "groupD"+i);
btnD.setAttribute("class", "btnD");
btnD.setAttribute("id","btnD"+j);
btnD.setAttribute("value",notesD[j]);
divD.appendChild(btnD);
}
}
}
function updateSeq(){
var selected = document.getElementById('div'+index);
selected.style.backgroundColor = "red";
selected.style.backgroundColor = "white";
for (let i = 0; i < selected.children.length; i++) {
if(selected.children[i].checked){
notesToPlay.unshift(selected.children[i].value);
console.log(notesToPlay);
}
}
}
function updateSeqB(){
var selectedB = document.getElementById('divB'+index);
selectedB.style.backgroundColor = "red";
selectedB.style.backgroundColor = "white";
for (let i = 0; i < selectedB.children.length; i++) {
if(selectedB.children[i].checked){
notesToPlayB.unshift(selectedB.children[i].value);
}
}
}
function updateSeqD(){
var selectedD = document.getElementById('divD'+index);
selectedD.style.backgroundColor = "red";
selectedD.style.backgroundColor = "white";
if(selectedD.children[0].checked){
notesToPlayK.unshift(selectedD.children[0].value);
}
if(selectedD.children[1].checked){
notesToPlayS.unshift(selectedD.children[1].value);
}
if(selectedD.children[2].checked){
notesToPlayC.unshift(selectedD.children[2].value);
}
if(selectedD.children[3].checked){
notesToPlayH.unshift(selectedD.children[3].value);
}
console.log(notesToPlayH);
}
var play = document.querySelector('input[id="play"]');
play.addEventListener('change', () => {
if(play.checked) {
isOn = true;
} else {
isOn = false;
}
});
setInterval(() => {
count++;
// console.log(count);
if(count > document.getElementById('tempo').value){
count = 0;
if(isOn===true){
Tone.start();
updateSeq();
updateSeqB();
updateSeqD();
index++;
var prevIndex = index -1;
if(notesToPlay.length === 1){
synth.triggerAttackRelease(notesToPlay[0],'64n');
}
if(notesToPlayB.length === 1){
synthB.triggerAttackRelease(notesToPlayB[0],'64n');
}
if(notesToPlayK.length === 1){
synthK.triggerAttackRelease(notesToPlayK[0],'64n');
synthK.frequency.rampTo(50,.01);
}
if(notesToPlayS.length === 1){
synthSTone.triggerAttackRelease("500hz",'64n');
synthSTone.frequency.rampTo(100,.002);
synthSN.triggerAttackRelease(.001);
}
if(notesToPlayC.length === 1){
synthC.triggerAttackRelease(.001);
}
if(notesToPlayH.length === 1){
synthH.triggerAttackRelease(.001);
}
if(index>=16){
index = 0;
}
if(index != prevIndex){
notesToPlay.length = 0;
notesToPlayB.length = 0;
notesToPlayK.length = 0;
notesToPlayS.length = 0;
notesToPlayC.length = 0;
notesToPlayH.length = 0;
var selected = document.getElementById('div'+index);
selected.style.backgroundColor = "red";
var selectedB = document.getElementById('divB'+index);
selectedB.style.backgroundColor = "red";
var selectedD = document.getElementById('divD'+index);
selectedD.style.backgroundColor = "red";
}
}}}
,refreshRate);
console.log(index);
function makeFile(){
isOn = false;
let leadDist=[];
let leadNotes=[];
let leadTime=[];
var timeSumMap = [];
let bassNotes=[];
let kickNotes=[];
let snareNotes=[];
let clapNotes=[];
let hhNotes=[];
var selectedDiv=[];
var time = 0;
var ptimeA =0;
var timeSum = 0;
var sum =0;
for (let i = 0; i < 16; i++) {
selectedDiv.push(document.getElementById('div'+i));
for (let j = 0; j < 12; j++) {
if(selectedDiv[i].children[j].checked===true){
leadNotes.push(selectedDiv[i].children[j].value);
}
if(selectedDiv[i].children[j].checked!=true){
console.log(time);
time=time+1;
// leadTime.push[time];
}else{
ptimeA= time;
leadDist.push(ptimeA);
time = 0;
}
}
}
for (let j = 0; j < leadDist.length; j++){
sum= sum + leadDist[j];
}
// leadDist.push(192-sum);
// leadDist.shift();
for (let j = 0; j < leadDist.length; j++){
timeSum= timeSum + leadDist[j];
leadTime.push(timeSum);
timeSumMap.push(Math.round(leadTime[j]/12)*(j*4));
}
// create a new midi file
var midi = new Midi();
// add a track
const track = midi.addTrack(
{
channel:1
}
);
const trackB = midi.addTrack(
{
channel:2
}
);
const trackC = midi.addTrack( {
channel:10
});
console.log(selectedDiv);
console.log(leadNotes);
console.log(leadDist);
var pos =0;
for (let i = 0; i < 16; i++) {
// if(leadNotes[i]==="rest"){
// }
// track.addNote({
// name : leadNotes[i],
// time : i,
// duration: 0.2
// })
}
console.log(leadTime);
console.log(timeSumMap); // 6
// fs.writeFileSync("output.mid", new Buffer(midi.toArray()))
}