Spaces:
Running
Running
Update index.html
Browse files- index.html +35 -17
index.html
CHANGED
|
@@ -11,17 +11,15 @@
|
|
| 11 |
p { font-size: 14px; color: #666; text-align: center; }
|
| 12 |
.upload-area { border: 2px dashed #ccc; border-radius: 8px; padding: 20px; text-align: center; margin: 20px 0; background: #fafafa; }
|
| 13 |
|
| 14 |
-
/* Скрываем стандартный инпут */
|
| 15 |
input[type="file"] { display: none; }
|
| 16 |
.custom-file-upload { display: inline-block; padding: 10px 20px; background: #007bff; color: white; border-radius: 6px; cursor: pointer; font-weight: bold; margin-bottom: 10px; }
|
| 17 |
.custom-file-upload:hover { background: #0056b3; }
|
| 18 |
|
| 19 |
-
/* Кнопка записи */
|
| 20 |
#recordBtn { display: inline-block; padding: 10px 20px; background: #6c757d; color: white; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; margin-left: 10px; }
|
| 21 |
#recordBtn:hover { background: #5a6268; }
|
| 22 |
|
| 23 |
#fileName { margin-top: 10px; font-size: 14px; color: #333; font-weight: bold; }
|
| 24 |
-
#recordStatus { display: block; font-size: 13px; color: #dc3545; margin-top: 5px; }
|
| 25 |
|
| 26 |
#analyzeBtn { display: block; width: 100%; padding: 12px; background: #28a745; color: white; border: none; border-radius: 6px; font-size: 16px; font-weight: bold; cursor: pointer; margin-top: 15px; }
|
| 27 |
#analyzeBtn:disabled { background: #6c757d; cursor: not-allowed; }
|
|
@@ -70,7 +68,6 @@
|
|
| 70 |
let mediaRecorder;
|
| 71 |
let audioChunks = [];
|
| 72 |
|
| 73 |
-
// Обработка загрузки файла
|
| 74 |
fileInput.addEventListener('change', (e) => {
|
| 75 |
if (e.target.files.length > 0) {
|
| 76 |
currentFile = e.target.files[0];
|
|
@@ -81,27 +78,41 @@
|
|
| 81 |
}
|
| 82 |
});
|
| 83 |
|
| 84 |
-
// Логика записи с микрофона
|
| 85 |
recordBtn.addEventListener('click', async () => {
|
|
|
|
| 86 |
if (mediaRecorder && mediaRecorder.state === 'recording') {
|
| 87 |
mediaRecorder.stop();
|
| 88 |
return;
|
| 89 |
}
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
try {
|
| 92 |
-
|
| 93 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
audioChunks = [];
|
| 96 |
|
| 97 |
mediaRecorder.ondataavailable = event => {
|
| 98 |
-
audioChunks.push(event.data);
|
| 99 |
};
|
| 100 |
|
| 101 |
mediaRecorder.onstop = () => {
|
| 102 |
-
|
| 103 |
-
const audioBlob = new Blob(audioChunks, { type:
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
|
| 106 |
fileNameDisplay.textContent = "Запись готова к анализу";
|
| 107 |
recordStatus.textContent = "";
|
|
@@ -110,7 +121,7 @@
|
|
| 110 |
resultDiv.style.display = 'none';
|
| 111 |
statusDiv.textContent = 'Нажмите "Анализировать аудио".';
|
| 112 |
|
| 113 |
-
// Отключаем микрофон
|
| 114 |
stream.getTracks().forEach(track => track.stop());
|
| 115 |
};
|
| 116 |
|
|
@@ -118,16 +129,24 @@
|
|
| 118 |
recordBtn.textContent = "🛑 Остановить запись";
|
| 119 |
recordBtn.style.background = "#dc3545";
|
| 120 |
recordStatus.textContent = "Идет запись... Спойте или сыграйте мелодию!";
|
|
|
|
| 121 |
fileNameDisplay.textContent = "";
|
| 122 |
statusDiv.textContent = "";
|
| 123 |
|
| 124 |
} catch (err) {
|
| 125 |
console.error("Ошибка доступа к микрофону:", err);
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
}
|
| 128 |
});
|
| 129 |
|
| 130 |
-
// Функция конвертации аудио
|
| 131 |
async function decodeAndResample(file) {
|
| 132 |
const arrayBuffer = await file.arrayBuffer();
|
| 133 |
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
@@ -144,7 +163,6 @@
|
|
| 144 |
return Float32Array.from(rendered.getChannelData(0));
|
| 145 |
}
|
| 146 |
|
| 147 |
-
// Запуск анализа
|
| 148 |
analyzeBtn.addEventListener('click', async () => {
|
| 149 |
if (!currentFile) {
|
| 150 |
statusDiv.textContent = 'Сначала выберите файл или запишите звук.';
|
|
@@ -153,7 +171,7 @@
|
|
| 153 |
|
| 154 |
analyzeBtn.disabled = true;
|
| 155 |
resultDiv.style.display = 'none';
|
| 156 |
-
statusDiv.textContent = 'Шаг 1/3: Загрузка модели
|
| 157 |
|
| 158 |
try {
|
| 159 |
const modelURL = 'https://huggingface.co/spotify/basic-pitch/resolve/main/model.json';
|
|
@@ -205,7 +223,7 @@
|
|
| 205 |
|
| 206 |
} catch (error) {
|
| 207 |
console.error(error);
|
| 208 |
-
statusDiv.textContent = '
|
| 209 |
} finally {
|
| 210 |
analyzeBtn.disabled = false;
|
| 211 |
}
|
|
|
|
| 11 |
p { font-size: 14px; color: #666; text-align: center; }
|
| 12 |
.upload-area { border: 2px dashed #ccc; border-radius: 8px; padding: 20px; text-align: center; margin: 20px 0; background: #fafafa; }
|
| 13 |
|
|
|
|
| 14 |
input[type="file"] { display: none; }
|
| 15 |
.custom-file-upload { display: inline-block; padding: 10px 20px; background: #007bff; color: white; border-radius: 6px; cursor: pointer; font-weight: bold; margin-bottom: 10px; }
|
| 16 |
.custom-file-upload:hover { background: #0056b3; }
|
| 17 |
|
|
|
|
| 18 |
#recordBtn { display: inline-block; padding: 10px 20px; background: #6c757d; color: white; border: none; border-radius: 6px; cursor: pointer; font-weight: bold; margin-left: 10px; }
|
| 19 |
#recordBtn:hover { background: #5a6268; }
|
| 20 |
|
| 21 |
#fileName { margin-top: 10px; font-size: 14px; color: #333; font-weight: bold; }
|
| 22 |
+
#recordStatus { display: block; font-size: 13px; color: #dc3545; margin-top: 5px; min-height: 18px; }
|
| 23 |
|
| 24 |
#analyzeBtn { display: block; width: 100%; padding: 12px; background: #28a745; color: white; border: none; border-radius: 6px; font-size: 16px; font-weight: bold; cursor: pointer; margin-top: 15px; }
|
| 25 |
#analyzeBtn:disabled { background: #6c757d; cursor: not-allowed; }
|
|
|
|
| 68 |
let mediaRecorder;
|
| 69 |
let audioChunks = [];
|
| 70 |
|
|
|
|
| 71 |
fileInput.addEventListener('change', (e) => {
|
| 72 |
if (e.target.files.length > 0) {
|
| 73 |
currentFile = e.target.files[0];
|
|
|
|
| 78 |
}
|
| 79 |
});
|
| 80 |
|
|
|
|
| 81 |
recordBtn.addEventListener('click', async () => {
|
| 82 |
+
// Если уже идет запись, останавливаем её
|
| 83 |
if (mediaRecorder && mediaRecorder.state === 'recording') {
|
| 84 |
mediaRecorder.stop();
|
| 85 |
return;
|
| 86 |
}
|
| 87 |
|
| 88 |
+
// Проверяем, поддерживает ли браузер микрофон
|
| 89 |
+
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
|
| 90 |
+
recordStatus.textContent = "Ваш браузер не поддерживает запись или вы открыли сайт без HTTPS.";
|
| 91 |
+
return;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
try {
|
| 95 |
+
recordStatus.textContent = "Запрос доступа к микрофону...";
|
| 96 |
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
|
| 97 |
+
|
| 98 |
+
// Определяем, какой формат записи поддерживает браузер (Safari любит mp4, Chrome любит webm)
|
| 99 |
+
let options = {};
|
| 100 |
+
if (MediaRecorder.isTypeSupported('audio/webm')) options = { mimeType: 'audio/webm' };
|
| 101 |
+
else if (MediaRecorder.isTypeSupported('audio/mp4')) options = { mimeType: 'audio/mp4' };
|
| 102 |
+
|
| 103 |
+
mediaRecorder = new MediaRecorder(stream, options);
|
| 104 |
audioChunks = [];
|
| 105 |
|
| 106 |
mediaRecorder.ondataavailable = event => {
|
| 107 |
+
if (event.data.size > 0) audioChunks.push(event.data);
|
| 108 |
};
|
| 109 |
|
| 110 |
mediaRecorder.onstop = () => {
|
| 111 |
+
const mimeType = mediaRecorder.mimeType || 'audio/webm';
|
| 112 |
+
const audioBlob = new Blob(audioChunks, { type: mimeType });
|
| 113 |
+
const extension = mimeType.includes('mp4') ? 'mp4' : 'webm';
|
| 114 |
+
|
| 115 |
+
currentFile = new File([audioBlob], `Запись_с_микрофона.${extension}`, { type: audioBlob.type });
|
| 116 |
|
| 117 |
fileNameDisplay.textContent = "Запись готова к анализу";
|
| 118 |
recordStatus.textContent = "";
|
|
|
|
| 121 |
resultDiv.style.display = 'none';
|
| 122 |
statusDiv.textContent = 'Нажмите "Анализировать аудио".';
|
| 123 |
|
| 124 |
+
// Отключаем микрофон
|
| 125 |
stream.getTracks().forEach(track => track.stop());
|
| 126 |
};
|
| 127 |
|
|
|
|
| 129 |
recordBtn.textContent = "🛑 Остановить запись";
|
| 130 |
recordBtn.style.background = "#dc3545";
|
| 131 |
recordStatus.textContent = "Идет запись... Спойте или сыграйте мелодию!";
|
| 132 |
+
recordStatus.style.color = "#28a745";
|
| 133 |
fileNameDisplay.textContent = "";
|
| 134 |
statusDiv.textContent = "";
|
| 135 |
|
| 136 |
} catch (err) {
|
| 137 |
console.error("Ошибка доступа к микрофону:", err);
|
| 138 |
+
// Выводим понятную ошибку на экран
|
| 139 |
+
if (err.name === 'NotAllowedError' || err.name === 'PermissionDeniedError') {
|
| 140 |
+
recordStatus.textContent = "Микрофон заблокирован. Разрешите доступ в настройках браузера (нажмите на замочек в адресной строке).";
|
| 141 |
+
} else if (err.name === 'NotFoundError') {
|
| 142 |
+
recordStatus.textContent = "Микрофон не найден. Проверьте, подключен ли он.";
|
| 143 |
+
} else {
|
| 144 |
+
recordStatus.textContent = "Ошибка: " + err.message;
|
| 145 |
+
}
|
| 146 |
+
recordStatus.style.color = "#dc3545";
|
| 147 |
}
|
| 148 |
});
|
| 149 |
|
|
|
|
| 150 |
async function decodeAndResample(file) {
|
| 151 |
const arrayBuffer = await file.arrayBuffer();
|
| 152 |
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
|
|
| 163 |
return Float32Array.from(rendered.getChannelData(0));
|
| 164 |
}
|
| 165 |
|
|
|
|
| 166 |
analyzeBtn.addEventListener('click', async () => {
|
| 167 |
if (!currentFile) {
|
| 168 |
statusDiv.textContent = 'Сначала выберите файл или запишите звук.';
|
|
|
|
| 171 |
|
| 172 |
analyzeBtn.disabled = true;
|
| 173 |
resultDiv.style.display = 'none';
|
| 174 |
+
statusDiv.textContent = 'Шаг 1/3: Загрузка модели...';
|
| 175 |
|
| 176 |
try {
|
| 177 |
const modelURL = 'https://huggingface.co/spotify/basic-pitch/resolve/main/model.json';
|
|
|
|
| 223 |
|
| 224 |
} catch (error) {
|
| 225 |
console.error(error);
|
| 226 |
+
statusDiv.textContent = 'Ошибка при обработке. Возможно, формат аудио не поддерживается.';
|
| 227 |
} finally {
|
| 228 |
analyzeBtn.disabled = false;
|
| 229 |
}
|