fix: realtime note display + MIDI key sound via refs

This commit is contained in:
2026-07-25 18:58:30 +07:00
parent da760dbb86
commit e6dd7ff13c
2 changed files with 38 additions and 37 deletions
+36 -35
View File
@@ -6282,19 +6282,21 @@ const App = () => {
const cmd = msg.data[0] >> 4; const cmd = msg.data[0] >> 4;
const pitch = msg.data[1]; const pitch = msg.data[1];
const velocity = msg.data[2]; const velocity = msg.data[2];
if (cmd === 0x9 && velocity > 0) { if (cmd === 0x9 && velocity > 0) {
lastMidiNoteRef.current = { pitch, velocity, startTime: performance.now(), length: 0 }; lastMidiNoteRef.current = { pitch, velocity, startTime: performance.now(), length: 0 };
setLastMidiNote({ pitch, velocity, length: 0, time: Date.now() }); setLastMidiNote({ pitch, velocity, length: 0, time: Date.now() });
activeMidiPitchesRef.current.add(pitch); activeMidiPitchesRef.current.add(pitch);
setActiveMidiPitches(new Set(activeMidiPitchesRef.current)); setActiveMidiPitches(new Set(activeMidiPitchesRef.current));
const activeTabInfo = subTabs.find(s => s.id === activeTab && s.type === 'PIANO_ROLL' && s.isArmed); try {
if (activeTabInfo && window.SonicSF) { const ar = activeTabRef && subTabsRef && subTabsRef.current.find(s => s.id === activeTabRef.current && s.type === 'PIANO_ROLL' && s.isArmed);
try { window.SonicSF.playNote(pitch, velocity, 500, undefined, activeTabInfo.instrumentProgram, null); } catch (e) {} if (ar && window.SonicSF) {
} window.SonicSF.playNote(pitch, velocity, 500, undefined, ar.instrumentProgram, null);
} else if (cmd === 0x8 || (cmd === 0x9 && velocity === 0)) { }
activeMidiPitchesRef.current.delete(pitch); } catch (e) {}
setActiveMidiPitches(new Set(activeMidiPitchesRef.current)); } else if (cmd === 0x8 || (cmd === 0x9 && velocity === 0)) {
const current = lastMidiNoteRef.current; activeMidiPitchesRef.current.delete(pitch);
setActiveMidiPitches(new Set(activeMidiPitchesRef.current));
const current = lastMidiNoteRef.current;
if (current && current.pitch === pitch) { if (current && current.pitch === pitch) {
const lenSec = (performance.now() - current.startTime) / 1000; const lenSec = (performance.now() - current.startTime) / 1000;
lastMidiNoteRef.current = { ...current, length: lenSec }; lastMidiNoteRef.current = { ...current, length: lenSec };
@@ -9867,29 +9869,28 @@ const App = () => {
midiRec.onNoteOn = (pitch, currentBeat) => { midiRec.onNoteOn = (pitch, currentBeat) => {
const elapsedBeats = Math.max(0, currentBeat); const elapsedBeats = Math.max(0, currentBeat);
const sec = elapsedBeats * (60.0 / (parseInt(bpm) || 120)); const sec = elapsedBeats * (60.0 / (parseInt(bpm) || 120));
setSubTabs(prev => prev.map(s => { const activeNotes = Array.from(midiRec.activeNotes.values()).map(n => ({
if (s.id !== tab.id) return s; id: 'rec_' + n.pitch + '_' + currentBeat,
const activeNotes = Array.from(midiRec.activeNotes.values()).map(n => ({ pitch: n.pitch,
id: 'rec_' + n.pitch + '_' + currentBeat, start_beat: n.start_beat,
pitch: n.pitch, duration_beats: Math.max(0.125, currentBeat - n.start_beat),
start_beat: n.start_beat, velocity: Math.min(1, (n.velocity || 0.8)),
duration_beats: Math.max(0.125, currentBeat - n.start_beat), pan: 0.0
velocity: Math.min(1, (n.velocity || 0.8)),
pan: 0.0
}));
const rec = midiRec.recordedNotes.map((n, i) => ({
id: 'rec_' + Date.now() + '_' + i,
pitch: n.pitch,
start_beat: n.start_beat,
duration_beats: n.duration_beats,
velocity: Math.min(1, (n.velocity || 0.8)),
pan: 0.0
}));
const allNotes = [...rec, ...activeNotes];
setRecTempMidiNotes(allNotes);
setCanvasRedrawCount(n => n + 1);
return { ...s, currentTime: startTime + sec, isDirty: true };
})); }));
const rec = midiRec.recordedNotes.map((n, i) => ({
id: 'rec_' + Date.now() + '_' + i,
pitch: n.pitch,
start_beat: n.start_beat,
duration_beats: n.duration_beats,
velocity: Math.min(1, (n.velocity || 0.8)),
pan: 0.0
}));
const allNotes = [...rec, ...activeNotes];
setRecTempMidiNotes(allNotes);
setCanvasRedrawCount(n => n + 1);
setSubTabs(prev => prev.map(s =>
s.id === tab.id ? { ...s, currentTime: startTime + sec, isDirty: true } : s
));
}; };
showToast('Recording MIDI to Piano Roll...', 'info'); showToast('Recording MIDI to Piano Roll...', 'info');
}; };
File diff suppressed because one or more lines are too long