-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhost.js
3257 lines (2727 loc) · 157 KB
/
host.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
/*
* copyright 2015 James Ingram
* https://james-ingram-act-two.de/
*
* Code licensed under MIT
*
* This file contains the implementation of the ResidentSynthHost's GUI.
*/
var ResSynth = ResSynth || {};
ResSynth.host = (function(document)
{
"use strict";
var
synth = null,
inputDevice = null,
currentChannel = 0,
allLongInputControls = [], // used by AllControllersOff control
channelPerKeyArrays = [], // initialized by getChannelPerKeyArrays(). This array has elements in range 0..15. Its length can be either 0 or 128.
currentChannelPerKeyArray,
globalChannelPressureType = "unused", // set by channelPressureSelect
globalChannelPressureFactor = 0, // fraction set by channelPressureSensitivityLongControl
presetRecordings = [], // the recordings in recordings.js, converted
// set by startRecording(), undefined by stopRecording() or discardRecording()
settingsBeforeRecording = undefined, // array of 16 currentSettings objects
recordedMessages = undefined, // array of 16 messages arrays
// set by stopRecording(), undefined by saveRecording() or discardRecording()
currentRecording = undefined,
recordingChannelIndices = undefined, // initialized by onplayRecordingButtonInputClick(), reset by restoreStateAfterRecording()
// used while playing back.
cancelPlayback = false,
recordingNoteOnError = false, // true if an attempt is made to record a NoteOn message into an existing recording.
getElem = function(elemID)
{
return document.getElementById(elemID);
},
sendMessage = function(msg, channelIndex)
{
synth.send(msg);
if(recordedMessages !== undefined)
{
let msPositionReRecording = performance.now();
recordedMessages[channelIndex].push({msg, msPositionReRecording});
}
},
throwError = function(errorString)
{
alert(errorString);
throw errorString;
},
setOptions = function(select, options)
{
var i;
for(i = select.options.length - 1; i >= 0; --i)
{
select.remove(i);
}
for(i = 0; i < options.length; ++i)
{
select.add(options[i]);
}
select.selectedIndex = 0;
},
sendLongControl = function(controlIndex, value)
{
let msg = new Uint8Array([ResSynth.constants.COMMAND.CONTROL_CHANGE + currentChannel, controlIndex, value]);
sendMessage(msg, currentChannel);
},
sendShortControl = function(controlIndex)
{
function resetGUILongControllersAndSendButton()
{
let sendButtonInput = getElem("sendButtonInput");
if(sendButtonInput.disabled === true)
{
sendButtonInput.disabled = false;
}
for(let i = 0; i < allLongInputControls.length; ++i)
{
let longInputControl = allLongInputControls[i];
longInputControl.setValue(longInputControl.numberInputElem.defaultValue);
}
}
if(controlIndex === ResSynth.constants.CONTROL.ALL_CONTROLLERS_OFF)
{
resetGUILongControllersAndSendButton();
}
// controlIndex === ResSynth.constants.CONTROL.ALL_CONTROLLERS_OFF || controlIndex === ResSynth.constants.CONTROL.ALL_SOUND_OFF
let msg = new Uint8Array([ResSynth.constants.COMMAND.CONTROL_CHANGE + currentChannel, controlIndex]);
sendMessage(msg, currentChannel);
},
setInputDeviceEventListener = function(inputDeviceSelect)
{
function handleInputMessage(e)
{
// Rectify performed velocities so that they are in range [6..127].
// The velocities generated by my E-MU keyboard are in range [20..127]
// So: (deviceVelocity - 20) is in range [0..107],
// ((deviceVelocity - 20) * 121 / 107) is in range [0..121],
// (121 / 107) is ca. 1.1308
// and (6 + Math.round(((deviceVelocity - 20) * 1.1308))) is in range [6..127]
// (The E-MU keyboard's velocity response curve is set to its curve option number 5.)
function getRectifiedEMUVelocity(deviceVelocity)
{
let rectifiedVelocity = deviceVelocity; // can be 0 (E-MU sends 0 for NoteOff)
if(rectifiedVelocity >= 20)
{
rectifiedVelocity = 6 + Math.round((deviceVelocity - 20) * 1.1308);
}
console.log("emuVel=" + deviceVelocity.toString() + " vel=" + rectifiedVelocity.toString());
return rectifiedVelocity;
}
function updateGUI_ControlsTable(ccIndex, ccValue)
{
let longInputControls = allLongInputControls.filter(elem => elem.numberInputElem.ccIndex === ccIndex);
// longInputControls.length will be > 1 if there is more than one control has no ccIndex.
// This function simply updates all the regParam controls even though only one of them has changed.
for(let i = 0; i < longInputControls.length; i++)
{
let longInputControl = longInputControls[i];
longInputControl.setValue(ccValue);
}
}
function updateGUI_CommandsTable(cmdIndex, cmdValue)
{
let longInputControl = allLongInputControls.find(elem => elem.numberInputElem.cmdIndex === cmdIndex);
longInputControl.setValue(cmdValue);
}
function midiValue(arg)
{
arg = (arg < 0) ? 0 : arg;
arg = (arg > 127) ? 127 : arg;
return arg;
}
let HOST_CMD_CHANNEL_PRESSURE = 0xD0,
CMD = ResSynth.constants.COMMAND,
data = e.data,
command = data[0] & 0xF0,
channel = (currentChannelPerKeyArray.length > 0) ? currentChannelPerKeyArray[data[1]] : data[0] & 0xF,
msg = new Uint8Array([((command + channel) & 0xFF), data[1], data[2]]);
currentChannel = channel;
switch(command)
{
case CMD.NOTE_OFF:
break;
case CMD.NOTE_ON:
if(inputDevice.name.localeCompare("E-MU Xboard49") === 0)
{
msg[2] = getRectifiedEMUVelocity(msg[2]);
}
//console.log("NoteOn: key=" + data[1] + ", velocity=" + data[2]);
break;
case CMD.CONTROL_CHANGE:
updateGUI_ControlsTable(data[1], data[2]);
//console.log("control change: " + getMsgString(data));
break;
case CMD.PRESET:
//console.log("preset: " + getMsgString(data));
break;
case CMD.PITCHWHEEL:
// This host uses pitchWheel values in range 0..127, so data[1] (the fine byte) is ignored here.
// But note that the residentSynth _does_ use both data[1] and data[2] when responding
// to PITCHWHEEL messages (including those that come from the E-MU keyboard), so PITCHWHEEL
// messages sent from this host's GUI use a data[1] value that is calculated on the fly.
updateGUI_CommandsTable(command, data[2]);
//console.log("pitchWheel: value=" + data[2]);
break;
case HOST_CMD_CHANNEL_PRESSURE: // Host ChannelPressure (ResidentSynth does not implement ChannelPressure)
{
if(globalChannelPressureType === "unused")
{
return
}
else
{
let CTL = ResSynth.constants.CONTROL,
channel = currentChannel,
hostChannelSettings = getElem("channelSelect").options[currentChannel].hostSettings,
channelPressureComponent = Math.round(data[1] * globalChannelPressureFactor);
// console.log(`channelPressure = ${data[1]}, globalChannelPressureFactor = ${globalChannelPressureFactor}, channelPressureComponent = ${channelPressureComponent}`);
// The residentSynth does not process CHANNEL_PRESSURE messages.
// They are ignored or converted here.
switch(globalChannelPressureType)
{
case "pitchWheelUp":
{
msg[0] = CMD.PITCHWHEEL + channel;
msg[1] = midiValue(hostChannelSettings.pitchWheel + channelPressureComponent);
msg[2] = msg[1];
break;
}
case "pitchWheelDown":
{
msg[0] = CMD.PITCHWHEEL + channel;
msg[1] = midiValue(hostChannelSettings.pitchWheel - channelPressureComponent);
msg[2] = msg[1];
break;
}
case "modWheelUp":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.MODWHEEL;
msg[2] = midiValue(hostChannelSettings.modWheel + channelPressureComponent);
break;
}
case "modWheelDown":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.MODWHEEL;
msg[2] = midiValue(hostChannelSettings.modWheel - channelPressureComponent);
break;
}
case "volumeUp":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.VOLUME;
msg[2] = midiValue(hostChannelSettings.volume + channelPressureComponent);
break;
}
case "volumeDown":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.VOLUME;
msg[2] = midiValue(hostChannelSettings.volume - channelPressureComponent);
break;
}
case "expressionUp":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.EXPRESSION;
msg[2] = midiValue(hostChannelSettings.expression + channelPressureComponent);
break;
}
case "expressionDown":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.EXPRESSION;
msg[2] = midiValue(hostChannelSettings.expression - channelPressureComponent);
break;
}
case "panLeft":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.PAN;
msg[2] = midiValue(hostChannelSettings.pan - channelPressureComponent);
break;
}
case "panRight":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.PAN;
msg[2] = midiValue(hostChannelSettings.pan + channelPressureComponent);
break;
}
case "reverberationUp":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.REVERBERATION;
msg[2] = midiValue(hostChannelSettings.reverberation + channelPressureComponent);
break;
}
case "reverberationDown":
{
msg[0] = CMD.CONTROL_CHANGE + channel;
msg[1] = CTL.REVERBERATION;
msg[2] = midiValue(hostChannelSettings.reverberation - channelPressureComponent);
break;
}
}
}
}
}
if(recordedMessages !== undefined
&& recordingChannelIndices !== undefined && recordingChannelIndices.includes(currentChannel)
&& command === CMD.NOTE_ON)
{
cancelPlayback = true;
recordingNoteOnError = true;
}
else
{
sendMessage(msg, currentChannel);
}
}
// if(inputDevice !== null) // 21.06.2024 replace by "if(inputDevice)". (undefined and null are falsy)
if(inputDevice) // 29.06.2024: both undefined and null are falsy
{
inputDevice.removeEventListener("midimessage", handleInputMessage, false);
inputDevice.close()
.then((device) => {console.log("Closed " + device.name);})
.catch((device) => {console.error("Error closing " + device.name);});
}
inputDevice = inputDeviceSelect.options[inputDeviceSelect.selectedIndex].inputDevice;
if(inputDevice) // 29.06.2024: both undefined and null are falsy
{
inputDevice.addEventListener("midimessage", handleInputMessage, false);
inputDevice.open()
.then((device) => {console.log("Opened " + device.name);})
.catch((device) => {console.error("Error opening " + device.name);});
}
else
{
// 29.06.2024 deleted the following line
// throwError("Error: the input device is not set in the device select control.");
inputDeviceSelect.disabled = true;
}
},
// exported
onInputDeviceSelectChanged = function()
{
let inputDeviceSelect = getElem("inputDeviceSelect");
setInputDeviceEventListener(inputDeviceSelect);
},
// exported
// See: https://developer.chrome.com/blog/audiocontext-setsinkid/
onAudioDeviceSelectChanged = function()
{
let audioDeviceSelect = getElem("audioDeviceSelect"),
option = audioDeviceSelect.options[audioDeviceSelect.selectedIndex],
deviceId = (option.deviceId === "default") ? "" : option.deviceId;
synth.setAudioOutputDevice(deviceId);
},
// Called by 'gitHub' and 'website' buttons
openInNewTab = function(url)
{
var win = window.open(url, '_blank');
win.focus();
},
// exported
webAudioFontWebsiteButtonClick = function()
{
let bankSelect = getElem("bankSelect"),
selectedOption = bankSelect[bankSelect.selectedIndex];
openInNewTab(selectedOption.url);
},
// exported
onChannelSelectChanged = function()
{
function setAndSendWebAudioFontDivControls(hostChannelSettings)
{
let bankSelect = getElem("bankSelect");
bankSelect.selectedIndex = hostChannelSettings.bankIndex; // index in bankSelect
// set the soundFont in the synth, and the presetSelect then call onPresetSelectChanged() (which calls onMixtureSelectChanged())
onBankSelectChanged();
}
function setAndSendTuningDivControls(hostChannelSettings)
{
let tuningGroupSelect = getElem("tuningGroupSelect");
tuningGroupSelect.selectedIndex = hostChannelSettings.tuningGroupIndex;
// set the tuningSelect then call onTuningtSelectChanged()
// (which calls onSemitonesOffsetNumberInputChanged() and onCentsOffsetNumberInputChanged())
onTuningGroupSelectChanged();
}
function setAndSendLongControls(hostChannelSettings)
{
let pitchWheelLC = getElem("pitchWheelLongControl"),
modWheelLC = getElem("modWheelLongControl"),
volumeLC = getElem("volumeLongControl"),
expressionLC = getElem("expressionLongControl"),
panLC = getElem("panLongControl"),
reverberationLC = getElem("reverberationLongControl"),
pitchWheelSensitivityLC = getElem("pitchWheelSensitivityLongControl"),
velocityPitchSensitivityLC = getElem("velocityPitchSensitivityLongControl");
pitchWheelLC.setValue(hostChannelSettings.pitchWheel);
modWheelLC.setValue(hostChannelSettings.modWheel);
volumeLC.setValue(hostChannelSettings.volume);
expressionLC.setValue(hostChannelSettings.expression);
panLC.setValue(hostChannelSettings.pan);
reverberationLC.setValue(hostChannelSettings.reverberation);
pitchWheelSensitivityLC.setValue(hostChannelSettings.pitchWheelSensitivity);
velocityPitchSensitivityLC.setValue(hostChannelSettings.velocityPitchSensitivity);
}
function setAndSendOrnamentsDivControls(hostChannelSettings)
{
let ornamentsSelect = getElem("ornamentsSelect");
ornamentsSelect.selectedIndex = hostChannelSettings.keyboardOrnamentsArrayIndex;
onOrnamentsSelectChanged();
}
let channelSelect = getElem("channelSelect"),
startRecordingButtonInput = getElem("startRecordingButtonInput"),
stopRecordingButtonInput = getElem("stopRecordingButtonInput"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings;
currentChannel = channel; // the global currentChannel is used when constructing all midi messages
setAndSendWebAudioFontDivControls(hostChannelSettings);
setAndSendTuningDivControls(hostChannelSettings);
setAndSendLongControls(hostChannelSettings);
setAndSendOrnamentsDivControls(hostChannelSettings);
startRecordingButtonInput.value = "start recording ch" + channel.toString();
stopRecordingButtonInput.value = "stop recording ch" + channel.toString();
},
// exported
onBankSelectChanged = function()
{
function getBankIndexMsg(channel, bankIndex)
{
return new Uint8Array([ResSynth.constants.COMMAND.CONTROL_CHANGE + channel, ResSynth.constants.CONTROL.BANK, bankIndex]);
}
function getPresetIndexMsg(channel, presetIndex)
{
return new Uint8Array([ResSynth.constants.COMMAND.PRESET + channel, presetIndex]);
}
let bankSelect = getElem("bankSelect"),
channelSelect = getElem("channelSelect"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
presetSelect = getElem("presetSelect"),
selectedBankOption = bankSelect[bankSelect.selectedIndex],
presetOptionsArray = selectedBankOption.presetOptionsArray,
bankIndexMsg = getBankIndexMsg(channel, bankSelect.selectedIndex),
hostChannelPresetIndex = (hostChannelSettings.presetIndex < presetOptionsArray.length) ? hostChannelSettings.presetIndex : 0,
presetIndexMsg = getPresetIndexMsg(channel, hostChannelPresetIndex);
sendMessage(bankIndexMsg, channel);
sendMessage(presetIndexMsg, channel);
setOptions(presetSelect, presetOptionsArray);
presetSelect.selectedIndex = hostChannelPresetIndex;
onPresetSelectChanged();
hostChannelSettings.bankIndex = bankSelect.selectedIndex;
},
// exported
onPresetSelectChanged = function()
{
function getPresetMsg(channel, presetIndex)
{
return new Uint8Array([ResSynth.constants.COMMAND.PRESET + channel, presetIndex]);
}
let channelSelect = getElem("channelSelect"),
presetSelect = getElem("presetSelect"),
mixtureSelect = getElem("mixtureSelect"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
presetIndex = presetSelect.selectedIndex,
presetMsg = getPresetMsg(channel, presetIndex);
sendMessage(presetMsg, channel);
mixtureSelect.selectedIndex = hostChannelSettings.mixtureIndex;
onMixtureSelectChanged();
hostChannelSettings.presetIndex = presetSelect.selectedIndex;
},
// exported
onMixtureSelectChanged = function()
{
function getMixtureMsg(channel, mixtureIndex)
{
return new Uint8Array([CMD.CONTROL_CHANGE + channel, CTL.MIXTURE_INDEX, mixtureIndex]);
}
let CMD = ResSynth.constants.COMMAND,
CTL = ResSynth.constants.CONTROL,
channelSelect = getElem("channelSelect"),
mixtureSelect = getElem("mixtureSelect"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
mixtureIndex = mixtureSelect.selectedIndex,
mixtureMessage = getMixtureMsg(channel, mixtureIndex);
sendMessage(mixtureMessage, channel);
hostChannelSettings.mixtureIndex = mixtureIndex;
},
// exported (c.f. onBankSelectChanged() )
onTuningGroupSelectChanged = function()
{
let channelSelect = getElem("channelSelect"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
tuningGroupSelect = getElem("tuningGroupSelect"),
tuningSelect = getElem("tuningSelect"),
selectedTuningGroupOption = tuningGroupSelect[tuningGroupSelect.selectedIndex],
tuningOptionsArray = selectedTuningGroupOption.tuningOptionsArray,
hostChannelTuningIndex = (hostChannelSettings.tuningIndex < tuningOptionsArray.length) ? hostChannelSettings.tuningIndex : 0;
setOptions(tuningSelect, tuningOptionsArray);
tuningSelect.selectedIndex = hostChannelTuningIndex;
onTuningSelectChanged();
hostChannelSettings.tuningGroupIndex = tuningGroupSelect.selectedIndex;
},
// exported
onTuningSelectChanged = function()
{
let channelSelect = getElem("channelSelect"),
tuningGroupIndex = getElem("tuningGroupSelect").selectedIndex,
semitonesOffsetNumberInput = getElem("semitonesOffsetNumberInput"),
centsOffsetNumberInput = getElem("centsOffsetNumberInput"),
tuningSelect = getElem("tuningSelect"),
tuningIndex = tuningSelect.selectedIndex,
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
CONST = ResSynth.constants,
setTuningGroupIndexMsg = new Uint8Array([((currentChannel + CONST.COMMAND.CONTROL_CHANGE) & 0xFF), CONST.CONTROL.TUNING_GROUP_INDEX, tuningGroupIndex]),
setTuningIndexMsg = new Uint8Array([((currentChannel + CONST.COMMAND.CONTROL_CHANGE) & 0xFF), CONST.CONTROL.TUNING_INDEX, tuningIndex]);
sendMessage(setTuningGroupIndexMsg, channel);
sendMessage(setTuningIndexMsg, channel);
semitonesOffsetNumberInput.value = hostChannelSettings.semitonesOffset;
onSemitonesOffsetNumberInputChanged();
centsOffsetNumberInput.value = hostChannelSettings.centsOffset;
onCentsOffsetNumberInputChanged();
hostChannelSettings.tuningIndex = tuningIndex;
},
// exported
onSemitonesOffsetNumberInputChanged = function()
{
let CONST = ResSynth.constants,
channelSelect = getElem("channelSelect"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
semitonesOffsetNumberInput = getElem("semitonesOffsetNumberInput"),
semitonesOffset = parseInt(semitonesOffsetNumberInput.value),
midiValue = semitonesOffsetNumberInput.midiValue(semitonesOffset),
semitonesOffsetMsg = new Uint8Array([((currentChannel + CONST.COMMAND.CONTROL_CHANGE) & 0xFF), CONST.CONTROL.SEMITONES_OFFSET, midiValue]);
sendMessage(semitonesOffsetMsg, channel);
hostChannelSettings.semitonesOffset = semitonesOffset;
},
// exported
onCentsOffsetNumberInputChanged = function()
{
let CONST = ResSynth.constants,
channelSelect = getElem("channelSelect"),
channel = channelSelect.selectedIndex,
hostChannelSettings = channelSelect.options[channel].hostSettings,
centsOffsetNumberInput = getElem("centsOffsetNumberInput"),
centsOffset = parseInt(centsOffsetNumberInput.value);
let midiValue = centsOffsetNumberInput.midiValue(centsOffset),
centsOffsetMsg = new Uint8Array([((channel + CONST.COMMAND.CONTROL_CHANGE) & 0xFF), CONST.CONTROL.CENTS_OFFSET, midiValue]);
sendMessage(centsOffsetMsg, channel);
hostChannelSettings.centsOffset = centsOffset;
},
onSettingsSelectChanged = function() // always updates both host and synth
{
function setGlobalSettingsInHost(settingsSelect, settingsChangePerSection)
{
let selectedIndex = settingsSelect.selectedIndex,
changedSettings = settingsChangePerSection[selectedIndex],
keyboardSplitIndex = changedSettings.keyboardSplitIndex;
if(selectedIndex === 0)
{
console.assert(keyboardSplitIndex !== undefined);
}
if(keyboardSplitIndex !== undefined)
{
let keyboardSplitSelect = getElem("keyboardSplitSelect");
keyboardSplitSelect.selectedIndex = keyboardSplitIndex;
onKeyboardSplitSelectChanged();
}
}
function setChannelSettingsInHostAndSynth(settingsSelect, settingsChangePerSection)
{
function updateChannels(changedSettings)
{
function updateAttribute(attributeName, value)
{
switch(attributeName)
{
case "bankIndex":
{
getElem("bankSelect").selectedIndex = value;
onBankSelectChanged();
break;
}
case "presetIndex":
{
getElem("presetSelect").selectedIndex = value;
onPresetSelectChanged();
break;
}
case "mixtureIndex":
{
getElem("mixtureSelect").selectedIndex = value;
onMixtureSelectChanged();
break;
}
case "tuningGroupIndex":
{
getElem("tuningGroupSelect").selectedIndex = value;
onTuningGroupSelectChanged();
break;
}
case "tuningIndex":
{
getElem("tuningSelect").selectedIndex = value;
onTuningSelectChanged();
break;
}
case "semitonesOffset":
{
getElem("semitonesOffsetNumberInput").value = value;
onSemitonesOffsetNumberInputChanged();
break;
}
case "centsOffset":
{
getElem("centsOffsetNumberInput").value = value;
onCentsOffsetNumberInputChanged();
break;
}
case "pitchWheel":
{
getElem("pitchWheelLongControl").setValue(value); // also sets synth
break;
}
case "modWheel":
{
getElem("modWheelLongControl").setValue(value); // also sets synth
break;
}
case "volume":
{
getElem("volumeLongControl").setValue(value); // also sets synth
break;
}
case "expression":
{
getElem("expressionLongControl").setValue(value); // also sets synth
break;
}
case "pan":
{
getElem("panLongControl").setValue(value); // also sets synth
break;
}
case "reverberation":
{
getElem("reverberationLongControl").setValue(value); // also sets synth
break;
}
case "pitchWheelSensitivity":
{
getElem("pitchWheelSensitivityLongControl").setValue(value); // also sets synth
break;
}
case "velocityPitchSensitivity":
{
getElem("velocityPitchSensitivityLongControl").setValue(value); // also sets synth
break;
}
case "keyboardOrnamentsArrayIndex":
{
getElem("ornamentsSelect").selectedIndex = value;
onOrnamentsSelectChanged();
break;
}
default: throw "error";
}
}
let channelSelect = getElem("channelSelect"),
savedChannel = channelSelect.selectedIndex,
channelSettingsArray = changedSettings.channelSettings;
for(let channel = 0; channel < channelSettingsArray.length; channel++)
{
channelSelect.selectedIndex = channel; // used by the event handling functions in updateAttribute()
currentChannel = channel; // global currentChannel is used to construct messages sent to the synth
let channelSettings = channelSettingsArray[channel],
attributeNames = Object.keys(channelSettings);
// update host and synth channel settings with each defined channel attribute.
for(let keyIndex = 0; keyIndex < attributeNames.length; keyIndex++)
{
let attributeName = attributeNames[keyIndex],
newValue = channelSettings[attributeName];
updateAttribute(attributeName, newValue);
}
}
channelSelect.selectedIndex = savedChannel;
onChannelSelectChanged(); // resets currentChannel and the GUI
}
let selectedIndex = settingsSelect.selectedIndex;
// must start at 0 because user may have changed GUI settings
for(let i = 0; i <= selectedIndex; i++)
{
let changedSettings = settingsChangePerSection[i];
updateChannels(changedSettings);
}
}
let settingsSelect = getElem("settingsSelect");
// The settingsSelect.settingsChangePerSection contains an array of sectionSettings, each of which
// contains only those settings that change from section to section (i.e. over time).
// The sectionSettings object at index 0 contains values that initialize _all_ the attributes in all
// the channels that are defined in synthSettingsDefs.js.
setGlobalSettingsInHost(settingsSelect, settingsSelect.settingsChangePerSection);
setChannelSettingsInHostAndSynth(settingsSelect, settingsSelect.settingsChangePerSection);
settingsSelect.previousIndex = settingsSelect.selectedIndex;
},
// exported
// Exports the current settings in a format similar to that of synthSettingsDefs.js
onExportSettingsButtonClicked = function()
{
function getChangedChannelSettingsArray(hostChannelOptions)
{
function removeTrailingDefaultSettings(defaultSettings, changedChannelSettingsArray)
{
for(let ch = 15; ch >= 0; ch--)
{
if(defaultSettings.isSimilar(changedChannelSettingsArray[ch])) // isSimilar ignores the _comment attributes
{
changedChannelSettingsArray.length -= 1;
}
else
{
break;
}
}
}
function addDefaultComments(defaultSettings, changedChannelSettingsArray)
{
for(let channel = 0; channel < changedChannelSettingsArray.length; channel++)
{
let changedChannelSettings = changedChannelSettingsArray[channel];
if(defaultSettings.isSimilar(changedChannelSettings)) // isSimilar ignores the ._comment attribute
{
changedChannelSettings._comment = changedChannelSettings._comment + ` (default settings)`;
}
}
}
let changedChannelSettingsArray = [],
defaultSettings = new ResSynth.channelSettings.ChannelSettings();
for(let channel = 0; channel < 16; channel++)
{
let hostSettings = hostChannelOptions[channel].hostSettings,
exportChannelSettings = {};
exportChannelSettings._comment = `channel ${channel}`;
exportChannelSettings.bankIndex = hostSettings.bankIndex;
exportChannelSettings.presetIndex = hostSettings.presetIndex;
exportChannelSettings.mixtureIndex = hostSettings.mixtureIndex;
exportChannelSettings.tuningGroupIndex = hostSettings.tuningGroupIndex;
exportChannelSettings.tuningIndex = hostSettings.tuningIndex;
exportChannelSettings.semitonesOffset = hostSettings.semitonesOffset;
exportChannelSettings.centsOffset = hostSettings.centsOffset;
exportChannelSettings.pitchWheel = hostSettings.pitchWheel;
exportChannelSettings.modWheel = hostSettings.modWheel;
exportChannelSettings.volume = hostSettings.volume;
exportChannelSettings.expression = hostSettings.expression;
exportChannelSettings.pan = hostSettings.pan;
exportChannelSettings.reverberation = hostSettings.reverberation;
exportChannelSettings.pitchWheelSensitivity = hostSettings.pitchWheelSensitivity;
exportChannelSettings.velocityPitchSensitivity = hostSettings.velocityPitchSensitivity;
exportChannelSettings.keyboardOrnamentsArrayIndex = hostSettings.keyboardOrnamentsArrayIndex;
changedChannelSettingsArray.push(exportChannelSettings);
}
removeTrailingDefaultSettings(defaultSettings, changedChannelSettingsArray);
addDefaultComments(defaultSettings, changedChannelSettingsArray);
return changedChannelSettingsArray;
}
let hostChannelOptions = getElem("channelSelect").options,
keyboardSplitIndex = getElem("keyboardSplitSelect").selectedIndex,
changedChannelSettingsArray = getChangedChannelSettingsArray(hostChannelOptions),
exportSettings = {};
exportSettings.name = "exported synth settings";
exportSettings.keyboardSplitIndex = keyboardSplitIndex;
if(changedChannelSettingsArray.length < 16)
{
exportSettings._comment = `Channels ${changedChannelSettingsArray.length}..15 contain the default settings`;
}
exportSettings.channelSettingsArray = changedChannelSettingsArray;
const a = document.createElement("a");
a.href = URL.createObjectURL(new Blob([JSON.stringify(exportSettings, null, "\t")], {type: "text/plain"}));
a.setAttribute("download", exportSettings.name + ".json");
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
},
// not exported, this function is bound internally
onQuertyKeyDown = function(event)
{
// increments or rotates the index if toIndex is less than 0 or out of range.
// otherwise sets the index to toIndex.
function setSettingsSelectIndex(toIndex)
{
let settingsSelect = getElem("settingsSelect"),
nOptions = settingsSelect.options.length;
settingsSelect.previousIndex = settingsSelect.selectedIndex;
toIndex = (toIndex >= 0 && toIndex < nOptions) ? toIndex : settingsSelect.previousIndex + 1;
toIndex = (toIndex === nOptions) ? 0 : toIndex;
settingsSelect.selectedIndex = toIndex;
onSettingsSelectChanged();
}
if(event.repeat)
{
return; // stops the reaction to a repeating key
}
let keyCode = event.keyCode;
if(keyCode === 32)
{
// space
console.log(event.key);
// advance/rotate settings selector
setSettingsSelectIndex(-1);
}
else if(keyCode >= 48 && keyCode <= 57)
{
// '0'..'9'
console.log(event.key);
// set the settingsSelect.selectedIndex
setSettingsSelectIndex(keyCode - 48);
}
else if(keyCode >= 65 && keyCode <= 90)
{
// 'a'..'z'
setSettingsSelectIndex(keyCode - 55); // indices 10..35
}
},
// exported
onplayRecordingButtonInputClicked = async function()
{
function getHostChannelState()
{
let channelSelect = getElem("channelSelect"),
channelIndex = channelSelect.selectedIndex,
hostChannelState = {};
hostChannelState.channelIndex = channelIndex;
return hostChannelState;
}
function setInitialRecordingButtonState(b)
{
if(b.startRecordingButtonInput.style.display === "block")
{
setPerformanceGUIState2();
}
else if(b.stopRecordingButtonInput.style.display === "block")
{
setPerformanceGUIState4();
}
else if(b.saveRecordingButtonInput.style.display === "block")
{
setPerformanceGUIState6();
}
}
function setFinalRecordingButtonState(b)
{
if(b.startRecordingButtonInput.style.display === "block")
{
setPerformanceGUIState1();
}
else if(b.stopRecordingButtonInput.style.display === "block")
{
setPerformanceGUIState3();
}
else
{
setPerformanceGUIState5();
}
}
function restoreHostAndSynthChannelState(hostChannelState)
{
let channelSelect = getElem("channelSelect");
channelSelect.selectedIndex = hostChannelState.channelIndex;
onChannelSelectChanged();
}
function getRecordingChannelIndices(currentRecording, presetRecording)
{
let recordingChannelIndices = [],
currentRecordingChannel = -1;
if(currentRecording !== undefined)
{
currentRecordingChannel = currentRecording.channels[0].channel;
recordingChannelIndices.push(currentRecordingChannel);
}
if(presetRecording !== undefined)
{
let channelInfos = presetRecording.channels;
for(let i = 0; i < channelInfos.length; i++)
{
let channel = channelInfos[i].channel;
if(channel != currentRecordingChannel)
{
recordingChannelIndices.push(channel);
}
}
}
recordingChannelIndices.sort((a, b) => a - b);