Simple monitor for displaying MIDI status for digital pianos.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

57 lignes
1.3 KiB

  1. import * as React from 'react'
  2. import KeyboardBase, { StyledAccidentalKey, StyledNaturalKey, } from '@theoryofnekomata/react-musical-keyboard'
  3. const NaturalKey = React.memo(StyledNaturalKey)
  4. const AccidentalKey = React.memo(StyledAccidentalKey)
  5. const Keyboard = ({
  6. startKey = 21,
  7. endKey = 108,
  8. }) => {
  9. const [keyChannels, setKeyChannels, ] = React.useState<any[]>([])
  10. React.useEffect(() => {
  11. const onMessage = (e: MessageEvent) => {
  12. if (e.data.event !== 'note') {
  13. return
  14. }
  15. const [key, velocity, ] = e.data.message.split(':')
  16. setKeyChannels(theKeyChannels => {
  17. if (velocity > 0) {
  18. return [
  19. ...theKeyChannels,
  20. {
  21. channel: 0,
  22. key: Number(key),
  23. velocity: Number(velocity),
  24. },
  25. ]
  26. }
  27. return theKeyChannels.filter(k => k.key !== Number(key))
  28. })
  29. }
  30. window.addEventListener('message', onMessage)
  31. return () => {
  32. window.removeEventListener('message', onMessage)
  33. }
  34. }, [])
  35. return (
  36. <KeyboardBase
  37. height="100%"
  38. startKey={startKey}
  39. endKey={endKey}
  40. keyChannels={keyChannels}
  41. keyComponents={{
  42. natural: NaturalKey,
  43. accidental: AccidentalKey,
  44. }}
  45. />
  46. )
  47. }
  48. export default Keyboard