Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I access individual midi notes inside of the useMIDINotes hook... #7

Closed
rchrdnsh opened this issue Jun 23, 2020 · 1 comment
Closed

Comments

@rchrdnsh
Copy link

Hi Nick,

I'm trying to make a piano that lights up individual (or multiple at the same time) notes as they are pressed on a MIDI keyboard. This is me trying to make a simple proof of concept but I cannot seem to figure out how to make it work. What I want is for each <div> to light up with a different background color when a specific midi note is pressed, but also have multiple <div>'s light up at the same time when multiple notes are pressed. Don't know how to do that, however. This is what I have so far, but it does not work. Have any idea how one might go about doing that? As a bonus I would also like the saturation of the note to be lighter or darker depending on the velocity of the note(s) as well. Burt on and off is the first goal. The approach below worked with the useMIDINote hook, but would only light up one note at a time, hence why I'm trying to do this with the useMIDINotes hook:

const MIDINotesLog = ({ input }) => {
  const notes = useMIDINotes(input, { channel: 1 })
  return (
    <div style={{display: `flex`, border: `1px solid black`}}>
      {
        notes.note === 60 ?
          <div style={{width: '2rem', height: '4rem', border: `1px solid black`, background: 'red'}}></div>
        :
          <div style={{width: '2rem', height: '4rem', border: `1px solid black`, background: 'white'}}></div>
      }
      {
        notes.note === 61 ?
          <div style={{width: '2rem', height: '4rem', border: `1px solid black`, background: 'red'}}></div>
        :
          <div style={{width: '2rem', height: '4rem', border: `1px solid black`, background: 'white'}}></div>
      }
      {
        notes.note === 62 ?
          <div style={{width: '2rem', height: '4rem', border: `1px solid black`, background: 'red'}}></div>
        :
          <div style={{width: '2rem', height: '4rem', border: `1px solid black`, background: 'white'}}></div>
      }
      </div>
  )
}
@rchrdnsh rchrdnsh changed the title How do I access individual midi notes inside of the useMIDINotes hook... How do I access individual midi notes inside of the useMIDINotes hook... Jun 23, 2020
@rchrdnsh
Copy link
Author

k, it took me a minute, but I came up with a two part approach...

  1. map all the notes from the hook into an array, using your example:
const noteNumbers = notes.map((n) => n.note).join(', ')
  1. Check if the array included certain notes. In this example, doing it with styled-components:
${props => props.noteNumbers.includes(24 + (12 * props.octave)) && css`${MIDINote}`};

so, yeah...got it working now :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant