Hezu06 commited on
Commit
bd26bbb
·
1 Parent(s): fdf4d3a

refactor music generation by enhancing audio effects chain and improving velocity and timing humanization

Browse files
Files changed (1) hide show
  1. index.html +56 -19
index.html CHANGED
@@ -459,12 +459,46 @@
459
  // ===============================
460
  // GENERATE MUSIC
461
 
462
- const gain = new Tone.Gain(1.3).toDestination();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463
 
464
- const limiter = new Tone.Limiter(-1).connect(gain);
 
 
 
 
 
465
 
466
- const filter = new Tone.Filter(4000, "lowpass").connect(limiter); // mở sáng hơn
467
- const reverb = new Tone.Reverb({ decay: 2.5, wet: 0.2 }).connect(filter);
 
 
 
 
 
468
 
469
  const piano = new Tone.Sampler({
470
  urls: {
@@ -485,7 +519,7 @@
485
  },
486
  baseUrl: "https://raw.githubusercontent.com/Tonejs/audio/master/salamander/",
487
  release: 1
488
- }).connect(reverb);
489
 
490
  // Gọi Tone.start() sẵn khi load trang nếu được
491
  log("🎹 Đã khởi tạo Lo-fi Synthesizer (Có Limiter chống rè).");
@@ -527,22 +561,25 @@
527
 
528
  ws.onmessage = (event) => {
529
  const data = JSON.parse(event.data);
530
-
531
  if (data.notes) {
532
  data.notes.forEach(n => {
533
- const freq = Tone.Frequency(n.note, "midi").toNote();
534
- let normalizedVelocity = n.velocity / 127;
535
-
536
- // Đảm bảo không có nốt nào quá nhỏ
537
- normalizedVelocity = Math.max(0.5, n.velocity / 127);
538
-
539
- // duration bây giờ là số (giây), truyền thẳng vào Tone.now()
540
- piano.triggerAttackRelease(
541
- Tone.Frequency(n.note, "midi").toNote(),
542
- Math.min(n.duration, 1.5), // limit duration
543
- Tone.now() + Math.random() * 0.02,
544
- normalizedVelocity
545
- );
 
 
 
546
  });
547
  }
548
  };
 
459
  // ===============================
460
  // GENERATE MUSIC
461
 
462
+ const limiter = new Tone.Limiter(-1);
463
+
464
+ // 2. Compressor (giữ âm ổn định)
465
+ const compressor = new Tone.Compressor({
466
+ threshold: -18,
467
+ ratio: 3,
468
+ attack: 0.01,
469
+ release: 0.2
470
+ });
471
+
472
+ // 3. EQ (làm ấm piano)
473
+ const eq = new Tone.EQ3({
474
+ low: 3, // tăng bass nhẹ
475
+ mid: -1, // giảm mid đục
476
+ high: 2 // tăng sáng nhẹ
477
+ });
478
+
479
+ // 4. Stereo widen (cho không gian rộng)
480
+ const stereo = new Tone.StereoWidener(0.6);
481
+
482
+ // 5. Reverb (không gian)
483
+ const reverb = new Tone.Reverb({
484
+ decay: 4,
485
+ wet: 0.3
486
+ });
487
 
488
+ // 6. Delay (lofi feel)
489
+ const delay = new Tone.FeedbackDelay({
490
+ delayTime: "8n",
491
+ feedback: 0.25,
492
+ wet: 0.15
493
+ });
494
 
495
+ // Connect chain
496
+ delay.connect(reverb);
497
+ reverb.connect(stereo);
498
+ stereo.connect(eq);
499
+ eq.connect(compressor);
500
+ compressor.connect(limiter);
501
+ limiter.toDestination();
502
 
503
  const piano = new Tone.Sampler({
504
  urls: {
 
519
  },
520
  baseUrl: "https://raw.githubusercontent.com/Tonejs/audio/master/salamander/",
521
  release: 1
522
+ }).connect(delay);
523
 
524
  // Gọi Tone.start() sẵn khi load trang nếu được
525
  log("🎹 Đã khởi tạo Lo-fi Synthesizer (Có Limiter chống rè).");
 
561
 
562
  ws.onmessage = (event) => {
563
  const data = JSON.parse(event.data);
564
+
565
  if (data.notes) {
566
  data.notes.forEach(n => {
567
+
568
+ const noteName = Tone.Frequency(n.note, "midi").toNote();
569
+
570
+ // 🎛 velocity humanize
571
+ let velocity = n.velocity / 127;
572
+ velocity = Math.max(0.25, velocity);
573
+
574
+ // 🎲 timing humanize
575
+ const humanize = (Math.random() - 0.5) * 0.02;
576
+
577
+ piano.triggerAttackRelease(
578
+ noteName,
579
+ n.duration,
580
+ Tone.now() + humanize,
581
+ velocity
582
+ );
583
  });
584
  }
585
  };