-
Notifications
You must be signed in to change notification settings - Fork 2
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
new MidiKeyboard #33
Merged
Merged
new MidiKeyboard #33
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,231 @@ | ||
import { memo, useCallback } from "react"; | ||
import { MidiNumbers } from "react-piano"; | ||
import { DimensionsProvider } from "../contexts/dimension"; | ||
import ResponsivePiano from "./ResponsivePiano"; | ||
import { triggerRemoteMidiNoteEvent } from "../actions/device"; | ||
import { FunctionComponent, memo, PointerEvent, useCallback, useEffect, useRef, useState } from "react"; | ||
import { Set as ImmuSet } from "immutable"; | ||
import { useAppDispatch } from "../hooks/useAppDispatch"; | ||
import { triggerRemoteMidiNoteEvent } from "../actions/device"; | ||
import styled from "styled-components"; | ||
import { clamp } from "../lib/util"; | ||
|
||
const MIDIWrapper = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
user-select: none; | ||
touch-action: none; | ||
`; | ||
|
||
|
||
const KeyWidth = 30; | ||
const OctaveWidth = KeyWidth * 7; | ||
const BaseOctave = 4; | ||
|
||
interface NoteElementProps { | ||
index: number; | ||
isActive: boolean; | ||
isWhiteKey: boolean; | ||
} | ||
|
||
const NoteElement = styled.div.attrs<NoteElementProps>(({ | ||
index, | ||
isWhiteKey | ||
}) => ({ | ||
style: { | ||
height: isWhiteKey ? "100%" : "60%", | ||
left: isWhiteKey ? index * KeyWidth : index * KeyWidth + 0.5 * KeyWidth, | ||
zIndex: isWhiteKey ? 2 : 3 | ||
} | ||
}))<NoteElementProps>` | ||
|
||
background-color: ${({ isActive, isWhiteKey, theme }) => isActive ? theme.colors.primary : isWhiteKey ? "#fff" : "#333" }; | ||
border-bottom-left-radius: 2px; | ||
border-bottom-right-radius: 2px; | ||
border-color: gray; | ||
border-style: solid; | ||
border-width: 1px; | ||
position: absolute; | ||
top: 0; | ||
width: ${KeyWidth}px; | ||
`; | ||
|
||
const Note: FunctionComponent<{ | ||
index: number; | ||
isActive: boolean; | ||
isWhiteKey: boolean; | ||
note: number; | ||
onNoteOn: (n: number) => any; | ||
onNoteOff: (n: number) => any; | ||
}> = memo(({ | ||
index, | ||
isActive, | ||
isWhiteKey, | ||
note, | ||
onNoteOff, | ||
onNoteOn | ||
}) => { | ||
|
||
const onPointerDown = (e: PointerEvent<HTMLDivElement>) => { | ||
if (isActive) return; | ||
onNoteOn(note); | ||
}; | ||
|
||
const onPointerEnter = (e: PointerEvent<HTMLDivElement>) => { | ||
if (e.pointerType === "mouse" && !e.buttons) return; | ||
onNoteOn(note); | ||
}; | ||
|
||
const onPointerLeave = (e: PointerEvent<HTMLDivElement>) => { | ||
if (!isActive) return; | ||
if (e.pointerType === "mouse" && !e.buttons) return; | ||
onNoteOff(note); | ||
}; | ||
|
||
const onPointerUp = (e: PointerEvent<HTMLDivElement>) => { | ||
if (!isActive) return; | ||
onNoteOff(note); | ||
}; | ||
|
||
return ( | ||
<NoteElement | ||
className={ isActive ? "active" : "" } | ||
index={ index } | ||
isActive={ isActive } | ||
isWhiteKey={ isWhiteKey } | ||
onPointerEnter={ onPointerEnter } | ||
onPointerDown={ onPointerDown} | ||
onPointerLeave={ onPointerLeave } | ||
onPointerCancel={ onPointerUp } | ||
onPointerUp={ onPointerUp } | ||
/> | ||
); | ||
}); | ||
|
||
Note.displayName = "Note"; | ||
|
||
.keyboardContainer { | ||
height: 15rem; | ||
width: 80%; | ||
const OctaveElement = styled.div` | ||
height: 150px; | ||
position: relative; | ||
user-select: none; | ||
width: ${OctaveWidth}px; | ||
|
||
&:not(:last-child) { | ||
border-right: none; | ||
} | ||
|
||
.keyboardContainer > div { | ||
> div { | ||
height: 100%; | ||
left: 0; | ||
position: absolute; | ||
top: 0; | ||
width: 100%; | ||
} | ||
`; | ||
|
||
const Octave: FunctionComponent<{ | ||
octave: number; | ||
activeNotes: ImmuSet<number>; | ||
onNoteOn: (note: number) => any; | ||
onNoteOff: (note: number) => any; | ||
}> = memo(({ | ||
octave, | ||
activeNotes, | ||
onNoteOn, | ||
onNoteOff | ||
}) => { | ||
|
||
const start = 12 * octave; | ||
const whiteNotes: JSX.Element[] = []; | ||
const blackNotes: JSX.Element[] = []; | ||
|
||
for (let i = 0, key = start; i < 7; i++) { | ||
|
||
@media screen and (max-width: 35.5em) { | ||
padding-top: 5rem; | ||
.keyboardContainer { | ||
height: 10rem; | ||
width: 100%; | ||
// create a white key for every entry | ||
whiteNotes.push(<Note | ||
key={ key } | ||
index={ i } | ||
note={ key } | ||
isActive={ activeNotes.has(key) } | ||
isWhiteKey={ true } | ||
onNoteOn={ onNoteOn } | ||
onNoteOff={ onNoteOff } | ||
/>); | ||
key++; | ||
|
||
// create black key?! | ||
if (i !== 2 && i !== 6) { | ||
blackNotes.push( | ||
<Note | ||
key={ key } | ||
index={ i } | ||
note={ key } | ||
isActive={ activeNotes.has(key) } | ||
isWhiteKey={ false } | ||
onNoteOn={ onNoteOn } | ||
onNoteOff={ onNoteOff } | ||
/> | ||
); | ||
key++; | ||
} | ||
} | ||
`; | ||
|
||
const noteRange = { | ||
first: MidiNumbers.fromNote("c3"), | ||
last: MidiNumbers.fromNote("f4") | ||
}; | ||
return ( | ||
<OctaveElement> | ||
<div> | ||
{ blackNotes } | ||
{ whiteNotes } | ||
</div> | ||
</OctaveElement> | ||
); | ||
}); | ||
|
||
const PianoKeyboard = memo(function WrappedPianoKeyboard() { | ||
Octave.displayName = "OctaveName"; | ||
|
||
export const PianoKeyboard: FunctionComponent<{}> = memo(() => { | ||
|
||
const dispatch = useAppDispatch(); | ||
const containerRef = useRef<HTMLDivElement>(); | ||
const [activeNotes, setActiveNotes] = useState(ImmuSet<number>()); | ||
const [noOfOctaves, setNoOfOctaves] = useState(4); | ||
|
||
const onNoteOn = useCallback((p: number) => { | ||
dispatch(triggerRemoteMidiNoteEvent(p, true)); | ||
setActiveNotes((notes: ImmuSet<number>) => notes.add(p)); | ||
}, [dispatch]); | ||
|
||
const onNoteOff = useCallback((p: number) => { | ||
dispatch(triggerRemoteMidiNoteEvent(p, false)); | ||
setActiveNotes((notes: ImmuSet<number>) => notes.delete(p)); | ||
}, [dispatch]); | ||
|
||
return ( | ||
<MIDIWrapper> | ||
<div className="keyboardContainer"> | ||
<DimensionsProvider> | ||
<ResponsivePiano noteRange={noteRange} onNoteOn={onNoteOn} onNoteOff={onNoteOff} /> | ||
</DimensionsProvider> | ||
</div> | ||
useEffect(() => { | ||
const onResize = (ev?: UIEvent) => { | ||
if (!containerRef.current) return; | ||
const { width } = containerRef.current.getBoundingClientRect(); | ||
setNoOfOctaves(clamp(Math.floor(width / OctaveWidth), 1, 6)); | ||
}; | ||
|
||
window.addEventListener("resize", onResize); | ||
onResize(); | ||
|
||
return () => window.removeEventListener("resize", onResize); | ||
}, []); | ||
|
||
const octs: JSX.Element[] = []; | ||
|
||
for (let i = 0; i < noOfOctaves; i++) { | ||
octs.push(<Octave | ||
key={ i } | ||
octave={ BaseOctave + i } | ||
activeNotes={ activeNotes } | ||
onNoteOn={ onNoteOn } | ||
onNoteOff={ onNoteOff } | ||
/>); | ||
} | ||
|
||
return ( | ||
<MIDIWrapper ref={ containerRef } > | ||
{ octs } | ||
</MIDIWrapper> | ||
); | ||
}); | ||
|
||
PianoKeyboard.displayName = "PianoKeyboard"; | ||
|
||
export default PianoKeyboard; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
export const sleep = (t: number): Promise<void> => new Promise(resolve => setTimeout(resolve, t)); | ||
|
||
export const clamp = (num: number, min: number, max: number): number => { | ||
return Math.min(Math.max(num, min), max); | ||
}; | ||
|
||
export const scale = (x: number, inLow: number, inHigh: number, outLow: number, outHigh: number): number => { | ||
return (x - inLow) * (outHigh - outLow) / (inHigh - inLow) + outLow; | ||
}; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😍 one of my favs. every time i need it i spend 35 minutes googling.