Hezu06 commited on
Commit
a0862a6
·
1 Parent(s): 491886c

refactor audio synthesis to improve sound quality and implement lo-fi piano samples

Browse files
Files changed (1) hide show
  1. index.html +22 -14
index.html CHANGED
@@ -466,24 +466,30 @@
466
  const limiter = new Tone.Limiter(-2).toDestination();
467
 
468
  // 2. Không gian âm thanh: Đẩy vào Limiter thay vì toDestination trực tiếp
469
- const filter = new Tone.Filter(900, "lowpass").connect(limiter);
470
- const reverb = new Tone.Reverb({ decay: 6, wet: 0.5 }).connect(filter);
471
  const delay = new Tone.FeedbackDelay("8n", 0.2).connect(reverb);
472
 
473
  // 3. Nhạc cụ chính: Giảm mạnh Volume gốc
474
- const piano = new Tone.PolySynth(Tone.Synth, {
475
- volume: -10, // Quan trọng: Đổi từ +5 thành -10 để lấy không gian cho hợp âm cộng dồn
476
- oscillator: {
477
- type: "triangle"
 
 
478
  },
479
- envelope: {
480
- attack: 0.1,
481
- decay: 0.5,
482
- sustain: 0.5,
483
- release: 3.5
484
- }
485
  }).connect(delay);
486
 
 
 
 
 
 
 
 
487
  // Gọi Tone.start() sẵn khi load trang nếu được
488
  log("🎹 Đã khởi tạo Lo-fi Synthesizer (Có Limiter chống rè).");
489
  setStatus("Sẵn sàng sinh nhạc AI", "ok");
@@ -501,7 +507,9 @@
501
 
502
  // Bắt buộc trình duyệt cho phép phát âm thanh
503
  await Tone.start();
504
-
 
 
505
  if (ws) ws.close(); // Đóng kết nối cũ nếu có
506
 
507
  setStatus(`Đang kết nối não AI với ${currentBpm} BPM…`, 'info');
@@ -531,7 +539,7 @@
531
  let normalizedVelocity = n.velocity / 127;
532
 
533
  // Đảm bảo không có nốt nào quá nhỏ
534
- if (normalizedVelocity < 0.5) normalizedVelocity = 0.5;
535
 
536
  // duration bây giờ là số (giây), truyền thẳng vào Tone.now()
537
  piano.triggerAttackRelease(freq, n.duration, Tone.now(), normalizedVelocity);
 
466
  const limiter = new Tone.Limiter(-2).toDestination();
467
 
468
  // 2. Không gian âm thanh: Đẩy vào Limiter thay vì toDestination trực tiếp
469
+ const filter = new Tone.Filter(2000, "lowpass").connect(limiter);
470
+ const reverb = new Tone.Reverb({ decay: 3, wet: 0.25 })
471
  const delay = new Tone.FeedbackDelay("8n", 0.2).connect(reverb);
472
 
473
  // 3. Nhạc cụ chính: Giảm mạnh Volume gốc
474
+ const piano = new Tone.Sampler({
475
+ urls: {
476
+ "C4": "C4.mp3",
477
+ "D#4": "Ds4.mp3",
478
+ "F#4": "Fs4.mp3",
479
+ "A4": "A4.mp3",
480
  },
481
+ baseUrl: "https://cdn.jsdelivr.net/gh/Tonejs/audio@master/salamander/",
482
+ release: 1,
483
+ volume: -6
 
 
 
484
  }).connect(delay);
485
 
486
+ piano.triggerAttackRelease(
487
+ freq,
488
+ n.duration,
489
+ Tone.now() + Math.random() * 0.02, // lệch nhẹ timing
490
+ normalizedVelocity
491
+ );
492
+
493
  // Gọi Tone.start() sẵn khi load trang nếu được
494
  log("🎹 Đã khởi tạo Lo-fi Synthesizer (Có Limiter chống rè).");
495
  setStatus("Sẵn sàng sinh nhạc AI", "ok");
 
507
 
508
  // Bắt buộc trình duyệt cho phép phát âm thanh
509
  await Tone.start();
510
+ await Tone.loaded();
511
+ console.log("✅ Piano samples loaded");
512
+
513
  if (ws) ws.close(); // Đóng kết nối cũ nếu có
514
 
515
  setStatus(`Đang kết nối não AI với ${currentBpm} BPM…`, 'info');
 
539
  let normalizedVelocity = n.velocity / 127;
540
 
541
  // Đảm bảo không có nốt nào quá nhỏ
542
+ normalizedVelocity = Math.max(0.2, n.velocity / 127);
543
 
544
  // duration bây giờ là số (giây), truyền thẳng vào Tone.now()
545
  piano.triggerAttackRelease(freq, n.duration, Tone.now(), normalizedVelocity);