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 pitch = msg.data[1];
const velocity = msg.data[2];
if (cmd === 0x9 && velocity > 0) {
lastMidiNoteRef.current = { pitch, velocity, startTime: performance.now(), length: 0 };
setLastMidiNote({ pitch, velocity, length: 0, time: Date.now() });
activeMidiPitchesRef.current.add(pitch);
setActiveMidiPitches(new Set(activeMidiPitchesRef.current));
const activeTabInfo = subTabs.find(s => s.id === activeTab && s.type === 'PIANO_ROLL' && s.isArmed);
if (activeTabInfo && window.SonicSF) {
try { window.SonicSF.playNote(pitch, velocity, 500, undefined, activeTabInfo.instrumentProgram, null); } catch (e) {}
}
} else if (cmd === 0x8 || (cmd === 0x9 && velocity === 0)) {
activeMidiPitchesRef.current.delete(pitch);
setActiveMidiPitches(new Set(activeMidiPitchesRef.current));
const current = lastMidiNoteRef.current;
if (cmd === 0x9 && velocity > 0) {
lastMidiNoteRef.current = { pitch, velocity, startTime: performance.now(), length: 0 };
setLastMidiNote({ pitch, velocity, length: 0, time: Date.now() });
activeMidiPitchesRef.current.add(pitch);
setActiveMidiPitches(new Set(activeMidiPitchesRef.current));
try {
const ar = activeTabRef && subTabsRef && subTabsRef.current.find(s => s.id === activeTabRef.current && s.type === 'PIANO_ROLL' && s.isArmed);
if (ar && window.SonicSF) {
window.SonicSF.playNote(pitch, velocity, 500, undefined, ar.instrumentProgram, null);
}
} catch (e) {}
} else if (cmd === 0x8 || (cmd === 0x9 && velocity === 0)) {
activeMidiPitchesRef.current.delete(pitch);
setActiveMidiPitches(new Set(activeMidiPitchesRef.current));
const current = lastMidiNoteRef.current;
if (current && current.pitch === pitch) {
const lenSec = (performance.now() - current.startTime) / 1000;
lastMidiNoteRef.current = { ...current, length: lenSec };
@@ -9867,29 +9869,28 @@ const App = () => {
midiRec.onNoteOn = (pitch, currentBeat) => {
const elapsedBeats = Math.max(0, currentBeat);
const sec = elapsedBeats * (60.0 / (parseInt(bpm) || 120));
setSubTabs(prev => prev.map(s => {
if (s.id !== tab.id) return s;
const activeNotes = Array.from(midiRec.activeNotes.values()).map(n => ({
id: 'rec_' + n.pitch + '_' + currentBeat,
pitch: n.pitch,
start_beat: n.start_beat,
duration_beats: Math.max(0.125, currentBeat - n.start_beat),
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 activeNotes = Array.from(midiRec.activeNotes.values()).map(n => ({
id: 'rec_' + n.pitch + '_' + currentBeat,
pitch: n.pitch,
start_beat: n.start_beat,
duration_beats: Math.max(0.125, currentBeat - n.start_beat),
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);
setSubTabs(prev => prev.map(s =>
s.id === tab.id ? { ...s, currentTime: startTime + sec, isDirty: true } : s
));
};
showToast('Recording MIDI to Piano Roll...', 'info');
};
File diff suppressed because one or more lines are too long