From b76d4960341edbf6df06f016b9e52a25134d3d62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 5 Sep 2023 15:29:35 +0200 Subject: [PATCH] fix ts issues 2 --- front/components/PartitionView.tsx | 6 ++++-- .../PartitionVisualizer/PhaserCanvas.tsx | 20 ++++++++++++++++--- front/tsconfig.json | 2 +- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/front/components/PartitionView.tsx b/front/components/PartitionView.tsx index 9103028..e9a6ed1 100644 --- a/front/components/PartitionView.tsx +++ b/front/components/PartitionView.tsx @@ -103,9 +103,11 @@ const PartitionView = (props: PartitionViewProps) => { // console.log('timestamp cursor', _osmd.cursor.iterator.CurrentSourceTimestamp); // console.log('timestamp cursor', _osmd.cursor.iterator.CurrentVoiceEntries); // console.log('current measure index', _osmd.cursor.iterator.CurrentMeasureIndex); - const osmdCanvas = document.querySelector('#' + OSMD_DIV_ID + ' canvas'); + const osmdCanvas = document.querySelector( + '#' + OSMD_DIV_ID + ' canvas' + ); + // this should never happen this is done to silent ts linter about maybe null if (!osmdCanvas) { - // this should never happen this is done to silent ts linter about maybe null throw new Error('No canvas found'); } // Ty https://github.com/jimutt/osmd-audio-player/blob/ec205a6e46ee50002c1fa8f5999389447bba7bbf/src/PlaybackEngine.ts#LL77C12-L77C63 diff --git a/front/components/PartitionVisualizer/PhaserCanvas.tsx b/front/components/PartitionVisualizer/PhaserCanvas.tsx index 5d57591..41d7d2e 100644 --- a/front/components/PartitionVisualizer/PhaserCanvas.tsx +++ b/front/components/PartitionVisualizer/PhaserCanvas.tsx @@ -17,7 +17,16 @@ const isValidSoundPlayer = (soundPlayer: SplendidGrandPiano | undefined) => { return soundPlayer && soundPlayer.loaded; }; -const playNotes = (notes: any[], soundPlayer: SplendidGrandPiano) => { +const myFindLast = (a: T[], p: (_: T, _2: number) => boolean) => { + for (let i = a.length - 1; i >= 0; i--) { + if (p(a[i]!, i)) { + return a[i]; + } + } + return undefined; +} + +const playNotes = (notes: PianoCursorNote[], soundPlayer: SplendidGrandPiano) => { notes.forEach(({ note, duration }) => { const fixedKey = note.ParentVoiceEntry.ParentVoice.Parent.SubInstruments.at(0)?.fixedKey ?? 0; @@ -59,7 +68,7 @@ const getPianoScene = ( if (status === 'playing') { const transitionTime = 75; - const cP = cursorPositions.findLast((cP: { timestamp: number; }, idx: number) => { + const cP = myFindLast(cursorPositions, (cP: { timestamp: number; }, idx: number) => { if ( cP.timestamp < currentTimestamp + transitionTime && idx > this.cursorPositionsIdx @@ -91,13 +100,18 @@ const getPianoScene = ( return PianoScene; }; +type PianoCursorNote = { + note: Note; + duration: number; +}; + export type PianoCursorPosition = { // offset in pixels x: number; // timestamp in ms timing: number; timestamp: number; - notes: Note[]; + notes: PianoCursorNote[]; }; export type UpdateInfo = { diff --git a/front/tsconfig.json b/front/tsconfig.json index 30d5af8..b28b02b 100644 --- a/front/tsconfig.json +++ b/front/tsconfig.json @@ -14,7 +14,7 @@ /* Language and Environment */ "target": "esnext" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */, "lib": [ - "es2023", + "es2022", "DOM" ] /* Specify a set of bundled library declaration files that describe the target runtime environment. */, "jsx": "react-native" /* Specify what JSX code is generated. */,