|
1 |
| -import { useEffect, useRef, useState } from "react"; |
2 |
| -import { Listener, WebMidi } from "webmidi"; |
| 1 | +import dynamic from "next/dynamic"; |
| 2 | +import { MidiInstrument } from "./ConnectMidiClient"; |
3 | 3 |
|
4 |
| -type Instrument = { |
5 |
| - start(note: { note: number; velocity: number }): void; |
6 |
| - stop(note: { stopId: number }): void; |
7 |
| -}; |
| 4 | +const Midi = dynamic( |
| 5 | + () => import("./ConnectMidiClient").then((module) => module.ConnectMidi), |
| 6 | + { |
| 7 | + ssr: false, |
| 8 | + } |
| 9 | +); |
8 | 10 |
|
9 |
| -export function ConnectMidi({ |
10 |
| - instrument, |
11 |
| -}: { |
12 |
| - instrument: Instrument | undefined; |
| 11 | +export function ConnectMidi(props: { |
| 12 | + shouldRenderComponent?: boolean; |
| 13 | + instrument: MidiInstrument | undefined; |
13 | 14 | }) {
|
14 |
| - const inst = useRef<Instrument | null>(null); |
15 |
| - const [midiDeviceNames, setMidiDeviceNames] = useState<string[]>([]); |
16 |
| - const [midiDeviceName, setMidiDeviceName] = useState(""); |
17 |
| - const [disconnectMidiDevice, setDisconnectMidiDevice] = useState< |
18 |
| - Listener[] | undefined |
19 |
| - >(); |
20 |
| - const [lastNote, setLastNote] = useState(""); |
| 15 | + const { shouldRenderComponent } = props; |
21 | 16 |
|
22 |
| - useEffect(() => { |
23 |
| - WebMidi.enable().then(() => { |
24 |
| - const deviceNames = WebMidi.inputs.map((device) => device.name); |
25 |
| - setMidiDeviceNames(deviceNames); |
26 |
| - setMidiDeviceName(deviceNames[0]); |
27 |
| - }); |
28 |
| - }, []); |
29 |
| - |
30 |
| - inst.current = instrument ?? null; |
31 |
| - |
32 |
| - return ( |
33 |
| - <> |
34 |
| - <button |
35 |
| - className={ |
36 |
| - "px-1 rounded " + |
37 |
| - (disconnectMidiDevice ? "bg-emerald-600" : "bg-zinc-700") |
38 |
| - } |
39 |
| - onClick={() => { |
40 |
| - if (disconnectMidiDevice) { |
41 |
| - setDisconnectMidiDevice(undefined); |
42 |
| - disconnectMidiDevice.forEach((listener) => listener.remove()); |
43 |
| - return; |
44 |
| - } |
45 |
| - const device = WebMidi.inputs.find( |
46 |
| - (device) => device.name === midiDeviceName |
47 |
| - ); |
48 |
| - if (!device) return; |
49 |
| - const listener = device.addListener("noteon", (event) => { |
50 |
| - const noteOn = { |
51 |
| - note: event.note.number, |
52 |
| - velocity: (event as any).rawVelocity, |
53 |
| - }; |
54 |
| - inst.current?.start(noteOn); |
55 |
| - setLastNote(`${noteOn.note} (${noteOn.velocity})`); |
56 |
| - }); |
57 |
| - const listenerOff = device.addListener("noteoff", (event) => { |
58 |
| - inst.current?.stop({ stopId: event.note.number }); |
59 |
| - setLastNote(""); |
60 |
| - }); |
61 |
| - |
62 |
| - setDisconnectMidiDevice([ |
63 |
| - ...(Array.isArray(listener) ? listener : [listener]), |
64 |
| - ...(Array.isArray(listenerOff) ? listenerOff : [listenerOff]), |
65 |
| - ]); |
66 |
| - }} |
67 |
| - > |
68 |
| - MIDI |
69 |
| - </button> |
70 |
| - <select |
71 |
| - className="bg-zinc-700 rounded py-[2px]" |
72 |
| - value={midiDeviceName} |
73 |
| - onChange={(e) => { |
74 |
| - const name = e.target.value; |
75 |
| - setMidiDeviceName(name); |
76 |
| - }} |
77 |
| - > |
78 |
| - {midiDeviceNames.map((name) => ( |
79 |
| - <option key={name} value={name}> |
80 |
| - {name} |
81 |
| - </option> |
82 |
| - ))} |
83 |
| - </select> |
84 |
| - <div className="opacity-50">{lastNote}</div> |
85 |
| - </> |
86 |
| - ); |
| 17 | + return shouldRenderComponent ? <Midi {...props} /> : null; |
87 | 18 | }
|
0 commit comments