|
1 | | - |
2 | 1 | import { useEffect } from "react"; |
3 | 2 | import { useStore } from "@/lib/store"; |
4 | | - |
5 | | -// Map MIDI note numbers to Note names |
6 | | -const noteMap = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]; |
7 | | - |
8 | | -function midiToNote(midi: number) { |
9 | | - const octave = Math.floor(midi / 12) - 1; |
10 | | - const note = noteMap[midi % 12]; |
11 | | - return `${note}${octave}`; |
12 | | -} |
| 3 | +import { Utils } from "@notater/core"; |
13 | 4 |
|
14 | 5 | export function useMidi() { |
15 | | - // We access the synth via the store's audio engine reference if possible, |
16 | | - // but the store keeps audio logic encapsulated. |
17 | | - // Ideally, we add a `triggerNote` action to the store or access the exported synth. |
18 | | - // The previous implementation had `globalSynth` exported from `lib/audio/index.ts`? |
19 | | - // Let's check imports. `lib/store.ts` imports `createSynth` but `globalSynth` is lazy. |
20 | | - |
21 | | - // Actually, `store.ts` has `setSynthPreset` but maybe not direct trigger actions exposed appropriately for real-time play |
22 | | - // without React overhead. |
23 | | - |
24 | | - // For low latency, direct access is better. |
25 | | - // Let's assume we can import `globalSynth` or use a store action. |
26 | | - |
27 | | - // If I use `useStore` actions, it might trigger state updates which is fine for UI but maybe slow for audio? |
28 | | - // Not necessarily. |
29 | | - |
30 | | - // However, `lib/audio/index.ts` exports `globalSynth` variable? |
31 | | - // Let's verify. If not, I'll update store to handle "noteOn" / "noteOff". |
32 | | - |
33 | 6 | useEffect(() => { |
34 | | - if (!navigator.requestMIDIAccess) return; |
35 | | - |
36 | | - const handleMidiMessage = (e: Event) => { |
37 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
38 | | - const event = e as any; // Cast to any because WebMIDI types are not standard in this env |
39 | | - if (!event.data) return; |
40 | | - |
41 | | - const [command, note, velocity] = event.data; |
42 | | - |
43 | | - // Note On |
44 | | - if (command === 144 && velocity > 0) { |
45 | | - const noteName = midiToNote(note); |
46 | | - // Trigger Store Action |
47 | | - useStore.getState().triggerAttack(noteName); |
48 | | - } |
49 | | - |
50 | | - // Note Off |
51 | | - if (command === 128 || (command === 144 && velocity === 0)) { |
52 | | - const noteName = midiToNote(note); |
53 | | - useStore.getState().triggerRelease(noteName); |
| 7 | + // Initialize Core MIDI Manager |
| 8 | + const midi = Utils.midiManager; |
| 9 | + midi.initialize(); |
| 10 | + |
| 11 | + // Set Handler to trigger Store Actions |
| 12 | + midi.setHandler({ |
| 13 | + onNoteOn: (note, velocity) => { |
| 14 | + useStore.getState().triggerAttack(note); |
| 15 | + }, |
| 16 | + onNoteOff: (note) => { |
| 17 | + useStore.getState().triggerRelease(note); |
54 | 18 | } |
55 | | - }; |
56 | | - |
57 | | - navigator.requestMIDIAccess().then((access) => { |
58 | | - const inputs = access.inputs.values(); |
59 | | - for (const input of inputs) { |
60 | | - input.onmidimessage = handleMidiMessage; |
61 | | - } |
62 | | - |
63 | | - access.onstatechange = (e: Event) => { |
64 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
65 | | - const event = e as any; |
66 | | - // creating/removing connections |
67 | | - console.log(event.port.name, event.port.state, event.port.connection); |
68 | | - }; |
69 | 19 | }); |
70 | 20 |
|
| 21 | + // Cleanup not strictly necessary for singleton, but good practice if we had unmount logic |
| 22 | + // For now, we leave the handler attached or allow it to be overwritten. |
71 | 23 | }, []); |
72 | 24 | } |
0 commit comments