Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
YeonV committed Oct 5, 2024
1 parent ea9be74 commit 82eaf75
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/components/Midi/MidiListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ import { WebMidi, Input, Output } from 'webmidi'
import useStore from '../../store/useStore'
import { getUiBtnNo } from '../../store/ui/storeMidi'

// function setAllButtonsToBlack(output: WebMidi.MIDIOutput) {
// for (let i = 0 i < 81 i++) {
// output?.send([0x90, i, 0])
// }
// }

const MIDIListener = () => {
const setFeatures = useStore((state) => state.setFeatures)
const togglePause = useStore((state) => state.togglePause)
Expand Down Expand Up @@ -95,6 +89,31 @@ const MIDIListener = () => {
const getMappingByButtonNumber = (buttonNumber: number) => {
return Object.values(midiMapping[0]).find(mapping => mapping.buttonNumber === buttonNumber)
}

const initLeds = (output: Output) => {
if (output) {
Object.entries(midiMapping[0]).forEach(([key, value]) => {
const buttonNumber = value.buttonNumber
if (value.command !== 'scene' && value.command && value.command !== 'none' && buttonNumber !== -1) {
if (output && buttonNumber !== -1) {
try {
output.send([parseInt(`0x${value.typeCommand}`) || 0x90, buttonNumber, parseInt(value.colorCommand || commandColor, 16) || 99])
} catch (error) {
console.error('Error sending MIDI message:', error)
}
}
} else if (value.command === 'scene') {
if (output && buttonNumber !== -1) {
try {
output.send([parseInt(`0x${value.typeSceneInactive}`) || 0x90, buttonNumber, parseInt(value.colorSceneInactive || midiSceneInactiveColor, 16) || 60])
} catch (error) {
console.error('Error sending MIDI message:', error)
}
}
}
})
}
}

useEffect(() => {
const handleMidiInput = (input: Input) => {
Expand All @@ -116,9 +135,9 @@ const MIDIListener = () => {
})

const mapping = getMappingByButtonNumber(event.note.number)
console.log('MIDI Mapping:', mapping, event.note.number)
// console.log('MIDI Mapping:', mapping, event.note.number)
if (mapping?.command !== undefined) {
console.log('MIDI Command:', mapping.command)
// console.log('MIDI Command:', mapping.command)
handleButtonPress(mapping.command, mapping.payload)
}
})
Expand Down Expand Up @@ -163,30 +182,7 @@ const MIDIListener = () => {
}
})
}
const initLeds = (output: Output) => {
if (output) {
Object.entries(midiMapping[0]).forEach(([key, value]) => {
const buttonNumber = value.buttonNumber
if (value.command !== 'scene' && value.command && value.command !== 'none' && buttonNumber !== -1) {
if (output && buttonNumber !== -1) {
try {
output.send([parseInt(`0x${value.typeCommand}`) || 0x90, buttonNumber, parseInt(value.colorCommand || commandColor, 16) || 99])
} catch (error) {
console.error('Error sending MIDI message:', error)
}
}
} else if (value.command === 'scene') {
if (output && buttonNumber !== -1) {
try {
output.send([parseInt(`0x${value.typeSceneInactive}`) || 0x90, buttonNumber, parseInt(value.colorSceneInactive || midiSceneInactiveColor, 16) || 60])
} catch (error) {
console.error('Error sending MIDI message:', error)
}
}
}
})
}
}


const enableWebMidi = () => {
WebMidi.enable({
Expand Down

0 comments on commit 82eaf75

Please sign in to comment.