Simple monitor for displaying MIDI status for digital pianos.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

110 linhas
2.9 KiB

  1. ((window, electron) => {
  2. const X_OFFSET = 0
  3. const NATURAL_KEY_WIDTH = 16;
  4. const ACCIDENTAL_KEY_WIDTH = 11;
  5. const accidental_key_x_offsets = [
  6. 10.0 / 112.0,
  7. 28.0 / 112.0,
  8. 57.0 / 112.0,
  9. 75.0 / 112.0,
  10. 93.0 / 112.0,
  11. ]
  12. const { ipcRenderer, } = electron
  13. const renderKeys = () => {
  14. let midi_note = 12
  15. const elements = {}
  16. let element
  17. const keyboard = window.document.getElementById('keyboard')
  18. for (let octave = 0; octave < 9; octave += 1) {
  19. for (let natural_key = 0; natural_key < 7; natural_key += 1) {
  20. element = window.document.createElement('div')
  21. element.classList.add('natural')
  22. element.classList.add('key')
  23. element.style.position = 'absolute'
  24. element.style.top = 0
  25. element.style.left = X_OFFSET + (octave * NATURAL_KEY_WIDTH * 7) + (natural_key * NATURAL_KEY_WIDTH) +
  26. 'px'
  27. element.style.width = NATURAL_KEY_WIDTH + 'px'
  28. element.appendChild(window.document.createElement('div'))
  29. elements[midi_note] = element
  30. keyboard.appendChild(element)
  31. switch (natural_key) {
  32. case 0:
  33. case 1:
  34. case 3:
  35. case 4:
  36. case 5:
  37. midi_note += 2;
  38. break;
  39. case 2:
  40. case 6:
  41. midi_note += 1;
  42. break;
  43. default:
  44. break;
  45. }
  46. }
  47. midi_note -= 11;
  48. for (let accidental_key = 0; accidental_key < 5; accidental_key += 1) {
  49. element = window.document.createElement('div')
  50. element.classList.add('accidental')
  51. element.classList.add('key')
  52. element.style.position = 'absolute'
  53. element.style.top = 0
  54. element.style.left = X_OFFSET + (octave * NATURAL_KEY_WIDTH * 7) + (NATURAL_KEY_WIDTH * 7 *
  55. accidental_key_x_offsets[accidental_key]) + 'px'
  56. element.style.width = ACCIDENTAL_KEY_WIDTH + 'px'
  57. element.appendChild(window.document.createElement('div'))
  58. elements[midi_note] = element
  59. keyboard.appendChild(element)
  60. switch (accidental_key) {
  61. case 0:
  62. case 2:
  63. case 3:
  64. midi_note += 2;
  65. break;
  66. case 1:
  67. case 4:
  68. midi_note += 3;
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. midi_note -= 1;
  75. }
  76. return elements
  77. }
  78. const elements = renderKeys(null, null)
  79. ipcRenderer.on('note', (event, message) => {
  80. const [key, value, ] = message.split(':')
  81. if (value > 0) {
  82. elements[key].classList.add('pressed')
  83. } else {
  84. elements[key].classList.remove('pressed')
  85. }
  86. })
  87. ipcRenderer.on('pedal', (event, message) => {
  88. const [which, value, ] = message.split(':')
  89. const pedalIds = {
  90. 64: 'sustain',
  91. 66: 'sostenuto',
  92. 67: 'unacorda',
  93. }
  94. window.document.getElementById(pedalIds[which]).style.opacity = value / 127 * 0.75 + 0.25
  95. })
  96. })(window, require('electron'))