File size: 8,873 Bytes
2372d15 | 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 | <!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bio-Vibe Web</title>
<link rel="icon" href="data:,">
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #1a1a2e;
color: #e94560;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
.container {
background-color: #16213e;
padding: 40px;
border-radius: 15px;
text-align: center;
box-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
h1 { color: #fff; margin-bottom: 5px; }
p { color: #a2a2bd; margin-bottom: 30px; }
#bpm-display {
font-size: 80px;
font-weight: bold;
margin: 20px 0;
text-shadow: 0 0 15px #e94560;
transition: color 0.3s ease;
}
@keyframes pulse {
0% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.05); opacity: 0.8; }
100% { transform: scale(1); opacity: 1; }
}
.button-group {
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
max-width: 600px;
}
button {
background-color: #0f3460;
color: white;
border: none;
padding: 15px 25px;
font-size: 16px;
border-radius: 8px;
cursor: pointer;
transition: 0.3s;
font-weight: bold;
}
button:hover { background-color: #4ecca3; color: #1a1a2e; }
button:disabled { background-color: #333; cursor: not-allowed; color: #777; }
/* CSS riêng cho nút Dừng nhạc */
#btn-stop { background-color: #8b0000; }
#btn-stop:hover { background-color: #ff4d4d; color: white; }
#btn-stop:disabled { background-color: #333; }
#status { margin-top: 20px; color: #4ecca3; font-style: italic; font-size: 14px;}
</style>
</head>
<body>
<div class="container">
<h1>Bio-Vibe 🎧</h1>
<p>Âm nhạc trị liệu AI theo Nhịp tim</p>
<div id="bpm-display">-- BPM</div>
<div class="button-group">
<button id="btn-connect">1. Bắt tay Đồng hồ</button>
<button id="btn-generate">2. Sinh nhạc AI</button>
<button id="btn-stop" disabled>3. Dừng nhạc</button>
</div>
<div id="status">Chưa kết nối... Mở nRF Connect trên điện thoại để chuẩn bị!</div>
</div>
<script>
let currentBpm = 0;
let currentAudio = null; // Biến toàn cục để quản lý file âm thanh
const bpmDisplay = document.getElementById('bpm-display');
const statusText = document.getElementById('status');
const btnConnect = document.getElementById('btn-connect');
const btnGenerate = document.getElementById('btn-generate');
const btnStop = document.getElementById('btn-stop');
// ==========================================
// 1. BLUETOOTH LOGIC
// ==========================================
btnConnect.addEventListener('click', async () => {
try {
statusText.innerText = "Đang mở máy quét Bluetooth...";
const device = await navigator.bluetooth.requestDevice({
acceptAllDevices: true,
optionalServices: ['heart_rate']
});
statusText.innerText = `Đã chọn: ${device.name}. Đang kết nối...`;
const server = await device.gatt.connect();
const service = await server.getPrimaryService('heart_rate');
const characteristic = await service.getCharacteristic('heart_rate_measurement');
await characteristic.startNotifications();
characteristic.addEventListener('characteristicvaluechanged', handleHeartRateData);
statusText.innerText = "Kết nối thành công! Đang đợi nhịp tim truyền lên...";
btnConnect.innerText = "✓ Đã kết nối Bluetooth";
btnConnect.style.backgroundColor = "#4ecca3";
btnConnect.style.color = "#1a1a2e";
} catch (error) {
console.error("Bluetooth Error:", error);
statusText.innerText = "Hủy kết nối hoặc lỗi thiết bị!";
statusText.style.color = "#ff4d4d";
}
});
function handleHeartRateData(event) {
const value = event.target.value;
const flags = value.getUint8(0);
if ((flags & 0x01) === 0) {
currentBpm = value.getUint8(1);
} else {
currentBpm = value.getUint16(1, true);
}
bpmDisplay.innerText = `${currentBpm} BPM`;
bpmDisplay.style.color = "#e94560";
statusText.innerText = "Đang đọc nhịp tim thành công. Sẵn sàng tạo nhạc!";
statusText.style.color = "#4ecca3";
}
// ==========================================
// 2. TẠO VÀ PHÁT NHẠC
// ==========================================
btnGenerate.addEventListener('click', async () => {
if (currentBpm === 0) {
alert("Bạn chưa kết nối Bluetooth hoặc chưa gửi số nhịp tim từ nRF Connect!");
return;
}
// TÍNH NĂNG MỚI: Tắt ngay bài nhạc cũ (nếu có) trước khi tạo bài mới
if (currentAudio) {
currentAudio.pause();
currentAudio.currentTime = 0; // Tua lại từ đầu
bpmDisplay.style.animation = "none";
}
statusText.innerText = `Đang gửi ${currentBpm} BPM cho bộ não AI xử lý...`;
btnGenerate.innerText = "AI đang sáng tác...";
btnGenerate.disabled = true;
btnStop.disabled = true;
try {
const response = await fetch('/generate-vibe', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ bpm: currentBpm })
});
const data = await response.json();
if (data.status === "success") {
statusText.innerText = "Sáng tác xong! Đang phát nhạc 🎵";
// Gắn file nhạc mới vào biến toàn cục
currentAudio = new Audio(data.audio_url);
currentAudio.play();
bpmDisplay.style.animation = "pulse 1s infinite";
btnStop.disabled = false; // Mở khóa nút Dừng
// Xử lý khi nhạc tự chạy hết
currentAudio.onended = () => {
bpmDisplay.style.animation = "none";
statusText.innerText = "Bản nhạc đã kết thúc. Sẵn sàng tạo trải nghiệm mới!";
btnStop.disabled = true;
};
} else {
statusText.innerText = `Lỗi từ Server: ${data.message}`;
}
} catch (error) {
console.error("API Error:", error);
statusText.innerText = "Mất kết nối với máy chủ AI!";
} finally {
btnGenerate.innerText = "2. Sinh nhạc AI";
btnGenerate.disabled = false;
}
});
// ==========================================
// 3. DỪNG NHẠC THỦ CÔNG
// ==========================================
btnStop.addEventListener('click', () => {
if (currentAudio) {
currentAudio.pause(); // Cắt âm thanh
currentAudio.currentTime = 0; // Xóa cache phát
bpmDisplay.style.animation = "none"; // Tắt nhấp nháy
statusText.innerText = "Đã dừng phát nhạc.";
btnStop.disabled = true; // Khóa lại nút dừng
}
});
</script>
</body>
</html> |