Front: Partition View: Management of long rests

This commit is contained in:
Arthur Jamet
2023-05-05 13:15:35 +01:00
parent cce560031a
commit 4034d29056

View File

@@ -18,6 +18,7 @@ type PartitionViewProps = {
const PartitionView = (props: PartitionViewProps) => {
const [osmd, setOsmd] = useState<OSMD>();
const [soundPlayer, setSoundPlayer] = useState<SoundFont.Player>();
const AudioContext = window.AudioContext || window.webkitAudioContext || false;
const audioContext = new AudioContext();
const [wholeNoteLength, setWholeNoteLength] = useState(0); // Length of Whole note, in ms (?)
const colorScheme = useColorScheme();
@@ -82,29 +83,34 @@ const PartitionView = (props: PartitionViewProps) => {
}
let previousCursorPosition = -1;
let currentCursorPosition = osmd.cursor.cursorElement.offsetLeft;
while(!osmd.cursor.iterator.EndReached && timestampToMs(osmd.cursor.NotesUnderCursor().at(0)?.getAbsoluteTimestamp() ?? new Fraction(-1)) < props.timestamp) {
while(!osmd.cursor.iterator.EndReached &&
timestampToMs(osmd.cursor.NotesUnderCursor().at(0)?.getAbsoluteTimestamp() ?? new Fraction(-1)) +
timestampToMs(osmd.cursor.NotesUnderCursor().at(0)?.Length ?? new Fraction(-1)) < props.timestamp
) {
previousCursorPosition = currentCursorPosition;
osmd.cursor.next();
osmd.cursor.show();
if (osmd.cursor.iterator.EndReached) {
osmd.cursor.hide(); // Lousy fix for https://github.com/opensheetmusicdisplay/opensheetmusicdisplay/issues/1338
props.onEndReached();
} else {
// Shamelessly stolen from https://github.com/jimutt/osmd-audio-player/blob/ec205a6e46ee50002c1fa8f5999389447bba7bbf/src/PlaybackEngine.ts#LL223C7-L224C1
osmd.cursor.NotesUnderCursor()
.filter((note) => note.isRest() == false)
.filter((note) => note.Pitch) // Pitch Can be null, avoiding them
.forEach((note) => {
// Put your hands together for https://github.com/jimutt/osmd-audio-player/blob/master/src/internals/noteHelpers.ts
const fixedKey = note.ParentVoiceEntry.ParentVoice.Parent.SubInstruments.at(0)?.fixedKey ?? 0;
const midiNumber = note.halfTone - fixedKey * 12;
let duration = getActualNoteLength(note);
const gain = note.ParentVoiceEntry.ParentVoice.Volume;
// console.log(midiNumber, duration, gain);
soundPlayer.play(midiNumber, audioContext.currentTime, { duration, gain })
});
currentCursorPosition = osmd.cursor.cursorElement.offsetLeft;
document.getElementById(OSMD_DIV_ID).scrollBy(currentCursorPosition - previousCursorPosition, 0)
}
// Shamelessly stolen from https://github.com/jimutt/osmd-audio-player/blob/ec205a6e46ee50002c1fa8f5999389447bba7bbf/src/PlaybackEngine.ts#LL223C7-L224C1
osmd.cursor.NotesUnderCursor()
.filter((note) => note.isRest() == false)
.filter((note) => note.Pitch) // Pitch Can be null, avoiding them
.forEach((note) => {
// Put your hands together for https://github.com/jimutt/osmd-audio-player/blob/master/src/internals/noteHelpers.ts
const fixedKey = note.ParentVoiceEntry.ParentVoice.Parent.SubInstruments.at(0)?.fixedKey ?? 0;
const midiNumber = note.halfTone - fixedKey * 12;
let duration = getActualNoteLength(note);
const gain = note.ParentVoiceEntry.ParentVoice.Volume;
soundPlayer.play(midiNumber, audioContext.currentTime, { duration, gain })
});
currentCursorPosition = osmd.cursor.cursorElement.offsetLeft;
document.getElementById(OSMD_DIV_ID).scrollBy(currentCursorPosition - previousCursorPosition, 0)
}
}, [props.timestamp]);