-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
713 lines (551 loc) · 17.6 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
// Set default melodies
var MELODY1 = presetMelodies['MELODY1'];
var MELODY2 = presetMelodies['MELODY2'];
// Set variables
let synth;
let size;
let startButton;
let hasStarted = false;
let verticalDiv;
let horizontalDiv;
let tileSize;
let discSize;
const numInterpolations = 8;
// Interpolated melodies
let interpolatedMelodies;
let generatedMelody1 = [];
let generatedMelody2 = [];
// Number of disc to play music
const numDiscs = 4;
// Start array of discs and tiles
let discs = [];
let tiles = [];
// Sampler
let xSamplers = [];
let pSamplers = [];
let samplerPattern;
let samplerParts = [];
let reverbs = [];
function setup() {
// Position of settings button
let settingsIcon = createElement('i');
settingsIcon.addClass('fa fa-cog fa-2x');
settingsIcon.position(10, 40);
settingsIcon.id('modalBtn');
// Start the samplers
for (let i = 0; i < numDiscs; i++) {
pSamplers[i] = new Tone.Sampler({
urls: {
A0: "A0.mp3",
C1: "C1.mp3",
"D#1": "Ds1.mp3",
"F#1": "Fs1.mp3",
A1: "A1.mp3",
C2: "C2.mp3",
"D#2": "Ds2.mp3",
"F#2": "Fs2.mp3",
A2: "A2.mp3",
C3: "C3.mp3",
"D#3": "Ds3.mp3",
"F#3": "Fs3.mp3",
A3: "A3.mp3",
C4: "C4.mp3",
"D#4": "Ds4.mp3",
"F#4": "Fs4.mp3",
A4: "A4.mp3",
C5: "C5.mp3",
"D#5": "Ds5.mp3",
"F#5": "Fs5.mp3",
A5: "A5.mp3",
C6: "C6.mp3",
"D#6": "Ds6.mp3",
"F#6": "Fs6.mp3",
A6: "A6.mp3",
C7: "C7.mp3",
"D#7": "Ds7.mp3",
"F#7": "Fs7.mp3",
A7: "A7.mp3",
C8: "C8.mp3"
},
release: 1,
baseUrl: "https://tonejs.github.io/audio/salamander/"
}).toDestination();
// Create Xylphone Sampler of the melody
xSamplers[i] = new Tone.Sampler({
urls: {
'A6': 'A.wav'
},
volume: -20
});
// Add Sampler to destination
reverbs[i] = new Tone.Reverb({decay: 3, wet: 0.1}).toDestination();
xSamplers[i].connect(reverbs[i]);
xSamplers[i].toDestination();
}
createCanvas(windowWidth, windowHeight);
// Div for vertical and horizontal div of the page
verticalDiv = windowHeight / 10;
horizontalDiv = windowWidth / 10;
// setup slider
slider = createSlider(60, 140, 98);
slider.position(width/2 - 40, 40);
slider.style('width', '80px');
slider.input(updateBPM);
// Calculate the tile and disc sizes
tileSize = windowHeight / 5;
discSize = windowWidth / 6;
background(0);
// Button to control start/stop of sound and setting of the app
startButton = createButton('Start');
startButton.position(windowWidth / 2, windowHeight - 2 * verticalDiv);
startButton.mousePressed(startPedal);
size = 1024;
// Synth for the pedal with filter and connect to wave form
pedal = new Tone.PolySynth({
volume: -20
});
let pedalFilter = new Tone.Filter({type: 'lowpass', frequency: 800});
waveForm = new Tone.Waveform(size);
pedal.connect(waveForm);
waveForm.connect(pedalFilter);
pedalFilter.toDestination();
strokeWeight(0.2);
// Space between discs
let spaceDiscs = 10;
// Draw the discs
for (let i = 0; i < numDiscs; i++) {
let x = verticalDiv * 5 + i * (discSize + spaceDiscs);
let y = horizontalDiv * 2;
let r = discSize;
// Add random angles to draw the notes in the discs
let randomAngleX = Math.floor(Math.random() * (12 - 7)) + 6;
let randomAngleY = Math.floor(Math.random() * (12 - 7)) + 6;
// Create disc and add it to array
let d = new Disc(x, y, r, randomAngleX, randomAngleY);
discs.push(d);
}
// Variable to control left or right tiles
let side = 0;
// Draw the tiles
for (let i = 0; i < numInterpolations; i++) {
// See if left or right side
if (i >= numInterpolations / 2) {
side = -1
} else {
side = 1
}
let x = ((width - tileSize) * Math.floor(i/4)) + ((horizontalDiv / 2) * side);
let y = 30 + (tileSize * (i % 4));
let w = tileSize;
let h = tileSize;
// Create tile and add it to array
let t = new Tile(x, y, w, h);
tiles.push(t);
}
// Create parts to each disc using the same sampler
for (let i = 0; i < discs.length; i++) {
if (discs[i].instrument == 'Piano') {
samplerParts[i] = new Tone.Part((time, note) => {
pSamplers[i].triggerAttackRelease(note, '2n', time);
}, samplerPattern).start();
samplerParts[i].loop = true;
samplerParts[i].loopStart = 0;
samplerParts[i].loopEnd = '2m';
} else {
samplerParts[i] = new Tone.Part((time, note) => {
xSamplers[i].triggerAttackRelease(note, '2n', time);
}, samplerPattern).start();
samplerParts[i].loop = true;
samplerParts[i].loopStart = 0;
samplerParts[i].loopEnd = '2m';
}
}
}
function updateBPM() {
Tone.Transport.bpm.value = slider.value();
}
function draw() {
// Just paint the lines
background(0);
// Text for the slider
push();
fill(255);
textSize(12);
textFont('Roboto Mono');
text('BPM: ' + slider.value(), width/2 - 20, 30);
pop();
// Draw pedal wave
const waveArray = waveForm.getValue();
const bandSize = (width - 2 * horizontalDiv) / size;
// Draw wave form of the pedal
beginShape();
// Color of the wave form
fill(255, 10, 10);
for (let i = 0; i < waveArray.length; i++) {
curveVertex(bandSize * i + horizontalDiv, map(waveArray[i], -1, 1, height, 0) + 4 * verticalDiv);
}
endShape();
// Variable to control left or right tiles
let side = 0;
// Show created tiles
push();
for (let i = 0; i < tiles.length; i++) {
// Draw the tiles
tiles[i].show()
// Draw the note when ready
if (interpolatedMelodies) {
tiles[i].drawNotes(interpolatedMelodies[i].notes);
}
// If mouse is over, change color and dragged when pressed
if (tiles[i].contains(mouseX, mouseY)) {
tiles[i].changeStyle(0, 3, tileSize + 1, tileSize + 4);
tiles[i].update(mouseX, mouseY);
} else {
tiles[i].changeStyle(100, 2, tileSize, tileSize);
}
}
pop();
// Show created discs
push();
for (let i = 0; i < discs.length; i++) {
discs[i].show();
// Check if tile is in the inner circle
for (let j = 0; j < tiles.length; j++) {
if (discs[i].contains(tiles[j].x + tileSize / 2, tiles[j].y + tileSize / 2)) {
discs[i].activate();
break;
} else {
if (!discs[i].releasedInside) {
discs[i].deactivate();
}
}
}
if (discs[i].isActive == true) {
discs[i].drawNotes(discs[i].notes, Tone.Transport.getTicksAtTime());
}
}
pop();
}
function mousePressed() {
// Check if mouse was pressed over the tile so it can start dragging
for (let i = 0; i < tiles.length; i++) {
tiles[i].pressed(mouseX, mouseY);
}
// Check if mouse was pressed over the center of the tile, reverse melody
for (let j = 0; j < discs.length; j++) {
if (discs[j].containsInner(mouseX, mouseY)) {
// Create a copy of the array
var reverseNotes = JSON.parse(JSON.stringify(discs[j].notes)).reverse();
let startStep = 0;
// Reverse the order of the melody
for (note of reverseNotes) {
duration = note.quantizedEndStep - note.quantizedStartStep;
note.quantizedStartStep = startStep;
note.quantizedEndStep = startStep + duration;
startStep = note.quantizedEndStep;
}
discs[j].notes = reverseNotes;
samplerParts[j].clear();
// Add new melody to Tone
for (let note of discs[j].notes) {
let noteName = Tone.Frequency(note.pitch, 'midi').toNote();
samplerParts[j].at(
{'16n': note.quantizedStartStep},
noteName
);
}
}
}
}
function mouseReleased() {
for (let j = 0; j < tiles.length; j++) {
for (let i = 0; i < discs.length; i++) {
if (discs[i].contains(tiles[j].x + tileSize / 2, tiles[j].y + tileSize / 2)) {
discs[i].releasedInside = true;
discs[i].notes = tiles[j].notes;
for (let note of tiles[j].notes) {
let noteName = Tone.Frequency(note.pitch, 'midi').toNote();
samplerParts[i].at(
{'16n': note.quantizedStartStep},
noteName
);
}
}
}
// See if left or right side
if (j >= numInterpolations / 2) {
side = -1
} else {
side = 1
}
// Send it back to its original position
tiles[j].x = ((width - tileSize) * Math.floor(j/4)) + ((horizontalDiv / 2) * side);
tiles[j].y = 30 + (tileSize * (j % 4));
tiles[j].notPressed();
}
}
function doubleClicked() {
for (let i = 0; i < discs.length; i++) {
if (discs[i].contains(mouseX, mouseY)) {
discs[i].deactivate();
samplerParts[i].clear();
}
}
}
function mouseWheel(event) {
let delta = event.delta;
let mVolume = 0;
for (let i = 0; i < discs.length; i++) {
if (discs[i].contains(mouseX, mouseY)) {
mVolume += (delta / 60);
if (mVolume < -60) {
mVolume = -60;
} else if (mVolume > 0) {
mVolume = 0;
}
discs[i].volume = mVolume;
if (discs[i].instrument == 'Piano') {
pSamplers[i].volume.value = discs[i].volume;
} else {
xSamplers[i].volume.value = discs[i].volume;
}
}
}
}
async function startPedal() {
hasStarted = !hasStarted;
if (hasStarted) {
// Change label of the button
startButton.html('Stop');
// Start Tone
await Tone.start();
pedal.triggerAttack(['C1', 'C2', 'G2']);
Tone.Transport.start();
} else {
// Change label of the button
startButton.html('Start');
pedal.releaseAll();
Tone.Transport.stop();
}
}
// The checkpoint url that contains the training data
let checkPoint = 'https://storage.googleapis.com/magentadata/js/checkpoints/music_vae/mel_2bar_small';
let melodiesVae = new music_vae.MusicVAE(checkPoint);
let melodiesVaeLoaded = melodiesVae.initialize();
// Interpolate the given melodies
async function interpolateMelodies(callback) {
await melodiesVaeLoaded;
let melody1;
let melody2;
// check if using default melodies
if (generatedMelody1.length === 0 && generatedMelody2.length === 0) {
melody1 = presetMelodies['MELODY1'];
melody2 = presetMelodies['MELODY2'];
} else if (generatedMelody2.length === 0) {
melody1 = generatedMelody1;
melody2 = presetMelodies['MELODY2'];
} else if (generatedMelody1.length === 0) {
melody1 = presetMelodies['MELODY1'];
melody2 = generatedMelody2;
} else {
melody1 = generatedMelody1;
melody2 = generatedMelody2;
}
// Interpolate melodies using MusicVAE
interpolatedMelodies = await melodiesVae.interpolate([melody1, melody2], numInterpolations);
// Callback
callback();
}
function melodiesReady() {
let bStart = document.getElementById('progress');
bStart.innerHTML = 'START';
bStart.style.cursor = 'pointer';
bStart.addEventListener('click', () => {
document.getElementById('loading').style.display = 'none';
})
}
interpolateMelodies(melodiesReady);
// If user does not want to use default melodies, magenta generates them
let checkPointRnn = 'https://storage.googleapis.com/magentadata/js/checkpoints/music_rnn/chord_pitches_improv'
let melodyRnn = new music_rnn.MusicRNN(checkPointRnn);
let melodyRnnLoaded = melodyRnn.initialize();
async function generateMelody(note, duration, temperature, callback) {
await melodyRnnLoaded;
let seed = {
notes: [
{pitch: Tone.Frequency(note).toMidi(), quantizedStartStep: 0, quantizedEndStep: duration}
],
totalQuantizedSteps: duration,
quantizationInfo: {stepsPerQuarter: 4}
};
let steps = 32 - duration;
let chordProgression = ['Cm7'];
let result = await melodyRnn.continueSequence(seed, steps, temperature, chordProgression);
let combined = core.sequences.concatenate([seed, result]);
callback();
console.log(combined);
return combined;
}
function generatingMelodies() {
document.getElementById('modalSendBtn').disabled = false;
}
// Callback function to use for waiting the response of settings
function melodiesPreparing() {
if (melodyPrep.classList.contains('melodyPrepClassActive')) {
melodyPrep.classList.remove('melodyPrepClassActive');
melodyPrep.classList.add('melodyPrepClass');
};
let seedOptions = document.getElementById('seedOptions1');
let seedOptions2 = document.getElementById('seedOptions2');
if (seedOptions.classList.contains('optionsShow')) {
seedOptions.classList.remove('optionsShow');
seedOptions.classList.add('optionsHide');
};
if (seedOptions2.classList.contains('optionsShow')) {
seedOptions2.classList.remove('optionsShow');
seedOptions2.classList.add('optionsHide');
};
}
// Javascript to handel the modal -> setting changed by the user
document.addEventListener('DOMContentLoaded', () => {
// Get modal div
var modal = document.getElementById("settingModal");
// Add event listener when button is clicked
let modalSendBtn = document.getElementById('modalSendBtn');
// Get selector of melodyes
var melody1Selector = document.getElementById('melody1');
var melody2Selector = document.getElementById('melody2');
melody1Selector.addEventListener('change', (event) => {
if (event.target.value == 'rnn') {
let seedOptions = document.getElementById('seedOptions1');
if (seedOptions.classList.contains('optionsHide')) {
seedOptions.classList.remove('optionsHide');
seedOptions.classList.add('optionsShow');
}
let mNote = document.getElementById('pitch1').value;
let mTemperature = parseFloat(document.getElementById('temperature1').value);
let mDuration = parseInt(document.getElementById('duration1').value);
modalSendBtn.disabled = true;
generateMelody(mNote, mDuration, mTemperature, generatingMelodies).then((result) => {
generatedMelody1 = result;
});
}
});
melody2Selector.addEventListener('change', (event) => {
if (event.target.value == 'rnn') {
let seedOptions = document.getElementById('seedOptions2');
if (seedOptions.classList.contains('optionsHide')) {
seedOptions.classList.remove('optionsHide');
seedOptions.classList.add('optionsShow');
}
let mNote = document.getElementById('pitch2').value;
let mTemperature = parseFloat(document.getElementById('temperature2').value);
let mDuration = parseInt(document.getElementById('duration2').value);
modalSendBtn.disabled = true;
generateMelody(mNote, mDuration, mTemperature, generatingMelodies).then((result) => {
generatedMelody2 = result;
});
}
});
// Add event listeners to change also in the seed options
let mNotes = [];
let mTemperatures = [];
let mDurations = [];
for (let i = 1; i < 3; i++) {
// Get the elements
mTemperatures[i] = document.getElementById('temperature' + i);
mNotes[i] = document.getElementById('pitch' + i);
mDurations[i] = document.getElementById('duration' + i);
// Add listeners to each event and generate melodies
mTemperatures[i].addEventListener('change', (event) => {
modalSendBtn.disabled = true;
console.log(mNotes[i].value, parseInt(mDurations[i].value), parseFloat(mTemperatures[i].value));
generateMelody(mNotes[i].value, parseInt(mDurations[i].value), parseFloat(mTemperatures[i].value), generatingMelodies).then((result) => {
if (i === 1) {
generatedMelody1 = result;
} else {
generatedMelody2 = result;
}
});
});
mNotes[i].addEventListener('change', (event) => {
modalSendBtn.disabled = true;
generateMelody(mNotes[i].value, parseInt(mDurations[i].value), parseFloat(mTemperatures[i].value), generatingMelodies).then((result) => {
if (i === 1) {
generatedMelody1 = result;
} else {
generatedMelody2 = result;
}
});
});
mDurations[i].addEventListener('change', (event) => {
modalSendBtn.disabled = true;
generateMelody(mNotes[i].value, parseInt(mDurations[i].value), parseFloat(mTemperatures[i].value), generatingMelodies).then((result) => {
if (i === 1) {
generatedMelody1 = result;
} else {
generatedMelody2 = result;
}
});
});
}
// When button is clicked show loading message and process the request
modalSendBtn.addEventListener('mousedown', () => {
let melodyPrep = document.getElementById('melodyPrep');
document.body.className = 'waiting';
if (melodyPrep.classList.contains('melodyPrepClass')) {
melodyPrep.classList.remove('melodyPrepClass');
melodyPrep.classList.add('melodyPrepClassActive');
}
});
modalSendBtn.addEventListener('click', () => {
// Clear old melodies if using the defaults
if (document.getElementById('melody1').value == 'default') {
generatedMelody1 = [];
}
if (document.getElementById('melody2').value == 'default') {
generatedMelody2 = [];
}
// Loop over the values assigned to the discs
for (let i = 1; i < discs.length + 1; i++) {
discs[i - 1].volume = document.getElementById('volumeDisc' + i).value;
discs[i - 1].instrument = document.getElementById('instrument' + i).value;
discs[i - 1].deactivate();
samplerParts[i - 1].clear();
// Update instruments and volume of the discs
if (discs[i - 1].instrument == 'Piano') {
pSamplers[i - 1].volume.value = discs[i - 1].volume;
samplerParts[i - 1] = new Tone.Part((time, note) => {
pSamplers[i - 1].triggerAttackRelease(note, '2n', time);
}, samplerPattern).start();
samplerParts[i - 1].loop = true;
samplerParts[i - 1].loopStart = 0;
samplerParts[i - 1].loopEnd = '2m';
} else {
xSamplers[i - 1].volume.value = discs[i - 1].volume;
samplerParts[i - 1] = new Tone.Part((time, note) => {
xSamplers[i - 1].triggerAttackRelease(note, '2n', time);
}, samplerPattern).start();
samplerParts[i - 1].loop = true;
samplerParts[i - 1].loopStart = 0;
samplerParts[i - 1].loopEnd = '2m';
}
}
callInterpolate();
melody1Selector.value = 'default';
melody2Selector.value = 'default';
for (let i = 1; i < 3; i++) {
document.getElementById('temperature' + i).value = '0.9';
};
document.getElementById('duration1').value = '4';
document.getElementById('duration2').value = '2';
document.getElementById('pitch1').value = 'C4';
document.getElementById('pitch2').value = 'G5';
});
function callInterpolate() {
let result = interpolateMelodies(melodiesPreparing).then(() => {
document.body.className = '';
modal.style.display = 'none';
});
};
});