-
Notifications
You must be signed in to change notification settings - Fork 15
/
PocketOperator.js
688 lines (565 loc) · 21.5 KB
/
PocketOperator.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
function PocketOperator(drumkit, chordkit, icons) {
this.sound_picking = false;
this.picked_sound = 3; // setting picked sound to hat
this.sounds_playing = [];
this.pattern_chaining = false;
this.pat_already_pressed = false;
this.pattern_chain = [0];
this.which_pattern = 0;
this.bpm_ms = 500;
this.bpmrange_ms = [60000/BPM_RANGE[0], 60000/BPM_RANGE[1]];
this.a_knob_pressed = false;
this.b_knob_pressed = false;
this.a_slider_phantom = a_slider.value();
this.b_slider_phantom = b_slider.value();
this.chord_picking = false;
this.chord_pattern = [];
this.ch_pat_len = this.chord_pattern.length;
this.ch_already_pressed = false;
this.which_chord = 0;
this.chord_volume = 0;
this.curr_chord = chordkit['C'];
this.suppress = 0;
this.fx_active = false;
this.playing = false;
this.writing = false;
this.metronome_tick = false;
this.cursor_pos = 16;
this.light_w = width*0.0416; // width of light indicator
this.light_h = width*0.0200; // height of light indicator
this.pressed = (x, y, kind) => {
// if the kind of button clicked is other than 'drum'
// then we activate function corresponding to it
if ( kind != 'drum' ) {
switch ( kind ) {
case 'sound': this.sound_switch(); break;
case 'pattern': this.pattern_switch(); break;
case 'bpm': this.bpm_switch(); break;
case 'knob_A': this.knob_A_switch(); break;
case 'knob_B': this.knob_B_switch(); break;
case 'chord': this.chord_switch(); break;
case 'FX': this.fx_switch(); break;
case 'play': this.play_switch(); break;
case 'write': this.write_switch(); break;
};
} else {
let button_num = y*4+x;
// setting a priority chain
// for passing the sequencer buttons
if ( this.pattern_chaining ) {
this.pattern_route(button_num);
return;
};
if ( this.sound_picking ) {
this.sound_route(button_num);
return;
};
if ( this.writing ) {
this.writing_route(button_num);
return;
};
if ( this.chord_picking ) {
this.chord_route(button_num, x, y);
return;
};
// if no special button is pressed, we just play
// a corresponding drum sample
drumkit[button_num].play();
};
};
this.get_bpm = () => {
return 60000/this.bpm_ms;
};
// functions we route button info to, when special mode
// (like writing or chord picking) is active
this.sound_route = (num) => {
this.picked_sound = num;
this.sound_picking = false;
};
this.pattern_route = (num) => {
if ( this.pat_already_pressed ) {
this.pattern_chain.length = 0;
};
this.pattern_chain.push(num);
this.pat_already_pressed = false;
};
this.writing_route = (num) => {
if( drumkit[this.picked_sound].is_there(num) ) {
drumkit[this.picked_sound]
.erase(num);
return;
};
drumkit[this.picked_sound]
.add(num);
if ( this.a_knob_pressed ) {
let value = a_slider.value();
let semitones = map(value, 0, 1, 0, 12);
drumkit[this.picked_sound]
.add_mod(num, 0, semitones);
};
if ( this.b_knob_pressed ) {
let b_value = b_slider.value();
let pan_value = map(b_value, 0, 1, -1, 1);
drumkit[this.picked_sound]
.add_mod(num, 1, pan_value);
};
};
this.chord_route = (num, x, y) => {
if ( this.ch_already_pressed ) {
this.chord_pattern.length = 0;
};
this.chord_pattern.push([CHORD_POS[y][x], num]);
this.ch_already_pressed = false;
};
// switch for 'sound' button pressed
this.sound_switch = () => {
this.sound_picking = !this.sound_picking;
};
// switch for 'pattern' button pressed
this.pattern_switch = () => {
if ( this.pattern_chaining && !this.playing ) {
this.which_pattern = 0;
this.push_pattern();
};
this.pattern_chaining = !this.pattern_chaining;
this.pat_already_pressed = true;
};
// if bpm_switch is activated it changes po's bpm in ms
this.bpm_switch = () => {
// well take bpm slider value as a param later its a todo thing
this.bpm_ms = 60000/bpm;
};
// handler for the A_knob
this.knob_A_switch = () => {
// listerner for slider dragging
a_slider.input( () => {
let value = a_slider.value();
// if we're in chord-picking special mode,
// we set this phantom var to knob_A's value,
// we do this so chord's volume doesn't change
// when we mod drumkit's sounds,
// the same thing happens to knob B
if ( this.chord_picking && this.chord_pattern.length > 0 ) {
this.a_slider_phantom = value;
this.chord_volume = value;
this.curr_chord.volume(this.chord_volume);
} else {
let semitones = map(value, 0, 1, 0, 12);
drumkit[this.picked_sound].modulate(semitones);
};
});
this.a_knob_pressed = !this.a_knob_pressed;
if ( this.a_knob_pressed ) {
a_slider.show();
a_slider.elt.focus();
} else {
a_slider.hide();
};
};
// handler for the B_knob
this.knob_B_switch = () => {
// listerner for slider dragging
b_slider.input( () => {
let value = b_slider.value();
if ( this.chord_picking && this.chord_pattern.length > 0 ) {
this.b_slider_phantom = value;
} else {
let pan_value = map(value, 0, 1, -1, 1);
drumkit[this.picked_sound].pan(pan_value);
};
});
this.b_knob_pressed = !this.b_knob_pressed;
if ( this.b_knob_pressed ) {
b_slider.show();
b_slider.elt.focus();
} else {
b_slider.hide();
};
};
// switch for 'chord' button pressed
this.chord_switch = () => {
this.chord_picking = !this.chord_picking;
this.ch_already_pressed = true;
};
// switch for 'fx' button pressed
this.fx_switch = () => {
this.fx_active = !this.fx_active;
};
// a safe way for external checking if fx button has been clicked
this.is_fx_active = () => {
return this.fx_active;
};
// a safe way for external checking if PocketOperator is in playing mode
this.is_playing = () => {
return this.playing;
};
// switch for 'write' button pressed
this.write_switch = () => {
this.writing = !this.writing;
};
// function changing current pattern to the next pattern
this.push_pattern = () => {
let chain_len = this.pattern_chain.length;
// this is essential so that modulo later on works out
if ( chain_len == 0 ) {
chain_len = 1;
};
// looping through every Sound in drumkit and changing
// their selected pattern
for ( let x = 0; x < 16; x++ ) {
drumkit[x].change_pattern(this.pattern_chain[this.which_pattern%chain_len]);
};
this.which_pattern+=1;
};
// function that deals with all the stuff that's about to happen
// when we click the almighty 'play' button, exciting stuff
this.play_switch = () => {
this.which_chord = 0;
this.chord_volume = 0;
this.which_pattern = 0;
this.push_pattern();
// this bit stops chord when we want to stop playing
if ( this.playing ) {
let ch_names_concat = chord_names.concat(chord_names_mod);
if ( this.chord_pattern.length > 0 ) {
for ( let x = 0; x < 16; x++ ) {
chordkit[ch_names_concat[x].name].stop();
};
};
};
// function for playing chords
this.play_chord = () => {
// if called, stops curently selected chord
this.curr_chord.stop();
// if there are chords selected
if ( this.chord_pattern.length > 0 ) {
this.curr_chord = chordkit[
this.chord_pattern[
this.which_chord%this.chord_pattern.length
][0]
];
// setting volume for our chord
this.curr_chord.volume(this.chord_volume);
// setting our selected chord to loop
this.curr_chord.loop();
// pushing selected chord to the next one
this.which_chord+=1;
};
};
// function helping push cursor_pos one step forward
// and dealing with all soundy things depending
// on cursor_pos
let push_cursor = () => {
// playing sounds on current cursor position
play_on_cursor();
// if cursor goes to next row we set next tick
// for the metronome
if ( this.cursor_pos % 4 == 0 ) {
this.metronome_tick = !this.metronome_tick;
};
// we want to reset sidechain suppressor a little bit earlier tho
// it sounds better
if ( this.cursor_pos % 4 == 0 ) {
this.suppress = 1;
if ( b_slider.value() == 0 ) {
this.chord_volume = this.a_slider_phantom;
};
}
// if cursor_pos reaches the last pos it can
// we push to the next pattern
if ( this.cursor_pos == 15 ) {
this.push_pattern();
};
// if cursor_pos reaches 0, we play a chord
if ( this.cursor_pos == 0 ) {
this.play_chord();
};
// pushing cursor one step forward
this.cursor_pos = (this.cursor_pos+1)%16;
};
// playing every sound that are programmed to play
// on current cursor_pos
let play_on_cursor = () => {
this.sounds_playing.length = 0;
// looping through the entire drumkit and checking
// if any pattern includes current cursor position
for ( let x = 0; x < 16; x++ ) {
if ( drumkit[x].is_there(this.cursor_pos) ) {
// if there's no modulation information
// we assign it
let mod_lvl = 0;
let pan_lvl = 0;
// if any of the knobs' sliders are visible
// we automate and write mod info on the fly
if ( this.writing && this.a_knob_pressed) {
let a_value = a_slider.value();
let semitones = map(a_value, 0, 1, 0, 24);
drumkit[this.picked_sound]
.add_mod(this.cursor_pos, 0, semitones);
};
if ( this.writing && this.b_knob_pressed ) {
let b_value = b_slider.value();
let pan_value = map(b_value, 0, 1, -1, 1);
drumkit[this.picked_sound]
.add_mod(this.cursor_pos, 1, pan_value);
};
// reading the sound's mod and pan levels
// for current cursor pos
mod_lvl = drumkit[x].mod_lvl(this.cursor_pos);
pan_lvl = drumkit[x].pan_lvl(this.cursor_pos);
// and modulating accordingly
drumkit[x].modulate(mod_lvl);
drumkit[x].pan(pan_lvl);
// after we're done with all this
// we can finally play our sample
drumkit[x].play();
// the last thing we do is push sound's pos
// so when we're not in any special mode
// we can see currently playing sounds light up
this.sounds_playing.push(x);
};
};
};
// main playing recursive function with interval
let rec_tim = () => {
// setTimeout( () => {
// if ( this.playing ) {
// push_cursor();
// rec_tim();
// }
// }, this.bpm_ms/4 );
// now this /\ but using tock for more accuracy
// and hopefully less stuttering
this.timer = new Tock({
countdown: true,
complete: () => {
if ( this.playing ) {
push_cursor();
rec_tim();
};
}
});
this.timer.start( Math.ceil(this.bpm_ms/4) );
};
// if the button is clicked when the playing mode is
// already on, then we set the cursor_pos to 16,
// just because we don't check that far
// also we dont have that many buttons,
// so nothing will light up
// its just more pretty than setting it to null
if ( !this.playing ) {
// and of course setting it to 0
// when pressed for the (first time)%2
this.cursor_pos = 0;
rec_tim();
} else {
this.cursor_pos = 16;
};
this.playing = !this.playing;
};
// po-20 chord sidechain effect function
this.sidechain = () => {
// lerping suppressor value we subtract from chord volume
// to achieve the sidechainy effect
let b_value = this.b_slider_phantom;
if ( this.playing && b_value > 0 ) {
let a_value = this.a_slider_phantom;
this.suppress = lerp(this.suppress,
0,
map(this.bpm_ms,
this.bpmrange_ms[1], this.bpmrange_ms[0],
0.01, 0.1));
this.chord_volume = (1 - this.suppress*b_value)*a_value;
this.curr_chord.volume(this.chord_volume);
};
};
this.backup = () => {
let drum_patterns = [];
let mod_array = [];
// looping through all the sounds
for ( let x = 0; x < 16; x++ ) {
drum_patterns.push(drumkit[x].ret_whole_pattern());
mod_array.push(drumkit[x].get_whole_mod());
};
// creating objects with data
let object = {
"chord_pattern": this.chord_pattern,
"bpm_ms": this.bpm_ms,
"pattern_chain": this.pattern_chain,
"drum_patterns": drum_patterns,
"mod_array": mod_array
};
return object;
};
this.retrieve = (data) => {
console.log(data);
this.bpm_ms = data.bpm_ms;
this.chord_pattern = data.chord_pattern;
this.pattern_chain = data.pattern_chain;
for ( let x = 0; x < 16; x++ ) {
drumkit[x].set_whole_mod(data.mod_array[x]);
drumkit[x].set_pattern(data.drum_patterns[x]);
};
console.log('retrieved :)');
};
// main drawing function
this.draw = () => {
// drawing knob_A's and knob_B's levels
fill(0);
rect(width*0.801, width*0.5, width*0.052, -(width*(a_slider.value()+0.10)*0.12));
rect(width*0.861, width*0.5, width*0.052, -(width*(b_slider.value()+0.10)*0.12));
// calling sidechain every frame so the lerping
// takes place
this.sidechain();
// drawing selected sound
let icon_size = width*0.16;
let icon_w = width*0.43;
let icon_h = width*0.34;
this.ch_pat_len = this.chord_pattern.length;
if ( !this.chord_picking || this.ch_pat_len == 0 ) {
image(icon_sep[this.picked_sound],
icon_w, icon_h, icon_size, icon_size);
} else {
this.which_chord = this.which_chord%this.chord_pattern.length;
if ( this.ch_already_pressed ) {
image(ch_icon_sep[this.chord_pattern[this.which_chord][1]],
icon_w, icon_h, icon_size, icon_size);
} else {
image(ch_icon_sep[this.chord_pattern[this.ch_pat_len-1][1]],
icon_w, icon_h, icon_size, icon_size);
};
};
// drawing the metronome
fill(0);
strokeWeight(2);
let sp_x = width*0.274;
let sp_y = width*0.30;
let ep_x = sp_x-width*0.03;
let ep_y = width*0.28;
let metronome_offset = width*0.06;
let ellipse_size = width*0.01;
if ( !this.metronome_tick ) {
line(sp_x, sp_y, ep_x, ep_y);
ellipse(ep_x, ep_y, ellipse_size);
} else {
line(sp_x, sp_y, ep_x+metronome_offset, ep_y);
ellipse(ep_x+metronome_offset, ep_y, ellipse_size);
};
strokeWeight(1);
fill(255);
// drawing play icon
if ( this.playing ) {
fill(0);
beginShape(TRIANGLE_STRIP);
let sp_x = width*0.146;
let sp_y = width*0.518;
let triangle_size = width*0.04;
vertex(sp_x, sp_y);
vertex(sp_x, sp_y+triangle_size);
vertex(sp_x+triangle_size, sp_y+triangle_size*0.5);
endShape();
fill(255);
};
// discerning which buttons we want do be lit up at the moment
this.to_show = [];
this.to_show_concat = () => {
// i know we just started, but
// from now on here things start to get messy cos of mixed
// priority for pages and lights, so i mix using returns at the end
// and just plain old long ugly 3 conditional if statements
if ( this.playing && !this.writing && !this.chord_picking ) {
this.to_show = this.to_show.concat(
this.sounds_playing
);
};
if ( this.pattern_chaining ) {
this.to_show = this.to_show.concat(
this.pattern_chain
);
return;
};
if ( this.writing || !this.playing && !this.chord_picking) {
this.to_show = this.to_show.concat(
drumkit[this.picked_sound].ret_pattern()
);
};
if ( this.chord_picking && !this.writing ) {
for ( let x = 0; x < this.chord_pattern.length; x++ ) {
this.to_show.push(this.chord_pattern[x][1]);
};
return;
};
this.to_show.push(this.cursor_pos);
};
this.to_show_concat();
// drawing button grid lights
// and i think we should pass this to draw in main script
// to be done ( coz it works for now lol )
for ( let x = 0; x < 4; x++ ) {
for ( let y = 0; y < 4; y++ ) {
let here_x = (interspace_x)*x+padding_x*0.82;
let here_y = (interspace_y)*y+padding_y*0.92;
if ( this.to_show.includes(y*4+x) ) {
fill(255, 0, 0);
} else {
fill(255);
};
rect(here_x, here_y, this.light_w, this.light_h);
};
};
// drawing the play button light
let here_x = (padding_x*0.82)+(interspace_x*4);
let here_y = (padding_y*0.92)+(interspace_y*2);
if ( this.playing && this.cursor_pos % 4 == 0 ) {
fill(255, 0, 0);
} else {
fill(255);
};
rect(here_x, here_y, this.light_w, this.light_h);
// drawing the write button pressed indicator
if ( this.writing ) {
let here_x = width*0.093;
let here_y = width*0.516;
let here_size = width*0.04;
fill(255, 40, 0);
rect(here_x, here_y, here_size, here_size);
fill(255);
};
// drawing the pattern button pressed indicator
if ( this.pattern_chaining ) {
let here_x = width*0.319;
let here_y = width*0.757;
let here_size = width*0.056;
fill(255, 0, 0);
ellipse(here_x, here_y, here_size);
fill(255);
};
// drawing the chord button pressed indicator
if ( this.chord_picking ) {
let here_x = width*0.874;
let here_y = width*0.966;
let here_size = width*0.056;
fill(255, 0, 0);
ellipse(here_x, here_y, here_size);
fill(255);
};
// drawing the fx button pressed indicator
if ( this.fx_active ) {
let here_x = width*0.874;
let here_y = width*1.176;
let here_size = width*0.056;
fill(255, 0, 0);
ellipse(here_x, here_y, here_size);
fill(255);
};
// drawing the sound button pressed indicator
if ( this.sound_picking ) {
let here_x = width*0.134;
let here_y = width*0.757;
let here_size = width*0.056;
fill(255, 0, 0);
ellipse(here_x, here_y, here_size);
fill(255);
};
};
};