Musical keyboard component written in React.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

94 líneas
2.5 KiB

  1. import * as React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import Keyboard from '../src'
  4. import * as Channel from './controllers/Channel'
  5. import * as Instrument from './controllers/Instrument'
  6. import * as Generator from './controllers/Generator'
  7. import keyboardMapping from './services/keyboardMapping'
  8. import SoundGenerator from './services/SoundGenerator'
  9. const App = () => {
  10. const [channel, setChannel] = React.useState(0)
  11. const [keyChannels, setKeyChannels] = React.useState<{ key: number; velocity: number; channel: number }[]>([])
  12. const [instruments, setInstruments, ] = React.useState<string[]>([])
  13. const [instrument, setInstrument] = React.useState(0)
  14. const generator = React.useRef<SoundGenerator | undefined>(undefined)
  15. const scrollRef = React.useRef<HTMLDivElement>(null)
  16. React.useEffect(() => {
  17. if (!generator.current) {
  18. return
  19. }
  20. Instrument.reflect({ generator: generator.current, channel, instrument })
  21. }, [channel, instrument])
  22. React.useEffect(() => {
  23. Generator
  24. .load()
  25. .then(g => {
  26. Instrument.initialize({ setInstruments, generator: generator.current = g, })
  27. })
  28. }, [])
  29. React.useEffect(() => {
  30. const { current } = scrollRef
  31. if (current) {
  32. current.scrollLeft = current.scrollWidth * 0.4668
  33. }
  34. }, [scrollRef])
  35. return (
  36. <React.Fragment>
  37. <input
  38. type="number"
  39. id="channel"
  40. min={0}
  41. max={15}
  42. onChange={Channel.change({ setChannel, })}
  43. defaultValue={0}
  44. />
  45. <select
  46. id="instrument"
  47. onChange={Instrument.change({ setInstrument, })}
  48. defaultValue={0}
  49. >
  50. {Array.isArray(instruments) && instruments.map((name, i) => (
  51. <option
  52. key={i}
  53. value={i}
  54. >
  55. {name}
  56. </option>
  57. ))}
  58. </select>
  59. <div
  60. id="keyboard"
  61. ref={scrollRef}
  62. >
  63. <div
  64. id="keyboard-scroll"
  65. >
  66. <Keyboard
  67. hasMap
  68. startKey={0}
  69. endKey={127}
  70. keyChannels={keyChannels}
  71. height="100%"
  72. onChange={Channel.handle({ setKeyChannels, generator: generator.current!, channel, })}
  73. keyboardMapping={keyboardMapping}
  74. />
  75. </div>
  76. </div>
  77. </React.Fragment>
  78. )
  79. }
  80. const container = window.document.createElement('div')
  81. container.style.display = 'contents'
  82. window.document.body.appendChild(container)
  83. ReactDOM.render(<App />, container)