-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoscillator.html
1893 lines (1655 loc) · 79.9 KB
/
oscillator.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>3D Oscillator with Advanced Features</title>
<style>
/* Ensure body and html take full height */
html,
body {
margin: 0;
padding: 0;
height: 100%;
width: 100%;
overflow: hidden;
font-family: Arial, sans-serif;
}
/* Canvas covers the entire viewport */
#oscillator-canvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 0;
/* CRT-style gradient background */
background:
linear-gradient(to right, #001000, #003300, #001000), /* Green gradient */
repeating-linear-gradient(to right, rgba(0, 64, 0, 0.8) 0, rgba(0, 64, 0, 0.8) 1px, transparent 1px, transparent 50px), /* Vertical grid */
repeating-linear-gradient(to bottom, rgba(0, 64, 0, 0.8) 0, rgba(0, 64, 0, 0.8) 1px, transparent 1px, transparent 50px); /* Horizontal grid */
/* Combine layers */
background-blend-mode: overlay;
}
/* Control Panel styling */
#controls {
position: fixed; /* Fixed position */
top: 10px;
left: 10px; /* Positioned to the left */
background: rgba(255, 255, 255, 0.95);
padding: 15px;
border-radius: 8px;
width: 25vw; /* Adjusted relative width */
max-width: 250px; /* Max width set to 250px */
min-width: 200px; /* Optional: Mindestbreite */
font-size: 14px;
overflow-y: auto;
max-height: 90vh;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.3);
z-index: 1;
transition: transform 0.3s ease, opacity 0.3s ease;
}
@media (max-width: 768px) {
#controls {
width: 80vw; /* On smaller screens, wider */
max-width: none;
left: 5vw;
}
}
@media (max-width: 480px) {
#controls {
width: 95vw; /* Almost full width on very small screens */
padding: 10px;
left: 2.5vw;
}
}
/* Toggle Button styling */
#toggle-controls {
position: fixed;
top: 10px;
left: 270px; /* Adjusted to accommodate Control Panel's max-width (250px + 20px Padding) */
z-index: 2;
background: rgba(255, 255, 255, 0.8);
border: none;
padding: 10px 15px;
border-radius: 5px;
cursor: pointer;
box-shadow: 0 0 5px rgba(0, 0, 0, 0.3);
font-size: 18px;
width: 45px;
transition: left 0.3s ease;
}
#controls.hidden {
transform: translateX(-100%);
opacity: 0;
}
#toggle-controls.hidden {
left: 10px; /* Move toggle button when Control Panel is hidden */
}
/* Control Panel Contents */
#controls h2 {
margin-top: 0;
text-align: center;
color: #333;
font-size: 18px;
border-bottom: 2px solid #ccc;
padding-bottom: 5px;
}
/* Tabs Styling */
.tabs {
display: flex;
justify-content: space-around;
margin-bottom: 15px;
}
.tab-button {
padding: 10px 15px;
cursor: pointer;
background: #f0f0f0;
border: 1px solid #ccc;
border-bottom: none;
border-radius: 5px 5px 0 0;
margin-right: 2px;
flex: 1;
text-align: center;
font-weight: bold;
}
.tab-button.active {
background: #fff;
border-bottom: 1px solid #fff;
}
/* Sections Styling */
.section {
margin-bottom: 20px;
}
.section h3 {
margin-bottom: 10px;
border-bottom: 1px solid #ccc;
padding-bottom: 5px;
}
.layer-controls,
.global-controls,
.rotation-controls,
.trail-controls,
.additional-controls,
.fourier-controls,
.energy-controls {
margin-bottom: 15px;
padding: 10px;
border: 1px solid #bbb;
border-radius: 5px;
background: rgba(240, 240, 240, 0.8);
}
.control-group {
margin-bottom: 8px;
}
label {
display: block;
margin-bottom: 4px;
}
input[type="range"],
select,
button {
width: 100%;
}
.button-group {
display: flex;
justify-content: space-between;
margin-bottom: 5px;
}
.button-group button {
width: 48%;
padding: 5px;
font-size: 14px;
cursor: pointer;
}
.reset-button {
width: 100%;
padding: 5px;
font-size: 14px;
cursor: pointer;
background-color: #f44336;
color: white;
border: none;
border-radius: 3px;
}
/* Fourier Analyse Canvas Stil */
#fourier-canvas {
width: 100%;
height: 200px;
background: rgba(0, 0, 0, 0.7);
display: block;
margin-top: 10px;
border-radius: 5px;
}
/* Energie Visualisierungs Canvas Stil */
#energy-canvas {
width: 100%;
height: 200px;
background: rgba(0, 0, 0, 0.7);
display: block;
margin-top: 10px;
border-radius: 5px;
}
</style>
</head>
<body>
<!-- Toggle Button -->
<button id="toggle-controls">✕</button>
<!-- Control Panel -->
<div id="controls">
<h2>3D Six-Layer Oscillator</h2>
<!-- Tab Navigation -->
<div class="tabs">
<div class="tab-button active" data-tab="0">Tab 1</div>
<div class="tab-button" data-tab="1">Tab 2</div>
<div class="tab-button" data-tab="2">Tab 3</div>
</div>
<!-- Global Controls -->
<div class="section global-controls" id="global-controls">
<h3>Global Controls</h3>
<!-- Frequency Control -->
<div class="control-group">
<label>
<input type="checkbox" id="global-frequency-enable">
Apply Global Frequency
</label>
<label for="global-frequency">Frequency: <span id="global-frequency-val">1.0</span></label>
<input type="range" id="global-frequency" min="0.1" max="5000.0" step="0.1" value="1.0" disabled>
</div>
<!-- Amplitude Control -->
<div class="control-group">
<label>
<input type="checkbox" id="global-amplitude-enable">
Apply Global Amplitude
</label>
<label for="global-amplitude">Amplitude: <span id="global-amplitude-val">1.0</span></label>
<input type="range" id="global-amplitude" min="0.1" max="5.0" step="0.1" value="1.0" disabled>
</div>
<!-- Phase Control -->
<div class="control-group">
<label>
<input type="checkbox" id="global-phase-enable">
Apply Global Phase
</label>
<label for="global-phase">Phase: <span id="global-phase-val">0</span></label>
<input type="range" id="global-phase" min="0" max="6.283" step="0.1" value="0" disabled>
</div>
</div>
<!-- Layers Controls -->
<div class="section layers-controls" id="layers-controls">
<h3>Layers Controls</h3>
<!-- Layer Controls werden dynamisch hinzugefügt -->
</div>
<!-- Additional Controls -->
<div class="section additional-controls" id="additional-controls">
<h3>Additional Controls</h3>
<!-- Oscillator Size Control -->
<div class="control-group">
<label for="oscillator-size">Oscillator Size: <span id="oscillator-size-val">0.10</span></label>
<input type="range" id="oscillator-size" min="0" max="1" step="0.01" value="0.10">
</div>
<!-- Trail Type Selection -->
<div class="control-group">
<label for="trail-type">Trail Type:</label>
<select id="trail-type">
<option value="points">Points</option>
<option value="line">Line</option>
</select>
</div>
<!-- Sound Toggle Buttons für jeden Tab -->
<div class="control-group">
<button id="sound-toggle-tab0">Sound On</button>
<button id="sound-toggle-tab1">Sound On</button>
<button id="sound-toggle-tab2">Sound On</button>
</div>
<!-- Toggle Yellow Oscillator and Trail -->
<div class="control-group">
<label>
<input type="checkbox" id="toggle-yellow-oscillator">
Show Yellow Oscillator and Trail
</label>
</div>
<!-- Toggle Axes Visibility -->
<div class="control-group">
<label>
<input type="checkbox" id="toggle-axes">
Show Axes
</label>
</div>
<!-- Axes Distance Control -->
<div class="control-group">
<label for="axes-distance">Axes Length: <span id="axes-distance-val">5</span></label>
<input type="range" id="axes-distance" min="1" max="10" step="0.5" value="5">
</div>
<!-- Toggle White Wave (Disabled by Default) -->
<div class="control-group">
<label>
<input type="checkbox" id="toggle-white-wave">
Show White Wave
</label>
</div>
<!-- Toggle Lighting (Disabled by Default) -->
<div class="control-group">
<label>
<input type="checkbox" id="toggle-lighting">
Enable Lighting and Shadows
</label>
</div>
</div>
<!-- Rotation Controls -->
<div class="section rotation-controls" id="rotation-controls">
<h3>Rotation Controls</h3>
<div class="control-group">
<div class="button-group">
<button id="rotate-x-+">+X</button>
<button id="rotate-x--">-X</button>
</div>
<div class="button-group">
<button id="rotate-y-+">+Y</button>
<button id="rotate-y--">-Y</button>
</div>
<div class="button-group">
<button id="rotate-z-+">+Z</button>
<button id="rotate-z--">-Z</button>
</div>
<button class="reset-button" id="reset-rotation">Reset Alignment</button>
</div>
</div>
<!-- Trail Controls -->
<div class="section trail-controls" id="trail-controls">
<h3>Trail Settings</h3>
<div class="control-group">
<label for="trail-length">Trail Length: <span id="trail-length-val">100</span></label>
<input type="range" id="trail-length" min="50" max="500" step="10" value="100">
</div>
</div>
<!-- Fourier Echtzeit Analyse -->
<div class="section fourier-controls" id="fourier-controls">
<h3>Fourier Echtzeit Analyse</h3>
<canvas id="fourier-canvas"></canvas>
</div>
<!-- Energieverteilung und Bewegungsdynamik -->
<div class="section energy-controls" id="energy-controls">
<h3>Energieverteilung und Bewegungsdynamik</h3>
<canvas id="energy-canvas"></canvas>
</div>
</div>
<!-- Three.js Canvas -->
<canvas id="oscillator-canvas"></canvas>
<!-- Three.js Library -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- Main JavaScript -->
<script>
// Global Variables
const totalTabs = 3;
const totalLayers = 6;
let activeTab = 0; // Indexing starts at 0
let oscillatorSize = 0.1;
let trailType = 'points'; // 'points' or 'line'
let trailLength = 100; // Default value
// Initialize Three.js Scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 10;
const renderer = new THREE.WebGLRenderer({ canvas: document.getElementById('oscillator-canvas'), antialias: true, alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = false; // Shadows disabled by default
renderer.shadowMap.type = THREE.PCFSoftShadowMap; // Optional: smoother shadows
window.addEventListener('resize', () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});
// Add Axes Helper
let axesLength = 5; // Initial length
const axesHelper = new THREE.AxesHelper(axesLength);
axesHelper.visible = false; // Initially invisible
scene.add(axesHelper);
// Initialize Fourier Analyse
const fourierCanvas = document.getElementById('fourier-canvas');
const fourierCtx = fourierCanvas.getContext('2d');
fourierCanvas.width = 800;
fourierCanvas.height = 200;
// Initialize Energieverteilung Visualisierung
const energyCanvas = document.getElementById('energy-canvas');
const energyCtx = energyCanvas.getContext('2d');
energyCanvas.width = 800;
energyCanvas.height = 200;
// Initialize Tabs Data
const tabs = [];
for (let t = 0; t < totalTabs; t++) {
const tabName = `tab${t + 1}`;
const tabGroup = new THREE.Group();
tabGroup.visible = (t === 0); // Only first tab visible initially
scene.add(tabGroup);
// Create Oscillators
const oscillatorGeometry = new THREE.SphereGeometry(oscillatorSize, 16, 16);
// Green Oscillator with Emissive Material for Glow Effect
const greenMaterial = new THREE.MeshStandardMaterial({
color: 0x00ff00,
emissive: 0x00ff00,
emissiveIntensity: 1,
metalness: 0.5,
roughness: 0.1
});
const greenOscillator = new THREE.Mesh(oscillatorGeometry, greenMaterial);
greenOscillator.castShadow = true; // Cast shadows
greenOscillator.receiveShadow = true; // Receive shadows
tabGroup.add(greenOscillator);
// Yellow Oscillator (initially invisible)
const yellowMaterial = new THREE.MeshBasicMaterial({ color: 0xffff00 });
const yellowOscillator = new THREE.Mesh(oscillatorGeometry, yellowMaterial);
yellowOscillator.visible = false; // Initially invisible
yellowOscillator.castShadow = true; // Cast shadows
yellowOscillator.receiveShadow = true; // Receive shadows
tabGroup.add(yellowOscillator);
// Initialize Trails
const greenTrail = initializeTrail(trailType, 0x00ff00, 0.8, tabGroup);
const yellowTrail = initializeTrail(trailType, 0xffff00, 0.5, tabGroup, true); // true for yellow trail
// Initialize Audio
const audio = initializeAudio();
// Store Tab Data
tabs.push({
tabName: tabName,
group: tabGroup,
greenOscillator: greenOscillator,
yellowOscillator: yellowOscillator,
greenTrail: greenTrail,
yellowTrail: yellowTrail,
layers: [],
audio: audio,
interferenceMesh: null, // For Interference Patterns
standingWaveMesh: null // For Standing Waves
});
// Create Layers
for (let l = 1; l <= totalLayers; l++) {
const controls = {}; // Will be filled later
tabs[t].layers.push({
layerNumber: l,
enabled: l === 1, // Only Layer 1 enabled by default
waveform: 'sine', // Default waveform
frequency: 1.0,
amplitude: 1.0,
phase: 0,
frequencyGlobal: 0,
amplitudeGlobal: 0,
phaseGlobal: 0,
phaseLock: {
target: 'none', // 'layer1', 'layer2', etc., or 'none'
harmonicOffset: false
},
currentPhase: 0, // To handle phase locking
controls: controls,
lfo: {
frequency: 0.5, // Default LFO frequency
amplitude: 0.5, // Default LFO amplitude
oscillator: null,
gainNode: null
}
});
}
}
// Function to Initialize Trails
function initializeTrail(type, colorHex, opacity, parentGroup, isYellow = false) {
let trail;
if (type === 'points') {
const trailGeometry = new THREE.BufferGeometry();
const trailPositions = new Float32Array(trailLength * 3); // Adjust size as needed
const trailColors = new Float32Array(trailLength * 3);
for (let i = 0; i < trailLength; i++) {
trailPositions[i * 3] = 0;
trailPositions[i * 3 + 1] = 0;
trailPositions[i * 3 + 2] = 0;
const alpha = 1.0 - (i / trailLength);
const color = new THREE.Color(colorHex);
trailColors[i * 3] = color.r * alpha;
trailColors[i * 3 + 1] = color.g * alpha;
trailColors[i * 3 + 2] = color.b * alpha;
}
trailGeometry.setAttribute('position', new THREE.BufferAttribute(trailPositions, 3));
trailGeometry.setAttribute('color', new THREE.BufferAttribute(trailColors, 3));
const trailMaterial = new THREE.PointsMaterial({
size: oscillatorSize,
vertexColors: true,
transparent: true,
opacity: opacity,
depthWrite: false,
blending: THREE.AdditiveBlending
});
const trailPoints = new THREE.Points(trailGeometry, trailMaterial);
trailPoints.castShadow = false; // Trails typically don't cast shadows
trailPoints.receiveShadow = false;
parentGroup.add(trailPoints);
trail = {
type: 'points',
geometry: trailGeometry,
positions: trailPositions,
colors: trailColors,
points: trailPoints,
array: []
};
} else if (type === 'line') {
const trailGeometry = new THREE.BufferGeometry();
const trailPositions = new Float32Array(trailLength * 3); // Adjust size as needed
for (let i = 0; i < trailLength; i++) {
trailPositions[i * 3] = 0;
trailPositions[i * 3 + 1] = 0;
trailPositions[i * 3 + 2] = 0;
}
trailGeometry.setAttribute('position', new THREE.BufferAttribute(trailPositions, 3));
const trailMaterial = new THREE.LineBasicMaterial({
color: colorHex,
transparent: true,
opacity: opacity,
linewidth: 2
});
const trailLine = new THREE.Line(trailGeometry, trailMaterial);
trailLine.castShadow = false; // Trails typically don't cast shadows
trailLine.receiveShadow = false;
parentGroup.add(trailLine);
trail = {
type: 'line',
geometry: trailGeometry,
positions: trailPositions,
line: trailLine,
array: []
};
}
return trail;
}
// Function to Initialize Audio for a Tab
function initializeAudio() {
return {
audioCtx: null, // AudioContext will be created later
oscillator: null,
gainNode: null,
analyser: null,
dataArray: null,
playing: false
};
}
// Function to Create Layer Controls
function createLayerControls(tab, layer, tabIndex) {
const layersControlsDiv = document.getElementById('layers-controls');
const layerDiv = document.createElement('div');
layerDiv.className = 'layer-controls';
layerDiv.id = `tab${tabIndex}-layer${layer.layerNumber}-controls`;
// Layer Checkbox
const layerLabel = document.createElement('label');
const layerCheckbox = document.createElement('input');
layerCheckbox.type = 'checkbox';
layerCheckbox.id = `tab${tabIndex}-layer${layer.layerNumber}-enable`;
layerCheckbox.checked = layer.enabled;
layerLabel.appendChild(layerCheckbox);
layerLabel.appendChild(document.createTextNode(` Layer ${layer.layerNumber}`));
layerDiv.appendChild(layerLabel);
// Waveform Selection
const waveformGroup = document.createElement('div');
waveformGroup.className = 'control-group';
const waveformLabel = document.createElement('label');
waveformLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-waveform`;
waveformLabel.innerHTML = `Waveform:`;
const waveformSelect = document.createElement('select');
waveformSelect.id = `tab${tabIndex}-layer${layer.layerNumber}-waveform`;
const waveforms = ['Sine', 'Cosine', 'Square', 'Triangle', 'Sawtooth'];
waveforms.forEach(wave => {
const option = document.createElement('option');
option.value = wave.toLowerCase();
option.text = wave;
if (wave.toLowerCase() === layer.waveform) {
option.selected = true;
}
waveformSelect.appendChild(option);
});
waveformGroup.appendChild(waveformLabel);
waveformGroup.appendChild(waveformSelect);
layerDiv.appendChild(waveformGroup);
// Frequency Control
const freqGroup = document.createElement('div');
freqGroup.className = 'control-group';
const freqLabel = document.createElement('label');
freqLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-frequency`;
freqLabel.innerHTML = `Frequency: <span id="tab${tabIndex}-layer${layer.layerNumber}-frequency-val">${layer.frequency.toFixed(1)}</span>`;
const freqSlider = document.createElement('input');
freqSlider.type = 'range';
freqSlider.id = `tab${tabIndex}-layer${layer.layerNumber}-frequency`;
freqSlider.min = '0.1';
freqSlider.max = '10.0';
freqSlider.step = '0.1';
freqSlider.value = layer.frequency;
freqSlider.disabled = !layer.enabled;
freqGroup.appendChild(freqLabel);
freqGroup.appendChild(freqSlider);
layerDiv.appendChild(freqGroup);
// Amplitude Control
const ampGroup = document.createElement('div');
ampGroup.className = 'control-group';
const ampLabel = document.createElement('label');
ampLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-amplitude`;
ampLabel.innerHTML = `Amplitude: <span id="tab${tabIndex}-layer${layer.layerNumber}-amplitude-val">${layer.amplitude.toFixed(1)}</span>`;
const ampSlider = document.createElement('input');
ampSlider.type = 'range';
ampSlider.id = `tab${tabIndex}-layer${layer.layerNumber}-amplitude`;
ampSlider.min = '0.1';
ampSlider.max = '5.0';
ampSlider.step = '0.1';
ampSlider.value = layer.amplitude;
ampSlider.disabled = !layer.enabled;
ampGroup.appendChild(ampLabel);
ampGroup.appendChild(ampSlider);
layerDiv.appendChild(ampGroup);
// Phase Control
const phaseGroup = document.createElement('div');
phaseGroup.className = 'control-group';
const phaseLabel = document.createElement('label');
phaseLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-phase`;
phaseLabel.innerHTML = `Phase: <span id="tab${tabIndex}-layer${layer.layerNumber}-phase-val">${layer.phase.toFixed(1)}</span>`;
const phaseSlider = document.createElement('input');
phaseSlider.type = 'range';
phaseSlider.id = `tab${tabIndex}-layer${layer.layerNumber}-phase`;
phaseSlider.min = '0';
phaseSlider.max = '6.283';
phaseSlider.step = '0.1';
phaseSlider.value = layer.phase;
phaseSlider.disabled = !layer.enabled;
phaseGroup.appendChild(phaseLabel);
phaseGroup.appendChild(phaseSlider);
layerDiv.appendChild(phaseGroup);
// Phase Lock Dropdown
const phaseLockGroup = document.createElement('div');
phaseLockGroup.className = 'control-group';
const phaseLockLabel = document.createElement('label');
phaseLockLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-phase-lock`;
phaseLockLabel.innerHTML = `Phase Lock to Layer:`;
const phaseLockSelect = document.createElement('select');
phaseLockSelect.id = `tab${tabIndex}-layer${layer.layerNumber}-phase-lock`;
// Add "None" option
const noneOption = document.createElement('option');
noneOption.value = 'none';
noneOption.text = 'None';
phaseLockSelect.appendChild(noneOption);
// Add options for other layers
for (let i = 1; i <= totalLayers; i++) {
if (i !== layer.layerNumber) { // Prevent locking to itself
const option = document.createElement('option');
option.value = `layer${i}`;
option.text = `Layer ${i}`;
if (`layer${i}` === layer.phaseLock.target) {
option.selected = true;
}
phaseLockSelect.appendChild(option);
}
}
phaseLockGroup.appendChild(phaseLockLabel);
phaseLockGroup.appendChild(phaseLockSelect);
layerDiv.appendChild(phaseLockGroup);
// Harmonic Offset Checkbox
const harmonicOffsetGroup = document.createElement('div');
harmonicOffsetGroup.className = 'control-group';
const harmonicOffsetLabel = document.createElement('label');
const harmonicOffsetCheckbox = document.createElement('input');
harmonicOffsetCheckbox.type = 'checkbox';
harmonicOffsetCheckbox.id = `tab${tabIndex}-layer${layer.layerNumber}-harmonic-offset`;
harmonicOffsetCheckbox.checked = layer.phaseLock.harmonicOffset;
harmonicOffsetLabel.appendChild(harmonicOffsetCheckbox);
harmonicOffsetLabel.appendChild(document.createTextNode(` Apply Harmonic Offset`));
harmonicOffsetGroup.appendChild(harmonicOffsetLabel);
layerDiv.appendChild(harmonicOffsetGroup);
// LFO Controls
const lfoGroup = document.createElement('div');
lfoGroup.className = 'control-group';
const lfoLabel = document.createElement('label');
lfoLabel.innerHTML = `LFO Controls:`;
lfoGroup.appendChild(lfoLabel);
// LFO Frequency
const lfoFreqLabel = document.createElement('label');
lfoFreqLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-lfo-frequency`;
lfoFreqLabel.innerHTML = `LFO Frequency: <span id="tab${tabIndex}-layer${layer.layerNumber}-lfo-frequency-val">${layer.lfo.frequency.toFixed(2)}</span>`;
const lfoFreqSlider = document.createElement('input');
lfoFreqSlider.type = 'range';
lfoFreqSlider.id = `tab${tabIndex}-layer${layer.layerNumber}-lfo-frequency`;
lfoFreqSlider.min = '0.1';
lfoFreqSlider.max = '5.0';
lfoFreqSlider.step = '0.1';
lfoFreqSlider.value = layer.lfo.frequency;
lfoFreqSlider.disabled = !layer.enabled;
lfoGroup.appendChild(lfoFreqLabel);
lfoGroup.appendChild(lfoFreqSlider);
// LFO Amplitude
const lfoAmpLabel = document.createElement('label');
lfoAmpLabel.htmlFor = `tab${tabIndex}-layer${layer.layerNumber}-lfo-amplitude`;
lfoAmpLabel.innerHTML = `LFO Amplitude: <span id="tab${tabIndex}-layer${layer.layerNumber}-lfo-amplitude-val">${layer.lfo.amplitude.toFixed(2)}</span>`;
const lfoAmpSlider = document.createElement('input');
lfoAmpSlider.type = 'range';
lfoAmpSlider.id = `tab${tabIndex}-layer${layer.layerNumber}-lfo-amplitude`;
lfoAmpSlider.min = '0.0';
lfoAmpSlider.max = '2.0';
lfoAmpSlider.step = '0.1';
lfoAmpSlider.value = layer.lfo.amplitude;
lfoAmpSlider.disabled = !layer.enabled;
lfoGroup.appendChild(lfoAmpLabel);
lfoGroup.appendChild(lfoAmpSlider);
layerDiv.appendChild(lfoGroup);
// Append the layer controls to the main controls div
layersControlsDiv.appendChild(layerDiv);
// Store Control Elements
layer.controls = {
enableCheckbox: layerCheckbox,
waveformSelect: waveformSelect,
frequencySlider: freqSlider,
frequencyVal: document.getElementById(`tab${tabIndex}-layer${layer.layerNumber}-frequency-val`),
amplitudeSlider: ampSlider,
amplitudeVal: document.getElementById(`tab${tabIndex}-layer${layer.layerNumber}-amplitude-val`),
phaseSlider: phaseSlider,
phaseVal: document.getElementById(`tab${tabIndex}-layer${layer.layerNumber}-phase-val`),
phaseLockSelect: phaseLockSelect,
harmonicOffsetCheckbox: harmonicOffsetCheckbox,
lfoFrequencySlider: lfoFreqSlider,
lfoFrequencyVal: document.getElementById(`tab${tabIndex}-layer${layer.layerNumber}-lfo-frequency-val`),
lfoAmplitudeSlider: lfoAmpSlider,
lfoAmplitudeVal: document.getElementById(`tab${tabIndex}-layer${layer.layerNumber}-lfo-amplitude-val`)
};
}
// Function to Initialize Layer Controls for Active Tab
function initializeLayerControls(tabIndex) {
const currentTab = tabs[tabIndex];
const layers = currentTab.layers;
const layersControlsDiv = document.getElementById('layers-controls');
layersControlsDiv.innerHTML = ''; // Clear existing controls
layers.forEach(layer => {
createLayerControls(currentTab, layer, tabIndex);
});
// After creating layer controls, set up event listeners
setupLayerControlEvents(tabIndex);
}
// Function to Set Up Event Listeners for Layer Controls
function setupLayerControlEvents(tabIndex) {
const currentTab = tabs[tabIndex];
const layers = currentTab.layers;
layers.forEach(layer => {
const ctrl = layer.controls;
// Enable/Disable layer
ctrl.enableCheckbox.addEventListener('change', () => {
layer.enabled = ctrl.enableCheckbox.checked;
// Enable or disable sliders based only on layer enabled state
ctrl.frequencySlider.disabled = !layer.enabled;
ctrl.amplitudeSlider.disabled = !layer.enabled;
ctrl.phaseSlider.disabled = !layer.enabled;
ctrl.waveformSelect.disabled = !layer.enabled;
ctrl.lfoFrequencySlider.disabled = !layer.enabled;
ctrl.lfoAmplitudeSlider.disabled = !layer.enabled;
// Apply Global Controls if enabled
const globalFreqEnabled = document.getElementById('global-frequency-enable').checked;
const globalAmpEnabled = document.getElementById('global-amplitude-enable').checked;
const globalPhaseEnabled = document.getElementById('global-phase-enable').checked;
if (globalFreqEnabled && layer.enabled) {
layer.frequencyGlobal = parseFloat(document.getElementById('global-frequency').value);
} else {
layer.frequencyGlobal = 0;
}
if (globalAmpEnabled && layer.enabled) {
layer.amplitudeGlobal = parseFloat(document.getElementById('global-amplitude').value);
} else {
layer.amplitudeGlobal = 0;
}
if (globalPhaseEnabled && layer.enabled) {
layer.phaseGlobal = parseFloat(document.getElementById('global-phase').value);
} else {
layer.phaseGlobal = 0;
}
// Update display values
updateLayerSliders(layer, 'frequency');
updateLayerSliders(layer, 'amplitude');
updateLayerSliders(layer, 'phase');
// Initialize or Stop LFO
if (layer.enabled) {
initializeLFO(layer, currentTab);
} else {
stopLFO(layer);
}
});
// Waveform Selection
ctrl.waveformSelect.addEventListener('change', () => {
layer.waveform = ctrl.waveformSelect.value;
});
// Frequency Slider
ctrl.frequencySlider.addEventListener('input', () => {
layer.frequency = parseFloat(ctrl.frequencySlider.value);
if (!document.getElementById('global-frequency-enable').checked) {
ctrl.frequencyVal.textContent = layer.frequency.toFixed(1);
} else {
ctrl.frequencyVal.textContent = (layer.frequency + layer.frequencyGlobal).toFixed(1);
}
});
// Amplitude Slider
ctrl.amplitudeSlider.addEventListener('input', () => {
layer.amplitude = parseFloat(ctrl.amplitudeSlider.value);
if (!document.getElementById('global-amplitude-enable').checked) {
ctrl.amplitudeVal.textContent = layer.amplitude.toFixed(1);
} else {
ctrl.amplitudeVal.textContent = (layer.amplitude + layer.amplitudeGlobal).toFixed(1);
}
});
// Phase Slider
ctrl.phaseSlider.addEventListener('input', () => {
layer.phase = parseFloat(ctrl.phaseSlider.value);
if (!document.getElementById('global-phase-enable').checked) {
ctrl.phaseVal.textContent = layer.phase.toFixed(1);
} else {
ctrl.phaseVal.textContent = (layer.phase + layer.phaseGlobal).toFixed(1);
}
});
// Phase Lock Dropdown
ctrl.phaseLockSelect.addEventListener('change', () => {
layer.phaseLock.target = ctrl.phaseLockSelect.value;
updatePhaseLocking(tabIndex);
});
// Harmonic Offset Checkbox
ctrl.harmonicOffsetCheckbox.addEventListener('change', () => {
layer.phaseLock.harmonicOffset = ctrl.harmonicOffsetCheckbox.checked;
updatePhaseLocking(tabIndex);
});
// LFO Frequency Slider
ctrl.lfoFrequencySlider.addEventListener('input', () => {
layer.lfo.frequency = parseFloat(ctrl.lfoFrequencySlider.value);
ctrl.lfoFrequencyVal.textContent = layer.lfo.frequency.toFixed(2);
if (layer.lfo.oscillator) {
layer.lfo.oscillator.frequency.setValueAtTime(layer.lfo.frequency, currentTab.audio.audioCtx.currentTime);
}
});
// LFO Amplitude Slider
ctrl.lfoAmplitudeSlider.addEventListener('input', () => {
layer.lfo.amplitude = parseFloat(ctrl.lfoAmplitudeSlider.value);
ctrl.lfoAmplitudeVal.textContent = layer.lfo.amplitude.toFixed(2);
if (layer.lfo.gainNode) {
layer.lfo.gainNode.gain.setValueAtTime(layer.lfo.amplitude, currentTab.audio.audioCtx.currentTime);
}
});
// Initialize LFO if layer is enabled
if (layer.enabled) {
initializeLFO(layer, currentTab);
}
});
}
// Initialize Global Event Listeners
function initializeGlobalControls() {
// Frequency Global Control
const globalFreqEnable = document.getElementById('global-frequency-enable');
const globalFreqSlider = document.getElementById('global-frequency');
const globalFreqVal = document.getElementById('global-frequency-val');
globalFreqEnable.addEventListener('change', () => {
const isEnabled = globalFreqEnable.checked;
globalFreqSlider.disabled = !isEnabled;
globalFreqVal.textContent = globalFreqSlider.value;
// Update layers based on global frequency
tabs.forEach((tab, tabIndex) => {
tab.layers.forEach(layer => {
if (isEnabled && layer.enabled) {
layer.frequencyGlobal = parseFloat(globalFreqSlider.value);
} else {
layer.frequencyGlobal = 0;
}
updateLayerSliders(layer, 'frequency');
});
});
});
globalFreqSlider.addEventListener('input', () => {
globalFreqVal.textContent = globalFreqSlider.value;
if (globalFreqEnable.checked) {
tabs.forEach((tab, tabIndex) => {
tab.layers.forEach(layer => {
if (layer.enabled) {
layer.frequencyGlobal = parseFloat(globalFreqSlider.value);
layer.controls.frequencyVal.textContent = (layer.frequency + layer.frequencyGlobal).toFixed(1);
}
});
});
}
});
// Amplitude Global Control
const globalAmpEnable = document.getElementById('global-amplitude-enable');
const globalAmpSlider = document.getElementById('global-amplitude');
const globalAmpVal = document.getElementById('global-amplitude-val');
globalAmpEnable.addEventListener('change', () => {
const isEnabled = globalAmpEnable.checked;
globalAmpSlider.disabled = !isEnabled;
globalAmpVal.textContent = globalAmpSlider.value;
// Update layers based on global amplitude
tabs.forEach((tab, tabIndex) => {
tab.layers.forEach(layer => {
if (isEnabled && layer.enabled) {
layer.amplitudeGlobal = parseFloat(globalAmpSlider.value);
} else {
layer.amplitudeGlobal = 0;
}
updateLayerSliders(layer, 'amplitude');
});
});
});
globalAmpSlider.addEventListener('input', () => {
globalAmpVal.textContent = globalAmpSlider.value;