Spaces:
Running
Running
File size: 24,544 Bytes
62e0bdf 38df87f 62e0bdf 4011f88 5959290 38df87f 62e0bdf 00b72b5 5c0aefa 0b313e4 f5d0a5c 5c0aefa 0b313e4 33ef2c6 00b72b5 62e0bdf 00b72b5 62e0bdf 5959290 62e0bdf 5c0aefa 38df87f 00b72b5 38df87f f5d0a5c 5c0aefa f5d0a5c 00b72b5 38df87f 5c0aefa 00b72b5 f5d0a5c 00b72b5 5c0aefa 00b72b5 38df87f 5c0aefa 38df87f 5c0aefa 38df87f 00b72b5 f5d0a5c 38df87f 5c0aefa 38df87f f5d0a5c 38df87f 00b72b5 38df87f f5d0a5c 00b72b5 f5d0a5c 5c0aefa 62e0bdf 5959290 38df87f 00b72b5 38df87f f5d0a5c 62e0bdf 38df87f 5c0aefa 62e0bdf 00b72b5 5c0aefa 38df87f f5d0a5c 00b72b5 62e0bdf 38df87f 62e0bdf 5959290 5c0aefa 38df87f 62e0bdf 714bb7a 5959290 00b72b5 f5d0a5c 5c0aefa f5d0a5c 62e0bdf 714bb7a 62e0bdf f5d0a5c 62e0bdf 714bb7a 00b72b5 f5d0a5c 38df87f 62e0bdf ff4ebe8 f5d0a5c 62e0bdf 33ef2c6 ff4ebe8 4011f88 0b313e4 f5d0a5c 62e0bdf 4011f88 f5d0a5c 62e0bdf 5959290 0b313e4 33ef2c6 5959290 38df87f 5959290 5c0aefa 00b72b5 f5d0a5c 5959290 33ef2c6 62e0bdf f5d0a5c 00b72b5 f5d0a5c 714bb7a f5d0a5c 5959290 38df87f f5d0a5c 714bb7a 5c0aefa 38df87f 714bb7a 38df87f 5959290 33ef2c6 00b72b5 5959290 33ef2c6 f5d0a5c 5959290 38df87f f5d0a5c 00b72b5 33ef2c6 0b313e4 00b72b5 62e0bdf 38df87f 00b72b5 38df87f 5c0aefa 5959290 5c0aefa 62e0bdf 5959290 38df87f 5c0aefa 714bb7a 5959290 00b72b5 5c0aefa f5d0a5c 5959290 714bb7a 00b72b5 33ef2c6 00b72b5 714bb7a 33ef2c6 00b72b5 714bb7a f5d0a5c 714bb7a 00b72b5 33ef2c6 00b72b5 5c0aefa 714bb7a 00b72b5 33ef2c6 5c0aefa 714bb7a 00b72b5 714bb7a 00b72b5 714bb7a de57dc5 33ef2c6 714bb7a 00b72b5 714bb7a 00b72b5 714bb7a 00b72b5 f5d0a5c 00b72b5 33ef2c6 714bb7a 00b72b5 714bb7a 00b72b5 f5d0a5c 00b72b5 38df87f 00b72b5 714bb7a 5c0aefa 5959290 33ef2c6 714bb7a 38df87f 5c0aefa 33ef2c6 5959290 5c0aefa 714bb7a 33ef2c6 38df87f 00b72b5 38df87f 33ef2c6 5959290 714bb7a 38df87f 33ef2c6 714bb7a 00b72b5 33ef2c6 00b72b5 714bb7a 00b72b5 38df87f 5959290 62e0bdf 38df87f 00b72b5 38df87f 00b72b5 38df87f 00b72b5 38df87f 33ef2c6 00b72b5 33ef2c6 38df87f 33ef2c6 00b72b5 38df87f 00b72b5 38df87f 00b72b5 5c0aefa 33ef2c6 38df87f 00b72b5 f5d0a5c 714bb7a 38df87f 33ef2c6 38df87f 33ef2c6 00b72b5 5c0aefa 714bb7a 00b72b5 33ef2c6 00b72b5 5c0aefa 00b72b5 33ef2c6 00b72b5 5c0aefa 714bb7a 0b313e4 33ef2c6 4011f88 714bb7a 00b72b5 714bb7a 00b72b5 4011f88 00b72b5 714bb7a 33ef2c6 00b72b5 714bb7a 4011f88 00b72b5 5959290 714bb7a 5c0aefa 00b72b5 33ef2c6 5959290 714bb7a 38df87f 33ef2c6 00b72b5 38df87f 00b72b5 f5d0a5c 33ef2c6 f5d0a5c 62e0bdf f5d0a5c 714bb7a f5d0a5c 62e0bdf |
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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title>Improved Piano Visualizer</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tone/14.8.49/Tone.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/MidiConvert.min.js"></script>
<style>
:root {
--bg-color: #080808;
--piano-bg-start: #1c1c1c;
--piano-bg-end: #0d0d0d;
--piano-border: #2f2f2f;
--white-key-bg-start: #fdfdfd;
--white-key-bg-end: #e4e4e4;
--white-key-border: #9e9e9e;
--black-key-bg-start: #353535;
--black-key-bg-end: #181818;
--black-key-border: #050505;
--hit-line-color: rgba(255, 80, 180, 0.85);
--key-active-glow: rgba(255, 80, 180, 0.9);
--particle-base-color-h: 320;
--default-note-color-h: 180;
}
body {
margin: 0;
height: 100vh;
background-color: var(--bg-color);
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
overflow: hidden;
font-family: 'Arial', sans-serif;
}
#controls {
position: absolute;
top: 15px;
left: 50%;
transform: translateX(-50%);
z-index: 100;
background-color: rgba(30, 30, 30, 0.88);
padding: 10px 15px;
border-radius: 7px;
box-shadow: 0 2px 7px rgba(0,0,0,0.4);
display: flex;
align-items: center;
}
#controls input[type="text"] {
padding: 9px 12px;
margin-right: 8px;
border: 1px solid #484848;
background-color: #252525;
color: #f0f0f0;
border-radius: 4px;
width: 320px;
font-size: 14px;
}
#controls button {
padding: 9px 16px;
font-size: 14px;
cursor: pointer;
background-color: #5cb85c;
color: white;
border: none;
border-radius: 4px;
transition: background-color 0.2s, box-shadow 0.2s;
box-shadow: 0 2px 3px rgba(0,0,0,0.25);
}
#controls button:hover {
background-color: #4cae4c;
box-shadow: 0 3px 5px rgba(0,0,0,0.3);
}
#controls button:disabled {
background-color: #484848;
cursor: not-allowed;
box-shadow: none;
}
#loading-indicator {
color: #c8c8c8;
font-size: 13px;
margin-left: 12px;
display: none;
}
.piano-container {
width: 98vw;
max-width: 1500px;
height: 28vh;
min-height: 190px;
display: flex;
justify-content: center;
align-items: flex-end;
position: relative;
padding-bottom: 5px;
}
.piano {
display: flex;
position: relative;
background: linear-gradient(to bottom, var(--piano-bg-start), var(--piano-bg-end));
padding: 10px 10px 0 10px;
border-radius: 10px 10px 0 0;
box-shadow: 0 0 35px rgba(170, 170, 255, 0.22),
0 0 55px rgba(255, 170, 220, 0.12);
border: 2px solid var(--piano-border);
border-bottom: none;
height: 100%;
}
.key {
box-sizing: border-box;
cursor: pointer;
user-select: none;
-webkit-tap-highlight-color: transparent;
transition: all 0.035s ease-out;
position: relative;
}
.white-key {
width: 30px; /* WHITE_KEY_WIDTH_PX */
height: 100%;
background: linear-gradient(to bottom, var(--white-key-bg-start), var(--white-key-bg-end));
border-left: 1px solid var(--white-key-border);
border-right: 1px solid var(--white-key-border);
border-bottom: 5px solid #909090;
border-radius: 0 0 4px 4px;
box-shadow: 0 2px 3px rgba(0,0,0,0.22), inset 0 -2px 2px rgba(255,255,255,0.68);
z-index: 1;
margin-right: -1px; /* WHITE_KEY_OVERLAP_PX (negated) */
}
.white-key:first-child { border-left: 1px solid #686868; }
.white-key:last-child { border-right: 1px solid #686868; margin-right: 0;}
.black-key {
width: 18px; /* BLACK_KEY_WIDTH_PX */
height: 58%;
background: linear-gradient(to bottom, var(--black-key-bg-start), var(--black-key-bg-end));
border: 1px solid var(--black-key-border);
border-bottom: 4px solid #181818;
border-radius: 0 0 3px 3px;
position: absolute;
z-index: 3; /* White keys: 1, Active keys: 3, Particles: 4 */
box-shadow: -1px 0 2px rgba(0,0,0,0.4), 1px 0 2px rgba(0,0,0,0.4), 0 2px 3px rgba(0,0,0,0.55), inset 0 -1px 1px rgba(60,60,60,0.3);
}
.key.active { /* Applied when key is pressed by user or MIDI */
z-index: 2 !important; /* Active keys always on top of other keys */
}
.white-key.active {
background: linear-gradient(to bottom, #d5d5d5, #c2c2c2);
transform: perspective(500px) rotateX(1.2deg) translateY(1.8px);
border-bottom-width: 3.2px;
box-shadow: 0 0 22px 6px var(--key-active-glow),
inset 0 -1px 1px rgba(255,255,255,0.38);
}
.black-key.active {
background: linear-gradient(to bottom, #222222, #020202);
transform: perspective(500px) rotateX(0.6deg) translateY(1.2px);
border-bottom-width: 2.8px;
box-shadow: 0 0 22px 6px var(--key-active-glow),
inset 0 -1px 1px rgba(60,60,60,0.18);
}
#note-fall-area {
width: var(--piano-actual-width, 100%);
height: calc(100vh - 28vh - 5px - 10px);
position: absolute;
top: 0;
left: 50%;
transform: translateX(-50%);
pointer-events: none;
}
.note-bar {
position: absolute;
box-sizing: border-box;
border-radius: 3px;
opacity: 0.9;
background: var(--note-gradient);
box-shadow: 0 0 10px var(--note-glow);
border: 1px solid rgba(255, 255, 255, 0.18);
will-change: transform;
}
#hit-line {
position: absolute;
bottom: calc(28vh + 5px);
left: 50%;
transform: translateX(-50%);
width: var(--piano-actual-width, 98vw);
max-width: 1500px;
height: 3.5px;
background: linear-gradient(to right, transparent, var(--hit-line-color), transparent);
border-radius: 1.75px;
z-index: 3; /* Can be same as active keys, or below particles if particles are z-index 4 */
box-shadow: 0 0 16px var(--hit-line-color);
}
.particle-container {
position: absolute;
width: var(--piano-actual-width, 100%);
height: calc(28vh + 5px + 10px); /* Cover piano base and slightly above */
bottom: 0;
left: 50%;
transform: translateX(-50%);
pointer-events: none;
z-index: 4; /* Particles above keys and hit-line */
}
.particle {
position: absolute;
width: 8px;
height: 8px;
background-color: var(--particle-color);
border-radius: 50%;
opacity: 1;
animation: particleAnim 0.75s cubic-bezier(0.1, 0.9, 0.6, 1) forwards;
will-change: transform, opacity;
}
@keyframes particleAnim {
0% { transform: translateY(0) translateX(0) scale(1.3); opacity: 0.95; }
100% {
transform: translateY(calc( (var(--random-y) - 0.7) * -110px - 60px))
translateX(calc( (var(--random-x) - 0.5) * 75px))
scale(0.1);
opacity: 0;
}
}
</style>
</head>
<body>
<div id="controls">
<input type="text" id="midiUrlInput" value="https://bitmidi.com/uploads/79829.mid">
<button id="loadMidiButton">Load & Play MIDI</button>
<span id="loading-indicator">Loading...</span>
</div>
<div id="note-fall-area"></div>
<div class="particle-container" id="particle-container"></div>
<div id="hit-line"></div>
<div class="piano-container">
<div class="piano" id="piano"></div>
</div>
<script>
const pianoElement = document.getElementById('piano');
const noteFallArea = document.getElementById('note-fall-area');
const particleContainer = document.getElementById('particle-container');
const midiUrlInput = document.getElementById('midiUrlInput');
const loadMidiButton = document.getElementById('loadMidiButton');
const loadingIndicator = document.getElementById('loading-indicator');
const hitLineElement = document.getElementById('hit-line');
const synth = new Tone.PolySynth(Tone.Synth, {
oscillator: { type: "triangle8" },
envelope: { attack: 0.015, decay: 0.35, sustain: 0.15, release: 0.9 },
volume: -10
}).toDestination();
const NOTES_ORDER = ['C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#', 'A', 'A#', 'B'];
const START_OCTAVE = 1;
const END_OCTAVE = 7;
const WHITE_KEY_WIDTH_PX = 30;
const BLACK_KEY_WIDTH_PX = 18;
const WHITE_KEY_OVERLAP_PX = 1; // Corresponds to white-key margin-right: -1px
const NOTE_FALL_DURATION_MS = 3800;
const pianoKeys = {};
let whiteKeyCount = 0;
let pianoActualWidth = 0; // Will be calculated based on white keys and their overlap
const keyboardMapping = {
'a': 'C4', 'w': 'C#4', 's': 'D4', 'e': 'D#4', 'd': 'E4', 'f': 'F4',
't': 'F#4', 'g': 'G4', 'y': 'G#4', 'h': 'A4', 'u': 'A#4', 'j': 'B4',
'k': 'C5', 'o': 'C#5', 'l': 'D5', 'p': 'D#5', ';': 'E5', "'": 'F5'
};
const activeKeyboardKeys = new Set();
function createPianoKeys() {
pianoElement.innerHTML = '';
whiteKeyCount = 0;
pianoActualWidth = 0;
let currentWhiteKeyVisualOffset = 0; // Tracks the visual offset for black key positioning
for (let octave = START_OCTAVE; octave <= END_OCTAVE; octave++) {
NOTES_ORDER.forEach((noteBase) => {
if (octave === END_OCTAVE && noteBase !== 'C') return;
if (octave === START_OCTAVE && !['A','A#','B'].includes(noteBase)) return;
const keyElement = document.createElement('div');
keyElement.classList.add('key');
const noteName = noteBase + octave;
keyElement.dataset.note = noteName;
pianoKeys[noteName] = keyElement;
if (noteBase.includes('#')) { // Black key
keyElement.classList.add('black-key');
// Position black key centered over the "crack" of the preceding white key
// currentWhiteKeyVisualOffset is the right edge of the *previous* white key
keyElement.style.left = (currentWhiteKeyVisualOffset - (BLACK_KEY_WIDTH_PX / 2) - (WHITE_KEY_OVERLAP_PX / 2) + 9) + 'px';
keyElement.style.top = '0px';
} else { // White key
keyElement.classList.add('white-key');
// If not the first white key, account for overlap with previous key
if (whiteKeyCount > 0) {
currentWhiteKeyVisualOffset -= WHITE_KEY_OVERLAP_PX;
}
currentWhiteKeyVisualOffset += WHITE_KEY_WIDTH_PX;
whiteKeyCount++;
}
pianoElement.appendChild(keyElement);
const playNote = (e) => {
if(e && e.preventDefault) e.preventDefault();
if(keyElement.classList.contains('active')) return;
keyElement.classList.add('active');
const hue = Math.random() * 360;
const activeColor = `hsla(${hue}, 100%, 65%, 0.9)`;
keyElement.style.setProperty('--key-active-glow', activeColor);
synth.triggerAttack(noteName, Tone.now());
createKeyParticles(keyElement, activeColor);
};
const releaseNote = (e) => {
if(e && e.preventDefault) e.preventDefault();
keyElement.classList.remove('active');
synth.triggerRelease(noteName, Tone.now() + 0.05);
};
keyElement.addEventListener('mousedown', playNote);
keyElement.addEventListener('mouseup', releaseNote);
keyElement.addEventListener('mouseleave', () => { if (keyElement.classList.contains('active')) releaseNote(); });
keyElement.addEventListener('touchstart', playNote, { passive: false });
keyElement.addEventListener('touchend', releaseNote);
});
}
// Calculate the actual width of the piano considering overlaps
if (whiteKeyCount > 0) {
pianoActualWidth = (whiteKeyCount * WHITE_KEY_WIDTH_PX) - ( (whiteKeyCount - 1) * WHITE_KEY_OVERLAP_PX );
} else {
pianoActualWidth = 0;
}
pianoElement.style.width = pianoActualWidth + 'px';
document.documentElement.style.setProperty('--piano-actual-width', pianoActualWidth + 'px');
}
createPianoKeys();
function createKeyParticles(keyElement, color) {
const keyRect = keyElement.getBoundingClientRect();
const particleContRect = particleContainer.getBoundingClientRect();
for (let i = 0; i < 15; i++) {
const particle = document.createElement('div');
particle.classList.add('particle');
particle.style.setProperty('--particle-color', color);
particle.style.setProperty('--random-x', Math.random());
particle.style.setProperty('--random-y', Math.random());
const xPos = (keyRect.left - particleContRect.left) + (keyRect.width / 2) - (8/2); // Center particle
const yPos = (keyRect.top - particleContRect.top) - 15; // Particles start slightly above the key
particle.style.left = `${xPos}px`;
particle.style.top = `${yPos}px`;
particleContainer.appendChild(particle);
setTimeout(() => particle.remove(), 800);
}
}
let currentMidiEvents = [];
let currentMidiTempo = 120;
async function loadAndPlayMidiFromUrl(url) {
if (Tone.Transport.state === 'started') {
Tone.Transport.stop();
Tone.Transport.cancel();
document.querySelectorAll('.note-bar').forEach(n => n.remove());
Object.values(pianoKeys).forEach(key => key.classList.remove('active')); // Reset active keys
loadMidiButton.textContent = 'Load & Play MIDI';
return;
}
if (!url) {
alert("Please enter a MIDI URL.");
return;
}
loadingIndicator.style.display = 'inline';
loadMidiButton.disabled = true;
loadMidiButton.textContent = 'Loading...';
try {
const response = await fetch(url);
if (!response.ok) throw new Error(`Failed to fetch MIDI: ${response.status} ${response.statusText}`);
const arrayBuffer = await response.arrayBuffer();
const midi = MidiConvert.parse(arrayBuffer);
if (!midi || !midi.tracks || midi.tracks.length === 0) {
throw new Error("Invalid MIDI data or no playable tracks found.");
}
currentMidiTempo = (midi.header && midi.header.tempos && midi.header.tempos[0]) ? midi.header.tempos[0].bpm : 120;
Tone.Transport.bpm.value = currentMidiTempo;
currentMidiEvents = [];
midi.tracks.forEach(track => {
if (track.notes) {
track.notes.forEach(note => {
currentMidiEvents.push({
time: note.time,
note: Tone.Frequency(note.midi, "midi").toNote(),
duration: note.duration,
velocity: note.velocity
});
});
}
});
currentMidiEvents.sort((a, b) => a.time - b.time);
scheduleMidiEventsVisuals();
Tone.Transport.start();
loadMidiButton.textContent = 'Stop MIDI';
} catch (error) {
console.error("Error loading/parsing MIDI:", error);
alert(`Error: ${error.message}. Please check the URL and ensure it's a valid MIDI file accessible via CORS.`);
loadMidiButton.textContent = 'Load & Play MIDI';
} finally {
loadingIndicator.style.display = 'none';
loadMidiButton.disabled = false;
}
}
function scheduleMidiEventsVisuals() {
const noteColors = [
[30, 144, 255], [255, 20, 147], [50, 205, 50], [255, 165, 0],
[147, 112, 219], [255, 255, 0], [0, 255, 255], [255,0,0]
];
let colorIdx = 0;
const fallAreaRect = noteFallArea.getBoundingClientRect();
const hitLineRect = hitLineElement.getBoundingClientRect();
const hitLineTopRelativeToFallArea = hitLineRect.top - fallAreaRect.top;
currentMidiEvents.forEach(noteData => {
Tone.Transport.scheduleOnce(time => {
const targetKeyElement = pianoKeys[noteData.note];
if (!targetKeyElement) return;
const noteElement = document.createElement('div');
noteElement.classList.add('note-bar');
const keyRect = targetKeyElement.getBoundingClientRect();
const keyIsBlack = noteData.note.includes('#');
// Adjust width for visual fit
const noteBarWidth = (keyIsBlack ? BLACK_KEY_WIDTH_PX : WHITE_KEY_WIDTH_PX) - (keyIsBlack ? 2 : 2);
noteElement.style.width = noteBarWidth + 'px';
// Center the note bar on the key
noteElement.style.left = (keyRect.left - fallAreaRect.left + (keyRect.width - noteBarWidth) / 2) + 'px';
const noteVisualHeight = Math.max(15, (noteData.duration / (60 / currentMidiTempo)) * 70);
noteElement.style.height = noteVisualHeight + 'px';
const [r, g, b] = noteColors[colorIdx % noteColors.length];
colorIdx++;
const noteMainColor = `rgba(${r}, ${g}, ${b}, 0.9)`;
const noteDarkerColor = `rgba(${Math.max(0,r-60)}, ${Math.max(0,g-60)}, ${Math.max(0,b-60)}, 0.9)`;
noteElement.style.setProperty('--note-gradient', `linear-gradient(to bottom, ${noteMainColor}, ${noteDarkerColor})`);
noteElement.style.setProperty('--note-glow', noteMainColor);
noteElement.style.top = `-${noteVisualHeight}px`;
noteFallArea.appendChild(noteElement);
const targetYForNoteTopToHitLine = hitLineTopRelativeToFallArea;
const targetYForNoteBottomToPassHitLine = hitLineTopRelativeToFallArea + noteVisualHeight;
const timeToHitLine = NOTE_FALL_DURATION_MS;
const timeAfterHitLine = noteData.duration * 1000;
const totalAnimationDuration = timeToHitLine + timeAfterHitLine;
noteElement.animate([
{ transform: `translateY(0px)` },
{ transform: `translateY(${targetYForNoteTopToHitLine}px)`, offset: timeToHitLine / totalAnimationDuration },
{ transform: `translateY(${targetYForNoteBottomToPassHitLine + 20}px)` } // +20 to ensure it goes off screen
], {
duration: totalAnimationDuration,
easing: 'linear'
}).onfinish = () => {
if (noteElement.parentNode) noteElement.remove();
};
Tone.Transport.scheduleOnce(hitTime => {
synth.triggerAttackRelease(noteData.note, noteData.duration, hitTime, noteData.velocity);
if (targetKeyElement) {
// Use noteMainColor for the glow of MIDI-played keys
targetKeyElement.style.setProperty('--key-active-glow', noteMainColor);
targetKeyElement.classList.add('active');
createKeyParticles(targetKeyElement, noteMainColor);
setTimeout(() => {
targetKeyElement.classList.remove('active');
// Optionally reset glow to default if needed
// targetKeyElement.style.removeProperty('--key-active-glow');
}, noteData.duration * 1000);
}
}, time + (NOTE_FALL_DURATION_MS / 1000));
}, noteData.time);
});
}
loadMidiButton.addEventListener('click', async () => {
await Tone.start();
const url = midiUrlInput.value.trim();
loadAndPlayMidiFromUrl(url);
});
window.addEventListener('keydown', (event) => {
if (event.repeat || midiUrlInput === document.activeElement) return;
const note = keyboardMapping[event.key.toLowerCase()];
if (note && pianoKeys[note] && !activeKeyboardKeys.has(note)) {
activeKeyboardKeys.add(note);
pianoKeys[note].dispatchEvent(new MouseEvent('mousedown'));
}
});
window.addEventListener('keyup', (event) => {
const note = keyboardMapping[event.key.toLowerCase()];
if (note && pianoKeys[note]) {
activeKeyboardKeys.delete(note);
pianoKeys[note].dispatchEvent(new MouseEvent('mouseup'));
}
});
window.addEventListener('resize', () => {
createPianoKeys();
// If MIDI is playing, you might want to reschedule visuals or handle this more gracefully.
// For now, stopping and clearing visuals might be simplest if a resize happens during playback.
if (Tone.Transport.state === 'started') {
Tone.Transport.stop();
Tone.Transport.cancel();
document.querySelectorAll('.note-bar').forEach(n => n.remove());
Object.values(pianoKeys).forEach(key => key.classList.remove('active'));
loadMidiButton.textContent = 'Load & Play MIDI';
// Optionally, you could try to re-calculate and re-schedule notes here,
// but it's complex to get right without visual jumps.
}
});
</script>
</body>
</html> |