Miberd commited on
Commit
79fb0a9
·
verified ·
1 Parent(s): 8c3f5b9

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +51 -6
index.html CHANGED
@@ -4,6 +4,8 @@
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); }
@@ -33,13 +35,16 @@
33
  #downloadLink { background: #17a2b8; }
34
  #downloadTxtBtn { background: #ff9800; }
35
 
36
- #textResult { text-align: left; background: #fff; padding: 15px; border-radius: 6px; max-height: 250px; overflow-y: auto; font-size: 13px; font-family: monospace; border: 1px solid #ccc; margin: 0; white-space: pre-wrap; }
 
 
 
37
  </style>
38
  </head>
39
  <body>
40
  <div class="container">
41
- <h1>🎵 Аудио в MIDI</h1>
42
- <p>Загрузи файл или запиши мелодию с микрофона. Модель создаст MIDI и текстовый список нот.</p>
43
 
44
  <div class="upload-area">
45
  <label for="audioFile" class="custom-file-upload">Выбрать файл</label>
@@ -59,7 +64,11 @@
59
  <a id="downloadLink" class="download-btn" href="#" download="melody.mid">Скачать MIDI</a>
60
  <button type="button" id="downloadTxtBtn" class="download-btn">Скачать текст (.txt)</button>
61
  </div>
62
- <h4 style="margin-top: 0; text-align: left; font-size: 15px;">Распознанные ноты:</h4>
 
 
 
 
63
  <pre id="textResult"></pre>
64
  </div>
65
  </div>
@@ -79,12 +88,28 @@
79
 
80
  const recordBtn = document.getElementById('recordBtn');
81
  const recordStatus = document.getElementById('recordStatus');
 
82
 
83
  let currentFile = null;
84
  let mediaRecorder;
85
  let audioChunks = [];
