forked from sugarlabs/musicblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetBlocks.js
More file actions
1682 lines (1512 loc) · 64.2 KB
/
WidgetBlocks.js
File metadata and controls
1682 lines (1512 loc) · 64.2 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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2019 Bottersnike
// Copyright (c) 2019-22 Walter Bender
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the The GNU Affero General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// You should have received a copy of the GNU Affero General Public
// License along with this library; if not, write to the Free Software
// Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
/*
global
last, _, FlowBlock, NOINPUTERRORMSG, MeterWidget,
DEFAULTVOICE, instrumentsEffects, StackClampBlock, Oscilloscope,
DEFAULTMODE, Tempo, PitchDrumMatrix, PhraseMaker, StatusMatrix,
RhythmRuler, FILTERTYPES, instrumentsFilters, DEFAULTFILTERTYPE,
TemperamentWidget, TimbreWidget, ModeWidget, PitchSlider,
MusicKeyboard, PitchStaircase, SampleWidget, _THIS_IS_MUSIC_BLOCKS_,
Arpeggio
*/
/*
Global locations
- js/utils/utils.js
_
- js/logo.js
NOINPUTERRORMSG
- js/widgets/meterwidget.js
MeterWidget
- js/utils/musicutils.js
DEFAULTVOICE, DEFAULTMODE, FILTERTYPES, DEFAULTFILTERTYPE
- js/utils/synthutils.js
instrumentsEffects, instrumentsFilters
- js/protoblocks.js
StackClampBlock
- js/widgets/oscilloscope.js
Oscilloscope
- js/widgets/tempo.js
Tempo
- js/widgets/pitchdrummatrix.js
PitchDrumMatrix
- js/widgets/phrasemaker.js
PhraseMaker
- js/widgets/status.js
StatusMatrix
- js/widgets/rhythmruler.js
RhythmRuler
- js/widgets/temperament.js
TemperamentWidget
- js/widgets/timbre.js
TimbreWidget
- js/widgets/modewidget.js
ModeWidget
- js/widgets/pitchslider.js
PitchSlider
- js/widgets/musickeyboard.js
MusicKeyboard
- js/widgets/pitchstaircase.js
PitchStaircase
*/
/* exported setupWidgetBlocks */
/**
* @param {string} activity - The activity for which blocks are being set up.
*/
function setupWidgetBlocks(activity) {
/**
* Represents a block for controlling sound envelope (ADSR).
* @extends FlowBlock
*/
class EnvelopeBlock extends FlowBlock {
/**
* Creates an EnvelopeBlock instance.
*/
constructor() {
//.TRANS: sound envelope (ADSR)
super("envelope", _("Envelope").toLowerCase());
this.setPalette("widgets", activity);
this.setHelpString();
this.formBlock({
args: 4,
defaults: [1, 50, 60, 1],
argLabels: [
//.TRANS: Attack time is the time taken for initial run-up of level from nil to peak, beginning when the key is first pressed.
_("attack"),
//.TRANS: Decay time is the time taken for the subsequent run down from the attack level to the designated sustain level.
_("decay"),
//.TRANS: Sustain level is the level during the main sequence of the sound's duration, until the key is released.
_("sustain"),
//.TRANS: Release time is the time taken for the level to decay from the sustain level to zero after the key is released.
_("release")
]
});
this.hidden = true;
}
/**
* Handles the flow of data for the sound envelope block.
* @param {number[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
*/
flow(args, logo, turtle, blk) {
const tur = activity.turtles.ithTurtle(turtle);
if (args.length === 4 && typeof args[0] === "number") {
if (args[0] < 0 || args[0] > 100) {
activity.errorMsg(_("Attack value should be from 0 to 100."));
}
if (args[1] < 0 || args[1] > 100) {
activity.errorMsg(_("Decay value should be from 0 to 100."));
}
if (args[2] < 0 || args[2] > 100) {
activity.errorMsg(_("Sustain value should be from 0 to 100."));
}
if (args[3] < 0 || args[3] > 100) {
activity.errorMsg(_("Release value should be from 0-100."));
}
// Push envelope values to corresponding arrays
tur.singer.attack.push(args[0] / 100);
tur.singer.decay.push(args[1] / 100);
tur.singer.sustain.push(args[2] / 100);
tur.singer.release.push(args[3] / 100);
}
if (logo.inTimbre) {
// Update timbre object with envelope values
logo.timbre.synthVals["envelope"]["attack"] = last(tur.singer.attack);
logo.timbre.synthVals["envelope"]["decay"] = last(tur.singer.decay);
logo.timbre.synthVals["envelope"]["sustain"] = last(tur.singer.sustain);
logo.timbre.synthVals["envelope"]["release"] = last(tur.singer.release);
if (logo.timbre.env.length != 0) {
activity.errorMsg(_("You are adding multiple envelope blocks."));
} else {
// Create the synth for the instrument.
logo.synth.createSynth(
turtle,
logo.timbre.instrumentName,
logo.timbre.synthVals["oscillator"]["source"],
logo.timbre.synthVals
);
}
// Push envelope block and values to timbre object
logo.timbre.env.push(blk);
logo.timbre.ENVs.push(Math.round(last(tur.singer.attack) * 100));
logo.timbre.ENVs.push(Math.round(last(tur.singer.decay) * 100));
logo.timbre.ENVs.push(Math.round(last(tur.singer.sustain) * 100));
logo.timbre.ENVs.push(Math.round(last(tur.singer.release) * 100));
}
}
}
/**
* Represents a block for applying filters to sound.
* @extends FlowBlock
*/
class FilterBlock extends FlowBlock {
/**
* Creates a FilterBlock instance.
*/
constructor() {
//.TRANS: a filter removes some unwanted components from a signal
super("filter", _("Filter").toLowerCase());
this.setPalette("widgets", activity);
this.setHelpString();
this.formBlock({
args: 3,
defaults: [_("highpass"), -12, 392],
argLabels: [
//.TRANS: type of filter, e.g., lowpass, highpass, etc.
_("type"),
//.TRANS: rolloff is the steepness of a change in frequency.
_("rolloff"),
_("frequency")
],
argTypes: ["anyin", "numberin", "numberin"]
});
this.hidden = true;
}
/**
* Handles the flow of data for the filter block.
* @param {any[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
*/
flow(args, logo, turtle, blk) {
let filtertype = DEFAULTFILTERTYPE;
let freq;
let rollOff;
if (args.length === 3 && typeof args[1] === "number") {
for (const ftype in FILTERTYPES) {
if (FILTERTYPES[ftype][0] === args[0]) {
filtertype = FILTERTYPES[ftype][1];
} else if (FILTERTYPES[ftype][1] === args[0]) {
filtertype = args[0];
}
}
if (![-12, -24, -48, -96].includes(args[1])) {
//.TRANS: rolloff is the steepness of a change in frequency.
activity.errorMsg(
_("Rolloff value should be either -12, -24, -48, or -96 decibels/octave.")
);
}
rollOff = args[1];
freq = args[2];
if (logo.inTimbre) {
if (!(logo.timbre.instrumentName in instrumentsFilters[turtle])) {
instrumentsFilters[turtle][logo.timbre.instrumentName] = [];
}
// Add the filter to the instrument
instrumentsFilters[turtle][logo.timbre.instrumentName].push({
filterType: filtertype,
filterRolloff: rollOff,
filterFrequency: freq
});
logo.timbre.fil.push(blk);
logo.timbre.filterParams.push(filtertype);
logo.timbre.filterParams.push(rollOff);
logo.timbre.filterParams.push(freq);
}
}
}
}
/**
* Represents a block for defining custom tuning (temperament).
* @extends StackClampBlock
*/
class TemperamentBlock extends StackClampBlock {
/**
* Creates a TemperamentBlock instance.
*/
constructor() {
super("temperament");
this.setPalette("widgets", activity);
this.setHelpString([
_("The Temperament tool is used to define custom tuning."),
"documentation",
null,
"temperament"
]);
this.formBlock({
args: 1,
name: _("temperament"),
canCollapse: true
});
this.makeMacro((x, y) => [
[0, "temperament", x, y, [null, 1, 2, 8]],
[1, ["temperamentname", { value: "equal" }], 0, 0, [0]],
[2, "pitch", 0, 0, [0, 3, 4, 5]],
[3, ["notename", { value: "C" }], 0, 0, [2]],
[4, ["number", { value: 4 }], 0, 0, [2]],
[5, "setkey2", 0, 0, [2, 6, 7, null]],
[6, ["notename", { value: "C" }], 0, 0, [5]],
[7, ["modename", { value: DEFAULTMODE }], 0, 0, [5]],
[8, "hiddennoflow", 0, 0, [0, null]]
]);
}
/**
* Handles the flow of data for the temperament block.
* @param {any[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
*/
flow(args, logo, turtle, blk) {
if (logo.temperament === null) {
logo.temperament = new TemperamentWidget();
}
logo.insideTemperament = true;
logo.temperament.inTemperament = args[0];
const scale = [];
if (
activity.blocks.blockList[activity.blocks.blockList[blk].connections[2]].name ===
"pitch"
) {
const pitchBlock =
activity.blocks.blockList[activity.blocks.blockList[blk].connections[2]];
const note = activity.blocks.blockList[pitchBlock.connections[1]].value;
const octave = activity.blocks.blockList[pitchBlock.connections[2]].value;
const setKey = activity.blocks.blockList[pitchBlock.connections[3]];
scale[0] = activity.blocks.blockList[setKey.connections[1]].value;
scale[1] = activity.blocks.blockList[setKey.connections[2]].value;
logo.synth.startingPitch = note + octave;
logo.temperament.scale = scale;
}
const listenerName = "_temperament_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.temperament.init(activity);
};
logo.setTurtleListener(turtle, listenerName, __listener);
}
}
/**
* Represents a block for sampling audio.
* @extends StackClampBlock
*/
class SamplerBlock extends StackClampBlock {
/**
* Creates a SamplerBlock instance.
*/
constructor() {
super("sampler");
this.setPalette("widgets", activity);
this.parameter = true;
this.beginnerBlock(false);
this.setHelpString([
_("Upload a sample and adjust its pitch center."),
"documentation",
null,
"sampler"
]);
//.TRANS: the speed at music is should be played.
this.formBlock({ name: _("sampler"), canCollapse: true });
this.makeMacro((x, y) => [
[0, "sampler", x, y, [null, 1, 8]],
[1, "settimbre", 0, 0, [0, 2, 6, 7]],
[2, ["customsample", { value: ["", "", "do", 4] }], 0, 0, [1, 3, 4, 5]],
[3, ["audiofile", { value: null }], 0, 0, [2]],
[4, ["solfege", { value: "do" }], 0, 0, [2]],
[5, ["number", { value: 4 }], 0, 0, [2]],
[6, "vspace", 0, 0, [1, null]],
[7, "hidden", 0, 0, [1, null]],
[8, "hiddennoflow", 0, 0, [0, null]]
]);
}
/**
* Handles the flow of data for the sampler block.
* @param {any[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The output values.
*/
flow(args, logo, turtle, blk) {
if (logo.sample === null) {
logo.sample = new SampleWidget();
}
logo.inSample = true;
logo.sample = new SampleWidget();
const listenerName = "_sampler_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
// eslint-disable-next-line no-unused-vars
const __listener = event => {
logo.sample.init(activity);
};
logo.setTurtleListener(turtle, listenerName, __listener);
return [args[0], 1];
}
}
/**
* Represents a block for defining timbre (sound character).
* @extends StackClampBlock
*/
class TimbreBlock extends StackClampBlock {
/**
* Creates a TimbreBlock instance.
*/
constructor() {
super("timbre");
this.setPalette("widgets", activity);
this.setHelpString();
this.formBlock({
//.TRANS: timbre is the character or quality of a musical sound
name: _("timbre"),
args: 1,
defaults: [_("custom")],
canCollapse: true
});
this.makeMacro((x, y) => [
[0, "timbre", x, y, [null, 1, 3, 2]],
[1, ["text", { value: _("custom") }], 0, 0, [0]],
[2, "hiddennoflow", 0, 0, [0, null]],
[3, "newnote", 0, 0, [0, 4, 7, 11]],
[4, "divide", 0, 0, [3, 5, 6]],
[5, ["number", { value: 1 }], 0, 0, [4]],
[6, ["number", { value: 4 }], 0, 0, [4]],
[7, "vspace", 0, 0, [3, 8]],
[8, "pitch", 0, 0, [7, 9, 10, null]],
[9, ["solfege", { value: "sol" }], 0, 0, [8]],
[10, ["number", { value: 4 }], 0, 0, [8]],
[11, "hidden", 0, 0, [3, 12]],
[12, "newnote", 0, 0, [11, 13, 16, 20]],
[13, "divide", 0, 0, [12, 14, 15]],
[14, ["number", { value: 1 }], 0, 0, [13]],
[15, ["number", { value: 4 }], 0, 0, [13]],
[16, "vspace", 0, 0, [12, 17]],
[17, "pitch", 0, 0, [16, 18, 19, null]],
[18, ["solfege", { value: "mi" }], 0, 0, [17]],
[19, ["number", { value: 4 }], 0, 0, [17]],
[20, "hidden", 0, 0, [12, 21]],
[21, "newnote", 0, 0, [20, 22, 25, 29]],
[22, "divide", 0, 0, [21, 23, 24]],
[23, ["number", { value: 1 }], 0, 0, [22]],
[24, ["number", { value: 2 }], 0, 0, [22]],
[25, "vspace", 0, 0, [21, 26]],
[26, "pitch", 0, 0, [25, 27, 28, null]],
[27, ["solfege", { value: "sol" }], 0, 0, [26]],
[28, ["number", { value: 4 }], 0, 0, [26]],
[29, "hidden", 0, 0, [21, null]]
]);
}
/**
* Handles the flow of data for the timbre block.
* @param {any[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The output values.
*/
flow(args, logo, turtle, blk) {
if (logo.timbre === null) {
logo.timbre = new TimbreWidget();
}
logo.inTimbre = true;
if (typeof args[0] === "string") {
// CLear out the instrument because we will recreate it in the widget.
logo.timbre.instrumentName = args[0];
} else {
logo.timbre.instrumentName = DEFAULTVOICE;
activity.errorMsg(NOINPUTERRORMSG, blk);
}
instrumentsEffects[turtle][logo.timbre.instrumentName] = {};
instrumentsEffects[turtle][logo.timbre.instrumentName]["vibratoActive"] = false;
instrumentsEffects[turtle][logo.timbre.instrumentName]["distortionActive"] = false;
instrumentsEffects[turtle][logo.timbre.instrumentName]["tremoloActive"] = false;
instrumentsEffects[turtle][logo.timbre.instrumentName]["phaserActive"] = false;
instrumentsEffects[turtle][logo.timbre.instrumentName]["chorusActive"] = false;
instrumentsFilters[turtle][logo.timbre.instrumentName] = [];
logo.timbre.blockNo = blk;
logo.timbre.env = [];
logo.timbre.ENVs = [];
logo.timbre.fil = [];
logo.timbre.filterParams = [];
logo.timbre.osc = [];
logo.timbre.oscParams = [];
logo.timbre.tremoloEffect = [];
logo.timbre.tremoloParams = [];
logo.timbre.vibratoEffect = [];
logo.timbre.vibratoParams = [];
logo.timbre.chorusEffect = [];
logo.timbre.chorusParams = [];
logo.timbre.phaserEffect = [];
logo.timbre.phaserParams = [];
logo.timbre.distortionEffect = [];
logo.timbre.distortionParams = [];
logo.timbre.AMSynthesizer = [];
logo.timbre.AMSynthParams = [];
logo.timbre.FMSynthesizer = [];
logo.timbre.FMSynthParams = [];
logo.timbre.duoSynthesizer = [];
logo.timbre.duoSynthParams = [];
logo.timbre.notesToPlay = [];
const listenerName = "_timbre_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.timbre.init(activity);
};
logo.setTurtleListener(turtle, listenerName, __listener);
return [args[1], 1];
}
}
/**
* Represents a block for setting up Meter Widget in the workspace.
* @extends StackClampBlock
*/
class MeterWidgetBlock extends StackClampBlock {
/**
* Creates a MeterWidgetBlock instance.
*/
constructor() {
super("meterwidget");
this.setPalette("widgets", activity);
this.setHelpString([
_("The Meter block opens a tool to select strong beats for the meter."),
"documentation",
null,
"meterwidget"
]);
this.formBlock({ name: _("meter"), canCollapse: true });
this.makeMacro((x, y) => [
[0, ["meterwidget", { collapsed: false }], x, y, [null, 1, 7]],
[1, "meter", 0, 0, [0, 2, 3, 6]],
[2, ["number", { value: 4 }], 0, 0, [1]],
[3, "divide", 0, 0, [1, 4, 5]],
[4, ["number", { value: 1 }], 0, 0, [3]],
[5, ["number", { value: 4 }], 0, 0, [3]],
[6, "vspace", 0, 0, [1, null]],
[7, "hidden", 0, 0, [0, null]]
]);
}
/**
* Handles the flow of data for the MeterWidgetBlock.
* @param {any[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The processed arguments.
*/
flow(args, logo, turtle, blk) {
logo.insideMeterWidget = true;
const listenerName = "_meterwidget_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.meterWidget = new MeterWidget(activity, blk);
logo.insideMeterWidget = false;
};
logo.setTurtleListener(turtle, listenerName, __listener);
if (args.length === 1) return [args[0], 1];
}
}
/**
* Represents a block for setting up Oscilloscope Widget in the workspace.
* @extends StackClampBlock
*/
class oscilloscopeWidgetBlock extends StackClampBlock {
/**
* Creates an OscilloscopeWidgetBlock instance.
*/
constructor() {
super("oscilloscope");
this.setPalette("widgets", activity);
this.setHelpString([
_("The oscilloscope block opens a tool to visualize waveforms."),
"documentation",
null,
"oscilloscope"
]);
this.formBlock({ name: _("oscilloscope"), canCollapse: true });
const addPrintTurtle = (blocks, turtle, prev, last) => {
const len = blocks.length;
const next = last ? null : len + 2;
blocks.push([len, "print", 0, 0, [prev, len + 1, next]]);
blocks.push([len + 1, ["text", { value: turtle.name }], 0, 0, [len, null]]);
return blocks;
};
this.makeMacro((x, y) => {
let blocks = [[0, "oscilloscope", x, y, [null, 1, null]]];
for (const turtle of activity.turtles.turtleList) {
if (!turtle.inTrash)
// eslint-disable-next-line max-len
blocks = addPrintTurtle(
blocks,
turtle,
Math.max(0, blocks.length - 2),
turtle == activity.turtles.getTurtle(activity.turtles.getTurtleCount() - 1)
);
}
blocks[0][4][2] = blocks.length;
blocks.push([blocks.length, "hiddennoflow", 0, 0, [0, null]]);
return blocks;
});
}
/**
* Handles the flow of data for the OscilloscopeWidgetBlock.
* @param {any[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The processed arguments.
*/
flow(args, logo, turtle, blk) {
logo.oscilloscopeTurtles = [];
logo.inOscilloscope = true;
const listenerName = "_oscilloscope_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.Oscilloscope = new Oscilloscope(activity);
logo.inOscilloscope = false;
};
logo.setTurtleListener(turtle, listenerName, __listener);
if (args.length === 1) return [args[0], 1];
}
}
/**
* Represents a block for selecting a custom musical mode.
* @extends StackClampBlock
*/
class ModeWidgetBlock extends StackClampBlock {
/**
* Creates a ModeWidgetBlock instance.
*/
constructor() {
super("modewidget");
this.setPalette("widgets", activity);
this.beginnerBlock(true);
this.setHelpString([
_("The Custom mode block opens a tool to explore musical mode (the spacing of the notes in a scale)."),
"documentation",
""
]);
//.TRANS: musical mode is the pattern of half-steps in an octave, e.g., Major or Minor modes
this.formBlock({ name: _("custom mode"), canCollapse: true });
this.makeMacro((x, y) => [
[0, "modewidget", x, y, [null, 1, 4]],
[1, "setkey2", 0, 0, [0, 2, 3, null]],
[2, ["notename", { value: "C" }], 0, 0, [1]],
[3, ["modename", { value: DEFAULTMODE }], 0, 0, [1]],
[4, "hiddennoflow", 0, 0, [0, null]]
]);
}
/**
* Handles the flow of data for the ModeWidget block.
* @param {number[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The output values.
*/
flow(args, logo, turtle, blk) {
logo.insideModeWidget = true;
const listenerName = "_modewidget_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.modeWidget = new ModeWidget(activity);
logo.insideModeWidget = false;
};
logo.setTurtleListener(turtle, listenerName, __listener);
if (args.length === 1) return [args[0], 1];
}
}
/**
* Represents a block for setting the tempo.
* @extends StackClampBlock
*/
class TempoBlock extends StackClampBlock {
/**
* Creates a TempoBlock instance.
*/
constructor() {
super("tempo");
this.setPalette("widgets", activity);
this.beginnerBlock(true);
this.setHelpString([
_("The Tempo block opens a metronome to visualize the beat."),
"documentation",
null,
"tempo"
]);
//.TRANS: the speed at music is should be played.
this.formBlock({ name: _("tempo"), canCollapse: true });
this.makeMacro((x, y) => [
[0, "tempo", x, y, [null, 1, 6]],
[1, "setmasterbpm2", 0, 0, [0, 2, 3, 7]],
[2, ["number", { value: 90 }], 0, 0, [1]],
[3, "divide", 0, 0, [1, 4, 5]],
[4, ["number", { value: 1 }], 0, 0, [3]],
[5, ["number", { value: 4 }], 0, 0, [3]],
[6, "hiddennoflow", 0, 0, [0, null]],
[7, "vspace", 0, 0, [1, null]]
]);
}
/**
* Handles the flow of data for the Tempo block.
* @param {number[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The output values.
*/
flow(args, logo, turtle, blk) {
if (logo.tempo === null) {
logo.tempo = new Tempo();
}
logo.inTempo = true;
logo.tempo.BPMBlocks = [];
logo.tempo.BPMs = [];
const listenerName = "_tempo_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.tempo.init(activity);
};
logo.setTurtleListener(turtle, listenerName, __listener);
return [args[0], 1];
}
}
/**
* Represents a block for composing chord sequences using the Arpeggio Widget.
* @extends StackClampBlock
*/
class ArpeggioMatrixBlock extends StackClampBlock {
/**
* Creates an ArpeggioMatrixBlock instance.
*/
constructor() {
super("arpeggiomatrix");
this.setPalette("widgets", activity);
this.setHelpString([
_("The Arpeggio Widget is used to compose chord sequences."),
"documentation",
""
]);
this.formBlock({
name: _("arpeggio"),
canCollapse: true,
args: 1,
defaults: [4]
});
this.makeMacro((x, y) => [
[0, "arpeggiomatrix", x, y, [null, 1, 3, 2]],
[1, ["number", { value: 4 }], 0, 0, [0]],
[2, "hiddennoflow", 0, 0, [0, null]],
[3, "newnote", 0, 0, [0, 4, 7, null]],
[4, "divide", 0, 0, [3, 5, 6]],
[5, ["number", { value: 1 }], 0, 0, [4]],
[6, ["number", { value: 16 }], 0, 0, [4]],
[7, "vspace", 0, 0, [3, 8]],
[8, "pitch", 0, 0, [7, 9, 10, null]],
[9, ["solfege", { value: "do" }], 0, 0, [8]],
[10, ["number", { value: 4 }], 0, 0, [8]]
]);
}
/**
* Handles the flow of data for the ArpeggioMatrix block.
* @param {number[]} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {number[]} - The output values.
*/
flow(args, logo, turtle, blk) {
if (logo.arpeggio === null) {
logo.arpeggio = new Arpeggio();
}
logo.inArpeggio = true;
if (args.length > 0) {
if (args[0] > 1 && args[0] < 21) {
logo.arpeggio.defaultCols = args[0];
}
}
logo.arpeggio.notesToPlay = [];
const listenerName = "_arpeggio_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.arpeggio.init(activity);
};
logo.setTurtleListener(turtle, listenerName, __listener);
return [args[1], 1];
}
}
/**
* Represents a block for mapping pitches to drum sounds using a matrix.
* @extends StackClampBlock
*/
class PitchDrumMatrixBlock extends StackClampBlock {
/**
* Creates a PitchDrumMatrixBlock instance.
*/
constructor() {
super("pitchdrummatrix");
this.setPalette("widgets", activity);
this.setHelpString([
_("The Pitch drum matrix is used to map pitches to drum sounds."),
"documentation",
""
]);
//.TRANS: makes a mapping between pitches and drum sounds
this.formBlock({ name: _("pitch-drum mapper"), canCollapse: true });
this.makeMacro((x, y) => [
[0, "pitchdrummatrix", x, y, [null, 1, 16]],
[1, "pitch", 0, 0, [0, 2, 3, 4]],
[2, ["solfege", { value: "sol" }], 0, 0, [1]],
[3, ["number", { value: 4 }], 0, 0, [1]],
[4, "pitch", 0, 0, [1, 5, 6, 7]],
[5, ["solfege", { value: "mi" }], 0, 0, [4]],
[6, ["number", { value: 4 }], 0, 0, [4]],
[7, "pitch", 0, 0, [4, 8, 9, 10]],
[8, ["solfege", { value: "re" }], 0, 0, [7]],
[9, ["number", { value: 4 }], 0, 0, [7]],
[10, "playdrum", 0, 0, [7, 11, 12]],
[11, ["drumname", { value: "kick drum" }], 0, 0, [10]],
[12, "playdrum", 0, 0, [10, 13, 14]],
[13, ["drumname", { value: "snare drum" }], 0, 0, [12]],
[14, "playdrum", 0, 0, [12, 15, null]],
[15, ["drumname", { value: "ride bell" }], 0, 0, [14]],
[16, "hiddennoflow", 0, 0, [0, null]]
]);
}
/**
* Handles the flow of data for the pitch drum matrix block.
* @param {array} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
*/
flow(args, logo, turtle, blk) {
if (logo.pitchDrumMatrix === null) {
logo.pitchDrumMatrix = new PitchDrumMatrix();
}
logo.inPitchDrumMatrix = true;
logo.pitchDrumMatrix.rowLabels = [];
logo.pitchDrumMatrix.rowArgs = [];
logo.pitchDrumMatrix.drums = [];
logo.pitchDrumMatrix.clearBlocks();
const listenerName = "_pitchdrummatrix_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
if (
logo.pitchDrumMatrix.drums.length === 0 ||
logo.pitchDrumMatrix.rowLabels.length === 0
) {
activity.errorMsg(
_("You must have at least one pitch block and one drum block in the matrix."),
blk
);
} else {
// Process queued up rhythms.
logo.pitchDrumMatrix.init(activity);
logo.pitchDrumMatrix.makeClickable();
}
};
logo.setTurtleListener(turtle, listenerName, __listener);
if (args.length === 1) return [args[0], 1];
}
}
/**
* Represents a block for generating pitches at selected frequencies using a slider.
* @extends StackClampBlock
*/
class PitchSliderBlock extends StackClampBlock {
/**
* Creates a PitchSliderBlock instance.
*/
constructor() {
super("pitchslider");
this.setPalette("widgets", activity);
this.beginnerBlock(true);
this.setHelpString([
_("The Pitch slider tool to is used to generate pitches at selected frequencies."),
"documentation",
""
]);
//.TRANS: widget to generate pitches using a slider
this.formBlock({ name: _("pitch slider"), canCollapse: true });
this.makeMacro((x, y) => [
[0, "pitchslider", x, y, [null, 1, 3]],
[1, "hertz", 0, 0, [0, 2, null]],
[2, ["number", { value: 392 }], 0, 0, [1]],
[3, "hiddennoflow", 0, 0, [0, null]]
]);
}
/**
* Handles the flow of data for the pitch slider block.
* @param {array} args - The arguments passed to the block.
* @param {object} logo - The logo object.
* @param {object} turtle - The turtle object.
* @param {object} blk - The block object.
* @returns {array} - The output array.
*/
flow(args, logo, turtle, blk) {
if (logo.pitchSlider === null) {
logo.pitchSlider = new PitchSlider();
}
logo.inPitchSlider = true;
logo.pitchSlider.frequencies = [];
const listenerName = "_pitchslider_" + turtle;
logo.setDispatchBlock(blk, turtle, listenerName);
const __listener = () => {
logo.pitchSlider.init(activity);
logo.inPitchSlider = false;
};
logo.setTurtleListener(turtle, listenerName, __listener);
return [args[0], 1];
}
}
/**
* Represents a block for controlling a chromatic keyboard.
* @extends StackClampBlock
*/
class ChromaticBlock extends StackClampBlock {
/**
* Creates a ChromaticBlock instance.
*/
constructor() {
super("chromatic");
this.setPalette("widgets", activity);
this.setHelpString();
this.formBlock({
name: _("chromatic keyboard"),
canCollapse: true
});
this.makeMacro((x, y) => [
[0, ["musickeyboard", { collapsed: false }], x, y, [null, 2, 1]],
[1, ["hiddennoflow", {}], 0, 0, [0, null]],
[2, ["setkey2", {}], 0, 0, [0, 3, 4, 5]],
[3, ["notename", { value: "C" }], 0, 0, [2]],
[4, ["modename", { value: "chromatic" }], 0, 0, [2]],
[5, ["pitch", {}], 0, 0, [2, 6, 7, 8]],
[6, ["solfege", { value: "do" }], 0, 0, [5]],
[7, ["number", { value: 5 }], 0, 0, [5]],
[8, ["repeat", {}], 0, 0, [5, 9, 10, null]],
[9, ["modelength", {}], 0, 0, [8]],
[10, ["steppitch", {}], 0, 0, [8, 11, null]],
[11, ["number", { value: -1 }], 0, 0, [10]]
]);
}
flow() {}
}
/**
* Represents a block for controlling a music keyboard.