SREAL commited on
Commit
479ec46
·
verified ·
1 Parent(s): 8ab8c77

its not initializing properly

Browse files
Files changed (1) hide show
  1. script.js +203 -5
script.js CHANGED
@@ -16,11 +16,38 @@ function initAudioContext() {
16
  Tone.start();
17
  audioContextStarted = true;
18
  console.log('Audio context started');
 
19
  }
 
20
  }
21
 
22
  // Setup Tone.js components
23
  function setupSynth() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  // Create effects
25
  reverb = new Tone.Reverb({
26
  decay: 2,
@@ -74,6 +101,7 @@ function setupSynth() {
74
  synth.connect(analyser);
75
 
76
  console.log('Synth initialized');
 
77
  }
78
  // MIDI handling
79
  let midiAccess = null;
@@ -509,7 +537,6 @@ function setupADSRVisualizer() {
509
  // Make drawADSR accessible globally for updates
510
  window.drawADSR = drawADSR;
511
  }
512
-
513
  // Initialize everything when page loads
514
  document.addEventListener('DOMContentLoaded', () => {
515
  // Setup UI controls
@@ -517,14 +544,19 @@ document.addEventListener('DOMContentLoaded', () => {
517
 
518
  // Setup visualizers
519
  setupVisualizer();
520
- setupADSRVisualizer();
521
 
522
- // Setup synth
523
- setupSynth();
 
524
 
525
  // MIDI connection button
526
  document.getElementById('midi-connect-btn').addEventListener('click', () => {
527
- initAudioContext();
 
 
 
 
528
  if (navigator.requestMIDIAccess) {
529
  navigator.requestMIDIAccess()
530
  .then(onMIDISuccess, onMIDIFailure);
@@ -533,3 +565,169 @@ document.addEventListener('DOMContentLoaded', () => {
533
  }
534
  });
535
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  Tone.start();
17
  audioContextStarted = true;
18
  console.log('Audio context started');
19
+ return true;
20
  }
21
+ return false;
22
  }
23
 
24
  // Setup Tone.js components
25
  function setupSynth() {
26
+ if (!audioContextStarted) {
27
+ console.warn('Audio context not started yet');
28
+ return false;
29
+ }
30
+
31
+ // Clean up existing components if they exist
32
+ if (synth) {
33
+ synth.dispose();
34
+ }
35
+ if (filter) {
36
+ filter.dispose();
37
+ }
38
+ if (reverb) {
39
+ reverb.dispose();
40
+ }
41
+ if (distortion) {
42
+ distortion.dispose();
43
+ }
44
+ if (delay) {
45
+ delay.dispose();
46
+ }
47
+ if (filterEnvelope) {
48
+ filterEnvelope.dispose();
49
+ }
50
+
51
  // Create effects
52
  reverb = new Tone.Reverb({
53
  decay: 2,
 
101
  synth.connect(analyser);
102
 
103
  console.log('Synth initialized');
104
+ return true;
105
  }
106
  // MIDI handling
107
  let midiAccess = null;
 
537
  // Make drawADSR accessible globally for updates
538
  window.drawADSR = drawADSR;
539
  }
 
540
  // Initialize everything when page loads
541
  document.addEventListener('DOMContentLoaded', () => {
542
  // Setup UI controls
 
544
 
545
  // Setup visualizers
546
  setupVisualizer();
547
+ // setupADSRVisualizer(); // This function doesn't exist, using draw functions directly
548
 
549
+ // Initial draw of ADSR envelopes
550
+ drawADSR();
551
+ drawFilterEnvelope();
552
 
553
  // MIDI connection button
554
  document.getElementById('midi-connect-btn').addEventListener('click', () => {
555
+ const wasStarted = initAudioContext();
556
+ if (wasStarted) {
557
+ setupSynth();
558
+ }
559
+
560
  if (navigator.requestMIDIAccess) {
561
  navigator.requestMIDIAccess()
562
  .then(onMIDISuccess, onMIDIFailure);
 
565
  }
566
  });
567
  });
568
+
569
+ // Add missing ADSR drawing functions
570
+ function drawADSR() {
571
+ const canvas = document.getElementById('amp-envelope-editor');
572
+ if (!canvas) return;
573
+
574
+ const ctx = canvas.getContext('2d');
575
+ if (!ctx) return;
576
+
577
+ // Set canvas size
578
+ const dpr = window.devicePixelRatio || 1;
579
+ canvas.width = canvas.clientWidth * dpr;
580
+ canvas.height = canvas.clientHeight * dpr;
581
+ ctx.scale(dpr, dpr);
582
+
583
+ // Clear canvas
584
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
585
+
586
+ // Draw grid
587
+ ctx.strokeStyle = 'rgba(6, 182, 212, 0.1)';
588
+ ctx.lineWidth = 1;
589
+
590
+ const width = canvas.clientWidth;
591
+ const height = canvas.clientHeight;
592
+
593
+ // Vertical lines
594
+ for (let x = 0; x < width; x += 20) {
595
+ ctx.beginPath();
596
+ ctx.moveTo(x, 0);
597
+ ctx.lineTo(x, height);
598
+ ctx.stroke();
599
+ }
600
+
601
+ // Horizontal lines
602
+ for (let y = 0; y < height; y += 20) {
603
+ ctx.beginPath();
604
+ ctx.moveTo(0, y);
605
+ ctx.lineTo(width, y);
606
+ ctx.stroke();
607
+ }
608
+
609
+ // Draw ADSR envelope
610
+ const attack = parseFloat(document.getElementById('envelope-attack').value);
611
+ const decay = parseFloat(document.getElementById('envelope-decay').value);
612
+ const sustain = parseFloat(document.getElementById('envelope-sustain').value);
613
+ const release = parseFloat(document.getElementById('envelope-release').value);
614
+
615
+ // Normalize values for visualization (max 5 seconds for attack/decay, 10 for release)
616
+ const attackX = (attack / 5) * width * 0.2;
617
+ const decayX = (decay / 5) * width * 0.2;
618
+ const sustainX = width * 0.4; // Fixed sustain time
619
+ const releaseX = (release / 10) * width * 0.2;
620
+
621
+ const sustainY = height - (sustain * height);
622
+
623
+ ctx.beginPath();
624
+ ctx.lineWidth = 3;
625
+ ctx.strokeStyle = '#06b6d4'; // cyan-500
626
+
627
+ // Start at 0
628
+ ctx.moveTo(0, height);
629
+
630
+ // Attack
631
+ ctx.lineTo(attackX, 0);
632
+
633
+ // Decay
634
+ ctx.lineTo(attackX + decayX, sustainY);
635
+
636
+ // Sustain
637
+ ctx.lineTo(attackX + decayX + sustainX, sustainY);
638
+
639
+ // Release
640
+ ctx.lineTo(attackX + decayX + sustainX + releaseX, height);
641
+
642
+ ctx.stroke();
643
+ }
644
+
645
+ function drawFilterEnvelope() {
646
+ const canvas = document.getElementById('filter-envelope-editor');
647
+ if (!canvas) return;
648
+
649
+ const ctx = canvas.getContext('2d');
650
+ if (!ctx) return;
651
+
652
+ // Set canvas size
653
+ const dpr = window.devicePixelRatio || 1;
654
+ canvas.width = canvas.clientWidth * dpr;
655
+ canvas.height = canvas.clientHeight * dpr;
656
+ ctx.scale(dpr, dpr);
657
+
658
+ // Clear canvas
659
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
660
+
661
+ // Draw grid
662
+ ctx.strokeStyle = 'rgba(6, 182, 212, 0.1)';
663
+ ctx.lineWidth = 1;
664
+
665
+ const width = canvas.clientWidth;
666
+ const height = canvas.clientHeight;
667
+
668
+ // Vertical lines
669
+ for (let x = 0; x < width; x += 20) {
670
+ ctx.beginPath();
671
+ ctx.moveTo(x, 0);
672
+ ctx.lineTo(x, height);
673
+ ctx.stroke();
674
+ }
675
+
676
+ // Horizontal lines
677
+ for (let y = 0; y < height; y += 20) {
678
+ ctx.beginPath();
679
+ ctx.moveTo(0, y);
680
+ ctx.lineTo(width, y);
681
+ ctx.stroke();
682
+ }
683
+
684
+ // Draw ADSR envelope
685
+ const attack = parseFloat(document.getElementById('filter-attack').value);
686
+ const decay = parseFloat(document.getElementById('filter-decay').value);
687
+ const sustain = parseFloat(document.getElementById('filter-sustain').value);
688
+ const release = parseFloat(document.getElementById('filter-release').value);
689
+
690
+ // Normalize values for visualization (max 5 seconds for attack/decay, 10 for release)
691
+ const attackX = (attack / 5) * width * 0.2;
692
+ const decayX = (decay / 5) * width * 0.2;
693
+ const sustainX = width * 0.4; // Fixed sustain time
694
+ const releaseX = (release / 10) * width * 0.2;
695
+
696
+ const sustainY = height - (sustain * height);
697
+
698
+ ctx.beginPath();
699
+ ctx.lineWidth = 3;
700
+ ctx.strokeStyle = '#06b6d4'; // cyan-500
701
+
702
+ // Start at 0
703
+ ctx.moveTo(0, height);
704
+
705
+ // Attack
706
+ ctx.lineTo(attackX, 0);
707
+
708
+ // Decay
709
+ ctx.lineTo(attackX + decayX, sustainY);
710
+
711
+ // Sustain
712
+ ctx.lineTo(attackX + decayX + sustainX, sustainY);
713
+
714
+ // Release
715
+ ctx.lineTo(attackX + decayX + sustainX + releaseX, height);
716
+
717
+ ctx.stroke();
718
+ }
719
+
720
+ // Update envelope drawing when controls change
721
+ document.addEventListener('DOMContentLoaded', () => {
722
+ // Amplitude envelope controls
723
+ document.getElementById('envelope-attack').addEventListener('input', drawADSR);
724
+ document.getElementById('envelope-decay').addEventListener('input', drawADSR);
725
+ document.getElementById('envelope-sustain').addEventListener('input', drawADSR);
726
+ document.getElementById('envelope-release').addEventListener('input', drawADSR);
727
+
728
+ // Filter envelope controls
729
+ document.getElementById('filter-attack').addEventListener('input', drawFilterEnvelope);
730
+ document.getElementById('filter-decay').addEventListener('input', drawFilterEnvelope);
731
+ document.getElementById('filter-sustain').addEventListener('input', drawFilterEnvelope);
732
+ document.getElementById('filter-release').addEventListener('input', drawFilterEnvelope);
733
+ });