-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
153 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,77 +1,81 @@ | ||
import { noop } from "svelte/internal" | ||
import { get } from "svelte/store" | ||
import { userConfig } from "./config" | ||
|
||
// TODO: convert to webmidi.js, much easier to work with | ||
import { EventEmitter } from "eventemitter3" | ||
import { WebMidi } from "webmidi" | ||
|
||
let midi | ||
let outputs = [] | ||
let buttonPressCallback = noop | ||
import { alert } from "./alerts" | ||
import { userConfig } from "./config" | ||
|
||
let lastLEDValue = 0 | ||
const MIDI_BTN_CTRL = 0x10 | ||
const MIDI_LED_CTRL = 0x11 | ||
const BTN_PRESSED = 0x7f | ||
|
||
export const LED_OFF = 0 | ||
export const LED_ON = 1 | ||
export const LED_FLASH = 2 | ||
export const LED_PULSATE_SLOW = 3 | ||
export const LED_PULSATE_FAST = 4 | ||
const ledStrings = ["off", "on", "flash", "pulsate/slow", "pulsate/fast"] | ||
|
||
const updateMidiDevices = (enabled = null) => { | ||
if (enabled === null) { | ||
enabled = get(userConfig).enableMIDIButtonBox | ||
} | ||
export const midiButtonPresses = new EventEmitter() | ||
|
||
if (!enabled) { | ||
setLED(LED_OFF, true) | ||
} | ||
let lastLEDValue = LED_OFF | ||
let enabled = false | ||
|
||
outputs = [] | ||
window.addEventListener("beforeunload", () => { | ||
midiSetLED(LED_OFF, true) | ||
}) | ||
|
||
for (const ports of [midi.inputs, midi.outputs]) { | ||
for (const [, port] of ports) { | ||
const input = port instanceof MIDIInput | ||
if (enabled) { | ||
port.open() | ||
if (input) { | ||
port.onmidimessage = ({ data }) => { | ||
if (data.length === 3 && data[0] === 0xb0 && data[1] === 0x10 && data[2] === 0x7f) { | ||
buttonPressCallback(data) | ||
} | ||
} | ||
} else { | ||
port.send([0xb0, 0x11, lastLEDValue]) | ||
outputs.push(port) | ||
const enableListeners = () => { | ||
WebMidi.addListener("connected", ({ port }) => { | ||
if (port.type === "input") { | ||
console.log(`Got midi input: ${port.name} (installing press listener)`) | ||
port.channels[1].addListener("controlchange", (event) => { | ||
if (event.controller.number === MIDI_BTN_CTRL) { | ||
midiButtonPresses.emit(event.rawValue === BTN_PRESSED ? "pressed" : "released") | ||
} | ||
} else { | ||
port.close() | ||
} | ||
}) | ||
} else if (port.type === "output") { | ||
console.log(`Got midi output: ${port.name} (set LED to ${ledStrings[lastLEDValue]})`) | ||
port.channels[1].sendControlChange(MIDI_LED_CTRL, lastLEDValue) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
userConfig.subscribe(({ enableMIDIButtonBox }) => { | ||
if (midi) { | ||
updateMidiDevices(enableMIDIButtonBox) | ||
} | ||
}) | ||
|
||
export let setLED = (value, skipSave = false) => { | ||
console.log(`Set LED to ${value}`) | ||
for (const port of outputs) { | ||
port.send([0xb0, 0x11, value]) | ||
export const midiSetLED = (value, skipSave = false) => { | ||
if (enabled) { | ||
console.log(`Set LED to ${ledStrings[value]}`) | ||
for (const output of WebMidi.outputs) { | ||
output.channels[1].sendControlChange(MIDI_LED_CTRL, value) | ||
} | ||
} | ||
// Save state, to re-enable if device is plugged in | ||
if (!skipSave) { | ||
lastLEDValue = value | ||
} | ||
} | ||
|
||
export let registerButtonPressCallback = (callback) => (buttonPressCallback = callback) | ||
;(async () => { | ||
midi = await navigator.requestMIDIAccess({ sysex: true }) | ||
updateMidiDevices() | ||
midi.onstatechange = () => updateMidiDevices() | ||
})() | ||
|
||
window.addEventListener("beforeunload", () => { | ||
setLED(LED_OFF, true) | ||
userConfig.subscribe(({ enableMIDIButtonBox }) => { | ||
if (enableMIDIButtonBox && !enabled) { | ||
enableListeners() | ||
WebMidi.enable({ | ||
sysex: true, | ||
callback: (error) => { | ||
if (error) { | ||
enabled = false | ||
console.error("Error enabling WebMidi!", error) | ||
alert("Error enabling MIDI subsystem for button box", "error") | ||
userConfig.update(($config) => ({ ...$config, enableMIDIButtonBox: false })) | ||
} else { | ||
console.log("WebMidi enabled") | ||
enabled = true | ||
} | ||
} | ||
}) | ||
} else if (!enableMIDIButtonBox && enabled) { | ||
midiSetLED(LED_OFF, true) | ||
WebMidi.disable() | ||
enabled = false | ||
console.log("WebMidi disabled") | ||
} | ||
}) | ||
|
||
window.WebMidi = WebMidi |
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