Nekochu commited on
Commit
bfe8919
·
1 Parent(s): 1214878

fix piano key positions, Svelte event dispatch, black key gap

Browse files
Files changed (1) hide show
  1. app.py +9 -8
app.py CHANGED
@@ -396,12 +396,15 @@ PIANO_HTML = """
396
  {midi:79,n:'G5'},{midi:81,n:'A5'},{midi:83,n:'B5'},
397
  {midi:84,n:'C6'}
398
  ];
 
 
399
  const BLACK_POSITIONS = {
400
- 49:[1,0], 51:[2,0], 54:[4,0], 56:[5,0], 58:[6,0],
401
- 61:[8,0], 63:[9,0], 66:[11,0], 68:[12,0], 70:[13,0],
402
- 73:[15,0], 75:[16,0], 78:[18,0], 80:[19,0], 82:[20,0]
403
  };
404
  const KEY_WIDTH = 36;
 
405
  let held = new Set();
406
  const piano = document.getElementById('piano');
407
 
@@ -424,7 +427,7 @@ PIANO_HTML = """
424
  const el = document.createElement('div');
425
  el.className = 'black-key';
426
  el.dataset.midi = midi;
427
- el.style.left = (wIdx * KEY_WIDTH + KEY_WIDTH * 0.65) + 'px';
428
  el.addEventListener('mousedown', (e) => { e.preventDefault(); toggleNote(parseInt(midi)); });
429
  piano.appendChild(el);
430
  });
@@ -460,13 +463,11 @@ PIANO_HTML = """
460
 
461
  function pushNotes() {
462
  const json = JSON.stringify([...held]);
463
- // Find Gradio hidden textbox by elem_id
464
  const tb = document.querySelector('#notes-state textarea');
465
  if (tb) {
466
- const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
467
- window.HTMLTextAreaElement.prototype, 'value').set;
468
- nativeInputValueSetter.call(tb, json);
469
  tb.dispatchEvent(new Event('input', { bubbles: true }));
 
470
  }
471
  }
472
  })();
 
396
  {midi:79,n:'G5'},{midi:81,n:'A5'},{midi:83,n:'B5'},
397
  {midi:84,n:'C6'}
398
  ];
399
+ // Each entry: MIDI -> [white-key-index-of-left-neighbor, 0]
400
+ // C#/Db is right of C (index 0,7,14), D# right of D (1,8,15), etc.
401
  const BLACK_POSITIONS = {
402
+ 49:[0,0], 51:[1,0], 54:[3,0], 56:[4,0], 58:[5,0],
403
+ 61:[7,0], 63:[8,0], 66:[10,0], 68:[11,0], 70:[12,0],
404
+ 73:[14,0], 75:[15,0], 78:[17,0], 80:[18,0], 82:[19,0]
405
  };
406
  const KEY_WIDTH = 36;
407
+ const KEY_STEP = 38; // KEY_WIDTH + gap(2)
408
  let held = new Set();
409
  const piano = document.getElementById('piano');
410
 
 
427
  const el = document.createElement('div');
428
  el.className = 'black-key';
429
  el.dataset.midi = midi;
430
+ el.style.left = (wIdx * KEY_STEP + KEY_WIDTH * 0.65) + 'px';
431
  el.addEventListener('mousedown', (e) => { e.preventDefault(); toggleNote(parseInt(midi)); });
432
  piano.appendChild(el);
433
  });
 
463
 
464
  function pushNotes() {
465
  const json = JSON.stringify([...held]);
 
466
  const tb = document.querySelector('#notes-state textarea');
467
  if (tb) {
468
+ tb.value = json;
 
 
469
  tb.dispatchEvent(new Event('input', { bubbles: true }));
470
+ tb.dispatchEvent(new Event('change', { bubbles: true }));
471
  }
472
  }
473
  })();