-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
717 lines (325 loc) · 15.6 KB
/
app.js
File metadata and controls
717 lines (325 loc) · 15.6 KB
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
714
715
716
717
// We want to create a loop through each pad when i press the play button
// When we loop each pad needs to have a scale(1)
class drumPad{
constructor(){
// Declare all pads and the play button -
// Now we want to click play button and select in the same time each of the pad -
// When we loop over the pads we want to add a scale(1) -
// Click button will execute the instructions from the next functions-
// Function 1 will execute the repeat functionality for each pad-
// Function 2 will start the first Function and give it a functionality on how fast it can repeat each pad-
// Stop functionality -
// Declare the stop button -
// When the stop button is clicked the interval will be cleared and the color of each pad will be removed -
// When each option from the select dropdown is clicked, it would select our desired source audios based on the values-
// To change the tempo, it needs to input with the slider a value that controls the rythm and output the value into the bpm slider
// Declare bpm slider-
// Create a function that updates the bpm output and based on that update the input value-
// Create a function that updates that bpm value once the click on the slider has been released, this allows us to just update the input value not starting the beatmaker once we change the value-
// Inside the last function we declared the play btn to start the function once the button has been activated-
// Event listeners were put on the bpm slider once we input a value with the slider and once when we release the click to update the bpm property-
// Make the button to be active when the input value has been updated in order to start the beatmaker with the new tempo-
// Create save project functionality - this allows us to save the colors of the drumpads and the sounds that are already activated
// When a pad is active, he has a number on it and that number is stored into an array that is saved into the local storage
this.pad = document.querySelectorAll('.pad');
this.playBtn = document.querySelector('.play-btn');
this.index = 0;
this.bpm = 150;
this.bpmInput = document.querySelector('.bpm-interval');
this.isPlaying = null;
this.stopBtn = document.querySelector('.mute-btn');
this.eraseBtn = document.querySelector('.erase-btn');
this.saveBtn = document.querySelector('.save-button');
this.loadBtn = document.querySelector('.load-button');
this.newBtn = document.querySelector('.new-button');
this.kickAudio = document.querySelector('.kick-sound');
this.snareAudio = document.querySelector('.snare-sound');
this.hihatAudio = document.querySelector('.hihat-sound');
this.selectAudio = document.querySelectorAll('.drum-tonalities');
this.popupContainer = document.querySelector('.popup-background');
this.loadMenu = document.querySelector('.load-container');
this.closeLib = document.querySelector('.close-container');
// Project menu and background
this.backMenu = document.querySelector('.project-menu-background');
this.inputMenu = document.querySelector('.project-menu');
this.closeProj = document.querySelector('.close-button');
this.setsName = document.querySelector('.drumpads-input');
this.submitBtn = document.querySelector('.input-sets');
this.savedPads = [];
}
// Function that selects the coloured pads-
// This function can input all the active pads already coloured and with the desired sound into the local storage-
// We need to save drumpads order by giving to every coloured pad a number of its position-
// Input method
activePad(e){
this.classList.toggle('active');
this.classList.toggle('saved');
const padNr = e.target.classList[2];
const name = e.target.classList[0];
const state = e.target.classList[4];
const padObj = {name, state, nr: padNr};
// Save all the pads with only the class of active
if(e.target.classList.contains('saved')){
beatMaker.savedPads.push(padObj);
}else{
// Push the objects without state property and they will no longer be readable
// When those pads are deactivated, make them colorful only if two properties are existent
// Avoid dupliicates
beatMaker.savedPads.push(padObj);
// const newArr = beatMaker.savedPads.filter((object) => {
// object !== object.state;
// });
// console.log(newArr);
}
// const newArr = beatMaker.savedPads.indexOf((object) => {
// clearContainer.push(padObj);
// return object.state === undefined;
// });
// console.log(newArr);
}
// Save method
saveToLocal(padObj){
// We need to add a statement that says:
// Introduce them into an array
// Create an object and then introduce him into the array everytime we click on save
let save;
// // // Check if it is something in the array
if(localStorage.getItem('pads') === null){
save = [];
}else {
save = JSON.parse(localStorage.getItem('pads'));
}
localStorage.setItem('pads', JSON.stringify(beatMaker.savedPads));
}
// Output method
loadSounds(){
// The second method needs to do the next things:
// Take all the active objects from the array and display them once the load btn is pressed
// For loop that check unitl the last object from the array
// Store all the objects inside a variable
// If an item from the array is active, try to add the correct color for that item(OBJECT)
// All the objects from the array will be transfred to the preview containers inside the load project
// We select each of the array saved in the load project by the number of the clicked pad
// This means the pressed button would have the class of the pad's nr property
// After it was clicked, we loop over each of the element inside the array and select each of them with the nr of the button
// Then we can select every element with the class id of the btn
// We can make another loop and say for every element inside the pad node color the one who has the specific value declared in the array
// Make input clear after the submit button was clicked
let save;
let check;
// Background and library menu
beatMaker.popupContainer.classList.add('active');
const popup = beatMaker.popupContainer.children[0];
popup.classList.add('active');
}
limitExceeds(save){
let limitLength = 7;
let exceedsLimit = false;
if(save.length > limitLength) {
exceedsLimit = true;
console.log(2);
}
return exceedsLimit;
}
// New project functionality
getLocal(){
// Takes all the element from the local storage
// Takes an input value and store it
const name = beatMaker.setsName.value;
const palette = document.createElement('div');
palette.classList.add('custom-palette');
const title = document.createElement('h4');
title.innerText = name;
const paletteBtn = document.createElement('button');
paletteBtn.classList.add('pick-palette-btn');
paletteBtn.innerText = 'Select';
let drumpadsSet = JSON.parse(localStorage.getItem('pads'));
paletteBtn.addEventListener('click', () => {
console.log(drumpadsSet);
// Close the popup
beatMaker.popupContainer.classList.remove('active');
const popup = beatMaker.popupContainer.children[0];
popup.classList.remove('active');
for(let value of drumpadsSet){
beatMaker.pad.forEach((pads) => {
if(pads.classList.contains(value.nr) && pads.classList.contains(value.state)){
pads.classList.toggle('active');
}
});
}
});
// Append to the load project menu
palette.appendChild(title);
palette.appendChild(paletteBtn);
beatMaker.loadMenu.appendChild(palette);
}
closeProjectMenu(){
beatMaker.backMenu.classList.remove('active');
const popup = beatMaker.backMenu.children[0];
popup.classList.remove('active');
}
erase(){
const allPads = this.pad
allPads.forEach((pads) => {
if(pads.classList.contains('active')){
pads.classList.remove('active');
}else if(pads.classList.contains('default') || this.isPlaying) {
pads.classList.remove('default');
clearInterval(this.isPlaying);
this.isPlaying = null;
this.index = 0;
}
});
}
repeat(){
let repeatIndex = this.index % 8;
const activeBars = document.querySelectorAll(`.b${repeatIndex}`)
this.index++;
console.log(repeatIndex);
console.log(activeBars);
console.log(this.bpm);
activeBars.forEach((bar) => {
bar.style.animation = "playTrack alternate 0.3s ease-in-out 2";
// Default behaviour of the drumpads that are not active
if(!bar.classList.contains('active')){
bar.classList.add('default');
}else {
bar.classList.remove('default');
}
// Check if it is any pad active
if(bar.classList.contains('active')){
// Check if it is kick-pad
if(bar.classList.contains('kick-pad')){
this.kickAudio.play();
this.kickAudio.currentTime = 0;
}
// Check if it is snare-pad
if(bar.classList.contains('snare-pad')){
this.snareAudio.play();
this.snareAudio.currentTime = 0;
}
// Check if it is hihat-pad
if(bar.classList.contains('hihat-pad')){
this.hihatAudio.play();
this.hihatAudio.currentTime = 0;
}
}
});
}
// We make the start function in order to match the bpm functionality and not raise up the speed when click on the start button
start(){
// We establish the formula of x bpm per minute depending on the desired bpm
const interval = (60/this.bpm) * 1000;
if(!this.isPlaying){
this.isPlaying = setInterval(() => {
this.repeat();
}, interval)
}
}
mute(){
if(this.isPlaying){
clearInterval(this.isPlaying);
this.isPlaying = null;
}
}
// We change of each category pad according to the value of each option inside the selector
changeSound(e){
const selector = e.target.value;
const audioSet = e.target.name;
// Put in the option value my desired sounds to match audios sources
// Matched the source of the audio with the value of each option from the entire selector
// Then matched name of each selector with the correct audio
switch(audioSet){
case 'kick-selector':
this.kickAudio.src = selector;
break;
case 'snare-selector':
this.snareAudio.src = selector;
break;
case 'hihat-selector':
this.hihatAudio.src = selector;
break;
}
}
slideTempo(e){
// This is the ouput that will display our desired bpm
const bpmOutput = document.querySelector('.bpm');
this.bpm = e.target.value;
bpmOutput.innerHTML = e.target.value;
console.log(this.bpm);
}
updateTempo(){
// This will change the tempo once the click on the slider is released and everything about the bpm will be updated
clearInterval(this.isPlaying);
this.isPlaying = null;
const playBtn = document.querySelector('.play-btn');
console.log(playBtn);
if(playBtn.classList.contains('active')){
this.start();
console.log(this.bpm);
console.log(playBtn);
}
}
}
const beatMaker = new drumPad();
// Event Listeners
beatMaker.playBtn.addEventListener('click', () => {
beatMaker.start();
});
beatMaker.stopBtn.addEventListener('click', () => {
beatMaker.mute();
});
beatMaker.eraseBtn.addEventListener('click', () => {
beatMaker.erase();
});
beatMaker.selectAudio.forEach((select) => {
select.addEventListener('change', (e) => {
beatMaker.changeSound(e);
});
});
beatMaker.bpmInput.addEventListener('input', (e) => {
beatMaker.slideTempo(e);
});
beatMaker.bpmInput.addEventListener('change', () => {
beatMaker.updateTempo();
});
// Save all the selected pads
beatMaker.saveBtn.addEventListener('click', (padObj) => {
beatMaker.saveToLocal(padObj);
});
beatMaker.loadBtn.addEventListener('click', () => {
beatMaker.loadSounds();
});
// Closing library menu
beatMaker.closeLib.addEventListener('click', () => {
beatMaker.popupContainer.classList.remove('active');
const popup = beatMaker.popupContainer.children[0];
popup.classList.remove('active');
});
// New project menu
beatMaker.newBtn.addEventListener('click', () => {
beatMaker.backMenu.classList.add('active');
const popup = beatMaker.backMenu.children[0];
popup.classList.add('active');
});
// Submiting the entire pad
beatMaker.submitBtn.addEventListener('click', () => {
beatMaker.getLocal();
beatMaker.setsName.value = '';
beatMaker.backMenu.classList.remove('active');
const popup = beatMaker.backMenu.children[0];
popup.classList.remove('active');
let reseted = JSON.parse(localStorage.getItem('pads'));
reseted = localStorage.clear();
});
// Closing save project menu
beatMaker.closeProj.addEventListener('click', beatMaker.closeProjectMenu);
// After the ending of each pad animation, start again the same animation style
beatMaker.pad.forEach((pads) => {
// What to do after i click on a specific pad
pads.addEventListener('click', beatMaker.activePad);
// What to do after the end of the animation
pads.addEventListener('animationend', function(){
this.style.animation = '';
});
});