forked from ultraabox/ultrabox_typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatternEditor.ts
More file actions
2622 lines (2333 loc) · 153 KB
/
PatternEditor.ts
File metadata and controls
2622 lines (2333 loc) · 153 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) 2012-2022 John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file.
import { getLocalStorageItem, Chord, Transition, Config } from "../synth/SynthConfig";
import { NotePin, Note, makeNotePin, FilterSettings, Channel, Pattern, Instrument, FilterControlPoint } from "../synth/synth";
import { ColorConfig } from "./ColorConfig";
import { SongDocument } from "./SongDocument";
import { Slider } from "./HTMLWrapper";
import { SongEditor } from "./SongEditor";
import { HTML, SVG } from "imperative-html/dist/esm/elements-strict";
import { ChangeSequence, UndoableChange } from "./Change";
import { ChangeVolume, FilterMoveData, ChangeTempo, ChangePan, ChangeReverb, ChangeDistortion, ChangeOperatorAmplitude, ChangeFeedbackAmplitude, ChangePulseWidth, ChangeDetune, ChangeVibratoDepth, ChangeVibratoSpeed, ChangeVibratoDelay, ChangePanDelay, ChangeChorus, ChangeEQFilterSimplePeak, ChangeNoteFilterSimplePeak, ChangeStringSustain, ChangeEnvelopeSpeed, ChangeSupersawDynamism, ChangeSupersawShape, ChangeSupersawSpread, ChangePitchShift, ChangeChannelBar, ChangeDragSelectedNotes, ChangeEnsurePatternExists, ChangeNoteTruncate, ChangeNoteAdded, ChangePatternSelection, ChangePinTime, ChangeSizeBend, ChangePitchBend, ChangePitchAdded, ChangeArpeggioSpeed, ChangeBitcrusherQuantization, ChangeBitcrusherFreq, ChangeEchoSustain, ChangeEQFilterSimpleCut, ChangeNoteFilterSimpleCut, ChangeFilterMovePoint, ChangeDuplicateSelectedReusedPatterns, ChangeHoldingModRecording, ChangeDecimalOffset } from "./changes";
import { prettyNumber } from "./EditorConfig";
function makeEmptyReplacementElement<T extends Node>(node: T): T {
const clone: T = <T>node.cloneNode(false);
node.parentNode!.replaceChild(clone, node);
return clone;
}
class PatternCursor {
public valid: boolean = false;
public prevNote: Note | null = null;
public curNote: Note | null = null;
public nextNote: Note | null = null;
public pitch: number = 0;
public pitchIndex: number = -1;
public curIndex: number = 0;
public start: number = 0;
public end: number = 0;
public part: number = 0;
public exactPart: number = 0;
public nearPinIndex: number = 0;
public pins: NotePin[] = [];
}
export class PatternEditor {
public controlMode: boolean = false;
public shiftMode: boolean = false;
private readonly _svgNoteBackground: SVGPatternElement = SVG.pattern({ id: "patternEditorNoteBackground" + this._barOffset, x: "0", y: "0", patternUnits: "userSpaceOnUse" });
private readonly _svgDrumBackground: SVGPatternElement = SVG.pattern({ id: "patternEditorDrumBackground" + this._barOffset, x: "0", y: "0", patternUnits: "userSpaceOnUse" });
private readonly _svgModBackground: SVGPatternElement = SVG.pattern({ id: "patternEditorModBackground" + this._barOffset, x: "0", y: "0", patternUnits: "userSpaceOnUse" });
private readonly _svgBackground: SVGRectElement = SVG.rect({ x: "0", y: "0", "pointer-events": "none", fill: "url(#patternEditorNoteBackground" + this._barOffset + ")" });
private _svgNoteContainer: SVGSVGElement = SVG.svg();
private readonly _svgPlayhead: SVGRectElement = SVG.rect({ x: "0", y: "0", width: "4", fill: ColorConfig.playhead, "pointer-events": "none" });
private readonly _selectionRect: SVGRectElement = SVG.rect({ class: "dashed-line dash-move", fill: ColorConfig.boxSelectionFill, stroke: ColorConfig.hoverPreview, "stroke-width": 2, "stroke-dasharray": "5, 3", "fill-opacity": "0.4", "pointer-events": "none", visibility: "hidden" });
private readonly _svgPreview: SVGPathElement = SVG.path({ fill: "none", stroke: ColorConfig.hoverPreview, "stroke-width": "2", "pointer-events": "none" });
public modDragValueLabel: HTMLDivElement = HTML.div({ width: "90", "text-anchor": "start", contenteditable: "true", style: "display: flex, justify-content: center; align-items:center; position:absolute; pointer-events: none;", "dominant-baseline": "central", });
public _svg: SVGSVGElement = SVG.svg({ id:'firstImage', style: `background-image: url(${getLocalStorageItem("customTheme", "")}); background-repeat: no-repeat; background-size: 100% 100%; background-color: ${ColorConfig.editorBackground}; touch-action: none; position: absolute;`, width: "100%", height: "100%" },
SVG.defs(
this._svgNoteBackground,
this._svgDrumBackground,
this._svgModBackground,
),
this._svgBackground,
this._selectionRect,
this._svgNoteContainer,
this._svgPreview,
this._svgPlayhead,
);
public readonly container: HTMLDivElement = HTML.div({ style: "height: 100%; overflow:hidden; position: relative; flex-grow: 1;" }, this._svg, this.modDragValueLabel);
private readonly _defaultModBorder: number = 34;
private readonly _backgroundPitchRows: SVGRectElement[] = [];
private readonly _backgroundDrumRow: SVGRectElement = SVG.rect();
private readonly _backgroundModRow: SVGRectElement = SVG.rect();
private _editorWidth: number;
private _modDragValueLabelLeft: number = 0;
private _modDragValueLabelTop: number = 0;
private _modDragValueLabelWidth: number = 0;
public editingModLabel: boolean = false;
private _modDragStartValue: number = 0;
private _modDragPin: NotePin;
private _modDragNote: Note;
private _modDragSetting: number;
private _modDragLowerBound: number = 0;
private _modDragUpperBound: number = 6;
private _editorHeight: number;
private _partWidth: number;
private _pitchHeight: number = -1;
private _pitchBorder: number;
private _pitchCount: number;
private _mouseX: number = 0;
private _mouseY: number = 0;
private _mouseDown: boolean = false;
private _mouseOver: boolean = false;
private _mouseDragging: boolean = false;
private _mouseHorizontal: boolean = false;
private _usingTouch: boolean = false;
private _copiedPinChannels: NotePin[][] = [];
private _copiedPins: NotePin[];
private _mouseXStart: number = 0;
private _mouseYStart: number = 0;
private _touchTime: number = 0;
private _shiftHeld: boolean = false;
private _dragConfirmed: boolean = false;
private _draggingStartOfSelection: boolean = false;
private _draggingEndOfSelection: boolean = false;
private _draggingSelectionContents: boolean = false;
private _dragTime: number = 0;
private _dragPitch: number = 0;
private _dragSize: number = 0;
private _dragVisible: boolean = false;
private _dragChange: UndoableChange | null = null;
private _changePatternSelection: UndoableChange | null = null;
private _lastChangeWasPatternSelection: boolean = false;
private _cursor: PatternCursor = new PatternCursor();
private _stashCursorPinVols: number[][] = [];
private _pattern: Pattern | null = null;
private _playheadX: number = 0.0;
private _octaveOffset: number = 0;
private _renderedWidth: number = -1;
private _renderedHeight: number = -1;
private _renderedBeatWidth: number = -1;
private _renderedPitchHeight: number = -1;
private _renderedFifths: boolean = false;
private _renderedDrums: boolean = false;
private _renderedMod: boolean = false;
private _renderedRhythm: number = -1;
private _renderedPitchChannelCount: number = -1;
private _renderedNoiseChannelCount: number = -1;
private _renderedModChannelCount: number = -1;
private _followPlayheadBar: number = -1;
constructor(private _doc: SongDocument, private _interactive: boolean, private _barOffset: number) {
for (let i: number = 0; i < Config.pitchesPerOctave; i++) {
const rectangle: SVGRectElement = SVG.rect();
rectangle.setAttribute("x", "1");
rectangle.setAttribute("fill", (i == 0) ? ColorConfig.tonic : ColorConfig.pitchBackground);
this._svgNoteBackground.appendChild(rectangle);
this._backgroundPitchRows[i] = rectangle;
}
this._backgroundDrumRow.setAttribute("x", "1");
this._backgroundDrumRow.setAttribute("y", "1");
this._backgroundDrumRow.setAttribute("fill", ColorConfig.pitchBackground);
this._svgDrumBackground.appendChild(this._backgroundDrumRow);
this._backgroundModRow.setAttribute("fill", ColorConfig.pitchBackground);
this._svgModBackground.appendChild(this._backgroundModRow);
if (this._interactive) {
this._updateCursorStatus();
this._updatePreview();
window.requestAnimationFrame(this._animatePlayhead);
this._svg.addEventListener("mousedown", this._whenMousePressed);
document.addEventListener("mousemove", this._whenMouseMoved);
document.addEventListener("mouseup", this._whenCursorReleased);
this._svg.addEventListener("mouseover", this._whenMouseOver);
this._svg.addEventListener("mouseout", this._whenMouseOut);
this._svg.addEventListener("touchstart", this._whenTouchPressed);
this._svg.addEventListener("touchmove", this._whenTouchMoved);
this._svg.addEventListener("touchend", this._whenCursorReleased);
this._svg.addEventListener("touchcancel", this._whenCursorReleased);
this.modDragValueLabel.addEventListener("input", this._validateModDragLabelInput);
} else {
this._svgPlayhead.style.display = "none";
this._svg.appendChild(SVG.rect({ x: 0, y: 0, width: 10000, height: 10000, fill: ColorConfig.editorBackground, style: "opacity: 0.5;" }));
}
this.resetCopiedPins();
}
private _getMaxPitch(): number {
return this._doc.song.getChannelIsMod(this._doc.channel) ? Config.modCount - 1 : ( this._doc.song.getChannelIsNoise(this._doc.channel) ? Config.drumCount - 1 : Config.maxPitch );
}
private _validateModDragLabelInput = (event: Event): void => {
const label: HTMLDivElement = <HTMLDivElement>event.target;
// Special case - when user is typing a number between zero and min, allow it (the alternative is quite annoying, when min is nonzero)
let converted: number = Number(label.innerText);
if (!isNaN(converted) && converted >= 0 && converted < this._modDragLowerBound)
return;
// Another special case - allow "" e.g. the empty string and a single negative sign, but don't do anything about it.
if (label.innerText != "" && label.innerText != "-") {
// Force NaN results to be 0
if (isNaN(converted)) {
converted = this._modDragLowerBound;
label.innerText = "" + this._modDragLowerBound;
}
let presValue: number = Math.floor(Math.max(Number(this._modDragLowerBound), Math.min(Number(this._modDragUpperBound), converted)));
if (label.innerText != presValue + "")
label.innerText = presValue + "";
// This is me being too lazy to fiddle with the css to get it to align center.
let xOffset: number = (+(presValue >= 10.0)) + (+(presValue >= 100.0)) + (+(presValue < 0.0)) + (+(presValue <= -10.0));
this._modDragValueLabelLeft = +prettyNumber(Math.max(Math.min(this._editorWidth - 10 - xOffset * 8, this._partWidth * (this._modDragNote.start + this._modDragPin.time) - 4 - xOffset * 4), 2));
this.modDragValueLabel.style.setProperty("left", "" + this._modDragValueLabelLeft + "px");
const sequence: ChangeSequence = new ChangeSequence();
this._dragChange = sequence;
this._doc.setProspectiveChange(this._dragChange);
sequence.append(new ChangeSizeBend(this._doc, this._modDragNote, this._modDragPin.time, presValue- Config.modulators[this._modDragSetting].convertRealFactor, this._modDragPin.interval, this.shiftMode));
}
}
private _getMaxDivision(): number {
if (this.controlMode && this._mouseHorizontal)
return Config.partsPerBeat;
const rhythmStepsPerBeat: number = Config.rhythms[this._doc.song.rhythm].stepsPerBeat;
if (rhythmStepsPerBeat % 4 == 0) {
// Beat is divisible by 2 (and 4).
return Config.partsPerBeat / 2;
} else if (rhythmStepsPerBeat % 3 == 0) {
// Beat is divisible by 3.
return Config.partsPerBeat / 3;
} else if (rhythmStepsPerBeat % 2 == 0) {
// Beat is divisible by 2.
return Config.partsPerBeat / 2;
}
return Config.partsPerBeat;
}
private _getMinDivision(): number {
if (this.controlMode && this._mouseHorizontal)
return 1;
return Config.partsPerBeat / Config.rhythms[this._doc.song.rhythm].stepsPerBeat;
}
private _snapToMinDivision(input: number): number {
const minDivision: number = this._getMinDivision();
return Math.floor(input / minDivision) * minDivision;
}
private _updateCursorStatus(): void {
this._cursor = new PatternCursor();
if (this._mouseX < 0 || this._mouseX > this._editorWidth || this._mouseY < 0 || this._mouseY > this._editorHeight || this._pitchHeight <= 0) return;
const minDivision: number = this._getMinDivision();
this._cursor.exactPart = this._mouseX / this._partWidth;
this._cursor.part =
Math.floor(
Math.max(0,
Math.min(this._doc.song.beatsPerBar * Config.partsPerBeat - minDivision, this._cursor.exactPart)
)
/ minDivision) * minDivision;
let foundNote: boolean = false;
if (this._pattern != null) {
for (const note of this._pattern.notes) {
if (note.end <= this._cursor.exactPart) {
if (this._doc.song.getChannelIsMod(this._doc.channel)) {
if (note.pitches[0] == Math.floor(this._findMousePitch(this._mouseY))) {
this._cursor.prevNote = note;
}
if (!foundNote)
this._cursor.curIndex++;
} else {
this._cursor.prevNote = note;
this._cursor.curIndex++;
}
} else if (note.start <= this._cursor.exactPart && note.end > this._cursor.exactPart) {
if (this._doc.song.getChannelIsMod(this._doc.channel)) {
if (note.pitches[0] == Math.floor(this._findMousePitch(this._mouseY))) {
this._cursor.curNote = note;
foundNote = true;
}
// Only increment index if the sought note has been found... or if this note truly starts before the other
else if (!foundNote || (this._cursor.curNote != null && note.start < this._cursor.curNote.start))
this._cursor.curIndex++;
}
else {
this._cursor.curNote = note;
}
} else if (note.start > this._cursor.exactPart) {
if (this._doc.song.getChannelIsMod(this._doc.channel)) {
if (note.pitches[0] == Math.floor(this._findMousePitch(this._mouseY))) {
this._cursor.nextNote = note;
break;
}
} else {
this._cursor.nextNote = note;
break;
}
}
}
if (this._doc.song.getChannelIsMod(this._doc.channel) && !this.editingModLabel) {
if (this._pattern.notes[this._cursor.curIndex] != null && this._cursor.curNote != null) {
let pinIdx: number = 0;
while (this._cursor.curNote.start + this._cursor.curNote.pins[pinIdx].time < this._cursor.exactPart && pinIdx < this._cursor.curNote.pins.length) {
pinIdx++;
}
// Decide if the previous pin is closer
if (pinIdx > 0) {
if (this._cursor.curNote.start + this._cursor.curNote.pins[pinIdx].time - this._cursor.exactPart > this._cursor.exactPart - (this._cursor.curNote.start + this._cursor.curNote.pins[pinIdx - 1].time)) {
pinIdx--;
}
}
this.modDragValueLabel.style.setProperty("color", "#666688");
this.modDragValueLabel.style.setProperty("display", "");
const mod: number = Math.max( 0, Config.modCount - 1 - this._cursor.curNote.pitches[0]);
let setting: number = this._doc.song.channels[this._doc.channel].instruments[this._doc.getCurrentInstrument(this._barOffset)].modulators[mod];
let presValue: number = this._cursor.curNote.pins[pinIdx].size + Config.modulators[setting].convertRealFactor;
// This is me being too lazy to fiddle with the css to get it to align center.
let xOffset: number = (+(presValue >= 10.0)) + (+(presValue >= 100.0)) + (+(presValue < 0.0)) + (+(presValue <= -10.0));
this._modDragValueLabelWidth = 8 + xOffset * 8;
this._modDragValueLabelLeft = +prettyNumber(Math.max(Math.min(this._editorWidth - 10 - xOffset * 8, this._partWidth * (this._cursor.curNote.start + this._cursor.curNote.pins[pinIdx].time) - 4 - xOffset * 4), 2));
this._modDragValueLabelTop = +prettyNumber(this._pitchToPixelHeight(this._cursor.curNote.pitches[0] - this._octaveOffset) - 17 - (this._pitchHeight - this._pitchBorder) / 2);
this._modDragStartValue = this._cursor.curNote.pins[pinIdx].size;
this._modDragNote = this._cursor.curNote;
this._modDragPin = this._cursor.curNote.pins[pinIdx];
this._modDragLowerBound = Config.modulators[setting].convertRealFactor;
this._modDragUpperBound = Config.modulators[setting].convertRealFactor + this._doc.song.getVolumeCapForSetting(true, setting, this._doc.song.channels[this._doc.channel].instruments[this._doc.getCurrentInstrument(this._barOffset)].modFilterTypes[mod]);
this._modDragSetting = setting;
this.modDragValueLabel.style.setProperty("left", "" + this._modDragValueLabelLeft + "px");
this.modDragValueLabel.style.setProperty("top", "" + this._modDragValueLabelTop + "px");
this.modDragValueLabel.textContent = "" + presValue;
}
else {
this.modDragValueLabel.style.setProperty("display", "none");
this.modDragValueLabel.style.setProperty("pointer-events", "none");
this.modDragValueLabel.setAttribute("contenteditable", "false");
}
}
else if (!this.editingModLabel) {
this.modDragValueLabel.style.setProperty("display", "none");
this.modDragValueLabel.style.setProperty("pointer-events", "none");
this.modDragValueLabel.setAttribute("contenteditable", "false");
}
}
else {
this.modDragValueLabel.style.setProperty("display", "none");
this.modDragValueLabel.style.setProperty("pointer-events", "none");
this.modDragValueLabel.setAttribute("contenteditable", "false");
}
let mousePitch: number = this._findMousePitch(this._mouseY);
if (this._cursor.curNote != null) {
this._cursor.start = this._cursor.curNote.start;
this._cursor.end = this._cursor.curNote.end;
this._cursor.pins = this._cursor.curNote.pins;
let interval: number = 0;
let error: number = 0;
let prevPin: NotePin;
let nextPin: NotePin = this._cursor.curNote.pins[0];
for (let j: number = 1; j < this._cursor.curNote.pins.length; j++) {
prevPin = nextPin;
nextPin = this._cursor.curNote.pins[j];
const leftSide: number = this._partWidth * (this._cursor.curNote.start + prevPin.time);
const rightSide: number = this._partWidth * (this._cursor.curNote.start + nextPin.time);
if (this._mouseX > rightSide) continue;
if (this._mouseX < leftSide) throw new Error();
const intervalRatio: number = (this._mouseX - leftSide) / (rightSide - leftSide);
const arc: number = Math.sqrt(1.0 / Math.sqrt(4.0) - Math.pow(intervalRatio - 0.5, 2.0)) - 0.5;
const bendHeight: number = Math.abs(nextPin.interval - prevPin.interval);
interval = prevPin.interval * (1.0 - intervalRatio) + nextPin.interval * intervalRatio;
error = arc * bendHeight + 0.95;
break;
}
let minInterval: number = Number.MAX_VALUE;
let maxInterval: number = -Number.MAX_VALUE;
let bestDistance: number = Number.MAX_VALUE;
for (const pin of this._cursor.curNote.pins) {
if (minInterval > pin.interval) minInterval = pin.interval;
if (maxInterval < pin.interval) maxInterval = pin.interval;
const pinDistance: number = Math.abs(this._cursor.curNote.start + pin.time - this._mouseX / this._partWidth);
if (bestDistance > pinDistance) {
bestDistance = pinDistance;
this._cursor.nearPinIndex = this._cursor.curNote.pins.indexOf(pin);
}
}
mousePitch -= interval;
this._cursor.pitch = this._snapToPitch(mousePitch, -minInterval, this._getMaxPitch() - maxInterval);
// Snap to nearby existing note if present.
if (!this._doc.song.getChannelIsNoise(this._doc.channel) && !this._doc.song.getChannelIsMod(this._doc.channel)) {
let nearest: number = error;
for (let i: number = 0; i < this._cursor.curNote.pitches.length; i++) {
const distance: number = Math.abs(this._cursor.curNote.pitches[i] - mousePitch + 0.5);
if (distance > nearest) continue;
nearest = distance;
this._cursor.pitch = this._cursor.curNote.pitches[i];
}
}
for (let i: number = 0; i < this._cursor.curNote.pitches.length; i++) {
if (this._cursor.curNote.pitches[i] == this._cursor.pitch) {
this._cursor.pitchIndex = i;
break;
}
}
} else {
this._cursor.pitch = this._snapToPitch(mousePitch, 0, this._getMaxPitch());
const defaultLength: number = this._copiedPins[this._copiedPins.length - 1].time;
const fullBeats: number = Math.floor(this._cursor.part / Config.partsPerBeat);
const maxDivision: number = this._getMaxDivision();
const modMouse: number = this._cursor.part % Config.partsPerBeat;
if (defaultLength == 1) {
this._cursor.start = this._cursor.part;
} else if (defaultLength > Config.partsPerBeat) {
this._cursor.start = fullBeats * Config.partsPerBeat;
} else if (defaultLength == Config.partsPerBeat) {
this._cursor.start = fullBeats * Config.partsPerBeat;
if (maxDivision < Config.partsPerBeat && modMouse > maxDivision) {
this._cursor.start += Math.floor(modMouse / maxDivision) * maxDivision;
}
} else {
this._cursor.start = fullBeats * Config.partsPerBeat;
let division = Config.partsPerBeat % defaultLength == 0 ? defaultLength : Math.min(defaultLength, maxDivision);
while (division < maxDivision && Config.partsPerBeat % division != 0) {
division++;
}
this._cursor.start += Math.floor(modMouse / division) * division;
}
this._cursor.end = this._cursor.start + defaultLength;
let forceStart: number = 0;
let forceEnd: number = this._doc.song.beatsPerBar * Config.partsPerBeat;
if (this._cursor.prevNote != null) {
forceStart = this._cursor.prevNote.end;
}
if (this._cursor.nextNote != null) {
forceEnd = this._cursor.nextNote.start;
}
if (this._cursor.start < forceStart) {
this._cursor.start = forceStart;
this._cursor.end = this._cursor.start + defaultLength;
if (this._cursor.end > forceEnd) {
this._cursor.end = forceEnd;
}
} else if (this._cursor.end > forceEnd) {
this._cursor.end = forceEnd;
this._cursor.start = this._cursor.end - defaultLength;
if (this._cursor.start < forceStart) {
this._cursor.start = forceStart;
}
}
if (this._cursor.end - this._cursor.start == defaultLength) {
if (this._copiedPinChannels.length > this._doc.channel) {
this._copiedPins = this._copiedPinChannels[this._doc.channel];
this._cursor.pins = this._copiedPins;
} else {
const cap: number = this._doc.song.getVolumeCap(false);
this._cursor.pins = [makeNotePin(0, 0, cap), makeNotePin(0, maxDivision, cap)];
}
} else {
this._cursor.pins = [];
for (const oldPin of this._copiedPins) {
if (oldPin.time <= this._cursor.end - this._cursor.start) {
this._cursor.pins.push(makeNotePin(0, oldPin.time, oldPin.size));
if (oldPin.time == this._cursor.end - this._cursor.start) break;
} else {
this._cursor.pins.push(makeNotePin(0, this._cursor.end - this._cursor.start, oldPin.size));
break;
}
}
}
if (this._doc.song.getChannelIsMod(this._doc.channel)) {
this._cursor.pitch = Math.max(0, Math.min(Config.modCount - 1, this._cursor.pitch));
// Return cursor to stashed cursor volumes (so pins aren't destroyed by moving the preview around several volume scales.)
if (this._stashCursorPinVols != null && this._stashCursorPinVols[this._doc.channel] != null) {
for (let pin: number = 0; pin < this._cursor.pins.length; pin++) {
this._cursor.pins[pin].size = this._stashCursorPinVols[this._doc.channel][pin];
}
}
// Scale volume of copied pin to cap for this row
let maxHeight: number = this._doc.song.getVolumeCap(this._doc.song.getChannelIsMod(this._doc.channel), this._doc.channel, this._doc.getCurrentInstrument(this._barOffset), this._cursor.pitch);
let maxFoundHeight: number = 0;
for (const pin of this._cursor.pins) {
if (pin.size > maxFoundHeight) {
maxFoundHeight = pin.size;
}
}
// Apply scaling if the max height is below any pin setting.
if (maxFoundHeight > maxHeight) {
for (const pin of this._cursor.pins) {
pin.size = Math.round(pin.size * (maxHeight / maxFoundHeight));
}
}
}
}
this._cursor.valid = true;
}
private _cursorIsInSelection(): boolean {
return this._cursor.valid && this._doc.selection.patternSelectionActive && this._doc.selection.patternSelectionStart <= this._cursor.exactPart && this._cursor.exactPart <= this._doc.selection.patternSelectionEnd;
}
private _cursorAtStartOfSelection(): boolean {
return this._cursor.valid && this._doc.selection.patternSelectionActive && this._cursor.pitchIndex == -1 && this._doc.selection.patternSelectionStart - 3 <= this._cursor.exactPart && this._cursor.exactPart <= this._doc.selection.patternSelectionStart + 1.25;
}
private _cursorAtEndOfSelection(): boolean {
return this._cursor.valid && this._doc.selection.patternSelectionActive && this._cursor.pitchIndex == -1 && this._doc.selection.patternSelectionEnd - 1.25 <= this._cursor.exactPart && this._cursor.exactPart <= this._doc.selection.patternSelectionEnd + 3;
}
private _findMousePitch(pixelY: number): number {
return Math.max(0, Math.min(this._pitchCount - 1, this._pitchCount - (pixelY / this._pitchHeight))) + this._octaveOffset;
}
private _snapToPitch(guess: number, min: number, max: number): number {
if (guess < min) guess = min;
if (guess > max) guess = max;
const scale: ReadonlyArray<boolean> = this._doc.prefs.notesOutsideScale ? Config.scales.dictionary["Free"].flags : this._doc.song.scale == Config.scales.dictionary["Custom"].index ? this._doc.song.scaleCustom : Config.scales[this._doc.song.scale].flags;
if (scale[Math.floor(guess) % Config.pitchesPerOctave] || this._doc.song.getChannelIsNoise(this._doc.channel) || this._doc.song.getChannelIsMod(this._doc.channel)) {
return Math.floor(guess);
} else {
let topPitch: number = Math.floor(guess) + 1;
let bottomPitch: number = Math.floor(guess) - 1;
while (!scale[topPitch % Config.pitchesPerOctave]) {
topPitch++;
}
while (!scale[(bottomPitch) % Config.pitchesPerOctave]) {
bottomPitch--;
}
if (topPitch > max) {
if (bottomPitch < min) {
return min;
} else {
return bottomPitch;
}
} else if (bottomPitch < min) {
return topPitch;
}
let topRange: number = topPitch;
let bottomRange: number = bottomPitch + 1;
if (topPitch % Config.pitchesPerOctave == 0 || topPitch % Config.pitchesPerOctave == 7) {
topRange -= 0.5;
}
if (bottomPitch % Config.pitchesPerOctave == 0 || bottomPitch % Config.pitchesPerOctave == 7) {
bottomRange += 0.5;
}
return guess - bottomRange > topRange - guess ? topPitch : bottomPitch;
}
}
private _copyPins(note: Note): void {
this._copiedPins = [];
for (const oldPin of note.pins) {
this._copiedPins.push(makeNotePin(0, oldPin.time, oldPin.size));
}
for (let i: number = 1; i < this._copiedPins.length - 1;) {
if (this._copiedPins[i - 1].size == this._copiedPins[i].size &&
this._copiedPins[i].size == this._copiedPins[i + 1].size) {
this._copiedPins.splice(i, 1);
} else {
i++;
}
}
this._copiedPinChannels[this._doc.channel] = this._copiedPins;
this._stashCursorPinVols[this._doc.channel] = [];
for (let pin: number = 0; pin < this._copiedPins.length; pin++) {
this._stashCursorPinVols[this._doc.channel].push(this._copiedPins[pin].size);
}
}
public movePlayheadToMouse(): boolean {
if (this._mouseOver) {
this._doc.synth.playhead = this._doc.bar + this._barOffset + (this._mouseX / this._editorWidth);
return true;
}
return false;
}
public resetCopiedPins = (): void => {
const maxDivision: number = this._getMaxDivision();
let cap: number = this._doc.song.getVolumeCap(false);
this._copiedPinChannels.length = this._doc.song.getChannelCount();
this._stashCursorPinVols.length = this._doc.song.getChannelCount();
for (let i: number = 0; i < this._doc.song.pitchChannelCount; i++) {
this._copiedPinChannels[i] = [makeNotePin(0, 0, cap), makeNotePin(0, maxDivision, cap)];
this._stashCursorPinVols[i] = [cap, cap];
}
for (let i: number = this._doc.song.pitchChannelCount; i < this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount; i++) {
this._copiedPinChannels[i] = [makeNotePin(0, 0, cap), makeNotePin(0, maxDivision, 0)];
this._stashCursorPinVols[i] = [cap, 0];
}
for (let i: number = this._doc.song.pitchChannelCount + this._doc.song.noiseChannelCount; i < this._doc.song.getChannelCount(); i++) {
this._copiedPinChannels[i] = [makeNotePin(0, 0, cap), makeNotePin(0, maxDivision, 0)];
this._stashCursorPinVols[i] = [cap, 0];
}
}
private _animatePlayhead = (timestamp: number): void => {
if (this._usingTouch && !this.shiftMode && !this._mouseDragging && this._mouseDown && performance.now() > this._touchTime + 1000 && this._cursor.valid && this._doc.lastChangeWas(this._dragChange)) {
// On a mobile device, the pattern editor supports using a long stationary touch to activate selection.
this._dragChange!.undo();
this._shiftHeld = true;
this._dragConfirmed = false;
this._whenCursorPressed();
// The full interface is usually only rerendered in response to user input events, not animation events, but in this case go ahead and rerender everything.
this._doc.notifier.notifyWatchers();
}
const playheadBar: number = Math.floor(this._doc.synth.playhead);
const noteFlashElements: NodeListOf<SVGPathElement> = this._svgNoteContainer.querySelectorAll('.note-flash');
if (this._doc.synth.playing && ((this._pattern != null && this._doc.song.getPattern(this._doc.channel, Math.floor(this._doc.synth.playhead)) == this._pattern) || Math.floor(this._doc.synth.playhead) == this._doc.bar + this._barOffset)) {
this._svgPlayhead.setAttribute("visibility", "visible");
const modPlayhead: number = this._doc.synth.playhead - playheadBar;
// note flash
for (var i = 0; i < noteFlashElements.length; i++) {
var element: SVGPathElement = noteFlashElements[i];
const noteStart: number = Number(element.getAttribute("note-start"))/(this._doc.song.beatsPerBar * Config.partsPerBeat)
const noteEnd: number = Number(element.getAttribute("note-end"))/(this._doc.song.beatsPerBar * Config.partsPerBeat)
if ((modPlayhead>=noteStart)&&this._doc.prefs.notesFlashWhenPlayed) {
const dist = noteEnd-noteStart
element.style.opacity = String((1-(((modPlayhead-noteStart)-(dist/2))/(dist/2))))
} else {
element.style.opacity = "0"
}
}
if (Math.abs(modPlayhead - this._playheadX) > 0.1) {
this._playheadX = modPlayhead;
} else {
this._playheadX += (modPlayhead - this._playheadX) * 0.2;
}
this._svgPlayhead.setAttribute("x", "" + prettyNumber(this._playheadX * this._editorWidth - 2));
} else {
this._svgPlayhead.setAttribute("visibility", "hidden");
// dogeiscut: lazy fix boohoo
for (var i = 0; i < noteFlashElements.length; i++) {
var element: SVGPathElement = noteFlashElements[i];
element.style.opacity = "0"
}
}
if (this._doc.synth.playing && (this._doc.synth.recording || this._doc.prefs.autoFollow) && this._followPlayheadBar != playheadBar) {
// When autofollow is enabled, select the current bar (but don't record it in undo history).
new ChangeChannelBar(this._doc, this._doc.channel, playheadBar);
// The full interface is usually only rerendered in response to user input events, not animation events, but in this case go ahead and rerender everything.
this._doc.notifier.notifyWatchers();
}
this._followPlayheadBar = playheadBar;
if (this._doc.currentPatternIsDirty) {
this._redrawNotePatterns();
}
window.requestAnimationFrame(this._animatePlayhead);
}
private _whenMouseOver = (event: MouseEvent): void => {
if (this._mouseOver) return;
this._mouseOver = true;
this._usingTouch = false;
}
private _whenMouseOut = (event: MouseEvent): void => {
if (!this._mouseOver) return;
this._mouseOver = false;
}
private _whenMousePressed = (event: MouseEvent): void => {
event.preventDefault();
const boundingRect: ClientRect = this._svg.getBoundingClientRect();
this._mouseX = ((event.clientX || event.pageX) - boundingRect.left) * this._editorWidth / (boundingRect.right - boundingRect.left);
this._mouseY = ((event.clientY || event.pageY) - boundingRect.top) * this._editorHeight / (boundingRect.bottom - boundingRect.top);
if (isNaN(this._mouseX)) this._mouseX = 0;
if (isNaN(this._mouseY)) this._mouseY = 0;
this._usingTouch = false;
this._shiftHeld = event.shiftKey;
this._dragConfirmed = false;
this._whenCursorPressed();
}
private _whenTouchPressed = (event: TouchEvent): void => {
event.preventDefault();
const boundingRect: ClientRect = this._svg.getBoundingClientRect();
this._mouseX = (event.touches[0].clientX - boundingRect.left) * this._editorWidth / (boundingRect.right - boundingRect.left);
this._mouseY = (event.touches[0].clientY - boundingRect.top) * this._editorHeight / (boundingRect.bottom - boundingRect.top);
if (isNaN(this._mouseX)) this._mouseX = 0;
if (isNaN(this._mouseY)) this._mouseY = 0;
this._usingTouch = true;
this._shiftHeld = event.shiftKey;
this._dragConfirmed = false;
this._touchTime = performance.now();
this._whenCursorPressed();
}
// For a given change type, check the modulator channels for a matching mod to the changed parameter. If it exists, add a pin onto the latest note, or make a new note if enough time elapsed since the last pin.
public setModSettingsForChange(change: any, songEditor: SongEditor): boolean {
const thisRef: PatternEditor = this;
const timeQuantum = Math.max(4, (Config.partsPerBeat / Config.rhythms[this._doc.song.rhythm].stepsPerBeat));
const currentBar: number = Math.floor(this._doc.synth.playhead);
const realPart: number = this._doc.synth.getCurrentPart();
let changedPatterns: boolean = false;
// Ceiling is applied usually to give the synth time to catch the mod updates, but rounds to 0 to avoid skipping the first part.
const currentPart: number = (realPart < timeQuantum / 2 ) ? 0 : Math.ceil(realPart / timeQuantum) * timeQuantum;
// For a given setting and a given channel, find the instrument and mod number that influences the setting.
function getMatchingInstrumentAndMod(applyToMod: number, modChannel: Channel, modInsIndex?: number | undefined, modFilterIndex?: number | undefined): number[] {
let startIndex: number = (modInsIndex == undefined) ? 0 : modInsIndex;
let endIndex: number = (modInsIndex == undefined) ? modChannel.instruments.length - 1 : modInsIndex;
for (let instrumentIndex: number = startIndex; instrumentIndex <= endIndex; instrumentIndex++ ) {
let instrument: Instrument = modChannel.instruments[instrumentIndex];
for (let mod: number = 0; mod < Config.modCount; mod++) {
// Non-song application
if ( instrument.modulators[mod] == applyToMod && !Config.modulators[instrument.modulators[mod]].forSong && (instrument.modChannels[mod] == thisRef._doc.channel) ) {
// This is a check if the instrument targeted is relevant. Is it the exact one being edited? An "all" or "active" target?
// For "active" target it doesn't check if the instrument is active, allowing write to other active instruments from an inactive one. Should be fine since audibly while writing you'll hear what you'd expect -
// the current channel's active instruments being modulated, which is what most people would expect even if editing an inactive instrument.
if ( thisRef._doc.getCurrentInstrument() == instrument.modInstruments[mod]
|| instrument.modInstruments[mod] >= thisRef._doc.song.channels[thisRef._doc.channel].instruments.length )
{
// If it's an eq/note filter target, one additional step is performed to see if it matches the right modFilterType.
if (modFilterIndex != undefined && (applyToMod == Config.modulators.dictionary["eq filter"].index || applyToMod == Config.modulators.dictionary["note filter"].index)) {
if (instrument.modFilterTypes[mod] == modFilterIndex)
return [instrumentIndex, mod];
}
else
return [instrumentIndex, mod];
}
}
// Song wide application
else if ( instrument.modulators[mod] == applyToMod && Config.modulators[instrument.modulators[mod]].forSong && (instrument.modChannels[mod] == -1) ) {
return [instrumentIndex, mod];
}
}
}
return [-1, -1];
}
// For the given duration, scans through and removes pins and notes that are within. If two pins of a note cross the interval boundary, the interior pin is moved to the boundary.
function sanitizeInterval(doc: SongDocument, startPart: number, endPart: number, pattern: Pattern, forMod: number, sequence: ChangeSequence) {
if (startPart >= endPart) return;
for (let noteIndex: number = 0; noteIndex < pattern.notes.length; noteIndex++) {
const note: Note = pattern.notes[noteIndex];
if (note.pitches[0] != forMod)
continue;
if (note.start < endPart && note.end > startPart) {
let couldIntersectStart: boolean = false;
let intersectsEnd: boolean = false;
let firstInteriorPin: number = -1;
let interiorPinCount: number = 0;
// The interval is spanned by the entire note. Just process internal pins, then done.
if (note.start <= startPart && note.end >= endPart) {
for (let pinIndex: number = 0; pinIndex < note.pins.length; pinIndex++) {
const pin: NotePin = note.pins[pinIndex];
if (note.start + pin.time > startPart && note.start + pin.time < endPart) {
if (firstInteriorPin < 0)
firstInteriorPin = pinIndex;
interiorPinCount++;
}
}
// Splice pins inside the interval.
if (interiorPinCount > 0)
note.pins.splice(firstInteriorPin, interiorPinCount);
return;
}
for (let pinIndex: number = 0; pinIndex < note.pins.length; pinIndex++) {
const pin: NotePin = note.pins[pinIndex];
if (note.start + pin.time >= startPart && note.start + pin.time <= endPart) {
if (firstInteriorPin < 0)
firstInteriorPin = pinIndex;
interiorPinCount++;
}
else {
if (interiorPinCount == 0)
couldIntersectStart = true;
if (interiorPinCount > 0)
intersectsEnd = true;
}
}
if (couldIntersectStart && interiorPinCount > 0) {
note.pins[firstInteriorPin].time = startPart - note.start;
firstInteriorPin++; interiorPinCount--;
}
if (intersectsEnd && interiorPinCount > 0) {
note.pins[firstInteriorPin + interiorPinCount - 1].time = endPart - note.start;
interiorPinCount--;
}
// Splice pins inside the interval.
note.pins.splice(firstInteriorPin, interiorPinCount);
if (note.pins.length < 2) {
sequence.append(new ChangeNoteAdded(doc, pattern, note, noteIndex, true));
noteIndex--;
continue;
}
// Clean up properties.
let timeAdjust: number = 0;
timeAdjust = note.pins[0].time;
note.start += timeAdjust;
for (let i: number = 0; i < note.pins.length; i++) {
note.pins[i].time -= timeAdjust;
}
note.end = note.start + note.pins[note.pins.length - 1].time;
if (note.end <= note.start) {
sequence.append(new ChangeNoteAdded(doc, pattern, note, noteIndex, true));
noteIndex--;
}
}
}
}
const sequence: ChangeSequence = new ChangeSequence();
const instrument: Instrument = this._doc.song.channels[this._doc.channel].instruments[this._doc.getCurrentInstrument()];
let applyToMods: number[] = [];
let applyToFilterTargets: number[] = [];
let applyValues: number[] = [];
let toApply: boolean = true;
let slider: Slider | null = null;
// Special case, treat null change as Song volume.
if (change == null) {
var modulator = Config.modulators.dictionary["song volume"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(this._doc.prefs.volume - modulator.convertRealFactor);
}
// Also for song volume, when holding the slider at a single value.
else if (this._doc.continuingModRecordingChange != null && this._doc.continuingModRecordingChange.storedChange == null && this._doc.continuingModRecordingChange.storedSlider == null) {
var modulator = Config.modulators.dictionary["song volume"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(this._doc.continuingModRecordingChange.storedValues![0]);
}
else if (change instanceof ChangeTempo) {
var modulator = Config.modulators.dictionary["tempo"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(this._doc.song.tempo - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
this._doc.song.tempo = slider.getValueBeforeProspectiveChange();
}
}
/* Song reverb - a casualty of splitting to reverb per instrument, it's not modulate-able via slider!
else if (change instanceof ChangeSongReverb) { } */
else if (change instanceof ChangeVolume) {
var modulator = Config.modulators.dictionary["mix volume"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.volume - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null )
instrument.volume = slider.getValueBeforeProspectiveChange();
}
else if (change instanceof ChangePan) {
var modulator = Config.modulators.dictionary["pan"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.pan - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if (slider != null) {
instrument.pan = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeReverb) {
var modulator = Config.modulators.dictionary["reverb"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.reverb - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.reverb = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeDistortion) {
var modulator = Config.modulators.dictionary["distortion"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.distortion - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.distortion = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeOperatorAmplitude) {
var modulator = Config.modulators.dictionary["fm slider " + (change.operatorIndex + 1)];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.operators[change.operatorIndex].amplitude - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.operators[change.operatorIndex].amplitude = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeFeedbackAmplitude) {
var modulator = Config.modulators.dictionary["fm feedback"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.feedbackAmplitude - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.feedbackAmplitude = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangePulseWidth) {
var modulator = Config.modulators.dictionary["pulse width"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.pulseWidth - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.pulseWidth = slider.getValueBeforeProspectiveChange();
}
}
// PWM decimal offset code for UB, DOUBLE-CHECK THAT THIS IS CORRECT
else if (change instanceof ChangeDecimalOffset) {
var modulator = Config.modulators.dictionary["decimal offset"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.decimalOffset - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.decimalOffset = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeDetune) {
var modulator = Config.modulators.dictionary["detune"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.detune - modulator.convertRealFactor - Config.detuneCenter);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.detune = slider.getValueBeforeProspectiveChange() + Config.detuneCenter;
}
}
else if (change instanceof ChangeVibratoDepth) {
var modulator = Config.modulators.dictionary["vibrato depth"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.vibratoDepth * 25 - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if ( slider != null ) {
instrument.vibratoDepth = slider.getValueBeforeProspectiveChange() / 25;
}
}
else if (change instanceof ChangeVibratoSpeed) {
var modulator = Config.modulators.dictionary["vibrato speed"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.vibratoSpeed - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if (slider != null) {
instrument.vibratoSpeed = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeVibratoDelay) {
var modulator = Config.modulators.dictionary["vibrato delay"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.vibratoDelay - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if (slider != null) {
instrument.vibratoDelay = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangeArpeggioSpeed) {
var modulator = Config.modulators.dictionary["arp speed"];
applyToMods.push(modulator.index);
if (toApply) applyValues.push(instrument.arpeggioSpeed - modulator.convertRealFactor);
// Move the actual value back, since we just want to update the modulated value and not the base slider.
slider = songEditor.getSliderForModSetting(modulator.index);
if (slider != null) {
instrument.arpeggioSpeed = slider.getValueBeforeProspectiveChange();
}
}
else if (change instanceof ChangePanDelay) {
var modulator = Config.modulators.dictionary["pan delay"];
applyToMods.push(modulator.index);