Skip to content

Commit

Permalink
MIDI Input: Fix scene trigger if multiple events detected
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonV committed Oct 3, 2024
1 parent e09ed4f commit 0564901
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/components/Dialogs/SceneDialogs/EditSceneDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const EditSceneDialog = () => {
const getUserPresets = useStore((state) => state.getUserPresets)
const getImage = useStore((state) => state.getImage)
const [imageData, setImageData] = useState(null)
const midiInput = useStore((state) => state.midiInput)

const getFullConfig = useStore((state) => state.getFullConfig)

Expand Down Expand Up @@ -237,14 +238,15 @@ const EditSceneDialog = () => {
if (err) {
console.error('WebMidi could not be enabled:', err)
} else {
// Get all input devices
const { inputs } = WebMidi
if (inputs.length > 0) {
// Listen for MIDI messages on all channels and all input devices
inputs.forEach((input: Input) =>
input.addListener('noteon', (event: NoteMessageEvent) => {
handleMidiEvent(input, event)
})
inputs.forEach((input: Input) => {
if (midiInput === input.name) {
return input.addListener('noteon', (event: NoteMessageEvent) => {
handleMidiEvent(input, event)
})
}
}
)
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/components/Midi/MidiListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ const MIDIListener = () => {
useEffect(() => {
const handleMidiInput = (input: Input) => {
if (!input || input.name !== midiInput) return
// console.log('Input Name:', input.name)
input.removeListener('noteon')
input.removeListener('noteoff')
input.addListener('noteon', (event: any) => {
if (!event.note || (midiEvent.button === event.note.number && midiEvent.name === input.name && midiEvent.note === event.note.identifier)) return
// console.log('Input Name:', input.name)
// console.log('MIDI Event:', event)
setMidiEvent({
name: input.name,
note: event.note.identifier,
Expand All @@ -105,6 +108,7 @@ const MIDIListener = () => {
})

input.addListener('noteoff', (event: any) => {
// console.log('MIDI Event:', event)
if (midiMapping[0][event.note.number]?.command === 'one-shot' && midiMapping[0][event.note.number]?.payload?.holdType === 'release') {
oneShotAll(
midiMapping[0][event.note.number]?.payload?.color || '#0dbedc',
Expand All @@ -121,6 +125,7 @@ const MIDIListener = () => {
})
})
input.addListener('controlchange', (event: any) => {
// console.log('MIDI Event:', event)
if (event.controller.number === midiEvent.button && midiEvent.name === input.name) return
if (event.value === 1) {
setMidiEvent({
Expand Down

0 comments on commit 0564901

Please sign in to comment.