-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
OutputChannel.js
1836 lines (1610 loc) · 74.9 KB
/
OutputChannel.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
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
import {EventEmitter} from "../node_modules/djipevents/src/djipevents.js";
import {WebMidi} from "./WebMidi.js";
import {Utilities} from "./Utilities.js";
import {Enumerations} from "./Enumerations.js";
/**
* The `OutputChannel` class represents a single output MIDI channel. `OutputChannel` objects are
* provided by an [`Output`](Output) port which, itself, is made available by a device. The
* `OutputChannel` object is derived from the host's MIDI subsystem and should not be instantiated
* directly.
*
* All 16 `OutputChannel` objects can be found inside the parent output's
* [`channels`]{@link Output#channels} property.
*
* @param {Output} output The [`Output`](Output) this channel belongs to.
* @param {number} number The MIDI channel number (`1` - `16`).
*
* @extends EventEmitter
* @license Apache-2.0
* @since 3.0.0
*/
export class OutputChannel extends EventEmitter {
/**
* Creates an `OutputChannel` object.
*
* @param {Output} output The [`Output`](Output) this channel belongs to.
* @param {number} number The MIDI channel number (`1` - `16`).
*/
constructor(output, number) {
super();
/**
* @type {Output}
* @private
*/
this._output = output;
/**
* @type {number}
* @private
*/
this._number = number;
/**
* @type {number}
* @private
*/
this._octaveOffset = 0;
}
/**
* Unlinks the MIDI subsystem, removes all listeners attached to the channel and nulls the channel
* number. This method is mostly for internal use. It has not been prefixed with an underscore
* since it is called by other objects such as the `Output` object.
*
* @private
*/
destroy() {
this._output = null;
this._number = null;
this._octaveOffset = 0;
this.removeListener();
}
/**
* Sends a MIDI message on the MIDI output port. If no time is specified, the message will be
* sent immediately. The message should be an array of 8-bit unsigned integers (`0` - `225`),
* a
* [`Uint8Array`]{@link https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array}
* object or a [`Message`](Message) object.
*
* It is usually not necessary to use this method directly as you can use one of the simpler
* helper methods such as [`playNote()`](#playNote), [`stopNote()`](#stopNote),
* [`sendControlChange()`](#sendControlChange), etc.
*
* Details on the format of MIDI messages are available in the summary of
* [MIDI messages]{@link https://www.midi.org/specifications-old/item/table-1-summary-of-midi-message}
* from the MIDI Manufacturers Association.
*
* @param message {number[]|Uint8Array|Message} A `Message` object, an array of 8-bit unsigned
* integers or a `Uint8Array` object (not available in Node.js) containing the message bytes.
*
* @param {object} [options={}]
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @throws {RangeError} The first byte (status) must be an integer between 128 and 255.
*
* @throws {RangeError} Data bytes must be integers between 0 and 255.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
send(message, options = {time: 0}) {
this.output.send(message, options);
return this;
}
/**
* Sends a MIDI **key aftertouch** message at the scheduled time. This is a key-specific
* aftertouch. For a channel-wide aftertouch message, use
* [`sendChannelAftertouch()`]{@link #sendChannelAftertouch}.
*
* @param target {number|Note|string|number[]|Note[]|string[]} The note(s) for which you are sending
* an aftertouch value. The notes can be specified by using a MIDI note number (`0` - `127`), a
* [`Note`](Note) object, a note identifier (e.g. `C3`, `G#4`, `F-1`, `Db7`) or an array of the
* previous types. When using a note identifier, octave range must be between `-1` and `9`. The
* lowest note is `C-1` (MIDI note number `0`) and the highest note is `G9` (MIDI note number
* `127`).
*
* When using a note identifier, the octave value will be offset by the local
* [`octaveOffset`](#octaveOffset) and by
* [`Output.octaveOffset`](Output#octaveOffset) and [`WebMidi.octaveOffset`](WebMidi#octaveOffset)
* (if those values are not `0`). When using a key number, `octaveOffset` values are ignored.
*
* @param [pressure=0.5] {number} The pressure level (between `0` and `1`). An invalid pressure
* value will silently trigger the default behaviour. If the `rawValue` option is set to `true`,
* the pressure is defined by using an integer between `0` and `127`.
*
* @param {object} [options={}]
*
* @param {boolean} [options.rawValue=false] A boolean indicating whether the value should be
* considered a float between `0` and `1.0` (default) or a raw integer between `0` and `127`.
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @return {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*
* @throws RangeError Invalid key aftertouch value.
*/
sendKeyAftertouch(target, pressure, options = {}) {
if (WebMidi.validation) {
// Legacy support
if (options.useRawValue) options.rawValue = options.useRawValue;
if (isNaN(parseFloat(pressure))) {
throw new RangeError("Invalid key aftertouch value.");
}
if (options.rawValue) {
if (!(pressure >= 0 && pressure <= 127 && Number.isInteger(pressure))) {
throw new RangeError("Key aftertouch raw value must be an integer between 0 and 127.");
}
} else {
if (!(pressure >= 0 && pressure <= 1)) {
throw new RangeError("Key aftertouch value must be a float between 0 and 1.");
}
}
}
// Normalize pressure to integer
if (!options.rawValue) pressure = Utilities.fromFloatTo7Bit(pressure);
// Plot total offset
const offset = WebMidi.octaveOffset + this.output.octaveOffset + this.octaveOffset;
// Make sure we are dealing with an array
if (!Array.isArray(target)) target = [target];
Utilities.buildNoteArray(target).forEach(n => {
this.send(
[
(Enumerations.CHANNEL_MESSAGES.keyaftertouch << 4) + (this.number - 1),
n.getOffsetNumber(offset),
pressure
],
{time: Utilities.toTimestamp(options.time)}
);
});
return this;
}
/**
* Sends a MIDI **control change** message to the channel at the scheduled time. The control
* change message to send can be specified numerically (`0` to `127`) or by using one of the
* following common names:
*
* | Number | Name |
* |--------|-------------------------------|
* | 0 |`bankselectcoarse` |
* | 1 |`modulationwheelcoarse` |
* | 2 |`breathcontrollercoarse` |
* | 4 |`footcontrollercoarse` |
* | 5 |`portamentotimecoarse` |
* | 6 |`dataentrycoarse` |
* | 7 |`volumecoarse` |
* | 8 |`balancecoarse` |
* | 10 |`pancoarse` |
* | 11 |`expressioncoarse` |
* | 12 |`effectcontrol1coarse` |
* | 13 |`effectcontrol2coarse` |
* | 18 |`generalpurposeslider3` |
* | 19 |`generalpurposeslider4` |
* | 32 |`bankselectfine` |
* | 33 |`modulationwheelfine` |
* | 34 |`breathcontrollerfine` |
* | 36 |`footcontrollerfine` |
* | 37 |`portamentotimefine` |
* | 38 |`dataentryfine` |
* | 39 |`volumefine` |
* | 40 |`balancefine` |
* | 42 |`panfine` |
* | 43 |`expressionfine` |
* | 44 |`effectcontrol1fine` |
* | 45 |`effectcontrol2fine` |
* | 64 |`holdpedal` |
* | 65 |`portamento` |
* | 66 |`sustenutopedal` |
* | 67 |`softpedal` |
* | 68 |`legatopedal` |
* | 69 |`hold2pedal` |
* | 70 |`soundvariation` |
* | 71 |`resonance` |
* | 72 |`soundreleasetime` |
* | 73 |`soundattacktime` |
* | 74 |`brightness` |
* | 75 |`soundcontrol6` |
* | 76 |`soundcontrol7` |
* | 77 |`soundcontrol8` |
* | 78 |`soundcontrol9` |
* | 79 |`soundcontrol10` |
* | 80 |`generalpurposebutton1` |
* | 81 |`generalpurposebutton2` |
* | 82 |`generalpurposebutton3` |
* | 83 |`generalpurposebutton4` |
* | 91 |`reverblevel` |
* | 92 |`tremololevel` |
* | 93 |`choruslevel` |
* | 94 |`celestelevel` |
* | 95 |`phaserlevel` |
* | 96 |`dataincrement` |
* | 97 |`datadecrement` |
* | 98 |`nonregisteredparametercoarse` |
* | 99 |`nonregisteredparameterfine` |
* | 100 |`registeredparametercoarse` |
* | 101 |`registeredparameterfine` |
* | 120 |`allsoundoff` |
* | 121 |`resetallcontrollers` |
* | 122 |`localcontrol` |
* | 123 |`allnotesoff` |
* | 124 |`omnimodeoff` |
* | 125 |`omnimodeon` |
* | 126 |`monomodeon` |
* | 127 |`polymodeon` |
*
* As you can see above, not all control change message have a matching name. This does not mean
* you cannot use the others. It simply means you will need to use their number
* (`0` to `127`) instead of their name. While you can still use them, numbers `120` to `127` are
* usually reserved for *channel mode* messages. See
* [`sendChannelMode()`]{@link OutputChannel#sendChannelMode} method for more info.
*
* To view a detailed list of all available **control change** messages, please consult "Table 3 -
* Control Change Messages" from the [MIDI Messages](
* https://www.midi.org/specifications/item/table-3-control-change-messages-data-bytes-2)
* specification.
*
* **Note**: messages #0-31 (MSB) are paired with messages #32-63 (LSB). For example, message #1
* (`modulationwheelcoarse`) can be accompanied by a second control change message for
* `modulationwheelfine` to achieve a greater level of precision. if you want to specify both MSB
* and LSB for messages between `0` and `31`, you can do so by passing a 2-value array as the
* second parameter.
*
* @param {number|string} controller The MIDI controller name or number (`0` - `127`).
*
* @param {number|number[]} value The value to send (0-127). You can also use a two-position array
* for controllers 0 to 31. In this scenario, the first value will be sent as usual and the second
* value will be sent to the matching LSB controller (which is obtained by adding 32 to the first
* controller)
*
* @param {object} [options={}]
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @throws {RangeError} Controller numbers must be between 0 and 127.
* @throws {RangeError} Invalid controller name.
* @throws {TypeError} The value array must have a length of 2.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*
* @license Apache-2.0
* @since 3.0.0
*/
sendControlChange(controller, value, options = {}) {
if (typeof controller === "string") {
controller = Utilities.getCcNumberByName(controller);
}
if (!Array.isArray(value)) value = [value];
if (WebMidi.validation) {
if (controller === undefined) {
throw new TypeError(
"Control change must be identified with a valid name or an integer between 0 and 127."
);
}
if (!Number.isInteger(controller) || !(controller >= 0 && controller <= 127)) {
throw new TypeError("Control change number must be an integer between 0 and 127.");
}
value = value.map(item => {
const output = Math.min(Math.max(parseInt(item), 0), 127);
if (isNaN(output)) throw new TypeError("Values must be integers between 0 and 127");
return output;
});
if (value.length === 2 && controller >= 32) {
throw new TypeError("To use a value array, the controller must be between 0 and 31");
}
}
value.forEach((item, index) => {
this.send(
[
(Enumerations.CHANNEL_MESSAGES.controlchange << 4) + (this.number - 1),
controller + (index * 32),
value[index]
],
{time: Utilities.toTimestamp(options.time)}
);
});
return this;
}
/**
* Selects a MIDI non-registered parameter so it is affected by upcoming data entry, data
* increment and data decrement messages.
*
* @param parameter {number[]} A two-position array specifying the two control bytes that identify
* the registered parameter. The NRPN MSB (99 or 0x63) is a position 0. The NRPN LSB (98 or 0x62)
* is at position 1.
*
* @private
*
* @param {object} [options={}]
*
* @param {number|string} [options.time] If `time` is a string prefixed with `"+"` and followed by
* a number, the message will be delayed by that many milliseconds. If the value is a number, the
* operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
_selectNonRegisteredParameter(parameter, options = {}) {
// parameter[0] = Math.floor(parameter[0]);
// if (!(parameter[0] >= 0 && parameter[0] <= 127)) {
// throw new RangeError("The control63 value must be between 0 and 127.");
// }
//
// parameter[1] = Math.floor(parameter[1]);
// if (!(parameter[1] >= 0 && parameter[1] <= 127)) {
// throw new RangeError("The control62 value must be between 0 and 127.");
// }
this.sendControlChange(0x63, parameter[0], options);
this.sendControlChange(0x62, parameter[1], options);
return this;
}
/**
* Deselects the currently active MIDI registered parameter so it is no longer affected by data
* entry, data increment and data decrement messages.
*
* Current best practice recommends doing that after each call to
* [_setCurrentParameter()]{@link #_setCurrentParameter}.
*
* @private
*
* @param {object} [options={}]
*
* @param {number|string} [options.time] If `time` is a string prefixed with `"+"` and followed by
* a number, the message will be delayed by that many milliseconds. If the value is a number, the
* operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
_deselectRegisteredParameter(options = {}) {
this.sendControlChange(0x65, 0x7F, options);
this.sendControlChange(0x64, 0x7F, options);
return this;
}
/**
* Deselects the currently active MIDI non-registered parameter so it is no longer affected by
* data entry, data increment and data decrement messages.
*
* @private
*
* @param {object} [options={}]
*
* @param {number|string} [options.time] If `time` is a string prefixed with `"+"` and followed by
* a number, the message will be delayed by that many milliseconds. If the value is a number, the
* operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
_deselectNonRegisteredParameter(options = {}) {
this.sendControlChange(0x65, 0x7F, options);
this.sendControlChange(0x64, 0x7F, options);
return this;
}
/**
* Selects a MIDI registered parameter so it is affected by upcoming data entry, data increment
* and data decrement messages.
*
* @private
*
* @param parameter {number[]} A two-position array of integers specifying the two control bytes
* (0x65, 0x64) that identify the registered parameter. The integers must be between 0 and 127.
*
* @param {object} [options={}]
*
* @param {number|string} [options.time] If `time` is a string prefixed with `"+"` and followed by
* a number, the message will be delayed by that many milliseconds. If the value is a number, the
* operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
_selectRegisteredParameter(parameter, options = {}) {
this.sendControlChange(0x65, parameter[0], options);
this.sendControlChange(0x64, parameter[1], options);
return this;
}
/**
* Sets the value of the currently selected MIDI registered parameter.
*
* @private
*
* @param data {number|number[]}
*
* @param {object} [options={}]
*
* @param {number|string} [options.time] If `time` is a string prefixed with `"+"` and followed by
* a number, the message will be delayed by that many milliseconds. If the value is a number, the
* operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
_setCurrentParameter(data, options = {}) {
data = [].concat(data);
// MSB
// data[0] = parseInt(data[0]);
// if (!isNaN(data[0]) && data[0] >= 0 && data[0] <= 127) {
this.sendControlChange(0x06, data[0], options);
// } else {
// throw new RangeError("The msb value must be between 0 and 127.");
// }
if (data.length < 2) return this;
// LSB
// data[1] = parseInt(data[1]);
// if (!isNaN(data[1]) && data[1] >= 0 && data[1] <= 127) {
this.sendControlChange(0x26, data[1], options);
// } else {
// throw new RangeError("The lsb value must be between 0 and 127.");
// }
return this;
}
/**
* Decrements the specified MIDI registered parameter by 1. Here is the full list of parameter
* names that can be used with this function:
*
* * Pitchbend Range (0x00, 0x00): `"pitchbendrange"`
* * Channel Fine Tuning (0x00, 0x01): `"channelfinetuning"`
* * Channel Coarse Tuning (0x00, 0x02): `"channelcoarsetuning"`
* * Tuning Program (0x00, 0x03): `"tuningprogram"`
* * Tuning Bank (0x00, 0x04): `"tuningbank"`
* * Modulation Range (0x00, 0x05): `"modulationrange"`
* * Azimuth Angle (0x3D, 0x00): `"azimuthangle"`
* * Elevation Angle (0x3D, 0x01): `"elevationangle"`
* * Gain (0x3D, 0x02): `"gain"`
* * Distance Ratio (0x3D, 0x03): `"distanceratio"`
* * Maximum Distance (0x3D, 0x04): `"maximumdistance"`
* * Maximum Distance Gain (0x3D, 0x05): `"maximumdistancegain"`
* * Reference Distance Ratio (0x3D, 0x06): `"referencedistanceratio"`
* * Pan Spread Angle (0x3D, 0x07): `"panspreadangle"`
* * Roll Angle (0x3D, 0x08): `"rollangle"`
*
* @param parameter {String|number[]} A string identifying the parameter's name (see above) or a
* two-position array specifying the two control bytes (0x65, 0x64) that identify the registered
* parameter.
*
* @param {object} [options={}]
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @throws TypeError The specified registered parameter is invalid.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
sendRpnDecrement(parameter, options = {}) {
if (!Array.isArray(parameter)) parameter = Enumerations.REGISTERED_PARAMETERS[parameter];
if (WebMidi.validation) {
if (parameter === undefined) {
throw new TypeError("The specified registered parameter is invalid.");
}
let valid = false;
Object.getOwnPropertyNames(Enumerations.REGISTERED_PARAMETERS).forEach(p => {
if (
Enumerations.REGISTERED_PARAMETERS[p][0] === parameter[0] &&
Enumerations.REGISTERED_PARAMETERS[p][1] === parameter[1]
) {
valid = true;
}
});
if (!valid) throw new TypeError("The specified registered parameter is invalid.");
}
this._selectRegisteredParameter(parameter, options);
this.sendControlChange(0x61, 0, options);
this._deselectRegisteredParameter(options);
return this;
}
/**
* Increments the specified MIDI registered parameter by 1. Here is the full list of parameter
* names that can be used with this function:
*
* * Pitchbend Range (0x00, 0x00): `"pitchbendrange"`
* * Channel Fine Tuning (0x00, 0x01): `"channelfinetuning"`
* * Channel Coarse Tuning (0x00, 0x02): `"channelcoarsetuning"`
* * Tuning Program (0x00, 0x03): `"tuningprogram"`
* * Tuning Bank (0x00, 0x04): `"tuningbank"`
* * Modulation Range (0x00, 0x05): `"modulationrange"`
* * Azimuth Angle (0x3D, 0x00): `"azimuthangle"`
* * Elevation Angle (0x3D, 0x01): `"elevationangle"`
* * Gain (0x3D, 0x02): `"gain"`
* * Distance Ratio (0x3D, 0x03): `"distanceratio"`
* * Maximum Distance (0x3D, 0x04): `"maximumdistance"`
* * Maximum Distance Gain (0x3D, 0x05): `"maximumdistancegain"`
* * Reference Distance Ratio (0x3D, 0x06): `"referencedistanceratio"`
* * Pan Spread Angle (0x3D, 0x07): `"panspreadangle"`
* * Roll Angle (0x3D, 0x08): `"rollangle"`
*
* @param parameter {String|number[]} A string identifying the parameter's name (see above) or a
* two-position array specifying the two control bytes (0x65, 0x64) that identify the registered
* parameter.
*
* @param {object} [options={}]
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @throws TypeError The specified registered parameter is invalid.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
sendRpnIncrement(parameter, options = {}) {
if (!Array.isArray(parameter)) parameter = Enumerations.REGISTERED_PARAMETERS[parameter];
if (WebMidi.validation) {
if (parameter === undefined) {
throw new TypeError("The specified registered parameter is invalid.");
}
let valid = false;
Object.getOwnPropertyNames(Enumerations.REGISTERED_PARAMETERS).forEach(p => {
if (
Enumerations.REGISTERED_PARAMETERS[p][0] === parameter[0] &&
Enumerations.REGISTERED_PARAMETERS[p][1] === parameter[1]
) {
valid = true;
}
});
if (!valid) throw new TypeError("The specified registered parameter is invalid.");
}
this._selectRegisteredParameter(parameter, options);
this.sendControlChange(0x60, 0, options);
this._deselectRegisteredParameter(options);
return this;
}
/**
* Plays a note or an array of notes on the channel. The first parameter is the note to play. It
* can be a single value or an array of the following valid values:
*
* - A [`Note`]{@link Note} object
* - A MIDI note number (integer between `0` and `127`)
* - A note name, followed by the octave (e.g. `"C3"`, `"G#4"`, `"F-1"`, `"Db7"`)
*
* The `playNote()` method sends a **note on** MIDI message for all specified notes. If a
* `duration` is set in the `options` parameter or in the [`Note`]{@link Note} object's
* [`duration`]{@link Note#duration} property, it will also schedule a **note off** message
* to end the note after said duration. If no `duration` is set, the note will simply play until
* a matching **note off** message is sent with [`stopNote()`]{@link OutputChannel#stopNote} or
* [`sendNoteOff()`]{@link OutputChannel#sendNoteOff}.
*
* The execution of the **note on** command can be delayed by using the `time` property of the
* `options` parameter.
*
* When using [`Note`]{@link Note} objects, the durations and velocities defined in the
* [`Note`]{@link Note} objects have precedence over the ones specified via the method's `options`
* parameter.
*
* **Note**: per the MIDI standard, a **note on** message with an attack velocity of `0` is
* functionally equivalent to a **note off** message.
*
* @param note {number|string|Note|number[]|string[]|Note[]} The note(s) to play. The notes can be
* specified by using a MIDI note number (`0` - `127`), a note identifier (e.g. `C3`, `G#4`,
* `F-1`, `Db7`), a [`Note`]{@link Note} object or an array of the previous types. When using a
* note identifier, the octave range must be between `-1` and `9`. The lowest note is `C-1` (MIDI
* note number `0`) and the highest note is `G9` (MIDI note number `127`).
*
* @param {object} [options={}]
*
* @param {number} [options.duration] A positive decimal number larger than `0` representing the
* number of milliseconds to wait before sending a **note off** message. If invalid or left
* undefined, only a **note on** message will be sent.
*
* @param {number} [options.attack=0.5] The velocity at which to play the note (between `0` and
* `1`). If the `rawAttack` option is also defined, it will have priority. An invalid velocity
* value will silently trigger the default of `0.5`.
*
* @param {number} [options.rawAttack=64] The attack velocity at which to play the note (between
* `0` and `127`). This has priority over the `attack` property. An invalid velocity value will
* silently trigger the default of 64.
*
* @param {number} [options.release=0.5] The velocity at which to release the note (between `0`
* and `1`). If the `rawRelease` option is also defined, it will have priority. An invalid
* velocity value will silently trigger the default of `0.5`. This is only used with the
* **note off** event triggered when `options.duration` is set.
*
* @param {number} [options.rawRelease=64] The velocity at which to release the note (between `0`
* and `127`). This has priority over the `release` property. An invalid velocity value will
* silently trigger the default of 64. This is only used with the **note off** event triggered
* when `options.duration` is set.
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
playNote(note, options = {}) {
// Send note on and, optionally, note off message (if duration is a positive number)
this.sendNoteOn(note, options);
const notes = Array.isArray(note) ? note : [note];
for(let note of notes) {
if (parseInt(note.duration) > 0) {
const noteOffOptions = {
time: (Utilities.toTimestamp(options.time) || WebMidi.time) + parseInt(note.duration),
release: note.release,
rawRelease: note.rawRelease
};
this.sendNoteOff(note, noteOffOptions);
} else if (parseInt(options.duration) > 0) {
const noteOffOptions = {
time: (Utilities.toTimestamp(options.time) || WebMidi.time) + parseInt(options.duration),
release: options.release,
rawRelease: options.rawRelease
};
this.sendNoteOff(note, noteOffOptions);
}
}
return this;
}
/**
* Sends a **note off** message for the specified notes on the channel. The first parameter is the
* note. It can be a single value or an array of the following valid values:
*
* - A MIDI note number (integer between `0` and `127`)
* - A note name, followed by the octave (e.g. `"C3"`, `"G#4"`, `"F-1"`, `"Db7"`)
* - A [`Note`]{@link Note} object
*
* The execution of the **note off** command can be delayed by using the `time` property of the
* `options` parameter.
*
* When using [`Note`]{@link Note} objects, the release velocity defined in the
* [`Note`]{@link Note} objects has precedence over the one specified via the method's `options`
* parameter.
*
* @param note {number|string|Note|number[]|string[]|Note[]} The note(s) to stop. The notes can be
* specified by using a MIDI note number (0-127), a note identifier (e.g. C3, G#4, F-1, Db7), a
* [`Note`]{@link Note} object or an array of the previous types. When using a note name, octave
* range must be between -1 and 9. The lowest note is C-1 (MIDI note number 0) and the highest
* note is G9 (MIDI note number 127).
*
* @param {object} [options={}]
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @param {number} [options.release=0.5] The velocity at which to release the note
* (between `0` and `1`). If the `rawRelease` option is also defined, `rawRelease` will have
* priority. An invalid velocity value will silently trigger the default of `0.5`.
*
* @param {number} [options.rawRelease=64] The velocity at which to release the note
* (between `0` and `127`). If the `release` option is also defined, `rawRelease` will have
* priority. An invalid velocity value will silently trigger the default of `64`.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
sendNoteOff(note, options = {}) {
if (WebMidi.validation) {
if (
options.rawRelease != undefined &&
!(options.rawRelease >= 0 && options.rawRelease <= 127)
) {
throw new RangeError("The 'rawRelease' option must be an integer between 0 and 127");
}
if (options.release != undefined && !(options.release >= 0 && options.release <= 1)) {
throw new RangeError("The 'release' option must be an number between 0 and 1");
}
// Legacy compatibility warnings
if (options.rawVelocity) {
options.rawRelease = options.velocity;
console.warn("The 'rawVelocity' option is deprecated. Use 'rawRelease' instead.");
}
if (options.velocity) {
options.release = options.velocity;
console.warn("The 'velocity' option is deprecated. Use 'attack' instead.");
}
}
let nVelocity = 64;
if (options.rawRelease != undefined) {
nVelocity = options.rawRelease;
} else {
if (!isNaN(options.release)) nVelocity = Math.round(options.release * 127);
}
// Plot total octave offset
const offset = WebMidi.octaveOffset + this.output.octaveOffset + this.octaveOffset;
Utilities.buildNoteArray(note, {rawRelease: parseInt(nVelocity)}).forEach(n => {
this.send(
[
(Enumerations.CHANNEL_MESSAGES.noteoff << 4) + (this.number - 1),
n.getOffsetNumber(offset),
n.rawRelease,
],
{time: Utilities.toTimestamp(options.time)}
);
});
return this;
}
/**
* Sends a **note off** message for the specified MIDI note number. The first parameter is the
* note to stop. It can be a single value or an array of the following valid values:
*
* - A MIDI note number (integer between `0` and `127`)
* - A note identifier (e.g. `"C3"`, `"G#4"`, `"F-1"`, `"Db7"`)
* - A [`Note`](Note) object
*
* The execution of the **note off** command can be delayed by using the `time` property of the
* `options` parameter.
*
* @param note {number|Note|string|number[]|Note[]|string[]} The note(s) to stop. The notes can be
* specified by using a MIDI note number (`0` - `127`), a note identifier (e.g. `C3`, `G#4`, `F-1`,
* `Db7`) or an array of the previous types. When using a note identifier, octave range must be
* between `-1` and `9`. The lowest note is `C-1` (MIDI note number `0`) and the highest note is
* `G9` (MIDI note number `127`).
*
* @param {Object} [options={}]
*
* @param {number} [options.release=0.5] The velocity at which to release the note
* (between `0` and `1`). If the `rawRelease` option is also defined, `rawRelease` will have
* priority. An invalid velocity value will silently trigger the default of `0.5`.
*
* @param {number} [options.rawRelease=64] The velocity at which to release the note
* (between `0` and `127`). If the `release` option is also defined, `rawRelease` will have
* priority. An invalid velocity value will silently trigger the default of `64`.
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @returns {Output} Returns the `Output` object so methods can be chained.
*/
stopNote(note, options = {}) {
return this.sendNoteOff(note, options);
}
/**
* Sends a **note on** message for the specified note(s) on the channel. The first parameter is
* the note. It can be a single value or an array of the following valid values:
*
* - A [`Note`]{@link Note} object
* - A MIDI note number (integer between `0` and `127`)
* - A note identifier (e.g. `"C3"`, `"G#4"`, `"F-1"`, `"Db7"`)
*
* When passing a [`Note`]{@link Note}object or a note name, the `octaveOffset` will be applied.
* This is not the case when using a note number. In this case, we assume you know exactly which
* MIDI note number should be sent out.
*
* The execution of the **note on** command can be delayed by using the `time` property of the
* `options` parameter.
*
* When using [`Note`]{@link Note} objects, the attack velocity defined in the
* [`Note`]{@link Note} objects has precedence over the one specified via the method's `options`
* parameter. Also, the `duration` is ignored. If you want to also send a **note off** message,
* use the [`playNote()`]{@link #playNote} method instead.
*
* **Note**: As per the MIDI standard, a **note on** message with an attack velocity of `0` is
* functionally equivalent to a **note off** message.
*
* @param note {number|string|Note|number[]|string[]|Note[]} The note(s) to play. The notes can be
* specified by using a MIDI note number (0-127), a note identifier (e.g. C3, G#4, F-1, Db7), a
* [`Note`]{@link Note} object or an array of the previous types.
*
* @param {object} [options={}]
*
* @param {number|string} [options.time=(now)] If `time` is a string prefixed with `"+"` and
* followed by a number, the message will be delayed by that many milliseconds. If the value is a
* positive number
* ([`DOMHighResTimeStamp`]{@link https://developer.mozilla.org/docs/Web/API/DOMHighResTimeStamp}),
* the operation will be scheduled for that time. The current time can be retrieved with
* [`WebMidi.time`]{@link WebMidi#time}. If `options.time` is omitted, or in the past, the
* operation will be carried out as soon as possible.
*
* @param {number} [options.attack=0.5] The velocity at which to play the note (between `0` and
* `1`). If the `rawAttack` option is also defined, `rawAttack` will have priority. An invalid
* velocity value will silently trigger the default of `0.5`.
*
* @param {number} [options.rawAttack=64] The velocity at which to release the note (between `0`
* and `127`). If the `attack` option is also defined, `rawAttack` will have priority. An invalid
* velocity value will silently trigger the default of `64`.
*
* @returns {OutputChannel} Returns the `OutputChannel` object so methods can be chained.
*/
sendNoteOn(note, options = {}) {
if (WebMidi.validation) {
if (options.rawAttack != undefined && !(options.rawAttack >= 0 && options.rawAttack <= 127)) {
throw new RangeError("The 'rawAttack' option must be an integer between 0 and 127");
}
if (options.attack != undefined && !(options.attack >= 0 && options.attack <= 1)) {
throw new RangeError("The 'attack' option must be an number between 0 and 1");
}
// Legacy compatibility warnings
if (options.rawVelocity) {
options.rawAttack = options.velocity;
options.rawRelease = options.release;
console.warn("The 'rawVelocity' option is deprecated. Use 'rawAttack' or 'rawRelease'.");
}
if (options.velocity) {
options.attack = options.velocity;
console.warn("The 'velocity' option is deprecated. Use 'attack' instead.");
}
}
let nVelocity = 64;
if (options.rawAttack != undefined) {
nVelocity = options.rawAttack;
} else {
if (!isNaN(options.attack)) nVelocity = Math.round(options.attack * 127);
}
// Plot total octave offset
const offset = WebMidi.octaveOffset + this.output.octaveOffset + this.octaveOffset;
Utilities.buildNoteArray(note, {rawAttack: nVelocity}).forEach(n => {
this.send(
[
(Enumerations.CHANNEL_MESSAGES.noteon << 4) + (this.number - 1),
n.getOffsetNumber(offset),
n.rawAttack
],
{time: Utilities.toTimestamp(options.time)}
);
});
return this;
}
/**
* Sends a MIDI **channel mode** message. The channel mode message to send can be specified
* numerically or by using one of the following common names:
*
* | Type |Number| Shortcut Method |
* | ---------------------|------|-------------------------------------------------------------- |
* | `allsoundoff` | 120 | [`sendAllSoundOff()`]{@link #sendAllSoundOff} |
* | `resetallcontrollers`| 121 | [`sendResetAllControllers()`]{@link #sendResetAllControllers} |
* | `localcontrol` | 122 | [`sendLocalControl()`]{@link #sendLocalControl} |
* | `allnotesoff` | 123 | [`sendAllNotesOff()`]{@link #sendAllNotesOff} |
* | `omnimodeoff` | 124 | [`sendOmniMode(false)`]{@link #sendOmniMode} |
* | `omnimodeon` | 125 | [`sendOmniMode(true)`]{@link #sendOmniMode} |
* | `monomodeon` | 126 | [`sendPolyphonicMode("mono")`]{@link #sendPolyphonicMode} |
* | `polymodeon` | 127 | [`sendPolyphonicMode("poly")`]{@link #sendPolyphonicMode} |
*
* **Note**: as you can see above, to make it easier, all channel mode messages also have a matching
* helper method.
*
* It should be noted that, per the MIDI specification, only `localcontrol` and `monomodeon` may
* require a value that's not zero. For that reason, the `value` parameter is optional and
* defaults to 0.