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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="ru">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Аудио в MIDI (Basic Pitch)</title>
|
| 7 |
+
<style>
|
| 8 |
+
body { font-family: Arial, sans-serif; max-width: 700px; margin: 20px auto; padding: 20px; background: #f9f9f9; color: #333; }
|
| 9 |
+
.container { background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
|
| 10 |
+
h1 { font-size: 24px; text-align: center; }
|
| 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 |
+
input[type="file"] { display: none; }
|
| 14 |
+
.custom-file-upload { display: inline-block; padding: 10px 20px; background: #007bff; color: white; border-radius: 6px; cursor: pointer; font-weight: bold; }
|
| 15 |
+
.custom-file-upload:hover { background: #0056b3; }
|
| 16 |
+
#fileName { margin-top: 10px; font-size: 14px; color: #333; }
|
| 17 |
+
#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: 10px; }
|
| 18 |
+
#analyzeBtn:disabled { background: #6c757d; cursor: not-allowed; }
|
| 19 |
+
#status { margin-top: 15px; font-size: 14px; color: #ff9800; text-align: center; min-height: 20px; }
|
| 20 |
+
#result { display: none; margin-top: 20px; padding: 15px; background: #e9ecef; border-radius: 8px; text-align: center; }
|
| 21 |
+
#downloadLink { display: inline-block; padding: 10px 20px; background: #17a2b8; color: white; text-decoration: none; border-radius: 6px; font-weight: bold; }
|
| 22 |
+
</style>
|
| 23 |
+
</head>
|
| 24 |
+
<body>
|
| 25 |
+
<div class="container">
|
| 26 |
+
<h1>🎵 Аудио в MIDI</h1>
|
| 27 |
+
<p>Загрузи короткий музыкальный фрагмент (мычание, гитара, пианино), и модель создаст MIDI-файл.</p>
|
| 28 |
+
|
| 29 |
+
<div class="upload-area">
|
| 30 |
+
<label for="audioFile" class="custom-file-upload">Выбрать аудиофайл</label>
|
| 31 |
+
<input type="file" id="audioFile" accept="audio/*">
|
| 32 |
+
<div id="fileName">Файл не выбран</div>
|
| 33 |
+
</div>
|
| 34 |
+
|
| 35 |
+
<button id="analyzeBtn">Анализировать аудио</button>
|
| 36 |
+
<div id="status"></div>
|
| 37 |
+
|
| 38 |
+
<div id="result">
|
| 39 |
+
<h3>Готово! 🎉</h3>
|
| 40 |
+
<a id="downloadLink" href="#" download="melody.mid">Скачать MIDI</a>
|
| 41 |
+
</div>
|
| 42 |
+
</div>
|
| 43 |
+
|
| 44 |
+
<script type="module">
|
| 45 |
+
import { BasicPitch, noteFramesToTime, outputToNotesPoly } from 'https://cdn.jsdelivr.net/npm/@spotify/basic-pitch@1.0.1/+esm';
|
| 46 |
+
import { Midi } from 'https://cdn.jsdelivr.net/npm/@tonejs/midi@2.0.28/+esm';
|
| 47 |
+
|
| 48 |
+
const fileInput = document.getElementById('audioFile');
|
| 49 |
+
const fileNameDisplay = document.getElementById('fileName');
|
| 50 |
+
const analyzeBtn = document.getElementById('analyzeBtn');
|
| 51 |
+
const statusDiv = document.getElementById('status');
|
| 52 |
+
const resultDiv = document.getElementById('result');
|
| 53 |
+
const downloadLink = document.getElementById('downloadLink');
|
| 54 |
+
|
| 55 |
+
let currentFile = null;
|
| 56 |
+
|
| 57 |
+
fileInput.addEventListener('change', (e) => {
|
| 58 |
+
if (e.target.files.length > 0) {
|
| 59 |
+
currentFile = e.target.files[0];
|
| 60 |
+
fileNameDisplay.textContent = currentFile.name;
|
| 61 |
+
resultDiv.style.display = 'none';
|
| 62 |
+
statusDiv.textContent = '';
|
| 63 |
+
}
|
| 64 |
+
});
|
| 65 |
+
|
| 66 |
+
async function decodeAndResample(file) {
|
| 67 |
+
const arrayBuffer = await file.arrayBuffer();
|
| 68 |
+
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
| 69 |
+
const decoded = await audioCtx.decodeAudioData(arrayBuffer);
|
| 70 |
+
await audioCtx.close();
|
| 71 |
+
|
| 72 |
+
const sampleRate = 22050;
|
| 73 |
+
const offlineCtx = new OfflineAudioContext(1, Math.max(1, Math.ceil(decoded.duration * sampleRate)), sampleRate);
|
| 74 |
+
const source = offlineCtx.createBufferSource();
|
| 75 |
+
source.buffer = decoded;
|
| 76 |
+
source.connect(offlineCtx.destination);
|
| 77 |
+
source.start();
|
| 78 |
+
const rendered = await offlineCtx.startRendering();
|
| 79 |
+
return Float32Array.from(rendered.getChannelData(0));
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
analyzeBtn.addEventListener('click', async () => {
|
| 83 |
+
if (!currentFile) {
|
| 84 |
+
statusDiv.textContent = 'Пожалуйста, сначала выберите аудиофайл.';
|
| 85 |
+
return;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
analyzeBtn.disabled = true;
|
| 89 |
+
resultDiv.style.display = 'none';
|
| 90 |
+
statusDiv.textContent = 'Шаг 1/3: Загрузка модели (может занять несколько секунд)...';
|
| 91 |
+
|
| 92 |
+
try {
|
| 93 |
+
// Загружаем модель
|
| 94 |
+
const modelURL = 'https://huggingface.co/spaces/Miberd/basic-pitch-model/resolve/main/model.json';
|
| 95 |
+
const basicPitch = new BasicPitch(modelURL);
|
| 96 |
+
|
| 97 |
+
statusDiv.textContent = 'Шаг 2/3: Обработка аудио...';
|
| 98 |
+
const pcm = await decodeAndResample(currentFile);
|
| 99 |
+
|
| 100 |
+
statusDiv.textContent = 'Шаг 3/3: Распознавание нот...';
|
| 101 |
+
const frames = [];
|
| 102 |
+
const onsets = [];
|
| 103 |
+
|
| 104 |
+
await basicPitch.evaluateModel(
|
| 105 |
+
pcm,
|
| 106 |
+
(f, o) => { frames.push(...f); onsets.push(...o); },
|
| 107 |
+
(progress) => { statusDiv.textContent = `Распознавание... ${Math.round(progress * 100)}%`; }
|
| 108 |
+
);
|
| 109 |
+
|
| 110 |
+
const minFrames = Math.max(1, Math.round(0.130 * (22050 / 256)));
|
| 111 |
+
const noteEvents = noteFramesToTime(
|
| 112 |
+
outputToNotesPoly(frames, onsets, 0.5, 0.3, minFrames)
|
| 113 |
+
);
|
| 114 |
+
|
| 115 |
+
if (noteEvents.length === 0) {
|
| 116 |
+
statusDiv.textContent = 'Не удалось распознать ноты. Попробуйте другой фрагмент.';
|
| 117 |
+
analyzeBtn.disabled = false;
|
| 118 |
+
return;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
const midi = new Midi();
|
| 122 |
+
const track = midi.addTrack();
|
| 123 |
+
noteEvents.forEach(note => {
|
| 124 |
+
track.addNote({
|
| 125 |
+
midi: note.pitchMidi,
|
| 126 |
+
time: note.startTimeSeconds,
|
| 127 |
+
duration: note.durationSeconds,
|
| 128 |
+
velocity: 0.8
|
| 129 |
+
});
|
| 130 |
+
});
|
| 131 |
+
|
| 132 |
+
const midiBytes = midi.toArray();
|
| 133 |
+
const blob = new Blob([midiBytes], { type: 'audio/midi' });
|
| 134 |
+
const url = URL.createObjectURL(blob);
|
| 135 |
+
|
| 136 |
+
downloadLink.href = url;
|
| 137 |
+
downloadLink.download = `${currentFile.name.replace(/\.[^/.]+$/, "")}.mid`;
|
| 138 |
+
resultDiv.style.display = 'block';
|
| 139 |
+
statusDiv.textContent = 'Готово!';
|
| 140 |
+
|
| 141 |
+
} catch (error) {
|
| 142 |
+
console.error(error);
|
| 143 |
+
statusDiv.textContent = 'Произошла ошибка при обработке. Попробуйте другой файл.';
|
| 144 |
+
} finally {
|
| 145 |
+
analyzeBtn.disabled = false;
|
| 146 |
+
}
|
| 147 |
+
});
|
| 148 |
+
</script>
|
| 149 |
+
</body>
|
| 150 |
+
</html>
|