86
  let currentTextOutput = "";
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  function midiToNoteName(midi) {
89
  const notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
90
  const octave = Math.floor(midi / 12) - 1;
@@ -219,7 +244,6 @@
219
  const frames = [];
220
  const onsets = [];
221
 
222
- // Заменили push(...f) на безопасный цикл, чтобы не было переполнения стека
223
  await basicPitch.evaluateModel(
224
  pcm,
225
  (f, o) => {
@@ -234,7 +258,6 @@
234
  outputToNotesPoly(frames, onsets, 0.5, 0.3, minFrames)
235
  );
236
 
237
- // УМНЫЙ ФИЛЬТР
238
  const filteredNotes = [];
239
  const lastPitchTime = {};
240
 
@@ -271,17 +294,39 @@
271
  downloadLink.href = url;
272
  downloadLink.download = `notes_${new Date().getTime()}.mid`;
273
 
 
274
  currentTextOutput = "СПИСОК РАСПОЗНАННЫХ НОТ\n";
275
  currentTextOutput += "----------------------------------------\n";
276
 
 
 
 
277
  filteredNotes.forEach((note, index) => {
278
  const noteName = midiToNoteName(note.pitchMidi);
279
  const startTime = note.startTimeSeconds.toFixed(2);
280
  const duration = note.durationSeconds.toFixed(2);
281
  currentTextOutput += `${index + 1}. Нота: ${noteName.padEnd(3)} | Время: ${startTime} сек | Длительность: ${duration} сек\n`;
 
 
 
 
 
 
 
 
282
  });
283
 
284
  textResultDiv.textContent = currentTextOutput;
 
 
 
 
 
 
 
 
 
 
285
  resultDiv.style.display = 'block';
286
  statusDiv.textContent = 'Готово!';
287
  statusDiv.className = '';
 
4
  <meta charset="UTF-8">
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>Аудио в MIDI (Basic Pitch)</title>
7
+ <!-- Подключаем библиотеку для рисования нот -->
8
+ <script src="https://cdn.jsdelivr.net/npm/abcjs@6.2.2/dist/abcjs-basic-min.js"></script>
9
  <style>
10
  body { font-family: Arial, sans-serif; max-width: 700px; margin: 20px auto; padding: 20px; background: #f9f9f9; color: #333; }
11
  .container { background: white; padding: 25px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.1); }
 
35
  #downloadLink { background: #17a2b8; }
36
  #downloadTxtBtn { background: #ff9800; }
37
 
38
+ #textResult { text-align: left; background: #fff; padding: 15px; border-radius: 6px; max-height: 150px; overflow-y: auto; font-size: 13px; font-family: monospace; border: 1px solid #ccc; margin: 0; white-space: pre-wrap; }
39
+
40
+ /* Контейнер для нотного стана */
41
+ #sheetMusic { background: #fff; border-radius: 6px; padding: 10px; margin-top: 15px; overflow-x: auto; border: 1px solid #ccc; }
42
  </style>
43
  </head>
44
  <body>
45
  <div class="container">
46
+ <h1>🎵 Аудио в MIDI и Ноты</h1>
47
+ <p>Загрузи файл или запиши мелодию. Модель создаст MIDI, текст и нотный стан.</p>
48
 
49
  <div class="upload-area">
50
  <label for="audioFile" class="custom-file-upload">Выбрать файл</label>
 
64
  <a id="downloadLink" class="download-btn" href="#" download="melody.mid">Скачать MIDI</a>
65
  <button type="button" id="downloadTxtBtn" class="download-btn">Скачать текст (.txt)</button>
66
  </div>
67
+
68
+ <h4 style="margin-top: 15px; text-align: left; font-size: 15px;">Нотный стан:</h4>
69
+ <div id="sheetMusic"></div>
70
+
71
+ <h4 style="margin-top: 15px; text-align: left; font-size: 15px;">Распознанные ноты (текст):</h4>
72
  <pre id="textResult"></pre>
73
  </div>
74
  </div>
 
88
 
89
  const recordBtn = document.getElementById('recordBtn');
90
  const recordStatus = document.getElementById('recordStatus');
91
+ const sheetMusicDiv = document.getElementById('sheetMusic');
92
 
93
  let currentFile = null;
94
  let mediaRecorder;
95
  let audioChunks = [];
96
  let currentTextOutput = "";
97
 
98
+ // Функция для перевода MIDI-ноты в формат ABC (для нотного стана)
99
+ function midiToAbcNote(midi) {
100
+ const notes = ['C', '^C', 'D', '^D', 'E', 'F', '^F', 'G', '^G', 'A', '^A', 'B'];
101
+ const octave = Math.floor(midi / 12) - 1;
102
+ let note = notes[midi % 12];
103
+
104
+ // В ABC-нотации октавы обозначаются запятыми (вниз) и апострофами (вверх)
105
+ if (octave === 4) return note; // Первая октава (C4)
106
+ if (octave === 5) return note.toLowerCase(); // Вторая октава (c5)
107
+ if (octave === 3) return note + ','; // Малая октава
108
+ if (octave === 6) return note.toLowerCase() + "'";
109
+ if (octave === 2) return note + ',,';
110
+ return note; // По умолчанию
111
+ }
112
+
113
  function midiToNoteName(midi) {
114
  const notes = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
115
  const octave = Math.floor(midi / 12) - 1;
 
244
  const frames = [];
245
  const onsets = [];
246
 
 
247
  await basicPitch.evaluateModel(
248
  pcm,
249
  (f, o) => {
 
258
  outputToNotesPoly(frames, onsets, 0.5, 0.3, minFrames)
259
  );
260
 
 
261
  const filteredNotes = [];
262
  const lastPitchTime = {};
263
 
 
294
  downloadLink.href = url;
295
  downloadLink.download = `notes_${new Date().getTime()}.mid`;
296
 
297
+ // Формируем текстовый отчет
298
  currentTextOutput = "СПИСОК РАСПОЗНАННЫХ НОТ\n";
299
  currentTextOutput += "----------------------------------------\n";
300
 
301
+ // Формируем ABC-нотацию для нотного стана
302
+ let abcString = "X:1\nT:Распознанная мелодия\nM:4/4\nL:1/4\nK:C\n";
303
+
304
  filteredNotes.forEach((note, index) => {
305
  const noteName = midiToNoteName(note.pitchMidi);
306
  const startTime = note.startTimeSeconds.toFixed(2);
307
  const duration = note.durationSeconds.toFixed(2);
308
  currentTextOutput += `${index + 1}. Нота: ${noteName.padEnd(3)} | Время: ${startTime} сек | Длительность: ${duration} сек\n`;
309
+
310
+ // Переводим в ABC-ноты (упрощенно: долгая нота = 2, короткая = /2)
311
+ let abcNote = midiToAbcNote(note.pitchMidi);
312
+ let abcDur = "";
313
+ if (note.durationSeconds > 1.0) abcDur = "2";
314
+ else if (note.durationSeconds < 0.5) abcDur = "/2";
315
+
316
+ abcString += abcNote + abcDur + " ";
317
  });
318
 
319
  textResultDiv.textContent = currentTextOutput;
320
+
321
+ // Рисуем нотный стан
322
+ sheetMusicDiv.innerHTML = ""; // Очищаем старое
323
+ if (window.ABCJS) {
324
+ ABCJS.renderAbc("sheetMusic", abcString, {
325
+ scale: 1.2, // Размер нот
326
+ add_classes: true
327
+ });
328
+ }
329
+
330
  resultDiv.style.display = 'block';
331
  statusDiv.textContent = 'Готово!';
332
  statusDiv.className = '';