Update frontend/index.html
Browse files- frontend/index.html +11 -21
frontend/index.html
CHANGED
|
@@ -3,7 +3,7 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
-
<title>Yue AI - Discord Connection
|
| 7 |
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
| 8 |
<script>
|
| 9 |
window.GLOBAL_CONF = {
|
|
@@ -42,18 +42,12 @@
|
|
| 42 |
let sharedAudioCtx = null;
|
| 43 |
let mainDest = null;
|
| 44 |
|
| 45 |
-
// 1. Khởi tạo hệ thống âm thanh tổng
|
| 46 |
async function initAudio() {
|
| 47 |
if (!sharedAudioCtx) {
|
| 48 |
sharedAudioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
| 49 |
mainDest = sharedAudioCtx.createMediaStreamDestination();
|
| 50 |
|
| 51 |
-
//
|
| 52 |
-
const gainNode = sharedAudioCtx.createGain();
|
| 53 |
-
gainNode.gain.value = 2.0; // Tăng gấp đôi để bù đắp suy hao khi nén
|
| 54 |
-
mainDest.stream.getAudioTracks()[0].enabled = true;
|
| 55 |
-
|
| 56 |
-
// Tăng bitrate để tránh "Empty Stream"
|
| 57 |
const recorder = new MediaRecorder(mainDest.stream, {
|
| 58 |
mimeType: 'audio/webm;codecs=opus',
|
| 59 |
audioBitsPerSecond: 128000
|
|
@@ -66,23 +60,24 @@
|
|
| 66 |
}
|
| 67 |
};
|
| 68 |
|
| 69 |
-
recorder.start(100);
|
| 70 |
-
console.log("🎙️
|
| 71 |
}
|
| 72 |
if (sharedAudioCtx.state === 'suspended') await sharedAudioCtx.resume();
|
| 73 |
}
|
| 74 |
|
| 75 |
-
//
|
| 76 |
const originalCreateElementSource = AudioContext.prototype.createMediaElementSource;
|
| 77 |
AudioContext.prototype.createMediaElementSource = function(element) {
|
| 78 |
const source = originalCreateElementSource.call(this, element);
|
| 79 |
if (mainDest) {
|
| 80 |
-
source.connect(mainDest);
|
|
|
|
| 81 |
}
|
| 82 |
return source;
|
| 83 |
};
|
| 84 |
|
| 85 |
-
//
|
| 86 |
setInterval(() => {
|
| 87 |
document.querySelectorAll('audio').forEach(a => {
|
| 88 |
if (!a.dataset.hooked) {
|
|
@@ -93,36 +88,31 @@
|
|
| 93 |
const s = sharedAudioCtx.createMediaElementSource(a);
|
| 94 |
s.connect(mainDest);
|
| 95 |
s.connect(sharedAudioCtx.destination);
|
| 96 |
-
} catch(e) {
|
| 97 |
-
// Audio source có thể đã được kết nối trước đó
|
| 98 |
-
}
|
| 99 |
}
|
| 100 |
}
|
| 101 |
});
|
| 102 |
}, 1000);
|
| 103 |
|
| 104 |
-
// Nút bấm kết nối Discord
|
| 105 |
window.connectDiscord = async () => {
|
| 106 |
await initAudio();
|
| 107 |
fetch('https://nopeprime-discord-bot.hf.space/join')
|
| 108 |
.then(() => console.log("✅ Đã gọi Bot vào phòng voice"));
|
| 109 |
};
|
| 110 |
|
| 111 |
-
//
|
| 112 |
const oldLog = console.log;
|
| 113 |
console.log = function(...args) {
|
| 114 |
oldLog.apply(console, args);
|
| 115 |
if (typeof args[0] === 'string' && args[0].includes('[[SING_COMMAND]]')) {
|
| 116 |
let song = args[0].split('[[SING_COMMAND]]')[1].trim().split(' ')[0].replace('.mp3','') + '.mp3';
|
| 117 |
let p = document.getElementById('m-player') || document.createElement('audio');
|
| 118 |
-
p.id = 'm-player';
|
| 119 |
-
p.src = `./music/${song}`;
|
| 120 |
if(!p.parentElement) document.body.appendChild(p);
|
| 121 |
p.play();
|
| 122 |
}
|
| 123 |
};
|
| 124 |
|
| 125 |
-
// Kích hoạt khi người dùng tương tác lần đầu
|
| 126 |
document.addEventListener('mousedown', initAudio, { once: true });
|
| 127 |
</script>
|
| 128 |
</body>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="UTF-8" />
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
| 6 |
+
<title>Yue AI - Discord Connection</title>
|
| 7 |
<script src="https://cdn.socket.io/4.7.2/socket.io.min.js"></script>
|
| 8 |
<script>
|
| 9 |
window.GLOBAL_CONF = {
|
|
|
|
| 42 |
let sharedAudioCtx = null;
|
| 43 |
let mainDest = null;
|
| 44 |
|
|
|
|
| 45 |
async function initAudio() {
|
| 46 |
if (!sharedAudioCtx) {
|
| 47 |
sharedAudioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
| 48 |
mainDest = sharedAudioCtx.createMediaStreamDestination();
|
| 49 |
|
| 50 |
+
// Recorder tối ưu cho Discord
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
const recorder = new MediaRecorder(mainDest.stream, {
|
| 52 |
mimeType: 'audio/webm;codecs=opus',
|
| 53 |
audioBitsPerSecond: 128000
|
|
|
|
| 60 |
}
|
| 61 |
};
|
| 62 |
|
| 63 |
+
recorder.start(100);
|
| 64 |
+
console.log("🎙️ Hệ thống Stream đã sẵn sàng!");
|
| 65 |
}
|
| 66 |
if (sharedAudioCtx.state === 'suspended') await sharedAudioCtx.resume();
|
| 67 |
}
|
| 68 |
|
| 69 |
+
// Hook tất cả AudioContext phát sinh từ Live2D hoặc Audio tag
|
| 70 |
const originalCreateElementSource = AudioContext.prototype.createMediaElementSource;
|
| 71 |
AudioContext.prototype.createMediaElementSource = function(element) {
|
| 72 |
const source = originalCreateElementSource.call(this, element);
|
| 73 |
if (mainDest) {
|
| 74 |
+
source.connect(mainDest);
|
| 75 |
+
console.log("🔗 Đã kết nối nguồn âm thanh mới vào luồng Discord");
|
| 76 |
}
|
| 77 |
return source;
|
| 78 |
};
|
| 79 |
|
| 80 |
+
// Tìm và gắn thẻ audio (nhạc, voice)
|
| 81 |
setInterval(() => {
|
| 82 |
document.querySelectorAll('audio').forEach(a => {
|
| 83 |
if (!a.dataset.hooked) {
|
|
|
|
| 88 |
const s = sharedAudioCtx.createMediaElementSource(a);
|
| 89 |
s.connect(mainDest);
|
| 90 |
s.connect(sharedAudioCtx.destination);
|
| 91 |
+
} catch(e) {}
|
|
|
|
|
|
|
| 92 |
}
|
| 93 |
}
|
| 94 |
});
|
| 95 |
}, 1000);
|
| 96 |
|
|
|
|
| 97 |
window.connectDiscord = async () => {
|
| 98 |
await initAudio();
|
| 99 |
fetch('https://nopeprime-discord-bot.hf.space/join')
|
| 100 |
.then(() => console.log("✅ Đã gọi Bot vào phòng voice"));
|
| 101 |
};
|
| 102 |
|
| 103 |
+
// Ghi đè log để bắt lệnh hát
|
| 104 |
const oldLog = console.log;
|
| 105 |
console.log = function(...args) {
|
| 106 |
oldLog.apply(console, args);
|
| 107 |
if (typeof args[0] === 'string' && args[0].includes('[[SING_COMMAND]]')) {
|
| 108 |
let song = args[0].split('[[SING_COMMAND]]')[1].trim().split(' ')[0].replace('.mp3','') + '.mp3';
|
| 109 |
let p = document.getElementById('m-player') || document.createElement('audio');
|
| 110 |
+
p.id = 'm-player'; p.src = `./music/${song}`;
|
|
|
|
| 111 |
if(!p.parentElement) document.body.appendChild(p);
|
| 112 |
p.play();
|
| 113 |
}
|
| 114 |
};
|
| 115 |
|
|
|
|
| 116 |
document.addEventListener('mousedown', initAudio, { once: true });
|
| 117 |
</script>
|
| 118 |
</body>
|